testsigma
left-mobile-bg

How to Run Selenium Tests in Docker

August 9, 2024
Neha Vaidya
right-mobile-bg
How to run Selenium Tests in Docker
image

Start automating your tests 10X Faster in Simple English with Testsigma

Try for free

Lately, in this Continuous Integration and Continuous Delivery (CI/CD) domain, containerization has gained a lot of popularity. Implementing containerization was mainly focused on the development phase. In the recent past, the use of containerization for testing has been gaining prominence as it helps resolve a lot of test environment related issues. In this blog, you will learn how to integrate Docker and Selenium technologies to perform more effective and hassle-free tests.

Automation Testing and Selenium

Automation testing has become a vital part of software development as it takes the pressure off manual testers and lets them focus on higher-value tasks like exploratory tests, reviewing test results, etc. A machine takes over and implements repetitive, time-confusing tasks such as regression tests. Automation testing is achieving great test coverage within short timelines, as well as the accuracy of results. It allows you to ensure that the application UI works as expected.

Selenium is one of the most widely used test suites. This is because of its adaptability and ease of use. It offers a range of features that simplify the implementation of Automated Testing. One such feature that makes Selenium stand out from its competitors is Cross Browser Testing. It helps testers perform tests across various browsers and devices efficiently and seamlessly.

What are Docker and Containers?

Docker is a platform that helps users build, run, and ship containers efficiently. This platform is majorly formed by the underlying components like Docker Daemon, Docker Engine, Docker Clients, Docker Registry, and Docker Compose. Docker works on a client-server model.

Containers are lightweight packages of the necessary components needed to run the application on any platform. Running and managing these containers in a standard and efficient way is a challenge. This is where Docker is used.

Why Docker for Selenium Testing?

The precision of the test results depends on the quality of the test environment used. Often, we find that teams perform limited testing or that releases are delayed due to unavailable or inadequate testing environments. The ephemeral nature of docker containers can be put into play to resolve this issue. Test environments can be created afresh for each run and destroyed after the run is complete. Thus offering a clean test environment for each run.

With implementations like Cross Browser Testing, maintaining a combination of Browser-OS test environment becomes tedious. With the help of Docker, multiple test environments can be set up on the go, and testing can be performed in parallel across various Browser-OS combinations.

Creating multiple containers on a single host also helps us exploit the underlying hardware to its fullest. Let’s discuss how to set up Docker and run Selenium tests in Docker.

Docker Installation

Follow the steps below to configure Docker on Windows to run Selenium tests in Docker.

  • Downloading the Installer: Click on the Windows Installer to download Docker Desktop on your system. Based on the type of Operating system, you can select the option. In this case, let’s do it for Windows Operating system.
Downloading the Installer
  • Docker Installation: Click the launch button to start the installer and select the Enable Hyper-V Windows Features option on the configuration page.
  • The user account should be added to the docker-users group if the user and admin accounts are different.
  • Start Docker Desktop: From the Start menu, click on the Docker Desktop to start.

Note: If the Docker desktop doesn’t start and ask for WSL, follow this link to update the kernel package.

2.Start Docker Desktop
3.Start Docker Desktop

You can now run containers on our system using Docker. You can see that the left pane provides options to toggle between Containers, Images, Volumes, and Dev Environments.

Verification: To verify if the Docker is configured properly, open Command Prompt and execute the below command. You can see the docker version that you have setup.

docker –version

To verify configuration -Command Prompt

How to Run Selenium Tests in Docker?

Now that the Docker Desktop is configured on your system, you can easily pull a few Docker images and execute them. You can create a Docker image step by step from start or pull an already configured base image from the Docker hub and then add on to it.

According to the official documentation, Docker hub is the central registry hosted by Docker, where developers and organizations build and host numerous containers for the community.

In this tutorial, I have used the selenium/standalone-chrome image which is hosted by Selenium on DockerHub.

Step 1. Pull the Docker image

Open the command prompt and execute the below command to retrieve the list of images present in your system.

docker images

1.Pull the Docker image

