How to Run Selenium Tests in Docker
Selenium WebDriver is a powerful and flexible tool that enables you to run functional tests on your web applications. By running Selenium tests through Docker containers, you can save time when creating your tests and get more testing options. This blog post will provide an overview of how to run Selenium Tests in Docker. It is based on a webinar by performance automation engineer Eliran Shani which you can view here. All scripts can also be found on GitHub, here.
What is Selenium WebDriver?
Before discussing Selenium WebDriver, let’s explain what Selenium is. Selenium is an open source tool that automates web browsers and web applications. As it runs across many browsers and operating systems and supports multiple programming languages and testing frameworks, there are endless possibilities to its testing capabilities. So, what you decide to do with it is up to you.
Selenium has three major tools. 1. Selenium IDE, a tool that reproduces scripts by recording and playing them back. Selenium IDE is more or less deprecated. 2. Selenium Server (Grid) for running tests in parallel on different machines. 3. Selenium WebDriver, a robust, browser-based regression automation suite, which enables distributing tests across many environments. This blog post will discuss Selenium WebDriver.
Selenium WebDriver Benefits
- Selenium WebDriver is open source, making it free and portable and open to contributions of people from all over the world. In addition, you can learn Selenium from discussions and resources across the web that are open to everyone. On the other hand, it has no official support.
- Selenium WebDriver supports many languages, including Python, C Sharp and Java. This enables flexibility when using it as well as making it accessible to many developers. However, Selenium requires experience and understanding of its abilities and way of work.
- Selenium WebDriver supports many browser types, operating systems and platforms, including mobile browsers, android, Windows, etc. However, the tester needs to be in sync with all the updates to ensure smooth testing.
- Selenium WebDriver is valuable for Continuous Integration development by working with CI tools, enabling developers to add functional testing to their Continuous Testing process. However, Selenium does not have inbuilt reporting capabilities, so it requires installing 3rd party plugins.
What is Docker?
Docker is a software containerization platform that provides virtualization from the operating system level. In Docker, all software parts can be organised in containers. This includes the operating system, software, dependencies, environment variables, etc. Containers can be shared among different users, enabling quick installation and running of software and services. This makes Docker handy for automation testing, as the relevant container can just be downloaded and run as part of the automated test. However, Docker is also secure because it runs as an isolated process on the host machine. Docker can be run on any computer on any infrastructure as well as in the cloud, including Linux, Windows, Mac, Amazon, Azure and more.
- Docker is open source. This means that you can find many images (used to generate containers) on the web and use them. However, this also means that there is no official Docker support.
- Docker can be an integral part of Continuous Deployment and Continuous Testing, by ensuring you have a consistent environment when developing and going into production. This is because configurations and dependencies are maintained internally, and will not change.
- Docker is isolated, secure and portable. However, it requires expertise as there is no GUI and it does not run in bare metal speed.
This blog post shows a demo that uses the following tools:
- Python, binded with Selenium WebDriver
- PyTest as a testing framework (you can use a framework of your preference, like Nose or JUnit)
- IntelliJ IDEA as IDE
- ChromeDriver, GeckoDriver (Firefox) or Headless Chrome
- Docker (install here)
- CI tool, like Jenkins, or TeamCity. Add the plugins you need, like GitHub for the repository connection, Allure for reporting, BlazeMeter for performance testing, etc.
Creating Your Selenium Test in Docker
1. If you want to make sure Docker is installed, open your console and write the command Docker –help. If you see a list of commands, it means that Docker is installed.
2. To check out which Docker images are installed, run docker images. An image has all the prerequisites needed for the tests. In the next steps, I will show you how to get images that you might be missing.
3. Create your Selenium test.
Let’s look at this test, which you can also find here. The test is named test_purchase_tickets.py.
This could be any test, according to the web application you need to be tested. This tests imports functions, imports PyTest, imports By from Selenium, tests blazedemo.com and asserts several pages.
If we run the tests we can see they are working:
4. Create and export the requirements.txt file, to implement your test in Docker. This file includes the packages needed to run the test. Use the command pip freeze > requirements.txt.
The requirements will look like this (you can also find them here):
5. Now it’s time to create a base Dockerfile to support Selenium, Chrome and Firefox:
This base Dockerfile creates a new, isolated container that includes everything you need to run Selenium to read Chrome.
Or, instead of creating the base Dockerfile, you can use Docker Hub, search for “Selenium” and use one of the ~2000 containers that are already there.
To use an image from Docker Hub, simply click on it and copy the pull command:
Now, put the command in the Dockerfile in the From section. When the test is run, Docker will pull the image from the cloud.
The benefit of using Docker Hub method is that you don’t have to handle the Dockerfile by yourself. This is also a limitation - you have no flexibility or control over containers. In my opinion, the flexibility is worth the effort.
In your base Dockerfile, make sure you specify the package version you want to test, so the browsers are tested correctly.
The test shown here includes:
- Downloading the browsers’ WebDriver (Gecho Driver, Chrome Driver)
- X Server Display - this is important because there is no UI, so you must install it to have a visual view of the test.
- Symlinks to the webdrivers, to connect everything together
6. Store the base DockerFile in the Root folder.
7. Create a project Dockerfile to support requirements.txt. This will be the Docker file that uses the base image:
8. Store this Docker base file in the project root.
9. Create a Bash script to orchestrate your test (and make your life easier).
The Bash script declares PyTest arguments, sets tag names, runs the Selenium test, runs the project image, generates an XML from the results, and more.
10. We are ready to run the script! Run the command:
/scripts/run-tests.bash --env=$ENV tests/test_purhcase_tickets.py -v
This command runs the bash file, and also points to the specific test to run.
The command created a container from the base Docker file. The first time this happens it will take a few minutes because the container needs to be installed. But next time, the test will run much quicker.
11. The tests are being run and you can see the results:
Now let’s see how we can view the results in a visual manner that is easy to analyze.
12. Create a script to generate test results
This script takes allure2 from the Docker hub. It maps the actual allure report that’s being created, will create an xml and a png file and create 3 more folders.
13. To generate the Allure report, run the command:
14. To generate the Allure report, run:
python -m SimpleHTTPServer 8080
This command includes the folder to store the reports in, the creation of a Python server for the reports and the presentation in port 8080 (for example).
15. Now, if you go to localhost, you will see the reports:
This report has been generated from the XML file. As you can see 8 tests are being executed. You can also click on the tests to get more details, like duration, or failure reasons.
That’s it! This has been an overview of how to run a Selenium test in Docker. To learn more about Selenium, you can read these blog posts:
- How to Automate Testing Using Selenium WebDriver, Jenkins and Allure
- 6 Easy Steps to Testing Your Chrome Extension With Selenium
- How to Understand Your Selenium WebDriver Code and Run it with Taurus
- Headless Execution of Selenium Tests in Jenkins
- Using Selenium with JMeter's WebDriver Sampler
- How to Run a Selenium Test with JUnit
- Mixing Selenium Into Your Load Scenario
View the complete webcast from this link, and learn how to create your own Selenium Docker script, just like this one:
You can also skip all these steps and upload your functional test script (in java and yaml) to BlazeMeter, and easily run it.