Page 1 of 1

get item count and item text for an unordered list?

Posted: Wed Nov 20, 2013 5:27 pm
by godzillajoe
<deleted>

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

Posted: Thu Nov 21, 2013 5:03 pm
by Support Team
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)

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

Posted: Thu Nov 21, 2013 5:09 pm
by godzillajoe
Thanks, I did it a slightly different way but eventually figured it out.

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

Posted: Thu Nov 21, 2013 10:01 pm
by godzillajoe
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"

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

Posted: Thu Nov 21, 2013 10:30 pm
by krstcs
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.

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

Posted: Fri Nov 22, 2013 4:24 pm
by godzillajoe
<deleted>

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

Posted: Fri Nov 22, 2013 4:50 pm
by krstcs
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
    }
}

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

Posted: Fri Nov 22, 2013 5:07 pm
by godzillajoe
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

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

Posted: Fri Nov 22, 2013 5:20 pm
by krstcs
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.

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

Posted: Fri Nov 22, 2013 5:28 pm
by godzillajoe
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.

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

Posted: Fri Nov 22, 2013 5:55 pm
by godzillajoe
BTW, I tried using the latest code snippet and get this

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

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

Posted: Fri Nov 22, 2013 6:51 pm
by krstcs
Hmm...

Maybe I missed something in my example. Which line is returning the error, specifically?

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

Posted: Fri Nov 22, 2013 7:48 pm
by godzillajoe
LiTag root = ......

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

Posted: Fri Nov 22, 2013 7:59 pm
by krstcs
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