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

HierarchyConstraint question

3 Answers 45 Views
General Discussions
This is a migrated thread and some comments may be shown as answers.
Martin
Top achievements
Rank 1
Martin asked on 08 Feb 2012, 12:08 PM
Hi

Can i use HierarchyConstraints in this example:

I know how to ID the child Node
I can ID the parent node(But not without the child)

Can i use HierarchyConstraint to instead of saying Find this child and then this parent 2 parents up. To just say this:

Find this child, then find a parent matching this htmlfindexpression(or similar) but not giving it a parent count to go up, so it will just move it way up until it finds the element or fail.


What I'm doing now is one of 2 things either use xpath or make a long htmlfindexpression with alot of | to get the elements, but i don't like this solution.

3 Answers, 1 is accepted

Sort by
0
Anthony
Telerik team
answered on 09 Feb 2012, 06:18 PM
Hello Martin,

Given the following HTML code:

<table>
<tr>
<td><b>Last Name</b></td><td>First Name</td><td>Carrier</td><td>Status</td><td>Notified</td>
</tr>
<tr>
<td>lastname1</td><td>firstname1</td><td>carrier1</td><td></td><td>No</td>
</tr>
<tr>
<td><a href="http://www.telerik.com" id="100">lastname3</a></td><td>firstname3</td><td>carrier3</td><td></td><td>No</td>
</tr>
</table>

The following code worked for me:

HtmlAnchor a = Find.ById<HtmlAnchor>("100");
Assert.IsNotNull(a);
 
HtmlTable t = a.Parent<HtmlTable>();
Assert.IsNotNull(t);
Log.WriteLine(t.InnerText);

If you prefer Visual Basic, use our Code Converter.

Regards,
Anthony
the Telerik team
Quickly become an expert in Test Studio, check out our new training sessions!
Test Studio Trainings
0
Martin
Top achievements
Rank 1
answered on 10 Feb 2012, 08:19 AM
As i understand it .parent will only find a parent tag e.g. .parent<htmldiv> will find the parent htmldiv.

What if the htmldiv is a htmldiv with class=mojo which is 4 levels up.

Note. i prefer to do it without a binding that says -4 like the normal hierachy constraint.
0
Accepted
Anthony
Telerik team
answered on 13 Feb 2012, 08:20 PM
Hello Martin,

As long as no DIV has the same attribute along the way up, you can use Xpath. Given the following HTML:

<div class="mojo">GET THIS ONE HERE!
   <div>
      <div>
         <div>
            <a href="page.aspx" id="100">Link</a>
         </div>
      </div>
   </div>
</div>

The following expression works:

HtmlDiv d = Find.ByXPath<HtmlDiv>("//a[@id='100']/ancestor::*[@class='mojo']");
Log.WriteLine(d.TextContent);

Greetings,
Anthony
the Telerik team
Quickly become an expert in Test Studio, check out our new training sessions!
Test Studio Trainings
Tags
General Discussions
Asked by
Martin
Top achievements
Rank 1
Answers by
Anthony
Telerik team
Martin
Top achievements
Rank 1
Share this question
or