Nightwatch 中 ChromeDriver 启用 Mobile Emulation 与 headless

在对移动端的项目进行 E2E 测试时,需要用到 ChromeDriver 的 Mobile Emulation 来模拟移动设备。

在 Javascript 中启用 Mobile Emulation 需要如下配置。

chrome: {
  desiredCapabilities: {
    browserName: 'chrome',
    javascriptEnabled: true,
    acceptSslCerts: true,
    chromeOptions: {
      args: ['--headless'],
      mobileEmulation: {
        deviceName: 'iPhone 6'
      }
    }
  }
}

deviceName 可以在 Chrome 浏览器的 开发者工具 - Settings - Devices 中找到。