简体   繁体   中英

Notification(Alert) comes randomly throughout application in selenium webdriver

I am using selenium webdriver with Java language. I am facing an issue that notification alert appears randomly through out application. Basically these alert have some information as it is functionality .

As script performs its steps but suddenly these alert appears on the screen and my script would fail due to alert message.

Please give your suggestions, How can we handle this type of alert which appears randomly on any window?

Below are two points come in my mind to handle this scenario:

  1. I will check Alert appears or not at every step (after click or other action) but it increases my execution time.

  2. Is there any way, we keep monitoring that Alert appears or not. If Alert appears just close the Alert if not then keep execute the steps of scripts.

Please suggest any workaround to handle such Alert so that our scripts do not fail.

This is the same scenario that we automate mobile application using Appium tool and suddenly any advertisement come on the screen.

It would be good if any one provide java code to handle this type of scenario.

Thanks in advance!!

If you want that unnecessary alert not appear during your script execution you can override you alert function before executing your script by using JavaScriptExecutor as below :-

JavascriptExecutor executor = (JavascriptExecutor)driver
executor.executeScript("window.alert = function () { return true}");

You can execute this script once every time when your page is loaded. this script will override your alert functionality and the alert will never occur.

I suggest you this script run when if your test is not depend on alert because after execute this script the alert will not be appear on the page.

other than you can handle alert as below :-

WebDriverWait wait = new WebDriverWait(driver, 100);
wait.until(ExpectedConditions.alertIsPresent());
Alert alert = webDriver.switchTo().alert();

Now if you want to accept alert, you can use :-

alert.accept();

and for cancel, you can use :-

alert.dismiss();

Note :- if alert is not present by using WebDriverWait it will throw TimeoutException .. so you need to handle it.

Edited..

For Appium automation to solve this problem, you can use a desired capability specifically designed to handle these alerts.

You can either always accept or always dismiss the alerts with these desired capabilities :-

autoAcceptAlerts = true
...
capabilities.SetCapability("autoAcceptAlerts", true);

or

autoDismissAlerts = true
...
capabilities.SetCapability("autoDismissAlerts", true);

Furthermore, some of the older versions of Appium haven't worked with this solution, so you might want to try a small workaround with this :-

driver.SwitchTo().Alert().Accept();

For more info follow this

Hope it will help you..:)

You can call this method wherever you are getting the alert.This method will accept the alert.

public void checkAlert(){
            if(ExpectedConditions.alertIsPresent() != null){
                driver.switchTo().alert().accept();
            }
        }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM