Skip to content Skip to sidebar Skip to footer

How To Start A Server Before Protractor Runs And Clean Up Afterwards

It seems protractor doesn't provide any out of the box solution for starting a server before it runs. Having to run multiple commands before functional tests will run is a bad us

Solution 1:

Protractor also has onCleanUp that will run after all the specs in the file have finished.

And you are doing the right thing by keeping a reference to your process so that you can kill it later.

let webpackServerProcess;
beforeLaunch: () {
    webpackServerProcess = exec('blah'); // you could use spawn instead of exec
},
onCleanUp: () {
    process.kill(webpackServerProcess.pid);
    //or webpackServerProcess.exit();
}

Since you are launching the serverProcess with child_process.exec, and not in a detached state, it should go away if the main process is killed with SIGINT or anything else. So you might not even have to kill it or cleanup.

Solution 2:

I found a much more reliable way to do it using the webpack-dev-server node api. That way no separate process is spawned and we don't have to clean anything. Also, it blocks protractor until webpack is ready.

beforeLaunch: () => {
    returnnewPromise((resolve, reject) => {
      newWebpackDevServer(webpack(require('./webpack.config.js')()), {
        // Do stuff
      }).listen(APP_PORT, '0.0.0.0', function(err) {
        console.log('webpack dev server error is ', err)
        resolve()
      }).on('error', (error) => {
        console.log('dev server error ', error)
        reject(error)
      })
    })
  },

Post a Comment for "How To Start A Server Before Protractor Runs And Clean Up Afterwards"