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

How to Click a Specified Link Within an HTML <UL>?

4 Answers 54 Views
General Discussions
This is a migrated thread and some comments may be shown as answers.
Gregory
Top achievements
Rank 1
Gregory asked on 13 Jan 2014, 10:11 PM
I am trying to loop through an HTML unordered list, find a specific value, and then click the associated delete link.  So far, I can loop through the <UL>, find the value, and click the first delete button in the <UL>.  Is there a way to cycle down through the HTML and tell test studio to click the delete link for a specific tag?

Example:

The <UL> has two values:
A role

Test Role

The script will loop through the list and find 'Test Role'.  At this point, it needs to click the delete link associated with this item.  My current script always clicks the delete link for 'A role', which is the first one in the list.  I cannot figure out how to make it click the delete link for 'Test Role'.

Does anybody have any suggestions?  I have attached screen shots of the DOM and the UI and my current script is below.

Thanks!


HtmlUnorderedList roles = ActiveBrowser.Find.ById<HtmlUnorderedList>("Roles");
HtmlAnchor a = Find.ById<HtmlAnchor>("deleteRole");

//Loop through each <li>, find role and click the delete link
foreach(HtmlListItem item in roles.AllItems)
{
     if (item.TextContent.ToString() == "Test Role")
     {
          Log.WriteLine(item.TextContent.ToString());
          a.MouseClick();
     }
}

 

 

 

4 Answers, 1 is accepted

Sort by
0
Accepted
Daniel
Top achievements
Rank 2
answered on 14 Jan 2014, 02:44 PM
Are you specifically looking for just Test Role?  Like will you know the text content of the list item you are in search of beforehand without needing to loop through the entire list?
HtmlUnorderedList roles = ActiveBrowser.Find.ById<HtmlUnorderedList>("Roles");
 
HtmlListItem li = roles.Find.ByContent<HtmlListItem>("l:Test Role");
HtmlAnchor a = li.Find.ByAttributes<HtmlAnchor>("id=deleteRole");
if (!object.Equals(a, null))
{
    a.MouseClick();
    Log.WriteLine("Mouse Click!");
}

The above finds the Unordered List "Roles".  From there, we search within Role to find the literal text "Test Role" (http://www.telerik.com/automated-testing-tools/support/documentation/user-guide/write-tests-in-code/intermediate-topics/element-identification/finding-page-elements.aspx).  Once we have the HtmlListItem, we can then find the HtmlAnchor with the id of "deleteRole".

The big problem is the list items don't have ID's.  Without ID's you are working against the system trying to find based on text content that could change.
0
Gregory
Top achievements
Rank 1
answered on 14 Jan 2014, 02:58 PM
Daniel,

Thank you so much for your response.  This is exactly what I needed.  Since the script preceeding this will add the role, we will know exactly what text to look for.  The next plan is to make the text parameter driven.  However, we are trying to get our devs to add id's to everything to make this easier.

Thanks for all your help (again)!
0
Daniel
Top achievements
Rank 2
answered on 14 Jan 2014, 02:59 PM
Great, glad it will work for you.  I know all about no ID's an brittle tests due to it.  Good luck.
0
Boyan Boev
Telerik team
answered on 15 Jan 2014, 04:15 PM
Hi,

@Daniel, thank you again for helping!

@Gregory, we are glad to hear that Daniel could help you. 

Let us know if you need further help.

Regards,
Boyan Boev
Telerik
The New Release of Telerik Test Studio Is Here! Download, install,
and send us your feedback!
Tags
General Discussions
Asked by
Gregory
Top achievements
Rank 1
Answers by
Daniel
Top achievements
Rank 2
Gregory
Top achievements
Rank 1
Boyan Boev
Telerik team
Share this question
or