How to count elements in search result and fail the step if max results is counted?

Hi there,

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:

  1. Perform a search
  2. Verify, that the search results on the search result page does not exceeds 25 results

I’ve defined my test object like this:

I’m a stranger to loop statements, and therefore I have not been able to make use of any of these statements:

I know, that my question is lacking some details, but I have not started writing the code - in other words: I’m in need of some guidance here!

PLS have a look at this post.

You do not need any “loop” statements. You just need to know the size of the List<WebElement> returned by WebUI.findWebElements keyword.

Hi Kazuraym!

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.

This should show some digit.0, 1, 2, … The digit depends on how the target HTML looks like.

What digit do you expect to see?

What digit do you actually see?

I would expect to see the actual number of results (which is 10 results).
I do not see any digits in the result in Katalon.

You can find the output from println in the Console tab as the following screenshot shows.

If you wan to see 10 in the Log Viewer tab, you should rather write:


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.

Now I need to write a step in my TC in Katalon which:

  1. Counts all the results on the page
  2. Verify, that the total count is not larger than 10

If I use your suggestion, how do I put it into use in this specific scenario?

I appologies for my many questions.

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.

assert trElements.size() <= 10
1 Like

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 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())

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):

Obtains the number of elements in this List.


int size()



Return Value

The size of the list.

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.


Why not you just try again? Be sure the list is stable or not.

Hmm. It seems pretty stable. Also, I increased the timout to 20 seconds - still the same result.

You create the TestObject using the Utils class, createTestObject() method.
Could you share the source code of this class, this method?

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!

1 Like