Issues with alerts and WaitForAlertKeyword

  1.  'org.openqa.selenium.NoAlertPresentException: no such alert
    

in case of WebUI.acceptAlert(FailureHandling.STOP_ON_FAILURE) used alone

/ i.e. w/o WebUI.waitForAlert(GlobalVariable.ShortTimeout, FailureHandling.STOP_ON_FAILURE) /

  1.  'builtin.WaitForAlertKeyword  - No alert found after 5 second(s) 
    

in case of

WebUI.waitForAlert(GlobalVariable.ShortTimeout, FailureHandling.STOP_ON_FAILURE)
WebUI.acceptAlert(FailureHandling.STOP_ON_FAILURE) used with

Visually, I can see the alert that appears almost immediately after clicking on the Remove link (time < 5 senonds) and then it almost immediately disappears.

As per https://docs.katalon.com/katalon-studio/docs/webui-wait-for-alert.html#description:

As usual, there is no explanation what ‘Timeout waiting for an alert to present.’ means. My expectation was that there should not be any error if the alert appears immediately or at any time within the timeout but it looks like waitForAlert() doesn’t work as I expected. The only explanation I can think of is that alert is too fast for waitForAlert() or waitForAlert() is too slow for the alert

Any idea what can be done to handle it? Thanks.

Try removing the WebUI.waitForAlert()

@Russ_Thomas, w/o WebUI.waitForAlert(), it’s just:

‘org.openqa.selenium.NoAlertPresentException: no such alert’

It was worth a shot – sometimes I don’t need it.

Now I think your test code is way ahead of the browser. Are you certain you’re placing the two lines immediately after the line that causes the alert?

@Russ_Thomas,

And is the alert a real window.alert() ?

@Russ_Thomas, I’m not sure what you mean by a real window.alert(). Could you please advise me how to determine whether it’s a real ‘window.alert’ or not? Visually, this alert looks like any other alert in our application. I noticed that when Katalon in the recording mode it’s impossible to accept this alert because it dissapears almost immediately.

A “real” window alert is an alert owned by the browser and is created from JavaScript code running on the page. It’s not typical to prefix the call with window. but for my purposes, it makes it clear which kind of alert I’m referring to.

Real alerts can be tested with methods like WebUI.waitForAlert, WebUI.acceptAlert, etc.

In contrast, a webpage might popup an alert constructed with pure HTML and shown to appear like an alert or message box. You cannot use the methods above to test those.

That’s a clue – it’s a real alert. You’re witnessing the webdriver/selenium clearing the alert.

So now I don’t know what to say to help you. Your code and my code are very similar – except mine works.

There were a few posts recently about alert issues – try searching. Maybe there’s a solution hiding in there.

@Russ_Thomas, to be precise, my code works as well (SOMETIMES)) but most of the time the very same code (please see above) is not working for the very same alert. That’s why it looks like the problem is with Katalon keywords not with the code and/or the alert.

Thank you for your help anyway! While you are here can you please answer the question I’ve been afraid to ask for a long time? How can I know that AJAX is being used on the page I’m automating and what is the best way to handle it in Katalon?

Firstly, don’t be afraid to ask questions - none of us are born with this knowledge. You have to wrestle with the stuff to learn and asking questions can sometimes let you leapfrog an otherwise arduous path.

Normally I would respond by saying “Create another topic as it’s off-topic for this thread”, but in this case, because it’s a tenuous (nebulous?) answer, I’ll offer you this:

Short answer: No, there is no one single way to find out.

Longer answer: Kind of. Maybe. If you’re lucky, if it’s a full moon :wink:

Seriously, if your webpage is using jQuery (just as an example) then yes, there is a really simple way to find out if jQuery is “busy”. For other technologies there may be similar things you can do. But mostly, you have to dig around and find out if there are any flags you can check for – every technology works differently.

Katalon’s Smart Wait tech is supposed to help but I’m not convinced it can cover all the possibilities now and in the future. Take this webpage – yes, the one you’re reading right now. Hit F12 to bring up the devtools. Open the console and watch the messages going to/from the server. It never stops. So, in answer to the question, “is AJAX finished?” No. It’s never finished (on this page, anyway).

So. It’s complicated and difficult to offer a singular, works-everywhere solution.

"So how do I test when I can’t find out whether AJAX is finished?"

Do exactly what humans do - go slower, wait for the effects of changes to complete. For example, if you know a certain click causes updates to a page, wait until ALL the changes are complete, THEN continue.

I think that about covers it. :sweat_smile:

1 Like