有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java如何处理Android设备上的允许/取消地理位置弹出窗口

enter image description here

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

下面是一些高层细节

  1. 我正在使用Saucelabs云来运行我的脚本
  2. 我正在使用Chrome浏览器
  3. Android仿真器:三星Galaxy S8 Plus高清GoogleAPI仿真器 Android-realdevice:Samsung_Galaxy_S10_POC173
  4. 测试环境:Selenium、Java、testNg

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

起初,我认为这是一个警报,并试图处理切换到警报

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

然后我尝试了配置文件,但它在windows上运行良好,但在安卓上运行不好

ChromeOptions options = new ChromeOptions();
DesiredCapabilities caps = DesiredCapabilities.安卓();
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(".//安卓.widget.Button[@text='Allow']")).click();

但我无法向RemoteWebDriver键入cast和rioddriver,对此有何建议

或者,如果有任何其他可能的方式来实现这一点,请让我知道


共 (1) 个答案

  1. # 1 楼答案

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

    您需要做一些更改:

    1. 创建AndroidOptions的实例而不是ChromeOptions,然后将browserName功能设置为Chrome
    2. 创建类型为WebElementAppiumDriver而不是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();