If the Selenium standalone-chrome docker image is not present, type the docker pull selenium/standalone-chrome command. This command downloads a copy of the image onto the system.

2.Pull the Docker image

Now, rerun the Docker images command on your command prompt to get selenium/standalone-chrome image. Refer to the below screenshot for the same.

3.Pull the Docker image

Step 2. Executing the Selenium WebDriver Docker container

Once you have Selenium/standalone-chrome image, you can now start the container by executing the below command:

docker run -d -p 4444:4444 -v /dev/shm:/dev/shm selenium/standalone-chrome

The above command is explained below:

  • docker run : starts a container from the image specified
  • d : starts the container in detached mode. (background mode)
  • -p 4444:4444 : Port 4444 on the container is mapped to Port 4444 on your local browser.
1.Executing
  • selenium/standalone-chrome : name of the image that has to be started

When you run this command, it will return the ContainerID of the conatiner created using the docker image.

The next step is to start the browser instance and navigate to http://localhost:4444/. It renders Selenium Grid UI, as shown in the below screenshot.

2.Executing

Step 3: Creating a sample test file

Test cases can be written in different languages in Selenium. Java and Python are the widely used languages to write a test case. In the example below, I am writing a test script using Python.

from selenium import webdriver

To instantiate a browser instance in Chrome browser, Chromedriver is mandatory. Therefore, initialize ChromeOptions to declare the connection and driver options.

<!-- wp:paragraph -->
<p>options = webdriver.ChromeOptions() </p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>options.add_argument('--ignore-ssl-errors=yes') </p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>options.add_argument('--ignore-certificate-errors')</p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>Create a Remote webDriver instance and pass the Selenium endpoint and chrome options defined. Refer to the below command</p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>driver = webdriver.Remote( </p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>command_executor='http://localhost:4444/wd/hub', </p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>options=options</p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p> )</p>
<!-- /wp:paragraph -->

Now, traverse to the Testsigma website and click on the Testsigma Cloud button.

<!-- wp:paragraph -->
<p>driver.get("https://www.testsigma.com/") </p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>driver.find_element_by_link_text("Testsigma Cloud").click()</p>
<!-- /wp:paragraph -->

The end to end script is given below.

seleniumDockerTest.py

<!-- wp:paragraph -->
<p>from selenium import webdriver </p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>import time </p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>print("Test Execution Started") </p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>options = webdriver.ChromeOptions() </p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>options.add_argument('--ignore-ssl-errors=yes') </p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>options.add_argument('--ignore-certificate-errors') </p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>driver = webdriver.Remote(command_executor='http://localhost:4444/wd/hub', </p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>options=options ) </p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>#maximize the window size </p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>driver.maximize_window() </p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>time.sleep(10) </p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>#navigate to testsigma.com</p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>driver.get("https://www.testsigma.com/") </p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>time.sleep(10)</p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>driver.find_element_by_link_text("Testsigma Cloud").click() </p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>time.sleep(10)</p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>#close the browser </p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>driver.close() </p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>driver.quit() </p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>print("Test Execution Completed Successfully!")</p>
<!-- /wp:paragraph -->

Step 4. Executing the test case

You can execute the python test case file using either an IDE or command prompt. Open a command prompt and execute the below command:

python <filename>

On executing this command, go to the sessions tab on the Selenium Grid UI. This will retrieve the active session.

Now that you have learned how to run Selenium tests in Docker, let’s take a look at the alternatives for testing in CI/CD.

Best Practices for Running Selenium Tests in Docker

If not managed correctly, running multiple containers for different tests on the same device can get messy. It is possible that the hardware can be blocked, rendering the entire purpose of using containers a waste.

Moreover, with the Selenium 4 release, having proper knowledge of the right techniques is evermore critical.

Here’s some best practices that you can use to avoid encountering the above scenario:

  • Use the official Selenium Docker images available on Docker Hub, which are maintained and updated by the Selenium team.
  • Use Docker Compose to define and run multi-container Docker applications that simplify managing dependencies and linking multiple services like Selenium Grid, browsers, and the application under test.
  • Use Selenium Grid to run tests in parallel across multiple browsers and environments.
  • Use VNC (Virtual Network Computing) to view the browser sessions running in Docker for debugging purposes.
  • Implement security best practices such as using non-root users, minimizing container privileges, and securing network communication.
  • Use Docker volumes to persist test results, logs, and screenshots outside the container.

