Run Selenium Grid using Docker
Using Docker to setup a Selenium Grid is an excellent and efficient way of testing web applications.
Although Selenium can be run as a standalone server on a development machine and executing the tests in a local environment it is not the most efficient way. It requires the developer or tester to have the right version of the browsers installed on their machine including all web drivers for those browsers. When a browser is upgraded it often causes Selenium to stop working correctly and it can be very time consuming to setup and maintain a working environment. If someone else needs to run the tests on their machine, they need to do the same setup. Another issue is that it is very disruptive and distracting to run Selenium test on your local computer since it will open browsers, navigate, click on things so there will be a lot happening on your screen. While the tests are running the user is unable to do much else on the computer since any interaction may cause the Selenium tests to fail.
Selenium Grid enables us to run our tests on browsers that run on a remote machine. It consists of a hub and one or several nodes. The hub handles requests from our tests and asks the relevant nodes to execute the actions in the browser. Nodes can run different browsers like Chrome or Firefox. Developers no longer need to setup the browser environments and web drivers on their machines. Instead they just connect to the Selenium hub. Since there are no browsers popping up on the screen developers can continue working on other things while the tests are running.
Setting up Selenium Grid is a step in the right direction but it is not very easy to do. You will have to make sure the nodes can communicate with the hub and setup the browsers and drivers needed. Maintaining this as new versions of Selenium and web browsers are released is still a cumbersome task. There are online services like Sauce Labs and BrowserStack that provides Selenium testing for a monthly fee. They are a good alternative for scaling testing and not needing to setup the infrastructure yourself. But what if you rather use your own servers and machines?
Docker to the rescue! Selenium has created Docker images for Selenium hub and different kinds of nodes with different browsers. This allows developers to simply pull the images to their machine and start the containers with Selenium hub and the nodes. Everything is already configured out of the box. This removes the burden of setting up the environment on your local machine with correct browser versions and web drivers. As the tests run inside containers they don’t interfere with anything on your local machine. It is also extremely easy for someone else to run the test under the same conditions as on your own machine. They just have to pull the same images and start the containers. Selenium publishes new docker images as new versions are available so maintenance is not an issue.
The containers can be customised if you wish. It is very easy to scale testing by adding new nodes. If a container ends up in a bad state you can simply throw it away and create a new one. In continuous integration containers are a great way to run tests as well since you really don’t want to run browsers on your build servers. By using Docker you can easily apply the same setup across many different environments.
You can find an example project in my BitBucket repository