Katalon Studio

Waiting on elements to appear using While Loops

Timing is everything. Certainly so when it comes to automation and waiting for elements to appear before moving on to the next step. While Katalon has several wait methods, including the new smart wait, there are times when you have to wait for a text change. In this case, we aren't waiting for an object, we are waiting for a condition to change. For example, we need the number of search results to increase or decrease. Or we already have a list of names, but the one we want isn't available yet.

In these cases we will use the while loop to get our status and add a delay until what we are searching for is available.

In the example below, we are waiting for a status change. We have previously created an item and now we need for the status to show as "Sold." When that happens, we can proceed with the next step.

The first thing we need to do is search for our item and read the status. If it already shows as "Sold," we can move on. If not, we reset our search criteria and search for the item again. We also add a 1 second delay. We keep repeating this until we get our "Sold" status.

itemStatus = WebUI.getText(findTestObject('select-Status Action'))
while (itemStatus.contains("Sold")!=true){
    //If the item is not set to Sold, search for it again to refresh the results
    WebUI.setText(findTestObject('input-Search Name'), GlobalVariable.itemList)
    WebUI.click(findTestObject('btn-Go Search'))
    itemStatus = WebUI.getText(findTestObject('select-Status Action'))

This will certainly work, but there's a problem. If the status doesn't change, we have an endless loop. What we need is to check the condition and implement a counter. We can then check that our condition is met AND we haven't tried too many time. This can be done using the AND (&&) as part of our loop.

In this case, if we don't find the user name AND we try 20 times, it's time to bail out and fail the test.

In the example below, we are searching for a user. Since we already have a table of names, we can't wait for an element, because it's already visible. We also can't assume the first name we is correct since it may not have changed correctly and we will get a false positive.

We read the first name in the list to see if it's the one we want. If not, we add a delay of 1 second, then read the name again.

Additionally, we have added a counter that will increment up to 20 times. This equates to 20 seconds, which should be more than enough time for the name to appear.

If for some reason it doesn't, we won't be able to proceed, so the test is marked as Failed and stopped.

    //User Name in column 1, User Role in column 5
    userName=WebUI.getText(findTestObject('table-User Role Details', [('row') : 1, ('column') : 1]))
    while(userName!=GlobalVariable.UserName && counter<=20) {
        if (counter>=20){
            log.logError('ERROR: Login and User Name DO NOT match')
            log.logError('Could not find user: ' + GlobalVariable.UserName)
            KeywordUtil.markFailedAndStop('ERROR: Login and User Name DO NOT match')
        userName=WebUI.getText(findTestObject('table-User Role Details', [('row') : 1, ('column') : 1]))

These are simple examples, but they show the process of waiting for something to change that isn't based on elements. In many cases, the page is loaded, we just need to wait for a certain piece of information to change. This is better than a forced delay WebUI.delay(1) because we can still have a dynamic test.

In one run it might take only a second for the status to change. In another run it might take 5 or even 10 seconds for the change to occur. With the while loop, we can handle both without the test waiting when the condition is actually correct.

Other articles of interest:

Connecting Katalon Studio into XLS and CSV files

Since reading from external source files has become an important part of how we get data, I wanted to put together some simple examples to show reading data from a CSV file, an XLS file and from a Database source. All three are very similar and since Katalon handles most of the work, it's easy to grab data from a file with just a few lines of code.

We start by using the TestData library and reference the "Test Data File" from the "Data Files" object in the project. That is done with:
TestData csvData = findTestData("Data Files/Data Source Name")

Once we have that, we can use some of the built in Katalon keywords to get the number of rows in the file and the column names.
int rowCount=csvData.getRowNumbers()
List columnNamesList=csvData.getColumnNames()

Once we have that information we know how many rows we can read and how many columns. We then use getValue to read our piece of information.
temp1=csvData.getValue(columnName, rowNumber)

When working with a database, there is one small addition. We can use getAllData to retrieve all the items that have been returned from the query. This isn't a necessary step since we can still get the number of rows and parse through the data. However, if the entire block of data is needed, getAllData can be used.
List resultSet=dbData.getAllData()

In such a case, your data will be returned in this format:
[[04GA, 61], [04OC, 93], [02OC, 334]]

For the above we have the SKU followed by the quantity.

Again, Katalon provides a very simple way of connecting into different kids of data sources and retrieving information. This has certainly improved my Test Cases and opened up a variety of test possibilities.

Read data from CSV file

TestData csvData = findTestData("Data Files/csvDataSource")

int rowCount=csvData.getRowNumbers()
println rowCount
int rowNumber=1
List columnNamesList=csvData.getColumnNames()

println columnName
temp1=csvData.getValue(columnName, rowNumber)
println temp1
Read data from XLS file

TestData xlsData = findTestData("Data Files/Inventory Items")

int rowCount=xlsData.getRowNumbers()
println rowCount
int rowNumber=1
List columnNamesList=xlsData.getColumnNames()

columnName=columnNamesList.indexOf("Item Name")+1
println columnName
temp1=xlsData.getValue(columnName, rowNumber)
println temp1
Read data from Database

TestData dbData = findTestData("Data Files/PostgresDB")
List columnNamesList=dbData.getColumnNames()
println columnNamesList
List resultSet=dbData.getAllData()
for (loop = 0; loop <=(numOfResults-1); loop++) {

temp1=dbData.getValue(2, 1)
println temp1
temp2=dbData.getValue(3, 1)
println temp2
columnName=columnNamesList.indexOf("column name")
println "column Position:" + columnName

Other articles of interest:

Adding Timing Flags to Test Cases within Katalon

I've made reference to this before, but due to the success I've had using it, I wanted to bring it up again.

Within my tests cases I have been adding markers to time how long it takes to complete an action. This is usually connected with the Save action or something similar. For example, how long does it takes to save the form data? Or how long does it take to move from one page to another.

Using the "feature flag" idea, I place the start of the code right before there is an action to click the button. After the click action, there is a waitForElementVisible action, which waits for an email address input field to become visible. Once we can take action on it, the Save action is complete and we can record the time between those two events.

if (timedTest==true){
    timeStart = new Date()
WebUI.click(findTestObject('btn-Save Cart))
WebUI.waitForElementVisible(findTestObject(input-Email Address'), 90)
if (timedTest==true){
    timeStop = new Date()
    TimeDuration duration = TimeCategory.minus(timeStop, timeStart)
    log.logWarning("Execution Time: " + duration)

The same process works when moving through pages. Click the link or button that moves to the next page and wait for an item to appear that proves the page has been loaded. Take the time between those two events and output it.

Additionally, I have been outputting these times to a text file so I can gather some benchmark data. As an example, the following is listed within the timedTest flag.

    File file1 = new File('/FileLocation/savetimes.txt')
    file1 << "Execution Time: " + timeStop +"," + duration"

This has been a huge benefit for our performance testing. I can run the same test dozens of times of and record how long it takes. I don't have to guess or write it all down manually. It all becomes part of the test and gets tracked over time.

After we make a change, I run the same tests again and record the new results. This easily creates the Before and After benchmarks.

You can even add a small bit of code to give you a visual cue things are going south.

    String timeCounter=duration
    timeCounter=timeCounter.replaceAll(' seconds','')
    float timeValue=Float.valueOf(timeCounter)
    if (timeValue>15){
        log.logError('ERROR: The save time for the item is higher than expected')
        KeywordUtil.markError('ERROR: The save time for the item is higher than expected')

Katalon Studio may not be a performance or load balancing tool, but you can easily track metrics and show processes that are taking longer than normal. This probably isn't part of a regression test, but by combining the feature flags with the TimeDuration library you can record all sorts of metrics about your test which can be incredibly value.

Other articles of interest:

Programmatic Database query with Katalon

As mentioned previously, making dynamic database queries was a big goal for this year. Katalon makes this quite easy with a UI to set up the connection and a straightforward way of connecting to the data itself.

To start the process, select Data Files, New Test Data and select Database as the source.
Taking the configuration string from before, we connect to the database using this screen and provide the default query. This creates a default table of data that Katalon will work with when executing tests.

Within our code, we use the TesData object to get at our database source.
TestData inventoryDB = findTestData('Data Files/database object name')

We can now get the number of rows in the database, the names of the columns and read data from each column as needed. We need to reference each column with an index number rather than it’s name. Even though Katalon displays "item_number" as the column title, internally that is column 1.

However, we can still use item_number by getting a list of the columns and finding it’s index. I am adding 1 to the index since the List starts at 0 and 0 isn’t a valid column.

From there, we read information from the database using the getValue statement:
inventoryData=inventoryDB.getValue(columnIndex, loop)

From this very simple example we can get the counts of rows and column, then read the correct item from the list.

int loop=1, columnIndex=1, rowCount=1
String inventoryData=''
//Connect to Database Object in Data Files
TestData inventoryDB = findTestData('Data Files/Inventory Items Query')
rowCount=inventoryDB.getRowNumbers() //Row count for the number of results returned
List columnNames=inventoryDB.getColumnNames() //A List of the column names
columnIndex=columnNames.indexOf("item_number")+1 //Get the index of the column name we want

for (loop = 1; loop <=rowCount; loop++) {
    //Read the column data from the database and assign to a List
    inventoryData=inventoryDB.getValue(columnIndex, loop) //Read data directly from the database query

Other articles of interest:

Connecting Katalon to a Postgres Database

One of our goals for this year was to connect Katalon into our Postgres instance. It turned out to be quite simple, but we ran into a couple of errors before we got all the magic to happen.

When using the standard connection string, we get an hba_conf error along with ssl=true errors. This can be fixed in the connection string by appending the two following parameters:


The connection string to be placed in Katalon looks like:


With this connection string in place, we were able to connect to the database without issue and execute queries.

Other articles of interest:

Recent Comments

  • How To Disable the Quicken Registration Prompt (30)
    • David: A BIG Thank you! I regularly reinstall Windows OS’s using different hardware configs. In short, Quicken 2011 (it’s March of 2020 as I write this) has been a program I’ve been very happy with and continue to use....
    • Karen L: Today I rang Reckon Australia again, got a different guy and he talked me through the process of getting me a new product key to enter since I already had the licence and it had been extended. Reckon have only extended it by 1...
    • Karen L: I have Australian Quicken 2008 on Windows 8.1. It keeps asking me to renew my licence which Reckon did for me however the activation won’t work. I tried this to disable the registration prompt however it did not stop the...
  • Parsing Strings in Katalon – Split, Substring and Readlines (6)
    • Don Pedro: String tempText=”Date Month January. Revenue $1,355,721.00″ println(tempText.split(“ \$”)[1]) You need to escape the $ with \$ The [1] on the split captures characters on the right of the split [0]...
    • Prasad: Hi, I need to get the substring from the below string ‘Date Month January. Revenue $1,355,721.00’ I want to extract text after $ value. Please help Thank you
  • Working with Dates and Date Formatting in Katalon Studio (15)
    • Sangeethaa: How to get previous day’s date(Yesterday’s date)? Actually I was using today.previous() before,it was working fine till today morning.Now its not retrieving right date. Can anyone please guide me on this.
    • Don Pedro: That could be a little tricky. First, is the text/format always going to be the same? You will need to do some parsing. Separate the first but of text at 2019. After that, split the text again to get rid of Central Standard...
    • Don Pedro: It should be of the same Date type as today. You could then do some calendar trickery with Calendar /*Calendar c = Calendar.getInstance(); //c.set(Calendar.MONTH, 1) //Set the month, 0=January, 11=December println...
    • josh: I’m testing a page with a string like “Updated November 21, 2019 16:25:32 PM Central Standard Time.” How might I capture the date and time and compare it to current time to verify that the update time was within...
    • zakir hussain: today = new Date() lastWeek = (today – 1.week) can u please help me in understanding what is the return type of lastweek? and I wanted to set nextyear date how can i do it
  • Output status messages and test information by writing to the Log File Viewer in Katalon Studio (6)
    • Saranya: Good One. Could you plz share link to access all your katalon related blogs. ThankQ
    • Rodrigo Calabretta: I’m using the KeywordUtil.markFailed or KeywordUtil.markError and my test stops is being shown as Error and If I use in the @afterTestCase the testCaseContext.getTestCaseSta tus() to show the status test case...
  • Setting up Environment Profiles in Katalon Studio (2)
    • Don Pedro: Not quite sure I understand. The term “Execution Profile” is normally used now, but they are still GlobalVariables and are created in the same way for version 7 as previous. You make a new execution profile, then...
  • Here is my drive cluster (2)
    • Don Pedro: While I still have those drives, the drive cluster looks nothing like that now. Almost all of my external drives have had the cases removed and they have been installed into a series of MediaSonic 8 Bay Drive Enclosures. I...
    • Marin Boucher: Hi! Verry funny blog with “go to the point” on many post! I spent good time reading some of your post. Regarding this one about NAS, “many NAS” in fact, I would be curious to see a screenshot of...