_ Blog / Technology

5+ Reasons to Use Selenium with Docker for Application Testing

by Suraj Vijayaraghavan
/ September 13, 2018
#Technology
Selenium with Docker

Application testing is a key element to make sure your web applications run smoothly, as it tells you whether the tested scenario works without a hitch and the features run as validated. Automating the tests and CI (continuous integration) is one of the best practices followed by developer teams and testing teams. However, several companies skip testing as they consider it an annoying extra step that slows the development process.

That could not be further from the truth. End to end testing of applications before they are deployed will save the company time and money in the long run. Not only do the users get a more reliable app, but they are also more likely to stick with it when it performs smoothly and as expected.

It is important to realize that high test coverage, code reviews and pull requests are required for a fast pace and high-quality code. Docker gives you the best of this because you can run your tests in containers or isolate them in development and deployment.

Selenium also helps you run automated tests through web browsers and UI level tasks like scrolling, clicking, filling in fields, etc. The core components in a Selenium Grid are Selenium Server and Selenium WebDriver.

State of Serverless 2020

ABOUT SELENIUM

Selenium is a framework that lets you run automated tests on web applications across a number of browsers and platforms. The framework supports a plethora of programming languages like C#, Java, Python, PHP, Groovy, Ruby, and Perl.

ABOUT DOCKER

Docker, on the other hand, is an open platform that helps developers and system admins to build, ship and run applications on a number of platforms like laptop, data centers, VMs, cloud and so on. It will feature the entire file system, which in turn, will contain everything the application would need in order to run, including codes, system tools, system libraries, runtime, etc. Everything wrapped by Docker will run systematically, regardless of the environment it runs on.

In this article, we will discuss the benefits of using Selenium with Docker for testing your applications and how you can easily configure Docker.

We hope your Docker is installed and running, and you can now use Selenium, as you don’t need to install or run a browser.

For instance, if you are using Chrome, you can use Docker for that. If you want to set up a continuous integration (CI) system, you do not have to install browsers or drivers for that, since Docker will do the job. While installing Docker, you can create Docker images with specific browser profiles and specific versions.

Now, getting into the benefits...

RUN TESTS LOCALLY

Selenium helps you run tests locally, and it makes the process easy as you can configure your Windows manager. Wrapping the tests with Docker will help you easily move it to a different workspace as required.

CAPACITY TO SCALE

One of the biggest benefits of Docker is its capacity to scale. It’s particularly helpful because running Selenium grid on virtual machines or even separate machines takes up a lot of unnecessary computing overhead. Docker images share some system resources, so you need less resources than a virtual machine. You can easily cram several nodes in a single instance. Both Docker and Compose also handle the networking part easily.

EASY TO START DOCKER WITH SELENIUM

Once Docker installation is complete, SeleniumHQ maintains the whole range of Docker images. These images can be pulled down easily and used right away. The list of images is available at Docker Hub or GitHub repository, and falls into four main categories:

Base - Where you can build your own images.

Hub - A cloud-based registry service where you can link to code repositories so you can create images, test and store them.

Node - Images used in correlation with Hub image and general Selenium grid. It is possible to start a number of node containers with Hub image.

Standalone - Standalone images on the Selenium server. However, it is possible to run only one at a time on the local machine, or there will be a 4444 port conflict.

MORE SECURE

The security levels in Docker-Selenium combination are highly commendable because you do not have to tunnel your local app to third-party solutions. In addition, it is much faster too; in fact, it is twice as fast as the paid, cloud-based Selenium solution.

VERY HELPFUL IN CASE OF CRASH

Selenium crashes easily, so you cannot just set it up and forget all about it. With Docker containers, you can develop a new Selenium instance whenever needed, trash it, and then start a new one. Moreover, if you have to run it for longer periods, then you can just set it to reboot if it crashes.

OTHER BENEFITS AT A GLANCE:

  • You do not have to manage Selenium server jar because it is automatically managed within the Selenium hub container.
  • Eliminates the need for downloading browser drivers and setting classpath because that is also done automatically.
  • You can register and manage the Selenium Grid, so it is easy to register nodes on the hub.
  • Chances of discrepancies between environments are very, very low because all the team members can use the same container, irrespective of the operating system.

CONFIGURING DOCKER

Here, we used Docker-compose. This is what lets us take our Docker images and spin them up as a pre-configured Selenium Grid cluster.

Docker lets you configure your browser/OS combinations in a programmatic way, and Compose is the central point from which you can spin everything up on the fly.

You’ll need to install Docker and Compose first. (Selenium Grid is installed inside Docker, so don’t bother downloading it locally.)

Here’s an overview of what we’re going to do:

  1. Create Docker images for your Selenium Grid hub and node(s)
  2. Add Java to the hub to run the Selenium server jar
  3. Add Java, plus Firefox and Xvfb to the node
  4. Create a docker-compose.yml file to define how the images will interact with each other
  5. Start docker-compose and scale out to as many nodes as you need or that your machine can handle.
Docker1
Docker2

You should now have a Selenium Grid consisting of hub and browsers. (You can see them running here: http://<localhost>:4444/grid/console.)

Grid ConsoleCLOSING THOUGHTS

Selenium with Docker can cut down the test execution time, considerably reduce the differences that crop up among development, production and acceptance environments and create clean test environments, helping you release high-quality products.

About Suraj 

Automation test lead at Cabot Solutions. He is passionate about technology, mainly test automation. He enjoys nothing more than learning the trends that technology is taking in order to work more efficiently and to see progress and success.