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
  • Working with Dates and Date Formatting in Katalon Studio (7)
    • Sharma: I want to choose date from any of the 12 months but I am not abel to do the same.
  • Securely storing passwords and login details with Set Encrypted Text in Katalon Studio (2)
    • sowmya: hi can u please help me how to read data from text file in katalon studio?
  • Custom Keywords for Custom Functions (1)
    • Sarah: Thank you for this. It helped me understand these better.
  • How To Disable the Quicken Registration Prompt (26)
    • Denise Defoor: I have a Quicken SE Version 6 .. My computer crashed a few weeks ago and I finally got it up and running. Now Quicken is telling me I must registered it. What? I have had this program for 20 years. How can I disable the...
    • Greg: For me, holding the *LEFT* CTL + Shift then clicking Online, One Step Update worked. I originally tried holding the right CTL + Shift, and it didn’t work. I’m using Quicken 2006, so I don’t know if it will work...
    • Joe SR>: My monthly income is deposited into my Credit Union account. I use debit whenever possible. I write checks manually and mail them. I use Quicken 2012 off-line only. I have entered all my money and investment accounts. I...
    • Prtet: Never say never….every time I swear I will never use Quicken again, I discover that there are still no viable alternatives. Amazing that there is no decent personal finance software.
    • Joe D.: Holding CTRL + Shift keys and selecting On Line | One Step Update from the main menu worked for my Quicken 2004. I’m grateful that you wrote a synopsis (“Simply put, …”) just beneath the link to the blog...
  • Parsing Strings in Katalon – Split, Substring and Readlines (4)
    • Anjana: Hey thanks for the response It is quite strange but initializing variable for delimiter and it worked String splitFormat = ‘-‘ WebUI.println(var_CampaignDura tion.toString().split(splitFor mat)[0])...
    • Don Pedro: I’m not really sure what the difference is, but I don’t think the “-“ in the split is the same as the one in the date. String var_CampaignDuration = “05/13/2019-06/07/2019&# 8221; String[] parsedDateRange =...
    • Anjana: Hi, I tried the same thing to split the string, but does not work for me, Could you plz point out whats wrong Variable var_CampaignDuration has value “05/13/2019-06/07/2019&# 8221; String[] parsedDateRange =...
    • Ellen: Thanks for sharing!! I like your contributions to Katalon topics.
  • How to Block games by Title and Tag on Steam (2)
    • Marcus861: I made an acc just to say: Same bro
    • JACK: Thanks, same just wanted to block anime games in my discover
  • Create a new and random UUID for an API call (1)
    • Anonymous: Hi ! I found this blog few weeks ago and it’s really helping me out with my tests! Thanks a lot for the Katalon tips and tricks ! Keep up the good work 🙂