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.

6 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