3

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.

Mate Mrše
  • 7,997
  • 10
  • 40
  • 77
brithwulf
  • 538
  • 10
  • 35

3 Answers3

3

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()
Mate Mrše
  • 7,997
  • 10
  • 40
  • 77
Tony Bui
  • 815
  • 5
  • 7
2

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!

Mate Mrše
  • 7,997
  • 10
  • 40
  • 77
Moshe Slavin
  • 5,127
  • 5
  • 23
  • 38
2

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
Mate Mrše
  • 7,997
  • 10
  • 40
  • 77