如何创建一个npm脚本来运行几个命令来运行一些测试?

Fre*_*ind 9 testing npm angularjs

当我为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
Run Code Online (Sandbox Code Playgroud)

当我启动它们时,前2个命令将继续运行.

我尝试添加一个npm脚本来定义一起运行它们的任务:

"scripts" : {
    "e2e-test": "webdriver-manager start && node_modules/http-server/bin/http-server . && protractor test/protractor-conf.js"
}
Run Code Online (Sandbox Code Playgroud)

问题是,当我运行它时:

npm run-script e2e-test
Run Code Online (Sandbox Code Playgroud)

它只运行第一个并阻塞,其他人没有机会运行.

这样做的最佳解决方案是什么?

Leo*_*cci 20

问题是,webdriver-manager start你的http服务器需要作为守护进程运行或在后台运行,&如下所示:

"e2e-test": "(webdriver-manager start &) && sleep 2 && (node_modules/http-server/bin/http-server . &) && protractor test/protractor-conf.js"
Run Code Online (Sandbox Code Playgroud)

还添加了一个sleep 2等待selenium服务器启动的一点,你可以通过阻止脚本与活动等待获得幻想

while ! nc -z 127.0.0.1 4444; do sleep 1; done
Run Code Online (Sandbox Code Playgroud)

在这种情况下,最好将所有"e2e-test"shell行提取到一个单独的脚本中,即

"e2e-test": "your-custom-script.sh"
Run Code Online (Sandbox Code Playgroud)

然后 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
Run Code Online (Sandbox Code Playgroud)