简体   繁体   中英

Running protractor when webdriver-manager start finishes

I'm having trouble writing a npm script for starting my protractor tests. Following the documentation https://github.com/angular/protractor/blob/master/docs/server-setup.md seems that first I have to run

webdriver-manager start

and leave it running while I'm starting the actual tests

./node_modules/.bin/protractor test/integration/conf.js

I want these two steps to execute inside one npm script, so I would end up with something like this:

"scripts": {
    "protractor-update": "./node_modules/.bin/webdriver-manager update",
    "protractor-start": "./node_modules/.bin/webdriver-manager start",
    "protractor-init": "npm run protractor-update && npm run protractor-start",
    "test-protractor": "npm run protractor-start && ./node_modules/.bin/protractor test/integration/conf.js",
},

Now obviously the problem is that ./node_modules/.bin/webdriver-manager start doesn't return an exit code so it never makes to the next command.

What would be the proper way of achieving this?

I had the same problem as you. This is the solution I found after browsing on StackOverflow and with the help of my colleague.

In package.json:

"scripts": {
  "e2e": "bash run-e2e-tests.sh"
}

The bash file:

# Start selenium server and trash the verbose error messages from webdriver
webdriver-manager start 2>/dev/null &
# Wait 3 seconds for port 4444 to be listening connections
while ! nc -z 127.0.0.1 4444; do sleep 3; done
#  run protractor
protractor test/e2e/conf.js

The answer lies in shell scripting basics.

Put the webdriver in the background with &

So for example

webdriver-manager start &

This will run the webdriver-manager and then immediately return control to the shell, allowing you to enter subsequent commands.

You will want to kill the webdriver-manager after the tests are done via some mechanism, such as kill %1 or pkill webdriver-manager . Otherwise you will eventually have dozens of unused webdriver processes running...

You can use concurrently to solve your problem.

Install concurrently with following command.

npm install concurrently --save

Then use in your package.json like below:

"e2e:all-steps": "concurrently -k -s first \"./node_modules/.bin/webdriver-manager update\" \"./node_modules/.bin/webdriver-manager start\" \"npm run protractor-update && npm run protractor-start\" \"npm run protractor-start && ./node_modules/.bin/protractor test/integration/conf.js\""

Hope that helps. :-)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM