Execute Javascript Alert Disappears

When using the WebUI.executJavaScript method in my test case to show an alert message, the alert will be shown for about 1 second and then automatically disappear. This wasn’t the functionality of older versions on Katalon Studio but seems to be the case now in 6.3.3. Is there a fix or workaround so that the alert message is shown without going away? I use alert messages to show if the test case passes or if it fails, I show the error message.

Here is an example of how I call the method:

WebUI.executeJavaScript(“alert(‘test’)”, null)

This is a tricky one to answer.

Immediately after the call to WebUI.executeJavaScript, webdriver is still running. That being the case, webdriver is supposed to issue an error, complaining about an alert that was not caught by the test script. (As to why you aren’t seeing that error is a matter for you). (As to why you think this worked before, I really can’t say).

Because webdriver is still running, the next attempt to access any JavaScript would fail catastrophically. The browser’s JavaScript engine is currently halted and it can’t run any other js on that thread (JavaScript is single-threaded).

Academic aside: To convince yourself this could be a problem, consider this code:

WebUI.executeJavaScript(“alert(‘test’)”, null)
WebUI.comment("Line after alert")

Question: will the comment appear after the alert dialog is displayed? Or will it be displayed after the alert is clicked and dismissed?

Now try it with a few different webdrivers (browsers).

Lastly, keeping in mind what we looked at above, if you ever decide to test remotely or decide to run your tests headless, who is going to click the button?

If you really want to make a report in the AUT (recommended only at the end of a test case) consider constructing a div containing your report and style it/position it to appear at the end of the HTML document. Hint: I do this. Benefit: nobody needs to click anything and it doesn’t matter if nobody sees it.

Here’s my report/div, a fail and a pass:

image

I confirmed it worked as I expected it to in version 5.10.1 as the alert message would stay until a user would click it away. We are a small company and only run these test scripts “on demand” right before and after we push updates to our web applications so there wouldn’t be a time where we do this remotely or headless.

I tested another thing by adding a WebUI.delay(10) after my WebUI.executeJavaScript(“alert(‘test’)”, null) call and my alert does indeed stay shown during the delay of 10 seconds. After that it goes away. This happens in both Chrome and Firefox as I am testing with both of those. It seems that once the test case finishes, it automatically clears out the alert, so I am not sure if there is a setting to change to prevent this from happening once the test case is done?

Is it the setting for “Terminate drivers after each TC” in Project Setting/Execution?

There was a chnnge in JS handling sometime during the 5.x branch - I posted about it at the time but I don’t recall enough to find it under Search.

Be pragmatic. Do what you gotta do re the problem.

The settings for terminate drivers are not checked as that would kill the actual browser session.

I don’t want to waste more time with this so I decided to display a MessageBox to the tester instead since the browser alerts auto-magically disappear with Katalon now.

import javax.swing.JOptionPane
import javax.swing.JDialog

final JDialog dialog = new JDialog();
dialog.setAlwaysOnTop(true);
JOptionPane.showMessageDialog(dialog, "Test Message");
1 Like

I use this work-around; not pretty but does the trick for me… Displays the same Alert 3 times so user can read the Alert

WebUI.openBrowser('')
def iCnt = 0
while (iCnt <= 3) {
    WebUI.executeJavaScript('alert(\'==> Java Alert Message <==\')', null, FailureHandling.OPTIONAL);
    Thread.sleep (1000);
    iCnt++;
}
WebUI.closeBrowser()
1 Like