How To Run Your Selenium Tests Headlessly in Docker
The Old Way
It used to be that in order to get your Selenium tests running on a given machine you had to install each individual browser and then the browser drivers (for instance ChromeDriver for Chrome). Some of my most popular posts are about installing these drivers. However that’s all changed if we instead use containers. Simply download and run a single Docker container with both the the browser and the driver pre-installed.
…Except now we’ll be running your tests headlessly which customers don’t do.
The Catch: Headless Browsers
In the context running a Selenium test headlessly in Chrome, it means Chrome launches and runs the test in the browser but you don’t see it because there is no GUI. Instead you get a command line to run and debug your tests. The downside is one customers don’t use your application in a headless state and two these tests are harder to debug.
The upside is we can manage the installation and running of the browsers and drivers in an easier way. We can also run our tests on devices that don’t have computer graphics like AWS EC2 instances (virtual machines), and CI/CD services like TravisCI and CircleCI to name a few.
Docker Desktop and Container Installation:
In this exercise we will download a single (standalone) Chrome browser with ChromeDriver already configured. Then we will simply run a test!
- First thing we need to do is install Docker Desktop for our respective OS (Mac or Windows)
- Once this is up and running we can download our container
- (Note: this is a desktop app but can also be installed from the command line for future virtual machine use)
- Second, since we already know what container we want, we simply need to copy and paste this command into our terminal
docker pull selenium/standalone-chrome
- This will download the Chrome container image
- Third, we run is to start the container so Chrome is running
- We can run
docker psat any time to see if any containers are running
- We now run
docker run -d -p 4444:4444 -v /dev/shm:/dev/shm selenium/standalone-chrome
- This sets the docker container to use the port 4444. We will use this later in our test file.
- We can run
Running Our Tests: An Example
Now we’re ready to write a test using Selenium and our now running Docker container with Chrome in it. For fun I’m going to write this in Ruby but it should work for any language:
When we save our file and run it (e.g.
ruby headless_chrome.rb) here is what will happen:
- A chrome browser session will open within the docker container
- Test runs and saves a screenshot
- Browser closes
- All we see is the result of the test
There you go! We managed to install a container with both Chrome and ChromeDriver installed on it and then wrote and ran a test to demonstrate it works. You’ve now run a Selenium Test Headlessly with Docker!
In the larger picture this means each time we need to install Selenium on a new computer (CI, EC2 instance, virtual machine, desktop) it’s simply about installing docker and downloading an image, spinning it up and connecting it to your test. After having done this half a dozen times I think it has huge advantages over the old ways (link to Installing ChromeDriver on macOS).
- Selenium Docker Hub for finding the approprite container
- This is not the same as using Headless Chrome (which is a configuration option in modern versions of Chrome). To learn more about using Headless Chrome go here.
Oh and if this article worked for you please consider sharing it or buying me coffee!