首页 > 解决方案 > 可以使用 CodeceptJS 在 chrome 浏览器上使用 devtools

问题描述

我必须为 Web 应用程序编写测试,而且我必须在移动 chrome 浏览器上使用它们。在测试期间是否有可能使用 chrome devtools 和移动设备模拟器?

感谢帮助

标签: node.jswebdriverselenium-chromedrivercodeceptjs

解决方案


对于 Puppeteer,在配置中使用带有 defaultViewport 值的 chrome 选项。

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

推荐阅读