当我为
angularjs应用程序运行e2e测试时,我需要在不同的shell会话中运行以下命令:
// start the selenium server webdriver-manager start // start a http server to serve current files node_modules/http-server/bin/http-server . // run the e2e tests protractor test/protractor-conf.js
当我启动它们时,前2个命令将继续运行.
我尝试添加一个npm脚本来定义一起运行它们的任务:
"scripts" : { "e2e-test": "webdriver-manager start && node_modules/http-server/bin/http-server . && protractor test/protractor-conf.js" }
问题是,当我运行它时:
npm run-script e2e-test
它只运行第一个并阻塞,其他人没有机会运行.
这样做的最佳解决方案是什么?
问题是webdriver-manager启动,你的http服务器需要作为守护进程运行,或者在后台使用&像这样:
"e2e-test": "(webdriver-manager start &) && sleep 2 && (node_modules/http-server/bin/http-server . &) && protractor test/protractor-conf.js"
还添加了一个睡眠2来等待selenium服务器启动,你可以通过阻止脚本来获得活跃的等待
while ! nc -z 127.0.0.1 4444; do sleep 1; done
在这种情况下,最好将所有“e2e-test”shell行提取到一个单独的脚本中,即
"e2e-test": "your-custom-script.sh"
然后你的–custom-script.sh
#!/usr/bin/env bash # Start selenium server just for this test run (webdriver-manager start &) # Wait for port 4444 to be listening connections while ! nc -z 127.0.0.1 4444; do sleep 1; done # Start the web app (node_modules/http-server/bin/http-server . &) # Guessing your http-server listen at port 80 while ! nc -z 127.0.0.1 80; do sleep 1; done # Finally run protractor protractor test/protractor-conf.js # Cleanup webdriver-manager and http-server processes fuser -k -n tcp 4444 fuser -k -n tcp 80