How can I get total count of objects in a page?

Hi guys,
I been trying to get this small test to pass…but I think i’m missing something quite simple.
So my test is basically applying some default elements to the page by selecting an option. And my validation is to GET count of all those elements created and basically match the count expected. These elements have different IDs, but share same “class = box-more” - so i thought I could just get count of all the items in the page that has “box-more” as class name.

This is what I did so far but test doesn’t work.

Ex: of Html -
<a href="#" class=“box-more” data-type=“income” …

Any solution?

This is a line of JavaScript using DOM interface code you can try and prove to yourself it works by placing it in the browser console in DevTools.

document.querySelectorAll(".box-more").length

To use that code in your Test Case…

String js = 'return document.querySelectorAll(".box-more").length;'
int boxMoreCount = WebUI.executeJavaScript(js, null)
println("Count is: " + boxMoreCount)
1 Like

You are too good at these stuff! it worked - I’ll use your method.

Would you also have a solution to the way I was trying to do it - for knowledge purpose. I might have been just missing some webDriver stuff since it was underlined? I also took off the ’ ’ from eleCount - which was the 2nd issue with my one.

1 Like

No, sorry. I rarely use that kind of code. This guy on the other hand…

@Brandon_Hein

To me, your original code looks fine. The error you are getting looks to be a missing import. If you have the original code you posted, try pressing crtl + shift + o on your keyboard, and import the appropriate classes.

After i put in the verifyMatch statement - it seems to be failing and can’t match. Am i missing something?

Hi, I already did that… and these are the imports in the test -


import org.openqa.selenium.By as By
import org.openqa.selenium.WebDriver as WebDriver
import org.openqa.selenium.remote.server.DriverFactory as DriverFactory

import com.kms.katalon.core.model.FailureHandling as FailureHandling
import com.kms.katalon.core.webui.keyword.WebUiBuiltInKeywords as WebUI

import internal.GlobalVariable as GlobalVariable

Ok this is a different error than the one in your original post. This new error is because the verifyMatch method only accepts String arguments, and you are passing int arguments.

In this case, I would just use a regular assert:

assert boxMoreCount == 27

You could also try verifyEqual:

WebUI.verifyEqual(boxMoreCount, 27)

1 Like

Yea, I was using Russ’s method for the test validation, after doing verifyEqual instead of match, it worked.

I’d still want to know why my original code was failing? all the imports were done but can’t figure out why it wasn’t working. Do you have any idea?

Your original error was definitely an import error.

@ashraf.madina

Don’t forget to fully verify your new code. Make sure you test it with errors/fails as well as passes. You can fake those by passing rogue values to verifyEqual.

I’ll leave it to you to check the .box-more elements actually contain something meaningful :wink:

Thanks @Brandon_Hein for stepping in.

1 Like

I was thinking that too but was assuming by pressing cnt+sht and O, katalon imported all the classes needed.
But after going back and reviewing it - it missed “import org.openqa.selenium.WebDriver”. Likely a Katalon issue.

Now, my code works. Thanks.

1 Like