Do you think IIT Guwahati certified course can help you in your career?
No
Introduction
Nowadays, there are several ways to automate web application testing. Selenium is one of the best frameworks that we can use for automated web testing. It allows developers to create the tests. When we want our testing process to be more efficient, scalable, and consistent, then the combination of Selenium and Docker comes to our minds.
In this blog, we will discuss about how to run Selenium tests in Docker. Firstly, we discuss about what Selenium and Docker are. Then we will explore the steps to run the Selenium tests in Docker.
So, let us start:
What is Selenium?
Selenium is a widely used test automation framework. It is an open-source framework that makes it easy to use for everyone. It is primarily used for automating web browsers.
It provides various tools and libraries. That’s why it enables you to automate web applications across different browsers. It is supported by multiple programming languages(Java, Python, C#, etc).
What is Docker?
Docker is a tool that helps developers and testers to create a more efficient and scalable environment. We call it as a container. This container contains everything that is required to run the application or the test cases. Docker is an open-source platform.
In the Docker container, we can put our code, dependencies, libraries, and other important things that are required. This helps to run the application on any computer or server.
Now, you might be thinking why we should use Docker with Selenium. Let us discuss this.
Why Use Docker with Selenium?
Suppose we have created some test cases using Selenium for our web application. These tests are working fine in our local environment, but they are failing in other systems. This may happen because of different testing environments. This is where Docker comes in as a survivor. It is an OS for containers. These containers work like a virtual machine, and it virtualizes the OS of a server.
In Selenium testing, Docker plays an important role to make the testing process efficient and reliable.
There are several reasons why Docker is good with Selenium:
It is easy to perform the testing without manually installing the requirements.
It helps to provide consistent test environments so that Selenium works smoothly.
It is more scalable and helps you to do parallel testing.
It is portable and easy to integrate.
It provides faster test execution compared to other methods.
Now, as you got an idea about Selenium and Docker, let us understand how to set up Docker in our system.
Docker Setup
There are several steps to set Docker on our local machine.
Step 2: Now, go to the file location and install it on your machine.
Step 3: After installation is done, you need to restart your machine.
Step 4: After the restart, you need to accept the license agreement.
Step 5: Now, you need to install the newest version of the WSL(Windows Subsystem for Linux) kernel. So you can open Windows Powershell and write wsl --update
Step 6: Now, you can start the Docker desktop application and sign in or sign up with your account.
Now, we are done with the Docker setup. Now, we will look at the steps to run Selenium tests in Docker.
Steps to Run Selenium Tests in Docker
To run the Selenium tests in Docker, we need to follow some steps. There are several browsers available, but for this example, we will be using Chrome. So, we will be using the image of Chrome in Docker.
Let us look at the steps:
Step 1: First, we need to pull the Docker image. So, we need to go to the Docker Hub website.
Step 2: Now, we need to click on the Explore tab and search for Selenium.
Step 3: Now, we can choose the first option, selenium/standalone-chrome, because we are going to use the Chrome image.
Step 4: Now, we need to copy the Docker pull command. Now, open your command prompt.
Now, check whether any Docker image is there or not. So, you can simply write a command:
docker images
So, there are no images currently.
Step 5: Now, we need to pull the Docker image, selenium/standalone-chrome, so we can paste the Docker pull command which we have copied and press the Enter key:
docker pull selenium/standalone-chrome
Step 6: Now, after downloading the image, we need to check again whether the image is there or not. So, we check it by
docker images
Now, we can see that image is pulled.
Step 7: Now, we need to run the image. So, to run the image we can write the following command in our command prompt:
docker run -d -p 4444:4444 -v /dev/shm:/dev/shm selenium/standalone-chrome:latest
After running the command, we will get a container id. It ensures that our image is running successfully.
Step 7: Now, if we want to check whether the container is running or not, then we can write the following command in the command prompt:
docker ps
This will give us the details about the container, such as ports, status, and so on.
Step 8: Now, we have to write a simple test script for Selenium. So, we can write that script in any IDE. So we are going to use Eclipse:
Step 9: Now, create a new Maven project in Eclipse IDE.
Step 10: Now, we need to add a dependency for Selenium in our project. So, go to Maven Repository. From there, we can copy the dependency and paste it to our pom.xml.
Step 11: Now, we need to go to the src/test and create a package docker and in that package, create a class SeleniumDocker.java
Step 12: Now, we need to write the script in the class SeleniumDocker.java. Suppose we want to test the title of the website of Coding Ninjas. So, we have to write the script in this way:
package docker;
import java.net.MalformedURLException;
import java.net.URL;
import org.openqa.selenium.remote.DesiredCapabilities;
import org.openqa.selenium.remote.RemoteWebDriver;
public class SeleniumDocker {
public static void main(String[] args) throws MalformedURLException, InterruptedException {
// Initiating the WebDriver
DesiredCapabilities dc=new DesiredCapabilities();
// Setting the browser as Chrome
dc.setBrowserName("chrome");
// Providing the URL for the docker Container
URL url=new URL("http://localhost:4444/wd/hub");
// Creating a webdriver
RemoteWebDriver remoteWebDriver=new RemoteWebDriver(url,dc);
remoteWebDriver.get("https://www.codingninjas.com/");
// Printing the title of the website
System.out.println("The title of the website is: "+remoteWebDriver.getTitle());
Thread.sleep(5000);
// Closing the webdriver
remoteWebDriver.quit();
}
}
You can also try this code with Online Java Compiler
So, as we can see in the output, we got our title of the website, it means our script is working fine and smoothly with docker container. To cross-check whether our docker container is running or not, we will again execute a command:
docker ps
You can also try this code with Online Java Compiler
These are the steps that we can follow to run Selenium tests in Docker. After running our tests, we need to stop the docker container. So we can write the command
docker stop {provide the container ID}
You can also try this code with Online Java Compiler
Selenium is an automated testing framework. It is an open-source framework. It is used to automate web applications for testing. It allows testers to write test scripts in various programming languages.
What do you understand by Docker?
Docker is a platform that allows developers to create, deploy, and run applications inside containers. These containers are lightweight and isolated environments. These containers package all the necessary dependencies and configurations.
What problems can occur using Docker with Selenium?
Some problems can occur if we use Docker with Selenium. We can face problems while managing container networking, handling browser versions and updates, and ensuring proper resource allocation for running multiple tests in parallel.
How to run Selenium tests in Docker?
To run the Selenium tests in Docker, we need to create a Docker image that includes your testing framework (e.g., Selenium WebDriver), browser drivers (e.g., ChromeDriver), and test scripts. Then, you can use Docker Compose or Kubernetes to orchestrate containers for running tests in parallel across different environments.
Conclusion
In this blog, we have discussed about how to run Selenium tests In Docker. Firstly, we covered what Selenium and Docker are. Then we explained the steps to run the Selenium tests in Docker. If you want to learn more about Selenium, then you can check out our blogs:
We hope this blog helps you to get knowledge about how to run Selenium tests In Docker. You can refer to our guided paths on the Codestudio platform. You can check our course to learn more aboutDSA, DBMS, Competitive Programming, Python, Java, JavaScript, etc.