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.

It's bad luck to be superstitious.

Author Signature for Posts

0