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.

search-results-found

//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.

profile-tabs

//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
  • How To Disable the Quicken Registration Prompt (16)
    • Dave: Works for Quicken 2007 too. Thanks, Gina!
    • Charlie Fellenbaum: Yay, thanks, and that worked for Quicken 2006 as well.
    • Ken: This didn’t work for me. I have 2006. I installed on a new laptop. I must have registered before, don’t remember. I tried to register this time, but got an error message. I’d rather not registered, though. I use...
    • Neil Murphy: I discovered that early on. I’m using 2001.
    • Jeremy: Thank you so so so much. I am still using Quicken 2010 because Intuit didn’t want to properly support users abroad anymore (never mind that they only link to US banks for transaction downloads; they didn’t want my...
  • Quasi Performance/Load Testing with Katalon Studio (1)
    • Anthony: I have done this with 100 plus machines at the same time and it worked well and provided some useful feedback.
  • A Try Catch example in Katalon Studio (3)
    • Venkatesh: Hi Don Pedro Can you share me sample project ….which consist of basic to advanced stuff…If your github or gitlab url aso present please share… As I am beginer to this I need to learn lot
    • Don Pedro: Hi Lucas. Thank you for the kind comments. I would be happy to help. Let me know how I could be of assistance.
    • Lucas: Thanks for sharing a lot of Katalon tips and tricks. The Katalon team highly appreciate your support and feedback. To make Katalon Studio better and be the best free automation solution for testing community, we wish to coordinate...
  • Securely storing passwords and login details with Set Encrypted Text in Katalon Studio (1)
    • Agus: Hi Don, Is there a way to set the encrypted text to be used in the ‘WebUI.authenticate̵ 7; method? Basically I am trying to encrypt the password that is passed to the ‘WebUI.authenticate̵ 7; method. Thank you.
  • Katalon Studio – Manual View – The start of a test script (2)
    • ben: Hello Don Great Website. Keep up the work. I learnt a lot. cheers Ben.
    • Lucas: Hi Don Pedro, Thanks for sharing a lot of great articles related to Katalon Studio and Selenium. I am Lucas from Katalon team. From your point of view, we’ve learnt alot to improve our product and add more valuable features...
  • Another success with Katalon Studio (1)
    • Mahesh Joshi: Hi Don, I just started following your blog in regards to katalon. I want to introduce katalon studio at my work we do manual testing at work at the moment. What approach I should use for proof of concept with katalon for...
  • What went wrong with Crime City? (34)
    • G D: One thing I have noticed is the awkwardness of items and the attack points and how you can earn them. I was pretty pathetic with attack points and then I played that limited time boss and got weapons that had attack points in the...
  • Disable the Quicken 2012 Registration Prompt (1)
    • Scott: Thanks this worked great and is better then having to upgrade to a subscription!