How To Run Selenium Tests In Docker

Learn via video courses
Topics Covered

Overview

Selenium Docker combines the Selenium testing framework and Docker containerization technology used to create a scalable and efficient testing environment for web applications. With Selenium Docker, tests can be executed in isolated containers that can be easily created, deployed, and managed, reducing the time and effort required for testing and improving the reliability and speed of the testing process.

Introduction

Imagine you are a chef preparing a meal for a large banquet. Instead of cooking each dish individually in a separate pot or pan, you use a set of identical, pre-made pots and pans that can be easily stacked and used interchangeably. This saves time and effort and allows you to scale up your cooking operation easily.

Similarly, Selenium Docker uses the containerization technology of Docker to create a set of identical, pre-made testing environments that can be easily deployed and managed. Instead of setting up and configuring individual testing environments for each test, you can use one of these pre-made containers, which saves time and effort and allows you to scale up your testing operation if needed easily.

Introduction to Automated Tests and Selenium

Automated testing uses software tools to execute tests, evaluate test results and compare them with expected outcomes. The main purpose of automated testing is to increase the efficiency and effectiveness of the software testing process by reducing the time and cost involved in manual testing. One popular tool for automating web application testing is Selenium, an open-source framework allowing testers to write scripts in various programming languages.

Selenium provides tools to automate web browsers across different platforms, including Chrome, Firefox, Internet Explorer, and Safari. It supports various programming languages, such as Java, Python, Ruby, C#, and JavaScript. Using Selenium, testers can simulate user actions, automate data input, and verify expected outputs, among other things. Using automated tests and Selenium helps improve the quality of software applications, reduce manual testing errors, and increase the efficiency of the testing process.

What are Containers and Docker?

Containers are a lightweight and portable way to package and distribute software applications. They provide an isolated environment for an application and all its dependencies, ensuring consistency across different computing environments. An analogy for containers is shipping containers, which are standardized and can be easily moved from one location to another while keeping the contents safe and secure.

Docker is a platform that enables the creation and management of containers. It uses a containerization approach to package an application with its dependencies into a container, which can be easily distributed and deployed on different systems. With Docker, developers can create, test, and deploy applications faster and more efficiently than traditional methods. For example, a developer can package a web application and its dependencies into a container and then easily deploy it on a cloud or on-premises server.

In summary, containers are a way to package and distribute software applications, and Docker is a platform that enables the creation and management of containers. Using containers and Docker helps improve software application deployment's consistency, portability, and efficiency.

Why Use Docker for Selenium Testing?

There are several reasons why Docker is a popular choice for running Selenium tests:

  • Isolation: Docker containers provide an isolated environment for running tests, which ensures that tests run consistently and reduces the risk of issues caused by dependencies on external systems. Each container contains its own file system, network stack, and resources, preventing conflicts between tests and ensuring that tests run clean. For example, suppose you are testing a web application requiring a specific browser driver version. In that case, you can easily create a Docker container with that specific version installed, ensuring that the test runs consistently every time.
  • Portability: Docker containers can be easily moved between systems, making it easy to run tests on different machines or environments. You can package your tests and dependencies into a Docker image, which can be run on any machine that supports Docker. This makes running tests in the cloud or moving tests between development, testing, and production environments easy. For example, suppose you are testing a web application that needs to run in different browsers. In that case, you can create a Docker image with all the necessary browser drivers installed and run the image on any machine that supports Docker.
  • Scalability: Docker containers can be easily scaled up or down to accommodate changes in workload. You can easily spin up additional containers to run more tests and then shut them down when they are no longer needed. This makes it easy to handle spikes in test traffic or to run tests in parallel, which can reduce the overall testing time. For example, suppose you have a large suite of Selenium tests that take a long time to run. In that case, you can spin up multiple Docker containers to run tests in parallel, significantly reducing the overall testing time.
  • Consistency: Docker containers provide a consistent environment for running tests, which ensures that tests run the same way every time. This helps reduce the risk of false positives or negatives and makes diagnosing issues easier when they arise. For example, suppose you are testing a web application that interacts with a database. In that case, you can create a Docker container with the specific version of the database software installed, ensuring that tests run consistently every time.
  • Easy setup and configuration: Docker containers can be easily created and configured, which makes it easy to set up new testing environments or modify existing ones. You can use Dockerfiles to define the configuration of a Docker image, which can be easily shared and modified. This can reduce the time and effort required to set up new testing environments and make collaborating with other developers or testers easier. For example, if you are working on a team that needs to run Selenium tests in different environments, you can create Docker images for each environment and share them with the team.

Setting Up Docker

Follow these steps to set up Docker on your local Machine.

  1. Go to this link to downlaod docker desktop installer. Once downloaded, open it.

selenium-docker1 2. Once the installation completes, hit the Close button. selenium-docker2 3. Accept the agreement which pops up. selenium-docker3 4. You can skip other settings. selenium-docker4 5. The Docker desktop will appear as shown: selenium-docker5 Sometimes it gives such an error to install WSL 2. Just follow their command and download the Linux kernel update package selenium-docker6 After that, restart the docker desktop. It will work fine.