Where does Testsigma Fit in?

There are already tools and frameworks like Selenium that are widely adopted across the industry. They also offer vivid features that help perform Automated testing in an effective way. But what these frameworks need is a person with a good understanding of programming to make the best use of them. Finding good resources and maintaining them is a major constraint most companies face.

Also, training resources to perform automated testing using these frameworks is another overhead. This brings out a need for a tool that is highly simplified and does not require a lot of technical acumen to use it. This is where Testsigma comes into the picture.

Testsigma is a unified test automation platform provided as a cloud service. It helps simplify and perform automated testing for web applications, mobile applications and APIs with ease and at a pace. Even a person with zero or limited coding skills can easily be onboarded to use this platform. Automated test suites can be built quickly with very less technical overhead. How does Testsigma help us achieve this?

It uses simple English commands powered by the NLP(Natural Language Processing) engine, which allows you to write test cases in minutes. With NLP and Integrated API testing capabilities, teams can achieve in sprint test automation with minimum effort. With Testsigma, maintaining different servers, operating systems, and mobile devices is no longer a problem.

You can easily build end-to-end tests 5 times faster for web, mobile apps, & APIs with English scripts that self-heal, enabling maintenance-free testing.

One can run tests on various operating systems, browsers and browser versions parallelly. Testsigma offers 200+ Android and iOS operating systems to run the tests.

Set up a Testsigma instance on your workstation in a few clicks or directly sign-up on our Cloud-hosted platform. Testsigma cloud needs no setup and can be integrated easily with CI/CD pipelines.

With this, we come to the end of this article on How to run Selenium Tests in Docker.

Conclusion

With raging competition and ever-changing requirements of the end users, it is necessary that organizations deliver high-quality software at the earliest.

Users today have very less tolerance towards a bad user experience. Thus, delivering quality software that meets the users’ expectations and caters to their timelines is highly important. This calls for short release cycles, and the testing needs to be effective and with wide coverage.

Execute parallel testing at the required scale with Selenium automated testing, and Docker’s ephemeral containers assist keep the test environment sane.

Frequently Asked Questions

What is the Alternative to Selenium in Docker?

There could be multiple alternatives to Selenium in Docker. Two popular alternatives are Cypress and Testsigma. Both are modern end-to-end automation testing platforms that provide automated approach to creating and running tests.

Cypress offers fast, reliable testing with built-in features like time travel debugging and real-time reloads. Testsigma, on the other hand, provides an easy-to-use interface with natural language scripting and integrates seamlessly with CI/CD pipelines.

Selenium Grid
A Complete Guide to Selenium Grid 4: Architecture and Configuration
Selenium automated testing
Selenium Automation Testing : Top Benefits & Challenges
Selenium Chromedriver
Selenium ChromeDriver & How to do Selenium Tests on Chrome?
Testsigma Author - Neha Vaidya

Neha Vaidya

Neha is an experienced Research Analyst with a demonstrated history of working in the field of testing and Instructional Designing. She's skilled in Automation Testing, Supply Chain Management with a Computer Science and Engineering background. Besides, she's a technical writer. On the personal front, she's a singer and loves going on road trips!

image

Start automating your tests 10X Faster in Simple English with Testsigma

Try for free
imageimage
Subscribe to get all our latest blogs, updates delivered directly to your inbox.

By submitting the form, you would be accepting the Privacy Policy.

RELATED BLOGS


How to Write Test Cases for Trading Application Testing?
RITIKA KUMARI
TEST AUTOMATIONTESTING DISCUSSIONS
Scriptless Test Automation | What , Why it Matters & Examples
KIRUTHIKA DEVARAJ
TEST AUTOMATION
Top 6 Game Testing Tools You Need to Know
RAUNAK JAIN
TEST AUTOMATION