Chrome 移动模拟。 Selenium 和 Devtools 的窗口大小不同

2024-02-28

需要测试网站的责任。 代码示例取自here http://chromedriver.chromium.org/mobile-emulation(在页面底部) 面对的是,硒以不同的尺寸/分辨率打开镀铬窗口,比我预期的要好。看起来 PixelRatio 没有被接受或接受不正确。 另外,在开发工具中,默认设备列表中的设备大小已经按 PixelRatio 划分。例如Iphone 6 的分辨率为 750 x 1334,pixelRatio 2.0,但在 devtools 中它是 375 x 667,非常适合原始分辨率划分于像素比。但如果我使用 PixelRatio 2.0 手动添加自定义设备 750 x 1334,它仍然显示 750 x 1334 在源代码 https://chromium.googlesource.com/chromium/blink/+/master/Source/devtools/front_end/emulated_devices/module.jsonIphone 6 已经定义了“分割”尺寸和 PixelRatio 2.0

"title": "Apple iPhone 6",
            "screen": {
                "horizontal": {
                    "width": 667,
                    "height": 375
                },
                "device-pixel-ratio": 2,
                "vertical": {
                    "width": 375,
                    "height": 667
                }

这是我的移动设备枚举示例。

public enum ChromeMobileDevice {
   IPHONE_8(1334, 750, 2.0, "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"),
   IPHONE_XS_MAX(2688, 1242, 3.0, "Mozilla/5.0 (iPhone; CPU iPhone OS 12_0 like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) CriOS/69.0.3497.105 Mobile/15E148 Safari/605.1"),
   GALAXY_S8(2960, 1440, 4.0, "Mozilla/5.0 (Linux; Android 7.0; SM-G892A Build/NRD90M; wv) AppleWebKit/537.36 (KHTML, like Gecko) Version/4.0 Chrome/60.0.3112.107 Mobile Safari/537.36"),
   IPAD_MINI(2048, 1536, 2.0, "Mozilla/5.0 (iPad; CPU OS 12_1 like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/12.0 Mobile/15E148 Safari/604.1"),
   GALAXY_TAB_10_1(1920, 1200, 1.0, "Mozilla/5.0 (Linux; Android 8.1.0; SM-T580) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/74.0.3729.157 Safari/537.36");

   private int height;
   private int width;
   private double pixelRatio;
   private String userAgent;
   private boolean touch = false;

   private ChromeMobileDevice(int height, int width, double pixelRatio, String userAgent) {
      this.height = height;
      this.width = width;
      this.pixelRatio = pixelRatio;
      this.userAgent = userAgent;
   }

   public static ChromeMobileDevice getByName(final String chromeMobileDeviceName) {
      return Stream.of(ChromeMobileDevice.values()).filter(b -> b.name().equalsIgnoreCase(chromeMobileDeviceName)).findAny()
            .orElseThrow(() -> new IllegalArgumentException(String.format("Unsupported chrome emulator name: %s", chromeMobileDeviceName)));
   }

   public HashMap<String, Object> getPortraitDeviceMetrics() {
      return Maps.newHashMap(ImmutableMap.of(
            "height", height,
            "width", width,
            "pixelRatio", pixelRatio,
            "touch", touch));
   }

   public HashMap<String, Object> getLandscapeDeviceMetrics() {
      return Maps.newHashMap(ImmutableMap.of(
            "width", height,
            "height", width,
            "pixelRatio", pixelRatio,
            "touch", touch));
   }
}

UPDchromedriver 创建示例

ChromeOptions capabilities = new ChromeOptions();
capabilities.setCapability("chrome.switches", "disable-extensions");
capabilities.setCapability(SUPPORTS_JAVASCRIPT, true);
capabilities.addArguments("--hide-scrollbars");
capabilities.addArguments("--allow-running-insecure-content");
capabilities.addArguments("--start-maximized");
capabilities.addArguments("--disable-infobars");

Map<String, Object> mobileEmulation = new HashMap<>();
ChromeMobileDevice chromeMobileDevice = ChromeMobileDevice.getByName(chromeMobileEmulation.get());
mobileEmulation.put("userAgent", chromeMobileDevice.getUserAgent());
mobileEmulation.put("deviceMetrics", chromeMobileDevice.getPortraitDeviceMetrics());

capabilities.setExperimentalOption("mobileEmulation", mobileEmulation);
WebDriver driver = new ChromeDriver(capabilities );

None

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

Chrome 移动模拟。 Selenium 和 Devtools 的窗口大小不同 的相关文章

随机推荐