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
  • Filling forms with random numbers in Katalon Studio (2)
    • Don Pedro: randomNumber is the variable that holds the result of generating a new random number. Depending on how things work on your site, you might need to convert the number to a String before it’s recognized correctly....
    • Seth: When you are setting up randomNumber is this a new keyword or a new variable? I am trying to randomly generate unique socials and can’t seem to connect my generator to my Set Text.
  • Working with Dates and Date Formatting in Katalon Studio (5)
    • Ann: THANK YOU ! That worked perfectly !
    • Don Pedro: import groovy.time.TimeCategory currentDate = new Date() println currentDate use( TimeCategory ) { after30Mins = currentDate + 30.minutes } println after30Mins https://stackoverflow.com/ques tions/21166927/incrementing...
    • ricky julianto nugroho: hay can u gave me an example for plus a hour in the time ? example : now is 15 pm i want to print 16 in the text thank you
    • Don Pedro: formattedDate=mydate.format(&# 8220;MMM”) – Nov formattedDate=mydate.format(&# 8220;MMMM”) – November
    • Ann: This was a huge help. Thank you ! Do you have something for capturing the month spelled out? and abreiveiated? Example November Nov Thanks !
  • Boostnote for Code Snippets (1)
    • Manual: Hi Don, Regarding the code snippets and examples that you have created. Is there any GitHub repo or something that can be shared? Thanks
  • Using TypeIt4Me with Katalon Studio (3)
    • Manual: That’s great thanks for sharing.
    • Don Pedro: Added a screenshot to show some examples of how I have shortcuts configured for Groovy code. TypeIt4Me supports groups, so you can create shortcuts related to Groovy, Selenium, and other topics to keep them organized. You can...
    • Manual: Hi Don, I like the idea of using the TypeIt4Me for scripting test code. Could you please share how I can create. I also like the idea of creating code templates to use with code editors. Thanks, Manual
  • Another success with Katalon Studio (2)
    • Don Pedro: Since Katalon is free, a very practical demonstration of what it can would be to create the code needed to fill in forms. That was one of my first tasks as Katalon could fill in a lengthy form in seconds versus the minute or...
  • How To Disable the Quicken Registration Prompt (18)
    • Susan Long: I have Quicken personal 2012 which I only use to enter and retrieve data. It suits my needs perfect. Now they have blocked my access and want me to re register. I don’t want to upgrade as it I am comfortable with this...
    • Stewart: I have been using Quicken 200 basic since it was introduced but I now have a new PC using windows 10 and can’t get rid of the registration prompt. I have tried the above control/shift trick but to no avail. Any ideas
  • Conditional Statements – IF .. ELSE IF in Katalon Studio (1)
    • Allyson: I used this : elementVisible=WebUI.verifyEle mentPresent… but it failed because “FAILED because (of) Unable to verify object ‘Object Repository/Homepage/b_vaolopho c’ is present (Root cause:...