End to End testing with Selenium on NodeJS
For my latest project I needed to add end to end tests to an Express/React application. The requirements were pretty straight-forward:
- The tests should be easy to setup and run.
- The tests should be able to run both on a desktop browser and on a headless environment.
I wanted to use Selenium for interacting with the browser, but I didn’t want to deal with installing a selenium server, and I definitely wanted to write my tests in Javascript. Luckily, I found a couple of tools to help me out:
- selenium-standalone takes care of installing and starting a selenium server for you.
- selenium-webdriver lets you interact with Selenium using Javascript.
The last piece of the puzzle was choosing a test framework, and getting both the selenium server and my app running before the test suite.
So, I went with Jasmine for the test framework (mostly because I was already familiar with it) and I wrote the following script for getting my tests running:
Now anyone can run the test suite with a simple npm install
& npm test
.
Note the script will start both a selenium server and an instance of your app, but you can also pass in SELENIUM_SERVER
and TEST_WEB_SERVER
as environment variables if you prefer to use your own.
You can take a look at the complete example here: https://github.com/alexishevia/blogExamples/tree/e2e_node