Alerts are not handled if they appear before app gets launched

Hi All,

I have added capabilities_device1.setCapability(“autoAcceptAlerts”, true);

but if alerts appear before app gets launched these are not accepted. Is there a way to accept these alerts.

Basically this alert shows a message that app under test would like to send notifications.

Appium 1.4.13

Regards,
Venkatesh

Try the waitForappScript capability and click on the alert using the alert name like this.

capabilities.setCapability("waitForAppScript", "if (target.frontMostApp().alert().name()=='App name Would Like to Send You Notifications') {$.acceptAlert(); true;}");

I added below code

	capabilities_device1.setCapability("waitForAppScript", "if (target.frontMostApp().alert().name()=='\"app\" Would Like to Send You Notifications') {$.acceptAlert(); true;}");

added backslash as app name is enclosed in double quotes on Alert. But the alert is not handeled.

This is talked out at great length in the post below:
Gist “…The only known workaround for this Apple bug is delaying (in the app code) the request for permissions by a few seconds (in older versions of iOS, people reported that about 8s was solid). This gives UIAutomation a chance to spin up, so that by the time the dialog appears, we have an automation context we can use to to handle the alert (using e.g., the autoAcceptAlerts capability).”

If you are using java for your scripting, backslash is not needed. i have used without back slash and it worked for me.

`capabilities_device1.setCapability("waitForAppScript", "if (target.frontMostApp().alert().name()=='"App name" Would Like to Send You Notifications') {$.acceptAlert(); true;}");`

I too had same issue, sometime Notification popup appeared before app started. Below solution was fine so far
JAVA:
capabilities.setCapability(“waitForAppScript”, “$.delay(3000);$.acceptAlert()”);

Before I used capabilities.setCapability(“autoAcceptAlerts”,true); but It did not dismiss alert some time and then pop use to stay in foreground always and whole UIAutomation crashed mercilessly. But found above SOS

It’s not working for me with appum 1.6.3. On macosx 10.12

@Vikas_Kumar did you find any solution?

Yes. Basically we don’t get the handle of the alert immediately so I am waiting for it. Following is the Java implementation that I have used. Try - Catch because in case if the alert does not come up I don’t want to fail the test. “OK” is the name of the button that appears on the alert.
public void handleNotificationAlert()
{
WebDriverWait wait = new WebDriverWait(driver, 5);
try
{
wait.until(ExpectedConditions.presenceOfElementLocated(By.name(“OK”)));
click(By.name(“OK”));
}
catch (ElementNotFoundException e)
{
// Alert did not show up. Nothing to be done here.
}
}