【问题标题】:Chrome mobile emulator for Selenium testing- can't use all devices用于 Selenium 测试的 Chrome 移动模拟器 - 无法使用所有设备
【发布时间】:2018-08-25 23:25:23
【问题描述】:

我正在使用带有模拟器的 Chrome 驱动程序为移动设备进行 Selenium JAVA 测试。问题是我不能使用最先进的移动设备,如 iPhone 7,8 等,尽管在 devtools 中手动测试时它在我的下拉列表中。 这是驱动程序初始化。与许多移动设备完美配合:

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 行更改为“iPhone 7”时,我收到此错误:

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

知道为什么吗?非常感谢

【问题讨论】:

  • 我认为问题在于 chromedriver 不支持 iPhone 7。您可以查看 here 获取模拟设备列表,查看 here 获取设备预设名称。
  • 谢谢!我之前访问过您提供的第二个链接,我看到有 __"title": "iPhone 6/7/8",也适用于 iPhone X...
  • 是的,我也看到了,不知道是怎么回事。 iPhone 6/7/8 Plus 的图像以 iPhone6Plus-landscape.svg 作为其图像,所以它可能只是作为占位符吗?
  • 谢谢!这解释了很多。但是,我仍然很困惑。我之前访问过您提供的第二个链接,我看到有“标题”:“iPhone 6/7/8”,还有 iPhone X 等等……那是什么意思?它在这里列出了所有设备。甚至是新的
  • mmm...占位符可能是原因。非常感谢!

标签: java google-chrome selenium mobile emulation


【解决方案1】:

这应该可行:

    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");

【讨论】:

    【解决方案2】:

    我找到了解决方案,它现在可以正常工作。我必须先设置 Device Metrics 对象:

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

    【讨论】:

      【解决方案3】:

      为什么不使用Dimension 类?

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

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

      【讨论】:

      • 它有什么帮助?只是为了屏幕的大小吗? (我的意思是,这是模拟器给我们的唯一配置更改,屏幕大小?)
      猜你喜欢
      • 2012-01-02
      • 2019-12-24
      • 2014-12-08
      • 1970-01-01
      • 2020-01-11
      • 1970-01-01
      • 2018-09-10
      • 1970-01-01
      相关资源
      最近更新 更多