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.net.MalformedURLException;
import java.net.MalformedURLException;
import java.net.URL;
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;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;



public class WaitTests {

 WebDriver driver;

 @Before
 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("http://127.0.0.1:4723/wd/hub"), capabilities);
  driver.manage().timeouts().implicitlyWait(15, TimeUnit.SECONDS);
  
  
 }

 @Test
 public void testFirst() {
  
   alllowAppPermission();
   driver.findElement(By.name("Login")).click();
   driver.manage().timeouts().implicitlyWait(30,TimeUnit.SECONDS); 
}

@After
 public void End() {
  driver.quit();
 }
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.

15 comments:

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

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

    ReplyDelete
  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.

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

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

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

    ReplyDelete
  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.

    ReplyDelete
  8. Try adding proper waits on this code.

    ReplyDelete
  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.

    ReplyDelete
  10. Hi i need a small explanation for this code

    driver.findElements(MobileBy.xpath("//*[@class='android.widget.Button'][2]")

    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.

    ReplyDelete
  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.

    ReplyDelete
  12. Please add this capabilities, it will solve the problem.

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

    ReplyDelete
  13. can we make apk from this?

    ReplyDelete
  14. This comment has been removed by the author.

    ReplyDelete
  15. Thank You, this helped.

    ReplyDelete