You can also check the docker version by running the following command in the command prompt. docker --version selenium-docker7

Running Selenium Tests in Docker

Step 1: Pull the Docker Image

  1. Run the command docker images, to check the list of images present. selenium-docker8

  2. Pull the docker image using the command docker pull selenium/standalone-firefox: latest selenium-docker9

  3. Again, run the docker images command to confirm the image is pulled into the system. selenium-docker10

Step 2: Running the Selenium Webdriver Docker Container

  1. Next, we need to launch a container using that image. For that, run the command docker run -d -p 4444:4444 -v /dev/shm:/dev/shm selenium/standalone-firefox: latest selenium-docker11

  2. To see the list of running containers, executedocker ps -a. You will see your container listed here.

selenium-docker12

  1. Open the browser and go to the link here to see the deployed Selenium Grid UI.

selenium-docker13

Step 3: Creating a Sample test File

Create a Java class test file. Here we have a test file that will visit scaler.com.topics and return the title of the page.

You can see the title attribute using the developer's tools Ctrl+Shift+I. selenium-docker14 Your code will look like this in the IDE. selenium-docker15

Step 4: Executing the Testcase

  1. Right-click and run the test class as Java Application.

selenium-docker16

  1. Check the selenium UI.

selenium-docker17

  1. In the console, you will see the title is rendered as output.

selenium-docker18

  1. Click on the video icon to check automation( for this, you have to give a time gap to see these changes. Otherwise, the automation does its job quickly.)

selenium-docker19 It will ask for a password. The default password is 'secret'.

selenium-docker20

  1. Here, you can check how your automation works.

selenium-docker21

Effective Alternatives for Testing in CI/CD

The use of Docker containers for Selenium testing provides many benefits, but alternative approaches for testing in CI/CD pipelines exist. One alternative is to use a cloud-based testing service, such as Sauce Labs or BrowserStack, which provides a range of browsers and platforms for testing. These services offer a scalable and flexible approach to testing without requiring the setup and management of local testing environments.

Another alternative is to use a virtual machine (VM) to run the tests. VMs provide a similar level of isolation and consistency as Docker containers and can be easily configured and deployed using tools like Vagrant. VMs can also be easily moved between systems and environments, making them a portable alternative to Docker.

Finally, it is also possible to run Selenium tests directly on the host machine without containerization or virtualization. This approach requires careful management of dependencies and environment configurations but can be a simpler and more lightweight approach to testing, especially for small projects.

Ultimately, the choice of approach will depend on the specific needs and constraints of the project, as well as the available resources and expertise of the development and testing teams.

Best Practices of Running Selenium Tests in Docker

Here are some best practices for running Selenium tests in Docker:

  • Use a lightweight base image: Choose a lightweight base image such as Alpine Linux or Ubuntu Minimal as it reduces the image size and provides faster container startup times.
  • Use a multi-stage build: Use a multi-stage Docker build to reduce the size of the final Docker image. In the first stage, build the application code, and in the second stage, copy only the necessary files to the final image.
  • Separate the tests and the application code: Separate the Selenium tests from the application code by creating a separate Docker image for the tests. This allows you to run the tests independently and keep the application container lean.
  • Use a headless browser: Use a headless browser such as Chrome or Firefox with the headless option enabled. This eliminates the need for a graphical user interface, reducing the image size and improving performance.
  • Use environment variables: Use environment variables to pass configuration options to the Docker container, such as the URL of the application under test and the browser to use for testing.
  • Use a Docker Compose file: Use a Docker Compose file to define and manage the Docker containers used for testing. This makes it easier to manage multiple containers and simplifies the testing environment setup.
  • Use a persistent storage volume: Use a persistent storage volume to store test reports, screenshots, and logs generated during testing. This allows you to access and analyze the results even after the container has been deleted.

Conclusion

  • Automated testing uses software tools to execute tests, evaluate test results, and compare them with expected outcomes to increase the efficiency and effectiveness of the software testing process.
  • Selenium is a popular open-source testing framework that allows testers to write scripts in various programming languages to automate web browsers across different platforms.
  • Containers are a lightweight and portable way to package and distribute software applications with their dependencies to ensure consistency across different computing environments.
  • Docker is a platform that enables the creation and management of containers to package applications with their dependencies, making it easier to create, test, and deploy applications faster and more efficiently.
  • Docker is a popular choice for running Selenium tests because it provides isolation, portability, scalability, consistency, and easy setup and configuration for testing environments.
  • Setting up Docker for Selenium tests involves pulling the Docker image, running the Selenium Webdriver Docker container, creating a sample test file, and executing the test case.
  • Best practices for using Docker for Selenium testing include keeping containers small and focused, using a version control system for Dockerfiles, and using a container orchestration tool to manage multiple containers.