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

66bbxpm5  于 2023-06-19  发布在  Go
关注(0)|答案(4)|浏览(148)

我正在使用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行更改为“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

知道为什么吗非常感谢

3lxsmp7m

3lxsmp7m1#

我已经找到了解决方案,现在它工作正常。我必须首先设置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); }
l3zydbqr

l3zydbqr2#

为什么不使用Dimension类?
定义枚举EmulatedDevices并使用以下样式:

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

mzillmmw3#

这应该可以工作:

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

mfpqipee4#

您可以使用Chrome devtools >设备中的“仿真设备”列表中提到的所有设备。如果列表中没有所需的设备,您可以“添加自定义设备”(screenshot)-提供基本信息,并在代码中使用deviceName。换句话说,您可以在自动化代码中直接提供所需设备的基本信息(width, height, pixelRatio),以便使用该设备进行测试。你可以得到详细的代码与解释here.

相关问题