在ChromeDriver中模拟移动设备

Pau*_*ish 14 mobile webdriver selenium-chromedriver

如果您将ChromeDriver与Chrome(通过Chromedriver)一起使用,则可能需要模拟移动视口特性.同样,您可能希望在桌面上自动执行测试,而无需在Android设置上使用正确的Chrome.

你是怎样做的?

Pau*_*ish 14

mobile_emulation功能已在2.11中添加到ChromeDriver

完整文档:https://sites.google.com/a/chromium.org/chromedriver/mobile-emulation

我的笔记如下:

使用mobile_emulation功能选项在Python中创建驱动程序:

 driver = self.CreateDriver(
        mobile_emulation = {
            'deviceMetrics': {'width': 360, 'height': 640, 'pixelRatio': 3}})
Run Code Online (Sandbox Code Playgroud)

目前,您可以模拟devicepixelratio,useragent,视口高度和宽度.

mobile_emulation dict的可能属性:

  • deviceName:如果使用,必须是唯一的财产.匹配Chrome中预设设备(例如'Google Nexus 5').
  • deviceMetrics:一个dict,可以包括width(int),height(int),pixelRatio(double),如上所示.
  • userAgent:一个欺骗请求标头和导航器对象的字符串.

  • 应该修复有效设备名称的链接?https://code.google.com/p/chromium/codesearch#chromium/src/third_party/WebKit/Source/devtools/front_end/emulated_devices/module.json&sq=package:chromium&type=cs (4认同)

小智 7

这是最新的官方chromedriver版本(2.11).

java中的示例:

final DesiredCapabilities dc = DesiredCapabilities.chrome();
dc.setCapability(ChromeOptions.CAPABILITY, new ChromeOptions() {
{
    setExperimentalOption("mobileEmulation", new HashMap<String, Object>() {
            {
                put("deviceName", "Google Nexus 5");
            }
        });
    }
});

ChromeDriver driver = new ChromeDriver(dc);
Run Code Online (Sandbox Code Playgroud)


Oli*_*upp 6

mobileEmulation选项在最后一个ChromeDriver版本(v 2.11)中实现.使用WebDriverJs,您必须将其作为属性添加到功能对象.

var webdriver = require('selenium-webdriver');
var capabilities = {
  browserName: 'chrome',
  chromeOptions: {
    mobileEmulation: {
      deviceName: 'Apple iPhone 5'
    }
  }
};
var
  driver = new webdriver
  .Builder()
  .withCapabilities(capabilities)
  .build();


driver.get('http://google.com');

var bool = false;
setTimeout(function () {
  bool = true;
}, 9000);
driver.wait(function() {
 return bool;
}, 10000);

driver.quit();
Run Code Online (Sandbox Code Playgroud)