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
  • Round and Round with the For..Next in Katalon Studio (1)
    • Sweet Ophaline Labador: Hello can you help me how to loop this scenario. Checking the elements is just the same. I want to check that the following function is available in www.siacargo.com: Track Shipment – clicking on this can...
  • How To Disable the Quicken Registration Prompt (30)
    • David: A BIG Thank you! I regularly reinstall Windows OS’s using different hardware configs. In short, Quicken 2011 (it’s March of 2020 as I write this) has been a program I’ve been very happy with and continue to use....
    • Karen L: Today I rang Reckon Australia again, got a different guy and he talked me through the process of getting me a new product key to enter since I already had the licence and it had been extended. Reckon have only extended it by 1...
    • Karen L: I have Australian Quicken 2008 on Windows 8.1. It keeps asking me to renew my licence which Reckon did for me however the activation won’t work. I tried this to disable the registration prompt however it did not stop the...
  • Parsing Strings in Katalon – Split, Substring and Readlines (6)
    • Don Pedro: String tempText=”Date Month January. Revenue $1,355,721.00″ println(tempText.split(“ \$”)[1]) You need to escape the $ with \$ The [1] on the split captures characters on the right of the split [0]...
    • Prasad: Hi, I need to get the substring from the below string ‘Date Month January. Revenue $1,355,721.00’ I want to extract text after $ value. Please help Thank you
  • Working with Dates and Date Formatting in Katalon Studio (15)
    • Sangeethaa: How to get previous day’s date(Yesterday’s date)? Actually I was using today.previous() before,it was working fine till today morning.Now its not retrieving right date. Can anyone please guide me on this.
    • Don Pedro: That could be a little tricky. First, is the text/format always going to be the same? You will need to do some parsing. Separate the first but of text at 2019. After that, split the text again to get rid of Central Standard...
    • Don Pedro: It should be of the same Date type as today. You could then do some calendar trickery with Calendar /*Calendar c = Calendar.getInstance(); //c.set(Calendar.MONTH, 1) //Set the month, 0=January, 11=December println...
    • josh: I’m testing a page with a string like “Updated November 21, 2019 16:25:32 PM Central Standard Time.” How might I capture the date and time and compare it to current time to verify that the update time was within...
    • zakir hussain: today = new Date() lastWeek = (today – 1.week) can u please help me in understanding what is the return type of lastweek? and I wanted to set nextyear date how can i do it
  • Output status messages and test information by writing to the Log File Viewer in Katalon Studio (6)
    • Saranya: Good One. Could you plz share link to access all your katalon related blogs. ThankQ
    • Rodrigo Calabretta: I’m using the KeywordUtil.markFailed or KeywordUtil.markError and my test stops is being shown as Error and If I use in the @afterTestCase the testCaseContext.getTestCaseSta tus() to show the status test case...
  • Setting up Environment Profiles in Katalon Studio (2)
    • Don Pedro: Not quite sure I understand. The term “Execution Profile” is normally used now, but they are still GlobalVariables and are created in the same way for version 7 as previous. You make a new execution profile, then...
  • Here is my drive cluster (2)
    • Don Pedro: While I still have those drives, the drive cluster looks nothing like that now. Almost all of my external drives have had the cases removed and they have been installed into a series of MediaSonic 8 Bay Drive Enclosures. I...