How to run selenium test in different browser with docker

Reading Time: 4 minutes

In the post, we have to elaborate how to run the Selenium test with Docker and execute both in Chrome and Firefox browsers. And We will also understand why we should run The Selenium tests in Docker?

Basic Points

There are few Basic Points that need to start the and install the docker

Why we should run the selenium test in Docker ?

we can run our tests in docker, so firstly it important to understand the problem for Docker then we can solve us.

Issues to creation for session

  • If we have run without Docker, The error will occur before - the session not created : Only Chrome Driver support this version 96.0
  • This is error occur when we use the latest version of the Chrome Driver and the version of the old browser doesn’t match. In my system , my latest browser version was 96 and the old Chrome Driver version was 89. Now, there are Two options to solve the problem-
  • if we need to run the test in lasted Chrome Driver I need to update the Chrome Driver to old to the new version And the current version was 96
  • if we run the test old version then we need to downgrade to our Chrome Driver
  • this is a most common problem to mismatch the current version and the old version. So we can solve this problem so we can use the Docker image that will be a specific Chrome Driver and as well as the Browser install which is compare with each other. Then What version we have to use in our system and you can go ahead and execute our tests with in Docker

Support Multiple Version

  • Other advantage to use with Docker that we have a multiple Chrome Version set up which we use the execute out tests. for example, I have run Chrome version 96 on the port of 4444 and Chrome version 81 running on port 4446 so we can execute our test on both port

The Docker provide us with a lot of more flexibility in the term of we can chose the different browser and choosing the different versions without having an kind of infrastructure setup

Run the Selenium Standalone Chrome Docker image locally

  • It provided us with a some images that we can run our tests on it. the one we have to use in the blog the selenium /standalone we use a chrome image to execute the following syntax or a command in our CMD or a terminal to run the image – docker run -d 4444:4444 –shm-size=2g selenium chrome version :3.141.59
  • Let we take a closer to look a command for docker
  • -d flag is used to run docker container in the execute mode
  • -p flag is use to pass the port, first port 4444: is the port of our system, and the second is :4444 is the port of inside the docker container —shm-size=2g is important flag to used so the docker container can have share the host’s memory to be selenium chrome :3.141 is the use a image tag that we have to use
  • once we have to execute our command, then it will be put the image of our provided and start up the container. you can see here the container running by doing docker
Now, we can see the Selenium Standalone running on the url  http://localhost:4444

Run to the Selenium tests on Docker

  • Now, we can have see docker par figured out it is time to start our test to run on the port of 4444. So this part should be specific to the framework that we are using in our selenium scripts. In our case, I have to update the following points to test the port 4444.
  • Now we can run the tests , I will do that doing node test.js , then we a head over to the site  http://localhost:4444/hub/static/resource/hub.html and then we can see a Chrome session have being created and that is running inside a Docker container.
It can run finish mode then it delete automatically

Selenium tests Run on Firefox Inside the Docker Container

  • Run Firefox it is almost the same as Chrome Browser , We can just need to run new image and we can point to the different port locally so that it does not compare with our existing running port. 
  • docker run -d -p 4445:4444 –shm-size 2g selenium/standalone-firefox:3.141
  • Note: the port inside you can see the docker container can remain the same but that is not connected with your Chrome Docker image. 
    You need to changes in your code to point your tests to Firefox and on the new port.


So there you can go, we used the Selenium Standalone Docker images to execute the tests inside Docker containers and run the both Firefox and Chrome Browser. So We also learned why we should run our tests inside the Docker container and the advantages we get from it.



Written by 

I am a software tester