【问题标题】:Unable to run mobile emulation in custome device无法在自定义设备中运行移动仿真
【发布时间】:2020-11-06 00:58:45
【问题描述】:

我必须在不同的移动视图中运行一些测试用例。我正在尝试使用 Google Chrome 在移动仿真模式下测试运行。如果选择了现有列表中的设备但尝试从此处创建自定义设备,则测试用例工作正常:

并使用以下代码运行测试:

@Test
public void responsive() {
    String driverPath = "/Users/resources/drivers/chromedriver";
    System.setProperty("webdriver.chrome.driver", driverPath);
    Map<String, String> mobileEmulation = new HashMap<>();
    mobileEmulation.put("deviceName", "Pixel 3");
    ChromeOptions chromeOptions = new ChromeOptions();
    chromeOptions.setExperimentalOption("mobileEmulation", mobileEmulation);
    WebDriver driver = new ChromeDriver(chromeOptions);
    driver.get("https://www.google.com");
}

导致以下异常:

org.openqa.selenium.InvalidArgumentException:无效参数:条目 'firstMatch' 的 0 因参数无效而无效:无法解析 能力:来自无效参数的goog:chromeOptions:无法解析 来自无效参数的 mobileEmulation:“Pixel 3”必须是有效的 来自未知错误的设备:必须是有效的设备构建信息:版本: '3.13.0',修订:'2f0d292',时间:'2018-06-25T15:24:21.231Z'

是否有任何额外的东西需要在自定义设备中运行测试?

【问题讨论】:

    标签: java selenium google-chrome automated-tests selenium-chromedriver


    【解决方案1】:

    上的默认设备列表中版本 83.0.4103.116 我不认为 Pixel 3 是列出的设备。

    快照 A:

    快照 B:

    快照 C:


    解决方案

    要添加自定义(新)设备,您必须添加设备详细信息:

    快照:


    参考

    Add a custom mobile device

    【讨论】:

    • 感谢您的回答。一些代码示例会很有帮助。
    • @NarendraR 您现有的代码就足够了。只需添加Pixel 3默认不可用的配置即可。
    • 我没有看到任何其他选项来添加设备像素比率User agent string User agent type device type 那里
    【解决方案2】:

    不确定 chrome 中的自定义设备有什么问题。即使我使用以下代码打印所有设备,它也不会打印创建的自定义设备。

    try {
            ObjectMapper mapper = new ObjectMapper();
            Map map = mapper.readValue(
                    new File("/Users/<username>/Library/Application Support/Google/Chrome/Default/Preferences"),
                    Map.class);
            Map devTools = (Map) map.get("devtools");
            Map preferences = (Map) devTools.get("preferences");
            String standardEmulatedDeviceList = (String) preferences.get("standardEmulatedDeviceList");
            List emulatorMap = mapper.readValue(standardEmulatedDeviceList, List.class);
            System.out.println(emulatorMap.size());
            for (Object object : emulatorMap) {
                Map device = (Map) object;
                System.out.println(device.get("title"));
            }
        } catch (IOException ex) {
            System.out.println("Look there is an error..");
        }
    }
    

    但是有一种替代方法可以做到这一点。可以创建具有所需设备高度、宽度和像素比的设备矩阵。在 Chrome 选项中传递该设备矩阵而不是设备名称。最重要的是,必须通过设备用户代理才能使其表现得像设备屏幕。 This 是一个很好的获取设备规格的网站。

    我的工作代码在这里 -

    Map<String, Object> deviceMetrics = new HashMap<>();
    deviceMetrics.put("width", 412);
    deviceMetrics.put("height", 797);
    deviceMetrics.put("pixelRatio", 1.0);
    Map<String, Object> mobileEmulation = new HashMap<>();
    mobileEmulation.put("deviceMetrics", deviceMetrics);
    mobileEmulation.put("userAgent", "Mozilla/5.0 (Linux; Android 9; Pixel 3 XL Build/PD1A.180621.003) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/67.0.3396.87 Mobile Safari/537.36");
    ChromeOptions chromeOptions = new ChromeOptions();
    chromeOptions.setExperimentalOption("mobileEmulation", mobileEmulation);
    WebDriver driver = new ChromeDriver(chromeOptions);
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-01-30
      • 1970-01-01
      • 2015-10-05
      • 1970-01-01
      • 1970-01-01
      • 2019-06-14
      • 2014-08-30
      • 2017-01-28
      相关资源
      最近更新 更多