简体   繁体   中英

Trying to accept location permission pop up with Appium/Android 7/Java

I am using Appium 1.6.3 and a Google Pixel with Android 7.1.1.

I am trying to load the Waze app which is already installed on the phone and accept the initial location permission request.

I have tried the following:

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

Also:

capabilities.SetCapability("autoAcceptAlerts", true);

But these doesn't seem to do the trick.

I have read on other sites, that the auto granting of permission is only available on iOS?

The code I have currently:

import io.appium.java_client.MobileBy;
import io.appium.java_client.android.AndroidDriver;
import io.appium.java_client.remote.MobileCapabilityType;
import org.apache.commons.io.FileUtils;
import org.openqa.selenium.*;
import org.openqa.selenium.remote.DesiredCapabilities;
import org.openqa.selenium.html5.Location;
import org.testng.annotations.AfterTest;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.Test;
import java.io.File;
import java.io.IOException;
import java.net.URL;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.concurrent.TimeUnit;

public class mapTests {
    AndroidDriver driver;
    Dimension size;
    String destDir;
    DateFormat dateFormat;

    @BeforeTest
    public void setup() throws Exception{
        DesiredCapabilities capabilities = new DesiredCapabilities();
        capabilities.setCapability("deviceName", "FA6C90301474");
        capabilities.setCapability(MobileCapabilityType.PLATFORM_NAME, "Android");
        capabilities.setCapability(MobileCapabilityType.PLATFORM_VERSION, "7.1.1");
        capabilities.setCapability("appPackage", "com.waze");
        capabilities.setCapability("appActivity", "com.waze.FreeMapAppActivity");
        capabilities.setCapability("noSign", true);
        capabilities.setCapability("autoGrantPermissions", "true");
        driver = new AndroidDriver(new URL("http://127.0.0.1:4723/wd/hub"), capabilities);
    }

    @Test
    public void testOne()throws Exception {
        Location location = new Location(53.7775174,-1.800881200000049,224);
        driver.setLocation(location);
        takeScreenShot();
    }

    public void takeScreenShot() {
        destDir = "screenshots";
        File scrFile = ((TakesScreenshot) driver).getScreenshotAs(OutputType.FILE);
        dateFormat = new SimpleDateFormat("dd-MMM-yyyy__hh_mm_ssaa");
        new File(destDir).mkdirs();
        String destFile = dateFormat.format(new Date()) + ".png";

        try {
            FileUtils.copyFile(scrFile, new File(destDir + "/" + destFile));
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    @AfterTest
    public void tearDown() throws Exception{
        driver.quit();
    }
}

Thank you!

Actually, I have found an solution to my problem:

WebElement allow_location = driver.findElement(By.id("com.android.packageinstaller:id/permission_allow_button"));
allow_location.click();
Thread.sleep(1000);

This works a charm!

Try to use:

driver.switchTo().alert().accept();

or

driver.switchTo().alert().dismiss();

And don't forget to wait until the alert is present and then click on it.

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.

尝试使用 AndroidMobileCapabilityType。它将启用 apk 所需的所有权限。

capabilities.setCapability(AndroidMobileCapabilityType.AUTO_GRANT_PERMISSIONS, "true");

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM