繁体   English   中英

如何在 Android 设备上处理允许/关闭地理位置弹出窗口

[英]How to handle allow/dismiss geo location popup on Android devices

在此处输入图片说明

我想知道如何在 Android 设备上处理地理位置弹出窗口

下面是一些高级细节

  1. 我正在使用 Saucelas 云来运行我的脚本
  2. 我正在使用 Chrome 浏览器
  3. Android-模拟器:Samsung Galaxy S8 Plus HD GoogleAPI Emulator Android-真实设备:Samsung_Galaxy_S10_POC173
  4. 测试环境:Selenium、Java、testNg

我已经尝试了所有可能的方法来处理这个问题,附件是我尝试过的代码片段

最初我认为这是一个警报并试图通过切换到警报来处理

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

然后我尝试使用配置文件,但它适用于 Windows,但不适用于 android

ChromeOptions options = new ChromeOptions();
DesiredCapabilities caps = DesiredCapabilities.android();
HashMap<String, Object> contentSettings = new HashMap<String, Object>();
HashMap<String, Object> profile = new HashMap<String, Object>();
HashMap<String, Object> prefs = new HashMap<String, Object>();
System.out.println("setting chrome options");
contentSettings.put("geolocation", 1);
profile.put("managed_default_content_settings", contentSettings);
prefs.put("profile", profile);
options.setExperimentalOption("prefs", prefs);*/
caps.setCapability(ChromeOptions.CAPABILITY, options);
driver = new RemoteWebDriver(new URL(pcUrl), caps);

然后我尝试提供一些选项,这也无法处理弹出窗口

options.addArguments("start-maximized");
options.addArguments("test-type");
options.addArguments("enable-strict-powerful-feature-restrictions");
options.addArguments("disable-geolocation");
caps.setCapability(ChromeOptions.CAPABILITY, options);
caps = caps.merge(DesiredCapabilities.chrome());

我想尝试以下https://www.browserstack.com/docs/automate/selenium/handle-permission-pop-ups#handle-know-your-location-pop-up

driver.context("NATIVE_APP");
driver.findElement(By.xpath(".//android.widget.Button[@text='Allow']")).click();

但是我无法将 AndriodDriver 输入到 RemoteWebDriver,对此有任何建议

或者如果有任何其他可能的方法来实现这一点,请告诉我。

您可以通过创建 Appium 会话来驱动 Mobile Chrome 而不是 Selenium 来实现这一点。 这应该允许您像在 Selenium 测试中一样与元素进行交互,但也可以更改为NATIVE_APP上下文以执行诸如关闭弹出窗口之类的操作。

您需要进行一些更改:

  1. 创建一个AndroidOptions实例而不是ChromeOptions ,然后将browserName功能设置为Chrome
  2. 创建AppiumDriver类型的WebElement代替RemoteWebDriver

我能够运行会话并关闭弹出窗口,如下所示:

    AndroidOptions options = new AndroidOptions();
    options.setCapability("deviceName","Google Pixel 3a GoogleAPI Emulator" );
    options.setCapability("platformVersion", "11.0");
    options.setCapability("automationName", "UIAutomator2");
    options.setCapability("browserName", "Chrome");

    Map<String, Object> sauceOptions = new HashMap<>();
    sauceOptions.put("name", "WW");
    sauceOptions.put("appiumVersion", "1.20.2");
    options.setCapability("sauce:options", sauceOptions);

    AppiumDriver<WebElement> driver = new AppiumDriver<WebElement>(new URL(address),options);
    driver.context("NATIVE_APP");
    driver.findElement(By.xpath(".//android.widget.Button[@text='Allow']")).click();

您可能还必须关闭 Android 本身的第二个弹出窗口:

   driver.findElement(By.xpath(".//android.widget.Button[@text='While using the app']")).click();

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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