How to create a npm script to run several commands to run some tests?

前端 未结 2 1148
春和景丽
春和景丽 2021-01-04 23:00

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-ma         


        
2条回答
  •  盖世英雄少女心
    2021-01-04 23:39

    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
    

提交回复
热议问题