如何创建一个npm脚本来运行几个命令来运行一些测试? [英] How to create a npm script to run several commands to run some tests?
问题描述
当我为我的angularjs应用程序运行e2e测试时,我需要在不同的shell会话中运行以下命令:
When I run the e2e tests for my angularjs application, I need to run following commands in different 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
当我启动它们时,前两个命令将继续运行.
The first 2 commands will keep running when I start them.
我试图添加一个npm脚本来定义一个将它们一起运行的任务:
I tried to add a npm script to define a task to run them together:
"scripts" : {
"e2e-test": "webdriver-manager start && node_modules/http-server/bin/http-server . && protractor test/protractor-conf.js"
}
问题是,当我通过以下方式运行它时:
The problem is, when I run it by:
npm run-script e2e-test
它只运行第一个并在那里阻塞,其他都没有运行的机会.
It just run the first one and blocking there, the other ones have no chance to run.
什么是最好的解决方案?
What's the best solution to do it?
推荐答案
问题是webdriver-manager start
和您的http服务器需要作为守护程序运行,或在后台与&
一起运行,如下所示:
Problem is that webdriver-manager start
and your http-server need to run as daemons or in background with &
like this:
"e2e-test": "(webdriver-manager start &) && sleep 2 && (node_modules/http-server/bin/http-server . &) && protractor test/protractor-conf.js"
还添加了一个sleep 2
来等待硒服务器启动,您可以通过使用
阻止脚本来进行主动等待
Also added a sleep 2
to wait a bit for the selenium server to start, you could get fancy with an active wait by blocking the script with
while ! nc -z 127.0.0.1 4444; do sleep 1; done
在这种情况下,最好将所有"e2e-test" shell行提取到单独的脚本中,即
In which case you'd be better off by extracting all that "e2e-test" shell line into a separate script, i.e.
"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
这篇关于如何创建一个npm脚本来运行几个命令来运行一些测试?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!