get item count and item text for an unordered list?

Class library usage, coding and language questions.
godzillajoe
Posts: 13
Joined: Wed Nov 20, 2013 4:16 pm

get item count and item text for an unordered list?

Post by godzillajoe » Wed Nov 20, 2013 5:27 pm

<deleted>
Last edited by godzillajoe on Tue Mar 11, 2014 6:45 pm, edited 1 time in total.

User avatar
Support Team
Site Admin
Site Admin
Posts: 11709
Joined: Fri Jul 07, 2006 4:30 pm
Location: Graz, Austria

Re: get item count and item text for an unordered list?

Post by Support Team » Thu Nov 21, 2013 5:03 pm

Hello godzillajoe,

Please take a look at code snippet below to get an idea how to do that:
// get a list of all items
var itemList = Host.Local.Find<Ranorex.LiTag>("/dom[@domain='www.ranorex.com' and @caption='Ranorex Test Page']//div[#'Content']//ul/li");
Report.Info(itemList.Count.ToString());    

foreach (var item in itemList)
{
	Report.Info(item.InnerText); // get name of item       		
}
Regards,
Markus (T)
.
Image

godzillajoe
Posts: 13
Joined: Wed Nov 20, 2013 4:16 pm

Re: get item count and item text for an unordered list?

Post by godzillajoe » Thu Nov 21, 2013 5:09 pm

Thanks, I did it a slightly different way but eventually figured it out.

godzillajoe
Posts: 13
Joined: Wed Nov 20, 2013 4:16 pm

Re: get item count and item text for an unordered list?

Post by godzillajoe » Thu Nov 21, 2013 10:01 pm

So, as a followup, I have another list where the A tag with the text I want is buried a few levels deep under the Li tag. I tried to modify the code to "FIND" the A tag but that doesn't seem to work.

Find<Ranorex.ATag> returns an empty list



I want to grab that 1st Choice text under the P tag and all the other ones down the tree.

Do I have to do a nested FIND at each "level"
Last edited by godzillajoe on Tue Mar 11, 2014 6:47 pm, edited 1 time in total.

krstcs
Ranorex Guru
Posts: 2683
Joined: Tue Feb 07, 2012 4:14 pm
Location: Austin, Texas, USA

Re: get item count and item text for an unordered list?

Post by krstcs » Thu Nov 21, 2013 10:30 pm

You should be able to do one find at the level you want, but you need to pass the RanorexPath from the object you are searching FROM to the object you are searching FOR.

So, for your example, assuming you are searching FROM the top Li tag (TopLi here):

Code: Select all

TopLi.Find<ATag>("./div/div/p/a");
That should return every object under TopLi that matches the path you gave it. Notice that the path you give starts where the TopLi path "ends", so to speak.

You can (and probably should) modify the path to be more specific so it takes less time and finds only the things you actually want.
Shortcuts usually aren't...

godzillajoe
Posts: 13
Joined: Wed Nov 20, 2013 4:16 pm

Re: get item count and item text for an unordered list?

Post by godzillajoe » Fri Nov 22, 2013 4:24 pm

<deleted>
Last edited by godzillajoe on Tue Mar 11, 2014 6:48 pm, edited 1 time in total.

krstcs
Ranorex Guru
Posts: 2683
Joined: Tue Feb 07, 2012 4:14 pm
Location: Austin, Texas, USA

Re: get item count and item text for an unordered list?

Post by krstcs » Fri Nov 22, 2013 4:50 pm

You need a common ancestor (named "root" here). Try something like this (working off the top of my head, so this is somewhat pseudo-code):

Code: Select all

LiTag root = Host.Local.Find<Ranorex.ATag>("/dom[@domain='dev.carecloud.local']//div[#'businesses']/div/ul[2]/li")[0];  //need to get the 1st element in the returned list, you don't want the whole thing.

foreach(ATag parentNode in Host.Local.Find<ATag>("ul/li/div/div[2]/p/a")) {
    // Print parent's name here
    foreach(ATag childNode in parentNode.Find<ATag>("ancestor::li/ul/li/div/div[2]/p/a")) {
        //Print child's name here
    }
}
Shortcuts usually aren't...

godzillajoe
Posts: 13
Joined: Wed Nov 20, 2013 4:16 pm

Re: get item count and item text for an unordered list?

Post by godzillajoe » Fri Nov 22, 2013 5:07 pm

I was gonna try using GetPath() and truncating that but I'll try this. I'm kinda digging this tool after initially thinking "bah, it's all record and playback" but once you pop the hood, there's some good stuff under there. I just have to get used to it :D

krstcs
Ranorex Guru
Posts: 2683
Joined: Tue Feb 07, 2012 4:14 pm
Location: Austin, Texas, USA

Re: get item count and item text for an unordered list?

Post by krstcs » Fri Nov 22, 2013 5:20 pm

Yeah, there's a lot of power in there if you know how to get to it.

GetPath returns the full, absolute path of the object, so it is useful for certain things, but not for "inner" loops like what you need.

If you use the repository (which I'm assuming you don't since you are heavy in code, but you should really consider it, makes maintenance very easy), then you should also look at using the BasePath (for folders) and Path (for items) attributes. They return just what is in the repository "Path" column, so if you setup your repo the right way you can use them to get the paths you need and only need to update the repo object paths instead of your code.
Shortcuts usually aren't...

godzillajoe
Posts: 13
Joined: Wed Nov 20, 2013 4:16 pm

Re: get item count and item text for an unordered list?

Post by godzillajoe » Fri Nov 22, 2013 5:28 pm

I'm learning the tool coming from SilkTest and Selenium/Java so I'm using the repository but also experimenting with straight coding. Not everyone on the team is comfortable writing code so we'll probably mix and match as needed.

godzillajoe
Posts: 13
Joined: Wed Nov 20, 2013 4:16 pm

Re: get item count and item text for an unordered list?

Post by godzillajoe » Fri Nov 22, 2013 5:55 pm

BTW, I tried using the latest code snippet and get this

Cannot implicitly convert type 'Ranorex.ATag' to 'Ranorex.LiTag' (CS0029)

krstcs
Ranorex Guru
Posts: 2683
Joined: Tue Feb 07, 2012 4:14 pm
Location: Austin, Texas, USA

Re: get item count and item text for an unordered list?

Post by krstcs » Fri Nov 22, 2013 6:51 pm

Hmm...

Maybe I missed something in my example. Which line is returning the error, specifically?
Shortcuts usually aren't...

godzillajoe
Posts: 13
Joined: Wed Nov 20, 2013 4:16 pm

Re: get item count and item text for an unordered list?

Post by godzillajoe » Fri Nov 22, 2013 7:48 pm

LiTag root = ......

krstcs
Ranorex Guru
Posts: 2683
Joined: Tue Feb 07, 2012 4:14 pm
Location: Austin, Texas, USA

Re: get item count and item text for an unordered list?

Post by krstcs » Fri Nov 22, 2013 7:59 pm

Yeah, my bad... Duh...


The root declaration should have been "...Find<LiTag>(..." instead of "...Find<ATag>(...".

The "<T>" (where T is a class type, like LiTag) is a generic declaration, and tells .NET to make sure what it finds is of type "T". I had changed the RXPath, but not the <T>. :D
Shortcuts usually aren't...