3

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.

shruti dinkar
  • 31
  • 1
  • 3
  • Have you tried `driver.switchTo().alert().accept();` ? – econoMichael Aug 16 '16 at 18:04
  • Hi @econoMichael, Thanks for the reply. We have already tried this. Even this isn't working. Do we need to typecast the driver to any other forms for using this code, as we are using AndroidDriver. – shruti dinkar Aug 17 '16 at 05:32

6 Answers6

3

App Permissions like Location, Storage, Camera etc can be accepted with the capabilities

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

This will allow all permission "Yes"

Please Note: ensure that "noReset" is "true" in the capabilities.

peeebeee
  • 2,541
  • 6
  • 21
  • 26
Sujit
  • 309
  • 1
  • 2
  • 8
  • It doesn't work with "noReset" is "true" in the capabilities. - requires to 'noReset' to 'false' – tan js Jan 04 '22 at 02:54
2

Late post.

You can use below code

capabilities.setCapability(“autoGrantPermissions”, true);

this will grant all permission to app and will not ask for any permissions.

Sawa
  • 41
  • 3
1

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();
Suman
  • 436
  • 4
  • 10
  • I have same issue , i have tried your option but is it is not working for me driver.findElement(By.xpath("//android.widget.Button[@resource-id='com.android.packageinstaller:id/permission_allow_button'])")).click(); Xpath is same for my web-element too – Pavan T Jul 17 '17 at 13:44
1

You can use this example to handle app permissions. This is taken from this BLOG POST .

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();
 }
}


}
anuja jain
  • 1,367
  • 13
  • 19
1

There are 2 ways

  1. by setting up permission capability as shown below

    capabilities.setCapability(“autoGrantPermissions”, true);
    
  2. By using xpath

    public WebElement alertPopup() {
    ele = driver.findElement(By.xpath("/hierarchy/android.widget.FrameLayout/android.widget.FrameLayout/android.widget.FrameLayout/android.view.ViewGroup/android.widget.ScrollView/android.widget.LinearLayout"));
    return ele;
    }
    
    public WebElement okBtnOnAlertPopup() {
    ele = driver.findElement(By.xpath("/hierarchy/android.widget.FrameLayout/android.widget.FrameLayout/android.widget.FrameLayout/android.view.ViewGroup/android.widget.ScrollView/android.widget.LinearLayout/android.widget.LinearLayout/android.widget.LinearLayout/android.widget.LinearLayout/android.widget.Button[2]"));
    return ele;
    }
    
    public void alertPopupIsPresent() {
    boolean alertPopupDisplayed = alertPopup().isDisplayed();
    System.out.println("alertPopupDisplayed - " + alertPopupDisplayed);
    if(alertPopupDisplayed){
        System.out.println("Inside IF");
        okBtnOnAlertPopup().click();
    }
    }
    
Suhail Ahmed
  • 504
  • 1
  • 9
  • 19
0

To enable location the best approach is

capabilities.setCapability("gpsEnabled", true);
TxSen
  • 1