How to handle Permission Requests on iOS?

Hey guys/gals,

On iOS, how do you handle Permission Requests (The alert pop-up that asks if the app would like to send you notifications)? Is it just switching to the alert and accept?

For the record I am using Appium 1.6.3 with iOS 10.2

Hi @James_B

unfortunately with XCUITest, the capability “autoAcceptAlerts” is not working.

In the meantime, I have the following code. I threw in the waiting for 2 seconds recently because sometimes it was too fast (surprisingly) when coming back to the app.

if (driver.FindElements(By.Name(“OK”)).Count == 1)
{
driver.FindElement(By.Name(“OK”)).Click();
Thread.Sleep(2000);
}

thank you for the help!

I forgot to mention I am using Java. Instead of count should I use size?

@James_B Hi
You can use following code,

WebElement alert = driver.findElements(By.name(“ok”));
if(alert != null) or if (driver.findElements(By.name(“ok”)) != null)
{
driver.findElements(By.name(“ok”)).click();
Thread.sleep(2000);
}

@Vlnoth_kumar thanks for the bit of code. I’ll give you an update if it works!

@James_B
Better you use this way always,

try {
      driver.findElements(By.name("ok")).click();
      Thread.sleep(2000);
} catch (Exception e) {
     system.out.println ("No alert presents!")
}
contine your code... 

This will work always without any issues.

Just as a note, the OK button is capitalized, so i think you want
try {
driver.findElements(By.name(“OK”)).click();
Thread.sleep(2000);
} catch (Exception e) {
system.out.println (“No alert presents!”)
}

I need to handle a similar situation, where sometimes the app brings up the Location Services prompt. Using code like above, it takes 15 seconds before moving on if the prompt is not there. 15 seconds is defined in my setup for implicitlyWait. I guess Appium is spending the 15 seconds on the “driver.findElements…” command looking for the element.

Since I need to address this situation in many scripts, it’s really slowing down the execution. Is there a way to implement code similar to the above without Appium taking spending the full implicityWait time? When the Location prompt comes up, it comes up right away. Unfortunately, it’s not 100% predictable when it will appear.

try the following

public IWebElement GetElement(By by)
{
WebDriverWait wait = new WebDriverWait(_driver, new TimeSpan(0, 0, 20));
wait.Until(ExpectedConditions.ElementToBeClickable(by));
return _driver.FindElement(by);
}
public IWebElement iOS_Ok()
{

            return GetElement(By.Name("OK"));
        
    }

this has a 20 second wait built in

1 Like

Thanks, I’ll give that a shot.

Incorrect code. Should use if(driver.findElements(By.name(“OK”)).size()>0)
driver.findElement(By.name(“OK”)).click();
for the prompt.

1 Like

Thank you so much
It works