Count List Items

Best practices, code snippets for common functionality, examples, and guidelines.
Posts: 22
Joined: Thu May 12, 2016 10:18 am

Count List Items

Post by roxter » Tue Oct 18, 2016 4:23 pm

I need to validate dynamic list output.What I would like to do is to count all Div 'ng-scope' items in a DOM (please see attached screenshot).
The 'ng-scope' items represent search results in a table I would like to validate.
How can this be done in C# user code?
list.png (5.02 KiB) Viewed 917 times

Posts: 256
Joined: Tue Mar 24, 2015 5:05 pm
Location: Des Moines, Iowa, USA

Re: Count List Items

Post by Vaughan.Douglas » Tue Oct 18, 2016 5:50 pm

Something along the lines of this should get you what you need:

Code: Select all

            Validate.AreEqual(Host.Local.Find<DivTag>("descendant::div[@class='ng-scope']", 30000).Count, myExpected,
                "The actual count was {0}, the expected count was {1}");
myExpected is a variable containing the expected count

Code: Select all

Host.Local.Find<DivTag>("descendant::div[@class='ng-scope']", 30000) 
will get you a collection (iList) of DivTag objects.

I would have to recommend finding a repo object to use the find method on rather than using Host.Local just to scale back the amount of searching Ranorex is going to have to do.

also I just assumed "ng-scope" was the class attribute, you'll need to update the snippet if that is not the case.
Doug Vaughan