Selenium webdriver on it’s own, or with it’s implementation, like Geb is arguably the most popular solution for testing web-based applications. Besides all it’s greatness, it has some flaws. Selenium tests are slow, and it’s cost of maintenance is big. The answer for the first issue is distributed testing with Selenium Grid, which I described previously.
From the DevOps perspective though, setting Selenium Grid configuration like that is highly over-expensive and non-scalable. The answer for this can be Docker with it’s docker-compose tool. In this post we will try to create vm provisioned by docker-compose and set up scale Selenium Grid. All of this will be run with one command.
What is Docker
In simple words, Docker – with use of linux-containers – allows you to pack all your application dependencies, like database, system libraries and so on, into standardised and portable units, called containers. The main difference from virtualization tools like vagrant, is that you don’t need to ship entire OS to your CI or production server. Instead of this, you manage containers with independent units. This is just a big picture of the Docker motivation. For detailed documentation and installation instructions, please visit Docker official site.
Since there are plenty of Docker installation’s instructions on the web, we’ll assume that you’ve already done it (you can reffer to get started section from official documentation), and you have default machine up and running. To test your installation, type:
$ docker info
…and you should see similar output:
Setting Grid configuration
Little reminder of Selenium Grid architecture. The entry point of our Grid is Selenium Hub. It’s a place (vm or bare metal machine) where we point our test execution. Next element are nodes, which are machines that – previously registered to hub – can execute our selenium tests.
In order to create the hub on our localhost, we need to pull and run container from Docker repository with selenium hub:
$ docker run -d ‐‐name selenium-hub -p 4444:4444 selenium/hub
This command will download and run hub container on our localhost. When container’s downlaod is complete, visit http://localhost:4444/grid/console, and you should see an empty grid console (if you create your docker machine with different address than standard, change localhost to choosen IP).
Now we will create two nodes, one with Firefox and second with Chrome. To download and run chrome container:
$ docker run -d -P ‐‐link selenium-hub:hub selenium/node-firefox
…and for Chrome:
$ docker run -d -P ‐‐link selenium-hub:hub selenium/node-chrome
We should have three docker containers running on our local docker machine. We can check that with:
$ docker ps
Output should be similar to this:
We’ve created Firefox and Chrome nodes, so web console of our grid should display:
Provisioning with docker-compose
Everything is great, but what about this one-line command to start this whole thing up? Here comes docker-compose tool. Docker-compose is definition file for multi-container docker set ups. First of all, let’s stop all running containers. You can do it with:
$ docker stop $(docker ps -a -q)
We have to create docker-compose.yml file with the following content:
File structure is rather simple. We’ve defined seleniumhub entity, pointed image name (images can be found in docker hub repository) and assign ports. Then, we’ve defined two node entities: chromenode and firefoxnode. Important thing here is that we have to link them to seleniumhub container. Since it’s and yaml file, you should be aware of proper indentation. When our file is ready, run:
$ docker-compose up -d
If everything went smooth, you can check docker ps or point directly to your browser and open http://localhost:4444/grid/console. In result, just like previously there is hub with two nodes, but this time the configuration is define in one file and can be run with one command. Docker-compose file can be added now to your repository and reuse.
When our test base grows, two nodes can be far not enough. Luckily, docker-compose comes with great feature which allows to scale number of similar containers on the fly. If your two-nodes grid is running, and you want to increase the number of chrome nodes to three, enter command:
$ docker-compose scale chromenode=3
Now you have two more containers with chrome node, registered to your hub.
If you want to continue reading and expand your knowledge in area of Docker and Selenium Grid, I recommend you these books:
- Docker in Action – if you want to truly understand Docker, this is the book for you. Great reference and source of knowledge.
- The DevOps 2.0 Toolkit: Automating the Continuous Deployment Pipeline with Containerized Microservices – this one is more practical, not only about containers but building whole deployment pipelines.
- Selenium WebDriver Practical Guide – there’re not many materials about Selenium Grid, but this books is a great read if you want to master not only Grid, but Selenium in general
If you are running Selenium Grid configuration, Docker can be great way to boost your productivity and to help you managing your stack. In future post I will describe some more advanced configurations with Docker. If you have any questions, please leave a comment.