Selenium

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'))
    WebUI.click(findTestObject('btn-Filter-All'))
    WebUI.delay(1)
    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')
        }
        WebUI.delay(1)
        userName=WebUI.getText(findTestObject('table-User Role Details', [('row') : 1, ('column') : 1]))
        counter++
    }

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()

columnName=columnNamesList.indexOf("columnName")+1
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")
numOfResults=dbData.getRowNumbers()
List columnNamesList=dbData.getColumnNames()
println columnNamesList
List resultSet=dbData.getAllData()
for (loop = 0; loop <=(numOfResults-1); loop++) {
    println(resultSet[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:

Adding Feature Flags to Test Cases

Feature flags are by no means a new concept, but when used within QA tests, they provide a lot of flexibility and extend the functionality without having to write and maintain multiple test cases.

I've recently added the TimeDuration library to several tests to record how long it takes to perform a "save" action. It's great for me, but I don't want this to run all the time, so it's being wrapped in a feature flag. I start with a simple list of flags at the top of the code like:

boolean timedTest=true

And then later in the code, there is an IF block if (useDataBase==true){ to see if the time should be recored. By default these are off for regression tests. But when I want to use them for functional testing, I change the flag.

Taking that a step further, I've been using feature flags to section off functionality. Let's say we are creating an order. That order needs a list of items. For a regression test, the list needs to be generic enough to apply to multiple scenarios and users.

However, with a feature flag, the list can be be specific for my functional test without having to write a whole new test case or disrupt the existing one. Right now, I am adjusting them at the test level.

Here is a simple example to either read a spreadsheet for generic items or query the database for a specific user and location.

//Read inventory from the database file or xls spreadshseet?
boolean useDataBase=true

if (useDataBase==true){
    WebUI.callTestCase(findTestCase('Populate Custom Inventory List - DB'), [:], FailureHandling.CONTINUE_ON_FAILURE)
}else{
    WebUI.callTestCase(findTestCase('Populate Inventory Items List'), [:], FailureHandling.CONTINUE_ON_FAILURE)
}

Another example would be whether or not to add a custom item to an order. A custom item has specific fields associated with it and isn't used in regression testing.

//add a custom item to the order?
boolean addCustomItem=false
if (addCustomItem==true){
    //Wait for Add Inventory Item button to be visible
    WebUI.waitForElementVisible(findTestObject('btn-Add Custom Item'), 15)

<additional code here>

    //Click to Add Custom Item to Order
    WebUI.click(findTestObject('btn-Save Custom Item'))
}

While these aren't complicated examples, they are the start of consolidating code for functional and regression testing. In the case above, my original test created a certain type of order. With a few small additions, the test is dynamic enough to fill in extra forms when the testing is for my benefit. But with the flags turned off, it acts as a regression test that works for 80-90% of the users in the system. This gives me extra testing capability without having to write and maintain two sets of tests.

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
    GlobalVariable.inventorySKU[loop]=inventoryData
}

Other articles of interest:

Recent Comments

  • 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...
  • Working with Dates and Date Formatting in Katalon Studio (14)
    • 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
    • ALLAN FORD: Useful !
    • Don Pedro: Not sure I understand what you are trying to do.
    • Don Pedro: formattedDate = mydate.format(“M/dd/yyy& #8221;)
  • 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...
    • Aparna: Hello, Your documentation is excellent and has helped me understand many things. But this article above seems outdated and I need help in creating my own Global variables and all the documentation out there seems to be point to...
  • 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...
  • How To Disable the Quicken Registration Prompt (27)
    • debra: didn’t work for me… my 2007 – which I am keeping on my older mac.
  • Adding entries to an open Excel spreadsheet during runtime (1)
    • IanG: Hi: Seems like nobody else has described (in detail) how Katalon can be configured to test an API by reading the first entry from a multi-row multi column XLSX spreadsheet, executing a test, writing the result to another column (or...