I’m trying to figure out the best way to count elements on a page (search results) and how to fail the test if the count exceeds 25 results on page X (the test case:
Perform a search
Verify, that the search results on the search result page does not exceeds 25 results
Thanks for your reply. I’ve tried to implement the code snippit in my TC like this:
List trElements = WebUI.findWebElements(
SearchPage.search_result, 10)
println “trElements.size()=${trElements.size()}”
Where I defined SearchPage.search_result as:
def static search_result = Utils.createTestObject(‘xpath’, ‘//*[@id=“resultList”]/section/div/div/div/div/div[2]/ul/li[1]/a/div/div’)
This is what happening:
The test runs without errors, but I’m not sure what to read from the result - I simply do not know what the output is telling me about my web elements that I want to count.
I thank you for your help, Kazurayam.
However, I’m afraid that I do not know how to utilize your examples.
I’ll try to break my problem further down:
Let’s say I’m a tester at Google, and I want to verify, that each search result page only shows 10 results per page.
I do not know the URL of your target Web page, I do not know how the HTML looks like, so I can not give you an entire solution for you out of box.
Please try to develop for yourself a test case, as far as you can, that implements the above 2 idea. You can leave a question as a comment into the script saying “here I want to do … how can I …”. Share the code here. Then I would be able to make an appropriate suggestion.
In the above code log output, you have trElements. Assuming you can use it or something similar, then you can use the result to compare to. An example is:
trElements = findWebElements(search_result, 10)
if (trElements.size() > 10) {
WebUI.verifyTextPresent(“Test failed”, false, FailureHandler.STOP_ON_FAILURE)
}
If you don’t want to stop but do some other action, then do it within the if statement braces.
Okay guys - thank you so much for your help!
I’ve come this far now:
List searchResults = WebUI.findWebElements(
SearchPage.search_result, 15)
println “searchResults.size()=${searchResults.size()}”
assert searchResults.size() <= 10
My testobject SearchPage.search has been defined like this:
def static search_result = Utils.createTestObject(‘class’, ‘item col-12’)
My HTML looks like this:
When I execute the test, the printed result in the console is:
9: println(searchResults.size()=$searchResults.size())
searchResults.size()=9
I expected it to print out “10”. Is this because the console counts from 0? So: 0, 1, 2, … 9 = 10?
Interesting. Although cycling through the list would start at zero, counting the items of the list should start at one. The below is from a Groovy tutorial website (the example shown on the site matches what we would expect to get: 10):
It’s like that kindergarten song: “One of these is not like the other…” Sorry, they all look the same, but I would make the change to 9 until something other tells what is different, and then you can tell us what the concern was.
I noticed that the <li> elements are contained in a <div class="news-list">. The class name “news-list” implies that the list is dynamic, may occasionally change in a few seconds.
Problem solved! I was pretty goofy - the problem was, that my code executed another search after the first one - therefore, I’ve been looking on the second result on the site all the time (which contains 10 results) whilst looking on the out print which correctly printed 9 as the first search result contained nine hits. Boy, do I feel stupid. Thank you guys so much for your help!