用于Selenium测试的Chrome移动模拟器无法使用所有设备

时间:2018-03-16 19:46:34

标签: java google-chrome selenium mobile emulation

我正在使用Chrome驱动程序及其模拟器对移动设备进行Selenium JAVA测试。问题是我无法使用最先进的移动设备,如iPhone 7,8等 - 尽管它在我的下拉列表中手动测试devtools时。 这是驱动程序init。与许多移动设备完美配合:

if(browser.equalsIgnoreCase("mobileIPhone6")){
          Map<String, String> mobileEmulation = new HashMap<String, String>();
          mobileEmulation.put("deviceName", "iPhone 6");
          String exePathChromeDriver = Consts.chromeDriverPath;
          System.setProperty("webdriver.chrome.driver", exePathChromeDriver);
          PropertyLoader.loadCapabilities();
          ChromeOptions chromeOptions = new ChromeOptions();
          chromeOptions.setExperimentalOption("mobileEmulation", mobileEmulation);
          driver = new ChromeDriver(chromeOptions);

但是,当我将第3行更改为&#34; iPhone 7&#34;我收到此错误:

2018-03-16 21:25:49 INFO  LogLog4j:210 - org.openqa.selenium.WebDriverException: unknown error: cannot parse capability: chromeOptions
from unknown error: cannot parse mobileEmulation
from unknown error: 'iPhone 7' must be a valid device
from unknown error: must be a valid device

知道为什么吗?非常感谢

3 个答案:

答案 0 :(得分:0)

为什么不使用Dimension课程?

定义枚举EmulatedDevices并使用以下样式:

driver.manage().window().setSize(new Dimension(EmulatedDevices.IPHONE7.getWidth(),EmulatedDevices.IPHONE7.getHeight()));

答案 1 :(得分:0)

我已经找到解决方案,并且现在可以正常工作。我必须先设置设备指标对象:

...else if(browser.equalsIgnoreCase("iPhone678")){
      String exePathChromeDriver = Consts.chromeDriverPath;
      System.setProperty("webdriver.chrome.driver", exePathChromeDriver);

      Map<String, Object> deviceMetrics = new HashMap<>();
      deviceMetrics.put("width", 375);
      deviceMetrics.put("height", 667);
      deviceMetrics.put("pixelRatio", 2.0);

      Map<String, Object> mobileEmulation = new HashMap<>();
      mobileEmulation.put("deviceMetrics", deviceMetrics);
      mobileEmulation.put("userAgent", "Mozilla/5.0 (iPhone; CPU iPhone OS 11_0 like Mac OS X) AppleWebKit/604.1.34 (KHTML, like Gecko) Version/11.0 Mobile/15A5341f Safari/604.1");

      ChromeOptions chromeOptions = new ChromeOptions();
      chromeOptions.setExperimentalOption("mobileEmulation", mobileEmulation);
      driver = new ChromeDriver(chromeOptions); }

答案 2 :(得分:0)

这应该有效:

    Map<String, String> mobileEmulation = new HashMap<>();
    mobileEmulation.put("deviceName", "iPhone 6");

    ChromeOptions chromeOptions = new ChromeOptions();
    chromeOptions.setExperimentalOption("mobileEmulation", mobileEmulation);

    driver = new ChromeDriver(chromeOptions);
    driver.get("https://www.google.co.uk");