This is a migrated thread and some comments may be shown as answers.

element Find fails when using relative xpath

8 Answers 239 Views
General Discussions
This is a migrated thread and some comments may be shown as answers.
Vince
Top achievements
Rank 1
Vince asked on 06 Aug 2013, 09:54 AM
Relative xpaths are failing to find any screen elements, leading to this error:
"System.NullReferenceException: Object reference not set to an instance of an object."

This problem occurs in a coded step, following this kind of pattern:
HtmlControl element1 = Manager.ActiveBrowser.Find.ByXPath<HtmlControl>("//div[@class='sw_b']");
HtmlControl element2 = element1.Find.ByXPath<HtmlControl>("./input[@id='sb_form_q']");

The first line, where it calls the Find method on the active browser and uses an absolute xpath, works fine. But the second line, calling the Find method on the particular HtmlControl and using a relative xpath to find another element, fails; the variable element2 remains null, and the further coded steps fail with the error above.

I've attached a complete project which uses a very simple test to demonstrate this problem.

8 Answers, 1 is accepted

Sort by
0
Velin Koychev
Telerik team
answered on 08 Aug 2013, 03:46 PM
Hi Vince,

You just need a small change in the code and it will work as expected:
HtmlControl element1 = Manager.ActiveBrowser.Find.ByXPath<HtmlControl>("//div[@class='sw_b']");
HtmlControl element2 = element1.Find.ByXPath<HtmlControl>("//input[@id='sb_form_q']");

Just replace ./ with //  That way you will find any elements that are relative to the first element.

You can read more about Xpath Syntax in this article
// -  Selects nodes in the document from the current node that match the selection no matter where they are.

I hope this helps.

Regards,
Velin Koychev
Telerik
The New Release of Telerik Test Studio Is Here! Download, install,
and send us your feedback!
0
Vince
Top achievements
Rank 1
answered on 08 Aug 2013, 04:15 PM
Thank you Velin, that stops the error occurring.

However, there is a difference between ./ and // notation, so if test studio only supports // notation it is a limitation.
// means to identify any descendant of the current context, while ./ means to identify any child of the current context.

So for example in this sample of html:

<div name="1">
    <div name="2">
        <div name="3">
            <div name="4">


If div 1 is the current node, then the following relative xpaths should return different results:

 //div should return div 2, div 3 and div4.
but 
 ./div should return only div 2.

Is there any reason why the ./ notation is not supported by telerik?
0
Bob
Top achievements
Rank 1
answered on 08 Aug 2013, 04:28 PM
I believe Test Studio only returns the first result because it only works with a single element at a time. Therefore, whether it returns 2,3,4 or 2 makes no difference.
0
Vince
Top achievements
Rank 1
answered on 09 Aug 2013, 08:30 AM
Actually Robert, it's often necessary to work with multiple elements - that's exactly what the Find.AllByXpath method (and similar methods) are designed for.
0
Bob
Top achievements
Rank 1
answered on 09 Aug 2013, 12:36 PM
Time permitting, what are some ways you use this? I'm hoping there are ways I can implement it in my own testing.

I read the following to try and get up to speed on all the element identification things I could. I just can't think of a way to use most of them.
http://www.telerik.com/automated-testing-tools/support/documentation/user-guide/write-tests-in-code/intermediate-topics/element-identification/finding-page-elements.aspx
0
Velin Koychev
Telerik team
answered on 13 Aug 2013, 12:49 PM
Hi Robert,

I spent some time investigating the issue with the XPath expression and you are absolutely right that it is not working as expected.

I filed a bug report on this problem, which you can follow in our Feedback Portal. I am sorry for any inconvenience it has probably caused and have updated your Telerik Points accordingly.

Let me know if you need assistance on writing the right Find Expression for some element.

Thank you for your understanding
.

Regards,
Velin Koychev
Telerik
The New Release of Telerik Test Studio Is Here! Download, install,
and send us your feedback!
0
Bob
Top achievements
Rank 1
answered on 13 Aug 2013, 12:57 PM
Would love to take credit, but I think Vince here was the one to report it.
0
Velin Koychev
Telerik team
answered on 13 Aug 2013, 01:11 PM
Hello Robert,

You are absolutely right, my mistake. It seems that I actually gave the points correctly to Vince, so we have justice. 
 

Regards,
Velin Koychev
Telerik
The New Release of Telerik Test Studio Is Here! Download, install,
and send us your feedback!
Tags
General Discussions
Asked by
Vince
Top achievements
Rank 1
Answers by
Velin Koychev
Telerik team
Vince
Top achievements
Rank 1
Bob
Top achievements
Rank 1
Share this question
or