当我为我的 angularjs 应用程序运行 e2e 测试时,我需要在不同的 shell session 中运行以下命令:
// 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 start
和你的 http-server 需要作为守护进程或在后台运行 &
像这样:
"e2e-test": "(webdriver-manager start &) && sleep 2 && (node_modules/http-server/bin/http-server . &) && protractor test/protractor-conf.js"
还添加了一个 sleep 2
来等待 selenium 服务器启动,您可以通过使用
while ! nc -z 127.0.0.1 4444; do sleep 1; done
在这种情况下,您最好将所有“e2e-test”shell 行提取到单独的脚本中,即
"e2e-test": "your-custom-script.sh"
然后 your-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
关于angularjs - 如何创建一个 npm 脚本来运行多个命令来运行一些测试?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25041643/