3

I'm writing selenium test suites for NodeJS. Here's one sample test file:

var Sails = require('sails');

// create a variable to hold the instantiated sails server
var app;
var client;

// Global before hook
before(function(done) {

  // Lift Sails and start the server
  Sails.lift({
    log: {
      level: 'error'
    },
    environment: 'test',
    port: 1338
  }, function(err, sails) {
    app = sails;
    done(err, sails);
  });
});

// Global after hook
after(function(done) {
  app.lower(done);
});

beforeEach(function(done) {
  client = require('webdriverjs').remote({desiredCapabilities:{browserName:'chrome'}});
  client.init(done);
});

afterEach(function(done) {
  client.end(done);
});

describe("Go to home page", function() {
  it('should work', function(done) {
    client
      .url('http://localhost:1338/')
      .pause(5000)
      .call(done);
  });
});

Currently:

  • Starting each test file, it boots up the Sails server
  • Finishing each test file, it shutdowns the Sails server
  • Starting each test, it boots up the browser
  • Finishing each test, it closes the browser

Therefore, if I have 10 selenium test files, it will boot/shutdown the Sails server 10 times. Is there any way to boot up the Sails server only once, running all test files, then shut it down?

I'm using Sails + Mocha + webdriverjs stack. Here's my Makefile config

test:
    @./node_modules/.bin/mocha -u bdd -R spec --recursive --timeout 15000
.PHONY: test
Dário
  • 1,780
  • 1
  • 14
  • 26
Hoan Nguyen
  • 296
  • 3
  • 7

2 Answers2

5

One possible solution is to switch to using npm test, store your test execution line in your package.json file, and then take advantage of the pretest and posttest script phases. Within these commands you could execute a script which will start up your server (startSailsServer.js), and shutdown your server, respectively. You could then take out the starting and stopping of your server in each test file.

So your package.json would have something like this (you would have to move the start/stop sails server logic to these startSailsServer.js and stopSailsServer.js files):

"scripts": {
    "pretest": "node startSailsServer.js",
    "test": "./node_modules/.bin/mocha -u bdd -R spec --recursive --timeout 15000",
    "posttest": "node stopSailsServer.js"
}

Then to run your tests, you would execute npm test

dylants
  • 19,092
  • 3
  • 24
  • 21
1

Thanks for dylants suggestion, I edited the Makefile to utilize the "pre/post-test" script phases:

## Makefile
test:
    /bin/bash test/script/startServer.sh
    @./node_modules/.bin/mocha -u bdd -R spec --recursive --timeout 15000
    /bin/bash test/script/stopServer.sh

## test/script/startServer.sh
# Start Selenium
start-selenium &
echo $! > tmp/selenium.pid
sleep 1

# Start Node server
NODE_ENV=test PORT=1338 node app.js &
echo $! > tmp/test.pid

## test/script/stopServer.sh
kill -SIGINT $(cat tmp/selenium.pid)
kill -SIGINT $(cat tmp/test.pid)
Hoan Nguyen
  • 296
  • 3
  • 7