Waiting for Elements to appear in Katalon Studio

As tests are running, there is a frequent need to pause the code execution to wait for an item to be available on the page. There might be a pause due to network speed or waiting for the results of a query. While it’s possible to use the Delay command, it might be better to use the WaitForElementVisible command.

This waits for the specific element you want to interact with to appear on the page before the test continues. For one of my tests, I need to wait for the number of results to be returned before moving on. I could wait for the page, but I specifically need this number, and this text doesn’t appear until the query has completed.


//Wait for the Results Found text to appear. This contains the number of Prospects on the page.
WebUI.waitForElementVisible(findTestObject('Page_/Prospect Page Objects/label-Prospect Results Found'), 10)

The code above waits for the label to appear, then my test can continue, which is to read that value.

You can couple waiting for an item to appear, with waiting for an item to have specific text. Katalon offers, verifyTextPresent, which waits until a specific block of text appears on the page. For example:

WebUI.verifyTextPresent("Month To Date Sales Detail", false)

Will wait for the words, “Month To Date Sales Detail” to appear before moving it. My reason for using this particular verification is, if that text doesn’t appear, the page didn’t load correctly so the next steps would be invalid.

//Look for the text, Month To Date Sales Detail on the Page.
//If it's not there, there is a problem loading the page and the test should exit as the rest of the steps will fail
WebUI.click(findTestObject('Page_/Sales Dashboard/Monthly Details/Monthly Sales Accordion'))
try {
 elementPresent=WebUI.verifyTextPresent("Month To Date Sales Detail", false)
catch (Exception e) {
 title = WebUI.getWindowTitle()
 log.logWarning('ERROR: The title of the Sales Detail Page is:=' + title)
 throw new AssertionError('ERROR: The Sales Detail Page did not load correctly', e)

This is a Try/Catch example, where the first step is to click to open a dashboard. It then checks if the header of the page has the text, “Month To Date Sales Detail”. If that is missing, the page is in error and the test needs to exit.

Further, if that page doesn’t appear, we’ve got a more serious problem to look at. Because of that problem, the test throws an AssertionError and marks the test as failed.

This could have been done using the MarkFailed command discussed previously. At the time it was a reasonable solution, so I’ve kept it. When I get around to refactoring this test, I will most likely change it to a VerifyElementPresent and use the method below.

Instead of the Try/Catch, I used VerifyElementPresent to determine which tabs are available for a user and run the appropriate test. The tabs available could be Contacts, Notes, Tasks and a few others.


//Determine which of the tabs is visible and run the appropriate test
elementVisible=WebUI.verifyElementPresent(findTestObject('Page_/Customer Profile/tab-Contacts'), 5, FailureHandling.CONTINUE_ON_FAILURE)
if (elementVisible==true){
log.logWarning('--- Contacts tab is available, running test ---')
WebUI.callTestCase(findTestCase('Customer Profile/Contacts'), [:], FailureHandling.CONTINUE_ON_FAILURE)
} else {
 log.logWarning('--- Contacts tab is not available for this customer. No test to execute ---')

A new parameter, FailureHandling.CONTINUE_ON_FAILURE) is added at the end. It still waits 5 seconds for the object to appear, but, the main test case will continue if it’s not there. This is because I’ve stacked several CallTestCase commands within a single test. If the tab is available, the test case is called to verify the contents of that tab. If the tab doesn’t exist, a log entry is made, but the test moves on to check for the next tab.

I could make five separate tests and let each fail because the tab isn’t there, but this is more dynamic and reacts to the available elements on the page. If the tab is missing, an error will be flagged, but the test doesn’t stop dead.

The Delay command is useful to get a script to wait a determined amount of time before moving on. Some say it’s not the best practice, but it does work. However, when you need to wait for a particular item to appear, waitForElementVisible, verifyElementPresent, verifyTextPresent can be for more effective and reliable.

Other articles of interest:

Leave a Reply

Your email address will not be published. Required fields are marked *

Recent Comments