In a web test I need to click an element defined by Xpath in a Coded Step:
* I need to do it in a coded step because I need some logic (while loop with try-catch)
* I need an xpath because it is best practice and more stable than any element definition auto-generated by Telerik
Here is my snippet:
int i = 1;
Element e = Find.ByXPath("(//i[@class=\"dx-icon dx-icon-close\"])[" + i.ToString() + "]");
System.ArgumentNullException: The target Element object passed in for this action is null. Action aborted. Possible causes to this exception is the element not present in the page markup and the Find.Byxx returning null.
bei ArtOfTest.WebAii.Core.Actions.ValidateTargetElement(Element targetElement)
bei ArtOfTest.WebAii.Core.Actions.Click(Element targetElement)
bei TestProject6.Login.Login_Schließe_Alle_Tabs_In_MDE() in c:\Dev\TestAutomation\Testfälle\Schritte\Login.tstest.cs:Zeile 66.
When I pause the test execution before executing the code, open the Developer tools and search for the xpath in the DOM, I find the desired result. So I wonder why the element seems to be null.
11 Answers, 1 is accepted
I am sorry to hear that you have encountered such difficulties and will be happy to assist you with this.
Since you have posted another thread I will refer to some of the resources in that one. The execution log from the other discussed topic contained a failure for the step, which uses the same element. That step seems to be using the built-in Test Studio mechanism for identifying elements and the find expression is also using the same XPath. However, it is listed that the element was found only by image.
Having that in mind, I suppose that the used XPath is not correctly pointing to an element from the DOM tree represented in the Test Studio recorder. You mentioned that you have double checked the XPath in the browser's DevTools, but this may differ from what is parsed in the recorder. Can you, please, use the Search field in the DOM explorer and check if the used XPath works as you expect it?
In case you still have troubles to identify what might be incorrect, please, execute that sample test and send me the failure details from the run - exporting these (from the Failure tab) will allow me explore the DOM tree at the time of failure.
Thank you for your cooperation in advance.
Thank you for you support. I was able to figure out the button click using the code in my first post.
I think the confusion arose from the fact that Telerik Coded Steps use a different way of Clicking (where Click() is a method of Actions, not of the element as it is in any other selenium implementation I have seen before...). So apparently, I mixed up my attempts to click the button.
I am glad to hear you sorted out what was wrong in the coded step you worked on.
Let me, please, share some resources about the Testing Framework, which will be of further help for you to get familiar with its specifics - find out more about the HTML element wrappers, finding page elements and navigate through these, HTML control actions and asserts, wait class.
Do not hesitate to contact us again in case you have any further questions. Thank you for your cooperation in our discussions.
I have another problem with getting an element in a Coded Step...
I want to read the Content of the Element. In my Test Studio test case I have a coded Step with following content:
naechstesTeilInPopup = ActiveBrowser.Find.ByXPath("//div[contains(@class,\"dx-popup-wrapper\")]//tr[@aria-rowindex=\"2\"]//span");
NaechstesTeilString = naechstesTeilInPopup.Content.Trim();
The Assertion fails as the Element is null. However, I verified that it is present. In fact, during the Thred.Sleep(60000) I can open the developer tools and search for the XPath and find it.
When I open the Step Failure Details > Page DOM, I can't find "dx-popup-wrapper". So apparently the DOM that I see in the browser during test execution right before failure is not the same that the Test Studio uses to search for the element.
I was able to solve the issue using
before using ActiveBrowser.Find.ByXPath(). Apparently, Telerik was not able to recognise that the DOM changed without explicitly telling it...
I am pleased to know you managed to sort out what was missing in the coded sample.
In general, Test Studio recorded steps and tests have built-in actions to refresh the DOM as per the changes in the application, based on the performed actions. When creating custom coded tests, refreshing the DOM tree needs to be considered in between the actions listed in the code. Where it will be required, depends again on the interactions sent to the page under test.
Let me know if you happen to face any further difficulties.
I have another problem with specifying an Element and Hover Over it.
string UvpColumnHeaderMenuXpath = "//td[@aria-colindex=\"" + columnIndex + "\" and contains(@class, \"dx-editor-cell\")]//div[@class=\"dx-menu-horizontal\"]/ul[@class=\"dx-menu-items-container\"]";
HtmlTableCell UvpColumnHeaderMenu = Find.ByExpression<HtmlTableCell>("XPath=" + UvpColumnHeaderMenuXpath);
UvpColumnHeaderMenu.MouseHover(0, 0, ArtOfTest.Common.OffsetReference.AbsoluteCenter);
From the Log Line I can see that the Xpath is
//td[@aria-colindex="7" and contains(@class, "dx-editor-cell")]//div[@class="dx-menu-horizontal"]/ul[@class="dx-menu-items-container"]
Which is what I want it to be. I verified in theStep Failure Details that the element is in fact present on the page.
I think I don't understand how to use the HTML Control HtmlTableCell correctly. However, as I want to select a <ul> Element, HtmlTableCell should be the correct HtmlControl.
I reviewed the provided details, but I am not completely sure what is the misbehavior you observe. Since I am not familiar in details with the application under test, I will need some further details for this case.
Thus, can you please go through the below notes and let me know your input on the topics below:
- What is the outcome of executing this test - it is not locating the correct element, or it is not highlighting it?
- Are you trying to highlight a table cell or a <ul>?
- If this is a <ul> nested under a table cell, why you are using <HtmlTableCell> to define the Find.ByXX()? How about using <UnorderedList>?
- What is your intention of using the HTML Control HtmlTableCell and what do you think is not correct in the usage?
Please, excuse me if these questions don't seem quite relevant to your doubts, but I am not familiar with the requirements you need to cover - that is why I am trying to sort these out.
Thank you for your cooperation in advance.
thank you again for your superb support!
Please exuse me for leaving out important details. The problem was that the Element was not found. So "UvpColumnHeaderMenu" was null.
Replacing "HtmlTableCell" by "HtmlUnorderedList" did the trick. The following "Wait" and "MouseHover" work as expected: The mouse hovers over the <ul>-Element, as a result the web application is showing a popup menu.
As you can see from my questions, I'm no expert of web technologies. Is there a table documenting the relavtion between HTML Elements in my web application and the HtmlControl type that I need to use in Test Studio? Something like this:
ul --> HtmlUnorderedList
I am pleased to know that the suggested approach helped you sort out the misbehaving piece of code.
As far as I can recall at the beginning of our conversation, I shared some useful resources about using the Test Studio Testing Framework. Of course, these may have left aside in your efforts in getting started with the coding, so I will list these here:
- the HTML element wrappers,
- finding page elements and
- navigate through these,
- HTML control actions and asserts,
- wait class.
I hope that these will bring some further understanding about the Testing Framework and how get the best of it. Of course, I remain available to continue the discussion or assist in any future difficulties you face.
Thank you for your continuous cooperation.