Thursday, 5 January 2017

How to Handle App Permissions popup in Appium Test

We have faced this issue of Marshmallow Android Devices that apps shows permission popups asking for user permission to allow or deny. To handle this inside our automated test case is required. So this post will explain how we can handle this with simple code.
Consider we have an mobile app which asks for all required permission at the time of launch and we need to click on allow button each time it displays the App permission popup.

Check the below Test Case Example where we are calling allowAppPermission() method to click on each app permission popup. Let us see how the method internally works. In this method we have added a while loop until we get the allow app permission button . To locate the allow button we have used MobileBy api which is provided by lastest Appium Java Client.

Video Tutorial -


import java.util.concurrent.TimeUnit;

import org.junit.After;
import org.junit.Before;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.remote.CapabilityType;
import org.openqa.selenium.remote.DesiredCapabilities;
import org.openqa.selenium.remote.RemoteWebDriver;

public class WaitTests {

 WebDriver driver;

 public void setUp() throws MalformedURLException {
  DesiredCapabilities capabilities = new DesiredCapabilities();
  capabilities.setCapability("deviceName", "XT1562");
  capabilities.setCapability(CapabilityType.BROWSER_NAME, "Android");
  capabilities.setCapability(CapabilityType.VERSION, "6.0.1");
  capabilities.setCapability("platformName", "Android");
  driver = new RemoteWebDriver(new URL(""), capabilities);
  driver.manage().timeouts().implicitlyWait(15, TimeUnit.SECONDS);

 public void testFirst() {

 public void End() {
public void allowAppPermission(){
 while (driver.findElements(MobileBy.xpath("//*[@class='android.widget.Button'][2]")).size()>0) 

 {  driver.findElement(MobileBy.xpath("//*[@class='android.widget.Button'][2]")).click();


I hope you find this useful. Please share your feedback in comments section below and follow me on social media for latest post updates.


  1. This comment has been removed by a blog administrator.

  2. hi...The same code i have used in my project which is not working ..Please help.

  3. According to me the method allowAppPermission() called just before the alert appears on the screen. In case you have to add little wait time to get element appeared.
    If It does not work. Please verify the above locator is working correctly.

  4. Hi after to accept all app permissions, I can't navigate anymore in the next window do you have any idea the why?

  5. Hi,
    For my case, 'MobileBy cannot be resolved' error is generated. Please help

  6. use a desired capability.......
    capabilities.setCapability("autoGrantPermissions", true); It works for me in tab and mobile devices.

  7. I am automating WebApplication using appium on Tablet device. We are using google maps in that as I open the browser and visit the URL i get popup of location access to Allow or Block but I am unable to take any action on that popup through the selenium code. Can you please help me with solutions.

  8. Try adding proper waits on this code.

  9. Hi I am using Protractor to run my web application. When I try to upload Files, allow Deny Popup comes up to access Camera. I tried using "autoGrantPermissions", true, but this does not help me. I am not able to locate the popup also. Can you please help me with Solution.

  10. Hi i need a small explanation for this code


    for u you are allowing two popup's so u used button[2] , in my case only one popup so i did nt used [2] but its continuously running in while loop. its not coming out.

  11. Hi Anuja
    Very nice tutorial. I was able to close the alert message but now my scripts do not continue. They got stuck after clicking allow button.