The following works for me:
KSE 8.2.0
Windows 10 Enterprise (64-bit)
Chrome: 97.0.4692.71 (Official Build) (64-bit)
import static com.kms.katalon.core.testobject.ObjectRepository.findTestObject
import com.kms.katalon.core.model.FailureHandling as FailureHandling
import com.kms.katalon.core.webui.keyword.WebUiBuiltInKeywords as WebUI
WebUI.openBrowser('http://demo.automationtesting.in/Alerts.html')
//Record the AlertBtn object or
//Create a manual AlertBtn object with XPath = //button[@onclick='alertbox()']
WebUI.waitForElementVisible(findTestObject('0.1 AlertChecks/AlertBtn'), 30, FailureHandling.OPTIONAL)
WebUI.click(findTestObject('0.1 AlertChecks/AlertBtn'), FailureHandling.OPTIONAL)
if (WebUI.verifyAlertPresent(30)) {
println('Alert is present')
}
I tried your code with KS v8.2.0 + Chrome 97.0.4692.71 on macOS 12.1. The Alert dialog disappeared instantly, and after 30 seconds the test case failed.
With Firefox 96.0.1 also failed.
With Safari 15.2 passed. I expected it to pass as reported previously.
Sadly the desired Capabilities solution did not work for me.
However, I did notice an interesting quirk, which I managed to take advantage of. In this situation I’m automating the keypress of the delete button on my form. The alert flashed on and then off. If I now manually pressed the delete button the Alert appeared and stayed. So with that in mind, I wrote this, which works. it’s not pretty, please let me know if there’s a nicer way.
Your code may be suffering from “compensating errors” and, luckily for you, you coded it in such a way (try-catch) that the final compensation “works”. The problem with compensating errors is this:
When you fix one of the errors, the other error then appears.
My guess is, you don’t need the try-catch. You merely need to…
WebUI.click(...)
WebUI.waitForAlert(5) // that's a LONG time!
WebUI.acceptAlert()
Of course, you may need to tinker with the timeouts, but I’d be surprised if you truly need anything else. And frankly, I couldn’t live with a try-catch like that – it’s just waiting to bite me.
You’re relaying on WebUI.verifyAlertPresent(3) to return a Boolean, which it does when the alert is there, but when it isn’t, it raises an exception. This seems to be the nature of most the WebUI functions that are suppose to return a Boolean.
It would have been nice if it just return a Boolean and let you decide how you want to handle it, but it doesn’t.
I have heard that if you put in the FailureHandling parameter onto the statement: while (!WebUI.verifyAlertPresent(3, FailureHandling.OPTIONAL))
or FailureHandling.CONTINUE_ON_FAILURE
that it should return the boolean instead of faulting.
And maybe
Also, just a note that you do not return a boolean with your method, although you have indicated you want to. You need to indicate the boolean with a return statement. Currently, it should be public static void verifyAlertPresent(TestObject to)
or public static verifyAlertPresent(TestObject to)
Also, how do you prevent an infinite loop with your while statement. Perhaps you should just have it as an if statement or add some reference for how many times you want it to search for your Alert before it gives up.
OMG, this issue is so annoying, guys. I got the same trying to refresh the page and need to accept the alert for not saving changes. I could not work around with delete button pressing.
I can try different steps to avoid this alert but again, this alert thing pisses me off