2019.08.19 修正文章中的错误
除了单元测试之外,前端还有一个额外的测试:e2e测试,也就是端到端测试,用来模拟用户操作。对于Vue来说,推荐的e2e测试框架有nightwatch和cypress,因为cypress只支持Chrome,而且尤大之前用的是nightwatch,所以我也选择了nightwatch。
vue-cli对nightwatch的整合可以说很完善了,基本上只需要一条命令就能完成安装和配置,然后就可以开始写测试了:
vue add @vue/e2e-nightwatch
之所以说基本上只需要一条命令,因为nightwatch基于java,需要先安装和配置Java。
本来一切都很美好,但是前段时间Chrome自动更新到了76版本,然后就出了问题。用vue-cli新建一个项目,新建的项目居然过不了测试!报错如下:
Error retrieving a new session from the selenium server
Connection refused! Is selenium server started?
{
value:
{
message:
'Unable to create new service: ChromeDriverService\nBuild info: version: \'3.141.59\', revision: \'e82be7d358\', time: \'2018-11-14T08:25:53\'\nSystem info: host: \'wensun\', ip:
\'192.168.199.1\', os.name: \'Windows 10\', os.arch: \'amd64\', os.version: \'10.0\', java.version: \'1.8.0_211\'\nDriver info: driver.version: unknown',
error: 'session not created' },
status: 33 }
这就很神秘了。网上转一圈,看到说是chromedriver版本过低,也就是测试需要的驱动掉链子了。因为从报错信息里可以看到,selenium无法识别驱动的版本,显示为unknown。那就装一个新版本的驱动:
npm install --save-dev chromedriver
但是连这个都报错了:
npm ERR! The operation was rejected by your operating system.
npm ERR! It's possible that the file was already in use (by a text editor or antivirus),
npm ERR! or that you lack permissions to access it.
npm ERR!
npm ERR! If you believe this might be a permissions issue, please double-check the
npm ERR! permissions of the file and its containing directories, or try running
npm ERR