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

**URL:** <https://forum.katalon.com/t/how-to-count-elements-in-search-result-and-fail-the-step-if-max-results-is-counted/43577>\
**Category:** Web Testing\
**Tags:** katalon-studio\
**Created:** [May 17, 2020, 4:11pm UTC](https://forum.katalon.com/t/how-to-count-elements-in-search-result-and-fail-the-step-if-max-results-is-counted/43577 "2020-05-17T16:11:00Z")\
**Posts on this page:** 16\
**Page:** 1

<div class="post-metadata">

**Author:** ![mgsserritzlev](https://avatars.discourse-cdn.com/v4/letter/m/7bcc69/32.png) [@mgsserritzlev](https://forum.katalon.com/u/mgsserritzlev)\
**Post date:** [May 17, 2020, 4:11pm UTC](https://forum.katalon.com/t/how-to-count-elements-in-search-result-and-fail-the-step-if-max-results-is-counted/43577/1 "2020-05-17T16:11:00Z")

</div>

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:  
 ![image](https://europe1.discourse-cdn.com/katalon/original/2X/1/1b2d1c436b8e13285571a31c9b5649e0aace0398.png)

I’m a stranger to loop statements, and therefore I have not been able to make use of any of these statements: [https://docs.katalon.com/katalon-studio/docs/statements.html#looping-statements](https://docs.katalon.com/katalon-studio/docs/statements.html#looping-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!

---

<div class="post-metadata">

**Author:** ![kazurayam](https://dub1.discourse-cdn.com/katalon/user_avatar/forum.katalon.com/kazurayam/32/333_2.png) [@kazurayam](https://forum.katalon.com/u/kazurayam)\
**Post date:** [May 17, 2020, 9:29pm UTC](https://forum.katalon.com/t/how-to-count-elements-in-search-result-and-fail-the-step-if-max-results-is-counted/43577/2 "2020-05-17T21:29:10Z")

</div>

PLS have a look at this post.

> [@Custom Keyword: get contents of HTML elements as a list of strings](http://forum.katalon.com/t/custom-keyword-get-contents-of-html-elements-as-a-list-of-strings/11137/2):
>
> I have found that we do not need a custom keyword. A built-in keyword WebUI.findWebElements(TestObject) is available. You can write this import org.openqa.selenium.WebElement import com.kms.katalon.core.testobject.TestObject import com.kms.katalon.core.webui.keyword.WebUiBuiltInKeywords as WebUI import com.kms.katalon.core.testobject.ConditionType ... // grasp \<tr\> elements List\<WebElement\> trElements = WebUI.findWebElements( new TestObject().addProperty("xpath", ConditionType.EQUALS, …

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

---

<div class="post-metadata">

**Author:** ![mgsserritzlev](https://avatars.discourse-cdn.com/v4/letter/m/7bcc69/32.png) [@mgsserritzlev](https://forum.katalon.com/u/mgsserritzlev)\
**Post date:** [May 18, 2020, 5:55am UTC](https://forum.katalon.com/t/how-to-count-elements-in-search-result-and-fail-the-step-if-max-results-is-counted/43577/3 "2020-05-18T05:55:43Z")

</div>

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:

 ![image](https://europe1.discourse-cdn.com/katalon/original/2X/0/0ecf1912a6ce170085c0b7f8ceae2902b97678bd.png)

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.

---

<div class="post-metadata">

**Author:** ![kazurayam](https://dub1.discourse-cdn.com/katalon/user_avatar/forum.katalon.com/kazurayam/32/333_2.png) [@kazurayam](https://forum.katalon.com/u/kazurayam)\
**Post date:** [May 18, 2020, 7:45am UTC](https://forum.katalon.com/t/how-to-count-elements-in-search-result-and-fail-the-step-if-max-results-is-counted/43577/5 "2020-05-18T07:45:21Z")

</div>

> [@mgsserritzlev](#):
>
> println “trElements.size()=${trElements.size()}”

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?

---

<div class="post-metadata">

**Author:** ![mgsserritzlev](https://avatars.discourse-cdn.com/v4/letter/m/7bcc69/32.png) [@mgsserritzlev](https://forum.katalon.com/u/mgsserritzlev)\
**Post date:** [May 18, 2020, 8:39am UTC](https://forum.katalon.com/t/how-to-count-elements-in-search-result-and-fail-the-step-if-max-results-is-counted/43577/6 "2020-05-18T08:39:29Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![kazurayam](https://dub1.discourse-cdn.com/katalon/user_avatar/forum.katalon.com/kazurayam/32/333_2.png) [@kazurayam](https://forum.katalon.com/u/kazurayam)\
**Post date:** [May 18, 2020, 10:10am UTC](https://forum.katalon.com/t/how-to-count-elements-in-search-result-and-fail-the-step-if-max-results-is-counted/43577/7 "2020-05-18T10:10:08Z")

</div>

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

 ![someDigit](https://europe1.discourse-cdn.com/katalon/original/2X/0/0fbfc8908bf347cc58f43da3680f41bf7472d285.png)

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

```auto
WebUI.comment("trElements.size()=${trElements.size()}")

```

---

<div class="post-metadata">

**Author:** ![mgsserritzlev](https://avatars.discourse-cdn.com/v4/letter/m/7bcc69/32.png) [@mgsserritzlev](https://forum.katalon.com/u/mgsserritzlev)\
**Post date:** [May 18, 2020, 1:07pm UTC](https://forum.katalon.com/t/how-to-count-elements-in-search-result-and-fail-the-step-if-max-results-is-counted/43577/8 "2020-05-18T13:07:33Z")

</div>

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.

 ![image](https://europe1.discourse-cdn.com/katalon/original/2X/c/c7b3690c73500e062e0be1590be47e0c18e31db9.png)

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.

---

<div class="post-metadata">

**Author:** ![kazurayam](https://dub1.discourse-cdn.com/katalon/user_avatar/forum.katalon.com/kazurayam/32/333_2.png) [@kazurayam](https://forum.katalon.com/u/kazurayam)\
**Post date:** [May 18, 2020, 1:56pm UTC](https://forum.katalon.com/t/how-to-count-elements-in-search-result-and-fail-the-step-if-max-results-is-counted/43577/9 "2020-05-18T13:56:28Z")

</div>

> [@mgsserritzlev](#):
>
> - Counts all the results on the page
> - Verify, that the total count is not larger than 10

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.

---

<div class="post-metadata">

**Author:** ![helpdesk](https://avatars.discourse-cdn.com/v4/letter/h/7c8e57/32.png) [@helpdesk](https://forum.katalon.com/u/helpdesk)\
**Post date:** [May 19, 2020, 3:57am UTC](https://forum.katalon.com/t/how-to-count-elements-in-search-result-and-fail-the-step-if-max-results-is-counted/43577/10 "2020-05-19T03:57:13Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![anon46315158](https://avatars.discourse-cdn.com/v4/letter/a/73ab20/32.png) [@anon46315158](https://forum.katalon.com/u/anon46315158)\
**Post date:** [May 19, 2020, 8:59am UTC](https://forum.katalon.com/t/how-to-count-elements-in-search-result-and-fail-the-step-if-max-results-is-counted/43577/11 "2020-05-19T08:59:45Z")

</div>

> [@mgsserritzlev](#):
>
> Verify, that the total count is not larger than 10

```auto
assert trElements.size() <= 10

```

---

<div class="post-metadata">

**Author:** ![mgsserritzlev](https://avatars.discourse-cdn.com/v4/letter/m/7bcc69/32.png) [@mgsserritzlev](https://forum.katalon.com/u/mgsserritzlev)\
**Post date:** [May 20, 2020, 7:08am UTC](https://forum.katalon.com/t/how-to-count-elements-in-search-result-and-fail-the-step-if-max-results-is-counted/43577/12 "2020-05-20T07:08:51Z")

</div>

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:

 ![image](https://europe1.discourse-cdn.com/katalon/original/2X/3/3d3bb24d76e7ad7942bf8578d1a477d05f387f7b.png)

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?

---

<div class="post-metadata">

**Author:** ![helpdesk](https://avatars.discourse-cdn.com/v4/letter/h/7c8e57/32.png) [@helpdesk](https://forum.katalon.com/u/helpdesk)\
**Post date:** [May 20, 2020, 3:05pm UTC](https://forum.katalon.com/t/how-to-count-elements-in-search-result-and-fail-the-step-if-max-results-is-counted/43577/13 "2020-05-20T15:05:49Z")

</div>

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

[https://www.tutorialspoint.com/groovy/groovy\_lists\_size.htm](https://www.tutorialspoint.com/groovy/groovy_lists_size.htm)

Obtains the number of elements in this List.

**Syntax**

int size()

**Parameters**

None

**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.

---

<div class="post-metadata">

**Author:** ![kazurayam](https://dub1.discourse-cdn.com/katalon/user_avatar/forum.katalon.com/kazurayam/32/333_2.png) [@kazurayam](https://forum.katalon.com/u/kazurayam)\
**Post date:** [May 21, 2020, 1:57am UTC](https://forum.katalon.com/t/how-to-count-elements-in-search-result-and-fail-the-step-if-max-results-is-counted/43577/14 "2020-05-21T01:57:14Z")

</div>

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.

 ![3d3bb24d76e7ad7942bf8578d1a477d05f387f7b](https://europe1.discourse-cdn.com/katalon/original/2X/7/79cf53d09d29e0a537cc1e8fcc4c7923dc239073.png)

@mgsserritzlev

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

---

<div class="post-metadata">

**Author:** ![mgsserritzlev](https://avatars.discourse-cdn.com/v4/letter/m/7bcc69/32.png) [@mgsserritzlev](https://forum.katalon.com/u/mgsserritzlev)\
**Post date:** [May 21, 2020, 11:40am UTC](https://forum.katalon.com/t/how-to-count-elements-in-search-result-and-fail-the-step-if-max-results-is-counted/43577/15 "2020-05-21T11:40:08Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![kazurayam](https://dub1.discourse-cdn.com/katalon/user_avatar/forum.katalon.com/kazurayam/32/333_2.png) [@kazurayam](https://forum.katalon.com/u/kazurayam)\
**Post date:** [May 21, 2020, 11:43am UTC](https://forum.katalon.com/t/how-to-count-elements-in-search-result-and-fail-the-step-if-max-results-is-counted/43577/16 "2020-05-21T11:43:30Z")

</div>

> [@mgsserritzlev](#):
>
> def static search\_result = Utils.createTestObject(‘class’, ‘item col-12’)

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

---

<div class="post-metadata">

**Author:** ![mgsserritzlev](https://avatars.discourse-cdn.com/v4/letter/m/7bcc69/32.png) [@mgsserritzlev](https://forum.katalon.com/u/mgsserritzlev)\
**Post date:** [May 21, 2020, 11:47am UTC](https://forum.katalon.com/t/how-to-count-elements-in-search-result-and-fail-the-step-if-max-results-is-counted/43577/17 "2020-05-21T11:47:53Z")

</div>

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!
