Selenium based Custom Keyword to Count Table Rows in Katalon Studio

When working with tables, one of the most common tasks is to count the number of rows in that table. Even though Katalon has the ability to create a parameterized version of the table object, you still need to know the number of rows or columns available. The most common way is through the Webdriver, get a List of the returned objects and then get the size.

After doing that a couple of times, I’ve switched over to calling that code from a Custom Keyword and simply call it from the Test Case. To make it dynamic, the xpath of the table is passed. This builds on the idea of passing the Katalon Object as a String. In this case, the xpath is passed as a String so it can be used anywhere.

The basic Keyword is quite simple and looks like this:

public class selenium {
KeywordLogger log = new KeywordLogger()
@Keyword
def countRowsPerPage(String xpath){

WebDriver driver = DriverFactory.getWebDriver()

//Find the table element on the page
WebElement Webtable=driver.findElement(By.xpath(xpath));

//Determine the number of elements in the table
List TotalRowCount=Webtable.findElements(By.xpath(xpath));

//Get the size of the List, this is the number of rows
int totalNumberOfRows=TotalRowCount.size()
log.logWarning('Total Number of rows on the page:= ' + totalNumberOfRows)
return totalNumberOfRows

   }
}

Within the Test Case, a variable would be set to the xpath of the table, such as:

xpath="//*/div[@id='search-results']/div/table/tbody/tr"

The Custom Keyword is then called with a variable set to the number of rows returned from the Custom Keyword Method.

rowResults=CustomKeywords.'commonCode.selenium.countRowsPerPage'(xpath)

The variable can now be used within a FOR loop or to verify the number of results.

This space for rent.

Author Signature for Posts

0