How to handle App permission pop ups (system popups like device location/contact list) in Appium android tests for OS Marshmallow and above?

PFB the code:

DesiredCapabilities capabilities = new DesiredCapabilities();
capabilities.setCapability(CapabilityType.BROWSER_NAME, ""); //Name of mobile web browser to automate. Should be an empty string if automating an app instead.
capabilities.setCapability(CapabilityType.VERSION, "6.0");
capabilities.setCapability(CapabilityType.PLATFORM, "Android");
capabilities.setCapability("deviceName", <deviceID>);
capabilities.setCapability("locationServicesAuthorized", true);
capabilities.setCapability("appPackage",<Package Name for app under test >); //Replace with your app's package	
capabilities.setCapability("appActivity",<Activity Name for app under test >); //Replace with app's Activity
driver = new AndroidDriver<AndroidElement>(new URL("http://127.0.0.1:4723/wd/hub"), capabilities);

When the app under test gets launched for the first time, it displays a Location permission popup.
The resource id for popup element is:
com.android.packageinstaller:id/dialog_container
which is outside package of app under test.
We are able to detect the elements of App permission pop up using uiAutomatorViewer.
But, we are unable to run script using the same element ids as those are not getting detected through the script.

We tried using below code:

**capabilities.setCapability("autoAcceptAlerts", true);**

This is not working.

We have even tried using:
driver.switchTo().a‌​lert().accept();
This also is not working.

Any suggestions will be appreciated.

Yeah… probably these alerts might be coming while your application contains maps. So, there are two possible ways to handle those.

  1. Turn off GPS setting on your test device.
  2. Switch between contexts by using driver.context… and you can identify those alert’s properties by UIAutomatorviewer too.

Happy Testing !! :smile:

Regards,
Vijay Bhaskar.

Hi @bhaskar,

Thanks for the reply but it didn’t work…
Even if we turn off GPS setting on our test device, we are getting GPS permission pop up on app launch.

Unfortunately, autoAcceptAlerts works only for iOS.

And, driver.switchTo().a‌​lert().accept(); works only for web view.

I am using Appium Inspector to find elements. The below code work for native app -

WebElement allowButton = driver.findElements(By.xpath("//android.widget.Button[@resource-id='com.android.packageinstaller:id/permission_allow_button'])"));
allowButton.click();

Not working, is there anything which can avoid such pop up for getting being dislayed

Hi Shruti,

I used the below code for the android Version 5.1 and its working perfectly for me.

if(driver.findElement(By.className(“android.widget.FrameLayout”)).isDisplayed());
{
driver.findElement(By.id(“button2”)).click();
}

	   Thread.sleep(2000);
1 Like

If you want to allow all the permissions to the app you are going to install, then you can auto grant all the permissions through DesiredCapability.
capabilities.setCapability(“autoGrantPermissions”, true);
This will not open the permission popup when the app is installed and launched.

5 Likes

Hi @Rohan1808 it’s work for me, I also face the same issue using API 23. Thanks for sharing

This capabilities worked for me:

caps = {}
caps[“platformName”] = “Android”
caps[“platformVersion”] = “7.0”
caps[“deviceName”] = “Pixel_C_API_24”
caps[“automationName”] = “UiAutomator2”
caps[“app”] = “/home/tests/app-debug.apk”
caps[“noReset”] = False
caps[“autoGrantPermissions”] = True
caps[“appPackage”] = “com.mypackage”
caps[“appActivity”] = “.MyActivity”

Regards.

Thanks it worked for me on Android version “Q”

This would work.

final WebElement allowButton = driverAndroid.findElement(By.id(“com.android.packageinstaller:id/permission_allow_button”));
allowButton.click();

final WebElement denyButton = driverAndroid.findElement(By.id(“com.android.packageinstaller:id/permission_deny_button”));
denyButton.click();

driver.switchTo().alert().accept(); worked for me on a native app. The trick was first adding a wait to make sure the alert existed.

WebDriverWait wait = new WebDriverWait(driver, 5);
wait.until(ExpectedConditions.alertIsPresent());
driver.switchTo().alert().accept();
1 Like

Hi @Rohan1808 i have used this code but after giving permissions my code is not working for further steps and even in console i am not getting any error , do i need to change view after giving permissions

Hi Rohan,
Will it work for both iOS and Android??

Hi
I used the following capabilities in my appium code but still my location pop is not disappearing.
caps.setCapability(“enablePerformanceLogging”, true);
caps.setCapability(“autoGrantPermissions”,true);
caps.setCapability(“extendedDebugging”, true);
caps.setCapability(“locationContextEnabled”, true);
caps.setCapability(“autoAcceptAlerts”, “true”);
caps.setCapability(“locationServicesAuthorized”, false)