Count items, rows, users, etc in Katalon Studio

Viewed 4701

I am having some problem with Katalon Studio.

Can I somehow count items on the page by class or something?

I can do it with JavaScript but I don't know how to do it with groovy language in Katalon studio.

document.getElementsByClassName("").length

I'm trying to convert this JavaScript code into groovy but nothing happens.

3 Answers

You can also use WebUiBuiltInKeywords to findWebElements as specified in the following URL. It will return a list of elements matching the locator.

static List<WebElement> findWebElements(TestObject to, int timeOut)
// Internal method to find web elements by test object

Examples

def elements = WebUiBuiltInKeywords.findWebElements(to, 5)
println elements.size()

I think you can use the same method of size() like done in Table:

See documentation.

import org.openqa.selenium.By as By

import org.openqa.selenium.WebDriver as WebDriver

import org.openqa.selenium.WebElement as WebElement

WebDriver driver = DriverFactory.getWebDriver()
'To locate table'
WebElement Table = driver.findElement(By.xpath("//table/tbody"))
'To locate rows of table it will Capture all the rows available in the table'
List<WebElement> rows_table = Table.findElements(By.tagName('tr'))
'To calculate no of rows In table'
int rows_count = rows_table.size()
println('No. of rows: ' + rows_count)

Hope this helps you!

Do this

WebDriver driver = DriverFactory.getWebDriver()
def eleCount = driver.findElements(By.className("your-class")).size()
println eleCount //prints out the number of the elements with "your-class" class
Related