Vue + nightwatch + Chrome76进行e2e测试的正确配置(二)

版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接: https://blog.csdn.net/HermitSun/article/details/99762388

在这个问题困扰了我好几个小时,因为在Windows下确实没有问题。直到我看到了这条issue:

Strangely, seeing this happen on RHEL (start_server=true and immediately a “cannot connect to :4444” before the Selenium server is started), but not on Windows. It looks like putting this into the configuration

selenium: {
  ...,
  check_process_delay: 5000,
}

has an effect - see https://github.com/nightwatchjs/nightwatch/blob/v1.1.13/lib/runner/wd-instances/selenium-server.js#L36-L38 - maybe some default value was lost?

那我们就来改一下配置:

// tests/e2e/specs/test.js
module.exports = {
  selenium: {
    check_process_delay: 5000
  }
};

但是并没有什么作用,因为这是测试文件,而不是配置文件。从官方文档里可以看到,selenium的配置是要在配置文件里进行的。事实上,我们之前写的配置,其实都是test_settings.default的一部分。但是我们之前是通过vue add @vue/e2e-nightwatch进行的,配置文件并不是自己写的。先在node_modules里改一下试试:

// node_modules/@vue/cli-plugin-e2e-nightwatch/nightwatch.config.js
module.exports = deepmerge({
  ...,
  selenium: {
    ...,
    check_process_delay: 5000
  },
  ...,
}, userOptions);

改完之后报错信息变了:

Response 500 POST /wd/hub/session (1421ms)
   { value:
      { error:
         [ '  (unknown error: DevToolsActivePort file doesn\'t exist)',
           '  (The process started from chrome location /usr/bin/google-chrome is no longer running, so ChromeDriver is assuming that Chrome has crashed.)',
           '  (Driver info: chromedriver=76.0.3809.68 (420c9498db8ce8fcd190a954d51297672c1515d5-refs/branch-heads/3809@{#864}),platform=Linux 4.15.0-1028-gcp x86_64) (WARNING: The server did not provide any stacktrace information)',
           'Command duration or timeout: 471 milliseconds',
           'Build info: version: \'3.141.59\', revision: \'e82be7d358\', time: \'2018-11-14T08:25:53\'',
           'System info: host: \'travis-job-bcc35d64-5515-45dc-90ef-ff805e5dfaf7\', ip: \'127.0.0.1\', os.name: \'Linux\', os.arch: \'amd64\', os.version: \'4.15.0-1028-gcp\', java.version: \'11.0.2\'',
           'Driver info: driver.version: unknown' ],
        message: 'unknown error: Chrome failed to start: exited abnormally' },
     status: 13 }
   Error: An error occurred while retrieving a new session: "unknown error: Chrome failed to start: exited abnormally"
       at endReadableNT (_stream_readable.js:1145:12)
       at process._tickCallback (internal/process/next_tick.js:63:19)

查了半天,发现是chromedriver没装上……但是在Windows上装上了啊?仔细想想,服务器上没有梯子,Google由于某些众所周知的原因连不上,所以……

换成淘宝源:

npm install chromedriver --chromedriver_cdnurl=http://cdn.npm.taobao.org/dist/chromedriver

装上是装上了,但还是一样的报错。又查了半天,想起来Linux是命令行界面,没有GUI,浏览器不能直接启动……所以再改一下配置文件,加上不启动GUI的设置:

module.exports = {
  desiredCapabilities: {
    chromeOptions: {
      args: ['--headless', '--no-sandbox', '--disable-gpu'],
      w3c: false
    }
  }
};

这下终于正常了。

但是直接在node_modules里改也不是个事,总得有个解决方案吧。回过头来读一下@vue/cli-plugin-e2e-nightwatch的README,可以看到这么一段:

We’ve pre-configured Nightwatch to run with Chrome by default. If you wish to run e2e tests in additional browsers, you will need to add a nightwatch.config.js or nightwatch.json in your project root to configure additional browsers. The config will be merged into the internal Nightwatch config.

原来只需要在根目录下创建一个配置文件就行了,js或者json都行。从扩展性上来说js文件要强一点,所以我选择了js文件(虽然放在一堆ts文件里有点突兀):

// nightwatch.config.js
module.exports = {
  selenium: {
    check_process_delay: 5000
  }
};

从官方的配置文件里也可以看出,这些配置会被deepmerge合并进去。再次运行,一切正常。

同样地,我们也可以把测试文件的配置拿出来,这样就不用每个测试文件配置一次了:

module.exports = {
  selenium: {
    check_process_delay: 5000
  },
  test_settings: {
    chrome: {
      desiredCapabilities: {
        browserName: 'chrome',
        javascriptEnabled: true,
        acceptSslCerts: true,
        chromeOptions: {
          args: ['--headless', '--no-sandbox', '--disable-gpu'],
          w3c: false
        }
      }
    }
  }
};

这下终于解决了。如果还是不清楚……可以看看示例项目:chrome76-e2e-test-conf-example的stage2分支。

目录

Vue + nightwatch + Chrome76进行e2e测试的正确配置(二)

猜你喜欢

转载自blog.csdn.net/HermitSun/article/details/99762388