Chrome移动设备仿真。窗口大小因Selenium和Devtools而异

时间:2019-07-16 11:15:55

标签: selenium google-chrome-devtools emulation mobile-chrome

需要测试网站的责任。 摘自here的代码示例(在页面底部) 面对这一问题,硒将以不同的尺寸/分辨率打开镀铬窗口,超出我的预期。看起来pixelRatio没有被接受或被错误地接受。 同样,面对在devtools中,默认设备列表中的设备大小已经在pixelRatio上划分了。例如Iphone 6具有750 x 1334和pixelRatio 2.0,但在devtools中为375 x 667,非常适合 原始分辨率 除以 pixelRatio 。但是,如果我用pixelRatio 2.0手动添加自定义设备750 x 1334,它仍然显示750 x 1334 在source code中,iPhone 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));
   }
}

UPD chromedriver创建示例

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

0 个答案:

没有答案