Running Nightwatch.js without Selenium

The first small speed improvement can be achieved by eliminating Selenium from the setup. As far as I know, the following approach, of running tests in Nightwatch.js without Selenium, for now is only possible with Google Chrome. If you want to run tests in Browsers other than Google Chrome, you still have to rely on Selenium or you can use BrowserStack, which also doesn’t require you to have Selenium installed locally.

To run tests in Google Chrome directly without using Selenium, you have to change your Nightwatch.js configuration.

Thats it! After amending your configuration and adding the hooks to start and stop the ChromeDriver, you’re ready to run your tests without having to rely on Selenium. Although the gains in speed are not huge, depending on your setup, you’ll save a few seconds here and there.

Parallelization with test workers

By default, Nightwatch.js runs your tests sequentially, but most modern computers are multi core machines and are able to run multiple processes in parallel. Luckily Nightwatch.js supports running your tests in parallel with so called test workers. To enable test workers, you have to add the following line to your configuration file.

module.exports={// ...
test_workers:true,// ...
};

By default, Nightwatch.js will use all available CPU cores – if you have 2 cores, it will run 2 tests in parallel, if you have 4 cores it will run 4 tests in parallel, and so on.

If you want to limit the number of cores which Nightwatch.js is allowed to use, you can specify a fixed number.

In the example above we’re utilizing 3 cores for running tests. But be careful: if you’re sharing your configuration in a Git repository with other users, there might be people with less powerful machines and less cores.

You might notice, that the output is slightly obscured when running tests in parallel. But there is a setting to improve readability of the output with test workers enabled.

Conclusion

In a world of TDD, there is nothing as annoying as having to wait for your tests to finish.

Eliminating Selenium from your test setup can improve the speed of your tests slightly. Enabling parallelization with test workers takes it a step further by speeding up your tests by a factor equal to the number of cores you have in your machine (at least theoretically speaking).