Step by Step Guide — Selenium & Docker — Run UI cases in Docker containers
Docker is a vast topic, and it is altogether a separate area of study.
Here, we study docker in the perspective of Selenium / Automation testing. w.r.t Selenium, we deal with docker to execute the UI / automation tests in Docker images / containers.
Note: Here I am not repeating any selenium concepts / Java, assuming that you have hands-on on automation.
Now, lets see how to setup the Docker and integrate it in selenium automation.
This involves below steps —
- Step1: Install Docker
- Step2: Pull the image, as per the requirement.
- Step3: Deploy the image in container.
- Step4: Create a Selenium Test and point it to Docker URL, and run it.
- Run the script and monitor the execution.
Step1: Docker Installation
- Download Docker from their official website based on your OS. https://www.docker.com/products/docker-desktop
2. Launch the installer and follow the wizard. This should
3. To make sure that docker is running on your system, launch command prompt or terminal and run below command.
docker — version
docker ps
This should give the output as —
Step2: Pull the required image from Docker Hub.
Pull the required image using below pull command —
docker pull <image_name>
<image_name> → Can be found from below link based on the browser and its version.
https://hub.docker.com/u/selenium
This is Docker official page where all the images by Selenium are posted. From here, based on the browser and Selenium version copy the image name, else copy the pull command given next to it.
For eg, if you need to run tests on Chrome browser —
Version of Selenium —
If you need specific version of selenium, then specify that version in the command like:
docker pull selenium/standalone-chrome:87.0
If you need the latest version (whichever it could be), then use below command —
docker pull selenium/standalone-chrome:latest
You should see below message on successfully pulling above image —
To see the list of images downloaded in your system —
docker images
To delete any image,
docker rmi <image_name>
Eg: docker rmi selenium/standalone-chrome
Step3: Deploy the image in container — docker run command
Now, you need to deploy the above image in a container which will serve as an remote system / environment to run our selenium tests.
Command for the same is —
docker run -d -p <port_number_of_Host_Sytem>:<port_number_of_container> -v /dev/shm:/dev/shm <image_name>
Reference url: https://github.com/SeleniumHQ/docker-selenium
Explaining the above command —
- “docker run” → docker run command
- -d → to run in background
- -p → to direct to specific port
- port_number_of_host_Sytem → The port# where the Selenium tests run on host system (your system). I will specify an available port# say, 4444 and configure my script to run on that port#.
- port_number_of_container → The port# where the Selenium tests need to redirect in Container system, and execute there. I will specify this too as 4444.
- -v /dev/shm:/dev/shm →. Selnium suggest to mount the /dev/shm as there may be failures in using the host memory by container. Using this option will prevent those.
- image_name → the name of the image which was downloaded, along with version, like “latest” or the one which you pulled earlier.
Now, this is the actual command —
docker run -d -p 4444:4444 -v /dev/shm:/dev/shm selenium/standalone-chrome:latest
To see the if the container is running, give this command —
docker ps
This should list the containers running on your system.
To stop any container
To delete any container
docker run -d -p 4444:4444 -v /dev/shm:/dev/shm selenium/standalone-firefox:4.0.0-alpha-7–20201119
https://github.com/SeleniumHQ/docker-selenium
Step4: Create a Selenium Test and point it to Docker URL.
Now, lets create a simple Selenium test and configure to run in Docker container.
public class DockerSeleniumDemo {
public static void main(String[] args) throws MalformedURLException {
//DesiredCapabilities & URL are required parameters inorder to invoke the driver object in Docker / any virtual machine.
// DesiredCapabilities — Specifies the browser
// URL — Specifies the url where docker was installed (here localhost, my system) and port# where docker is running.
DesiredCapabilities cap = DesiredCapabilities.chrome();
URL u = new URL(“http://localhost:4444/wd/hub");
//RemoteWebDriver is class to use to run tests in Docker / remote, like WebDriver which we use while running tests in your local
RemoteWebDriver driver = new RemoteWebDriver(u, cap);
driver.get(“https://www.google.com/");
String pageTitle = driver.getTitle();
System.out.println(“Page title of browser opened in Docker: “+pageTitle);
}
}
Run this test case, and verify the page title printed in console, without opening browser in Local.
Note: Here, I have explained only the part of running Selenium tests in Docker.
I have not touched other parts of — Framework changes, and pulling the images and running containers automatically from script (writing docker-compose files), and later integrating with Jenkins.
Will add those topics if required, based on the comments.
Please comment / provide your feedback on this post.