Build a Selenium-Grid in minutes with Docker

To parallelize integration tests with Selenium you will need a Selenium-Grid. This is quite easy with Docker and Docker Compose (the new fig). It is also possible to build your own Docker container with different Firefox or Google Chrome versions and you can register your Windows or Mac computer with it's browsers to the Selenium-Hub. With a VNC client you can view the testing process, if necessary. At the end of this blog post, you will find a link to a full example on GitHub.

An extended german version was released in PHP Magazin 5.16.

Setup docker-compose.yml

Ok, let's create the docker-compose.yml with the following content. Details of the Docker container can be found at the Docker-Hub page of the specific container.

  image: selenium/hub
    - 4444:4444

  image: selenium/node-firefox-debug
    - 5900
    - selhub:hub

  image: selenium/node-chrome-debug
    - 5900
    - selhub:hub

Now you can start the Docker container with the command docker-compose up -d. After some minutes of downloading the images, the Docker containers (one Selenium-Hub and a Firefox and Google Chrome Selenium-Node) should run. This can be checked with the command docker ps. Three containers should be displayed.

Selenium-Grid console

To check if your Selenium hub and nodes are working open your browser with URL http://localhost:4444/grid/console. You should see something like this.
Selenium-Hub console

Scale Selenium-Nodes

If you want to run five tests in parallel for Firefox you must scale your Firefox Selenium-Node. This is done by one command, really. Execute docker-compose scale nodeff=5 and you should see five Firefox Docker container running and registered to the Selenium-Hub.

Connect to Selenium-Nodes with VNC Viewer

In the example above, we use the debug image so you can establish a connection with RealVNC Viewer to display the test progress. This could be helpful to check if something goes wrong. Note that you can also take a screenshot of your test case if it fails. Please look at the specific Selenium driver documentation.

Register other Selenium-Nodes

Currently only nodes for Firefox and Chrome are available, but you can also register an Internet Explorer or Safari to your Selenium-Hub. You can do this by using your computer or you create a virtual machine which registers the browser on start automatically. However register a new node is done by the following command java -jar selenium-server-standalone-2.45.0.jar -role node http://localhost:4444/grid/register -browser browserName=safari. Note that you must download a standalone Selenium server jar file before. Important are the options which describe the browser. So the Selenium-Hub can redirect the incoming test request to the specific Selenium-Node.

If your containers runs all the time, don't forget to enable Logrotate for them. Let me know if this article was helpful or if you have any suggestions. Please feel free to start a discussion.


Article picture: Hex by CreativeMagic under CC0 1.0

Discuss (0 Comments)