【发布时间】:2014-08-19 22:41:18
【问题描述】:
如果您将 WebDriver 与 Chrome 一起使用(通过 Chromedriver),您可能希望模拟移动视口特性。同样,您可能希望在桌面上自动执行测试,而无需在 Android 上使用正确的 Chrome 设置。
你是怎么做到的?
【问题讨论】:
标签: mobile webdriver selenium-chromedriver
如果您将 WebDriver 与 Chrome 一起使用(通过 Chromedriver),您可能希望模拟移动视口特性。同样,您可能希望在桌面上自动执行测试,而无需在 Android 上使用正确的 Chrome 设置。
你是怎么做到的?
【问题讨论】:
标签: mobile webdriver selenium-chromedriver
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}})
目前您可以模拟 devicepixelratio、useragent、视口高度和宽度。
Possible properties 用于 mobile_emulation 字典:
deviceName :如果使用,必须是唯一的属性。匹配 Chrome 中的 device preset(例如 'Google Nexus 5')。 deviceMetrics:一个字典,可以包含宽度(int)、高度(int)、pixelRatio(double),如上图所示。userAgent:用于欺骗请求标头和导航器对象的字符串。【讨论】:
这是最新的官方 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);
【讨论】:
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();
【讨论】: