Wait For Alert, Verify Alert Present in Katalon Studio

We’ve looked at waiting for an element to appear, so now we’ll look at waiting for a an alert to appear. For the site I’m working with, there are two alerts to check for. The first is a confirmation that the Prospect has been created correctly and appears every time the Prospect details are Saved.

The second is a warning dialog that presents when there is an error retrieving results. This is a search/query error and indicates a data error. For most search types this will not appear, but when it does, it means something is wrong.

Katalon has two commands to handle both of these:

WebUI.waitForAlert
WebUI.verifyAlertPresent

In most cases, either command will be followed by an:

WebUI.acceptAlert()

Which clicks OK for the dialog and continues executing code.

For my tests, I have mainly used waitForAlert. For the first scenario, I wait for the alert because I know it’s coming. For the second, I check to see if it pops up, and if not, continue on with the test.

After creating the Prospect, I wait for the confirmation dialog to appear. Once it does, I confirm it contains the “success” text. If it does, I log that and pass the test. If it contains something else, I accept the alert, but log an error as the result is unexpected.

//After the Prospect is created, a confirmation dialog is presented. This Accepts that dialog and allows the script to continue.
//Confirm the text is a Success message
elementPresent=WebUI.waitForAlert(20)
if (elementPresent==true) {
 alertText = WebUI.getAlertText()
 log.logWarning('The title of the alert is:=' + alertText)
 if (alertText=='Successfully Created Contact'){
 KeywordUtil.markPassed('SUCCESS: Successfully Created the Prospect')
 WebUI.acceptAlert()
 } else {
 WebUI.acceptAlert()
 KeywordUtil.markFailed('ERROR: There was an error creating the Prospect')
 }
}

The script waits 20 seconds for the confirmation dialog to appear. That seems like a lot, but in the QA environment, things take a little longer. The default is 30, so it saves a few seconds.

Once the dialog presents, the getAlertText gets the text of the message. This is compared to the success message I expect. If they match, write a success message to the log, mark the test as passed and click OK on the alert.

If the text is something else, mark the test as failed with an error message in the log file. It doesn’t really matter what the message was, it wasn’t a success, so this test needs manual follow up.

The code for the second scenario is almost identical as it is for the first. It’s more about it’s placement than anything else. In the first case, the alert check is at the end, when I know it will come up. The second is located earlier, right after I search for the product name. If the alert presents itself, the test is marked as failed and exits. If the alert doesn’t appear, the test continues as normal.

//Click the product name and pause to make sure a query error isn't displayed.
WebUI.click(findTestObject('Page_/Search Inventory/Search - Returned Product Name'))
 elementPresent=WebUI.waitForAlert(5)
 if (elementPresent==true) {
 alertText = WebUI.getAlertText()
 log.logWarning('The title of the alert is:=' + alertText)
 if (alertText=='There was an error retrieving results'){
 WebUI.acceptAlert() 
 KeywordUtil.markErrorAndStop('ERROR: There was an error retrieving results, output may not be complete.') 
 }
}

The script waits 5 seconds for the error to appear. If it does, the script confirms the error of the alert and writes that to log file. If it’s an error with the results, the dialog is dismissed, the test is marked as an error and execution stops.

If the error doesn’t appear the rest of the code continues, which is to verify details about the item I searched for.

Alerts can be tricky business, but Katalon has a few ways of handling them. My dialogs are pretty simple and straightforward and waitForAlert has served me well.

Other articles of interest:

Leave a Reply

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

Recent Comments
  • 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 🙂
  • How To Disable the Quicken Registration Prompt (25)
    • 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...
    • Susan Long: I bought my quicken disc in the beginning and it came with a registration number. I rang the helpline and they gave me the code to put in and talked me through it. It you downloaded your version then you don’t own it...
    • Peter: You might consider running your old version of Quicken on an ancient computer. This is what I have done for years. The newer versions are fraught with problems- criminal, in my opinion. One version made mathmatical errors when...
  • Working with Dates and Date Formatting in Katalon Studio (6)
    • Ajoo: Thank you for the details. How do i remove leading “0” from dates. i.e. while formatting i receive 04/21/2019, but i need 4/21/2019. (same applies for date)
  • What is Katalon Studio? A Distro of Selenium, Groovy and Eclipse (1)
    • Mahesh: Looking for more posts on katalon studio.your katalon stuffs are always exiting