9

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.

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?

2 Answers 2

21

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"

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

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"

Then 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
Sign up to request clarification or add additional context in comments.

1 Comment

Leo, could you please describe how to make it cross platform? I mean I'd like to run it on windows too.
2

You should use npm-run-all (or concurrently, parallelshell), because it has more control over starting and killing commands. The operators &, | are bad ideas because you'll need to manually stop it after all tests are finished.

Once npm-run-once, protractor, http-server installed, you can modify package.json like that:

scripts: {
  "webdriver-start": "./node_modules/protractor/bin/webdriver-manager update && ./node_modules/protractor/bin/webdriver-manager start",
  "protractor": "./node_modules/protractor/bin/protractor ./tests/protractor.conf.js",
  "http-server": "./node_modules/http-server/bin/http-server -a localhost -p 8000",
  "python-example": "python -m SimpleHTTPServer",
  "test1": "npm-run-all -p -r webdriver-start http-server protractor",
  "test2": "npm-run-all -p -r webdriver-start python-example protractor"
}

-p = Run commands in parallel.

-r = Kill all commands when one of them finishes with zero.

Running npm run test1 will start Selenium driver, start http server (to serve you files) and run protractor tests. Once all tests are finished, it will close the http server and the selenium driver.

1 Comment

All npm-run-all does is literally combines the commands with either & for parallel, or && for serial. How does your example wait until webdriver and http-server are completely started before launching protractor?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.