🚀Run your selenium test in Docker! (With selenium grid)🐳

Alan Liew
4 min readMar 20, 2023

--

Well, this article assumes you’ve got the fundamentals of basic Docker command, how containers and between them work together.

(If you’re not, you can read these articles here

  1. Docker Networking 🌎 (The only article you need to read)
  2. “Understand what Docker is in 5 minutes: Challenged Accepted”

✅ First, we start with the question WHAT and WHY?

Selenium Grid allows the execution of WebDriver scripts on remote machines by routing commands sent by the client to remote browser instances.

Selenium Grid provides an easy way to run tests in parallel on multiple machines. Allow testing on different browser versions and enable cross-browser testing.

✅ Is it worth using Selenium Grid?

Selenium Grid runs test suites in parallel against multiple machines (called Nodes).

Execution time can be expressed as a simple formula:

Number of Tests * Average Test Time / Number of Nodes = Total Execution Time

15      *       45s        /        1        =      11m 15s   // Without Grid
15 * 45s / 5 = 2m 15s // Grid with 5 Nodes
15 * 45s / 15 = 45s // Grid with 15 Nodes
100 * 120s / 15 = 13m 20s // Would take over 3 hours without Grid

As you can see in the diagram above, the time to execute tests with and without using Selenium Grid is huge.

With that, we knew that with Selenium Grid, we could run our test a lot faster.

✅ Here are 2 Methods to use Selenium Grid

  1. Run the standalone on your local machine, it is the easiest mode to spin up a Selenium Grid. By default, the server will listen for RemoteWebDriver requests on http://localhost:4444.
  2. Run the standalone on docker.

In this article, we will only focus on how to run standalone on docker.

✅ Run the standalone on docker.

Here is an example of (Hybrid Framework ) involving maven + testng in selenium Java

First, we will need to have a remote web driver and make sure it points to https://localhost:4444, the default port for the selenium grid.

Desired Capabilities

We can create and initiate DesiredCapabilities. DesiredCapabilities are a set of key-value pairs encoded as a JSON object.

DesiredCapabilities cap = new DesiredCapabilities();



cap.setBrowserName("chrome");
driver = new RemoteWebDriver(new URL("http://localhost:4444/"), cap);

And we named the browser chrome. Then create a RemoteWebDriver that points to the URL with the DesiredCapabilities.

Now, we can run this selenium grid in the docker container using the standalone image.

docker run -d -p 4444:4444 -p 7900:7900 --shm-size="2g" --name mymamatest seleniarm/standalone-chromium 

The above docker image is for Mac M1 / M2, you can choose the docker image here if you’re using Mac Intel Chip / Windows OS.

Above command is to ask the docker to run this in detached mode, pointing it to the 4444 port and 7900 port (VNC) and assigning 2GB ram to this container with the seleniarm/standalone-chromium image.

One thing you have to make sure of is that you have started docker and that docker is running in the background.

Here is how it should look when the container is started successfully.

Now navigate to https://localhost:4444 and https://localhost:7900

https://localhost:4444
https://localhost:7900

Now, run this command in the terminal.

mvn test

You will see the test is occupying your node and creating sessions in localhost:4444.

You can also log in to localhost:7900 with the default password of “secret” if you want to see how is the browser testing going in live.

There is 1 session and 1 node being occupied.
The VNC shows the live view of the test.

Ta-da, this is how you run tests using Selenium Grid in Docker container.

✅ Related Articles:

  1. Cloud Computing, let’s talk about it [Part 2]
  2. AWS ! the terms you must know 🤫
  3. ISTQB 🧙🏻‍♀️The software testing Myth, do you need that?
  4. Why is software testing needed in the first place?
  5. What is Agile Methodology?
  6. Java 101 for software testing (Using fruits and Vehicle as examples)
  7. What do you need to know to test using Selenium in Java? (that I wished somebody had told me)
  8. “Understand what Docker is in 5 minutes : Challenged Accepted”

Or connect with me in LinkedIn to discuss more !

#Softwaretesting #softwaretestere #SDLC #STLC #ISTQB #Java101 #SeleniumJava #docker #dockercontainer #dockerimage #docker101

--

--

Alan Liew
Alan Liew

Written by Alan Liew

I eat, drink and talk about software testing.. "No Bugs is allowed"

No responses yet