可以使用 CodeceptJS 在 chrome 浏览器上使用 devtools
Possibility to use devtools on chrome browser using CodeceptJS
我必须为 Web 应用程序编写测试,而且我必须在移动设备上使用它们 chrome browser.It 是否有可能在测试期间使用 chrome devtools 和移动设备模拟器?
感谢帮助
您可以通过将 chrome 选项传递给网络驱动程序来使用 Chrome 移动仿真。
例如,如果您使用 WebDriverIO 助手并希望使用 Nexus 5:
helpers: {
WebDriverIO: {
url: "https://rambler.ru",
browser: "chrome",
...
desiredCapabilities: {
chromeOptions: {
mobileEmulation: {
deviceName: "Nexus 5"
}
}
}
}
}
或者如果您想指定更具体的内容:
chromeOptions: {
mobileEmulation: {
deviceMetrics: { width: 360, height: 640, pixelRatio: 3.0 },
userAgent:
"Mozilla/5.0 (Linux; Android 4.2.1; en-us; Nexus 5 Build/JOP40D) AppleWebKit/535.19 (KHTML, like Gecko) Chrome/18.0.1025.166 Mobile Safari/535.19"
}
}
}
对于 Puppeteer,请在配置中使用 chrome 选项和 defaultViewport 值。
https://codecept.io/helpers/Puppeteer/#configuration
https://github.com/GoogleChrome/puppeteer/blob/master/docs/api.md#puppeteerlaunchoptions
"Puppeteer": {
"url": "https://rambler.ru",
"browser": "chrome",
...
"chrome": {
"defaultViewport": {
"width": 640,
"height": 360,
"deviceScaleFactor": 1,
"isMobile": true,
"hasTouch": true,
"isLandscape": false
}
}
}
或在每次测试前使用page.emulate()
https://github.com/GoogleChrome/puppeteer/blob/master/docs/api.md#pageemulateoptions
UPD:添加page.emulate示例
page.emulate
使用:
在自定义助手中,创建自己的函数,它将与页面一起使用,例如:
async emulateDevice(options) {
const currentPage = this.helpers['Puppeteer'].page;
await currentPage.emulate(options);
}
其中选项是带有视口和 userAgent 的对象:
https://github.com/GoogleChrome/puppeteer/blob/master/docs/api.md#pageemulateoptions
https://codecept.io/helpers/Puppeteer/#access-from-helpers
然后在测试中:
创建选项:
const myCustomViewPort = {
viewport: {
width: 640,
height: 360,
deviceScaleFactor: 1,
isMobile: true,
hasTouch: true,
isLandscape: false
},
userAgent: ""
}
您可以在代码中调用它:
Before(async (I) => {
await I.emulateDevice(myCustomViewPort);
});
我必须为 Web 应用程序编写测试,而且我必须在移动设备上使用它们 chrome browser.It 是否有可能在测试期间使用 chrome devtools 和移动设备模拟器?
感谢帮助
您可以通过将 chrome 选项传递给网络驱动程序来使用 Chrome 移动仿真。
例如,如果您使用 WebDriverIO 助手并希望使用 Nexus 5:
helpers: {
WebDriverIO: {
url: "https://rambler.ru",
browser: "chrome",
...
desiredCapabilities: {
chromeOptions: {
mobileEmulation: {
deviceName: "Nexus 5"
}
}
}
}
}
或者如果您想指定更具体的内容:
chromeOptions: {
mobileEmulation: {
deviceMetrics: { width: 360, height: 640, pixelRatio: 3.0 },
userAgent:
"Mozilla/5.0 (Linux; Android 4.2.1; en-us; Nexus 5 Build/JOP40D) AppleWebKit/535.19 (KHTML, like Gecko) Chrome/18.0.1025.166 Mobile Safari/535.19"
}
}
}
对于 Puppeteer,请在配置中使用 chrome 选项和 defaultViewport 值。
https://codecept.io/helpers/Puppeteer/#configuration https://github.com/GoogleChrome/puppeteer/blob/master/docs/api.md#puppeteerlaunchoptions
"Puppeteer": {
"url": "https://rambler.ru",
"browser": "chrome",
...
"chrome": {
"defaultViewport": {
"width": 640,
"height": 360,
"deviceScaleFactor": 1,
"isMobile": true,
"hasTouch": true,
"isLandscape": false
}
}
}
或在每次测试前使用page.emulate()
https://github.com/GoogleChrome/puppeteer/blob/master/docs/api.md#pageemulateoptions
UPD:添加page.emulate示例
page.emulate
使用:
在自定义助手中,创建自己的函数,它将与页面一起使用,例如:
async emulateDevice(options) {
const currentPage = this.helpers['Puppeteer'].page;
await currentPage.emulate(options);
}
其中选项是带有视口和 userAgent 的对象: https://github.com/GoogleChrome/puppeteer/blob/master/docs/api.md#pageemulateoptions https://codecept.io/helpers/Puppeteer/#access-from-helpers
然后在测试中: 创建选项:
const myCustomViewPort = {
viewport: {
width: 640,
height: 360,
deviceScaleFactor: 1,
isMobile: true,
hasTouch: true,
isLandscape: false
},
userAgent: ""
}
您可以在代码中调用它:
Before(async (I) => {
await I.emulateDevice(myCustomViewPort);
});