Selenium Grid Setup with Docker
Hello readers,
from past few days I’ve been thinking about reducing automation test execution time by creating an environment where I could perform cross-browser and/or parallel execution.
I came-across once solution where we could leverage docker to setup selenium grid on our local machine.
Selenium Grid Setup
Pre-requisite: Docker
Pulling images with below commands:
# pulling hub imagedocker pull selenium/hub:latest
# pulling node images
# pulling node-chromedocker pull selenium/node-chrome:latest# pulling node-firefoxdocker pull selenium/node-firefox:latest# pulling node-edgedocker pull selenium/node-edge:latest
Now that we have all required images, we shall move ahead with using this image for our execution
# creating custom networkdocker network create grid# running hub imagedocker run -d --rm -p 4442–4444:4442–4444 --net grid --name selenium-hub selenium/hub:latest# running node-chrome imagedocker run -d --rm --net grid -e SE_EVENT_BUS_HOST=selenium-hub --name node-chrome -e SE_EVENT_BUS_PUBLISH_PORT=4442 -e SE_EVENT_BUS_SUBSCRIBE_PORT=4443 selenium/node-chrome:latest# running node-edge imagedocker run -d --rm --net grid -e SE_EVENT_BUS_HOST=selenium-hub --name node-edge -e SE_EVENT_BUS_PUBLISH_PORT=4442 -e SE_EVENT_BUS_SUBSCRIBE_PORT=4443 selenium/node-edge:latest# running node-firefox imagedocker run -d --rm --net grid -e SE_EVENT_BUS_HOST=selenium-hub --name node-firefox -e SE_EVENT_BUS_PUBLISH_PORT=4442 -e SE_EVENT_BUS_SUBSCRIBE_PORT=4443 selenium/node-firefox:latest

Script Execution
As you can see ahead you can use the same driver variable with different browser options to run your script on different browsers.
URL is a must for hub identification, here I’m using localhost as I’m using it on a local machine.
# remote driver setupURL url = new URL(“http://localhost:4444/wd/hub");
To test on Chrome
# to execute your scripts on chrome browserWebDriver driver = new RemoteWebDriver(url,new ChromeOptions());
To test on Edge
# to execute your scripts on edge browserWebDriver driver = new RemoteWebDriver(url,new EdgeOptions());
To test on Firefox
# to execute your scripts on firefox browserWebDriver driver = new RemoteWebDriver(url,new FirefoxOptions());
rest of you code will be as it is, difference is only in initialization
Stopping containers
# stopping all containers
docker stop node-chrome node-edge node-firefox selenium-hub# removing all stopped containers
docker container prune
Multiple Browser instances
Before we worked on how to set up a grid for execution.
Now we can try how to increase the no. of instances of the browser so that we can go head with parallel execution.
# run chrome node
docker run -d --rm --net grid -e SE_EVENT_BUS_HOST=selenium-hub --name node-chrome --shm-size="2g" -e SE_NODE_MAX_SESSIONS=8 -e SE_EVENT_BUS_PUBLISH_PORT=4442 -e SE_EVENT_BUS_SUBSCRIBE_PORT=4443 selenium/node-chrome:latest# run edge node
docker run -d --rm --net grid -e SE_EVENT_BUS_HOST=selenium-hub --name node-edge --shm-size="2g" -e SE_NODE_MAX_SESSIONS=8 -e SE_EVENT_BUS_PUBLISH_PORT=4442 -e SE_EVENT_BUS_SUBSCRIBE_PORT=4443 selenium/node-edge:latest# run firefox node
docker run -d --rm --net grid -e SE_EVENT_BUS_HOST=selenium-hub --name node-firefox --shm-size="2g" -e SE_NODE_MAX_SESSIONS=8 -e SE_EVENT_BUS_PUBLISH_PORT=4442 -e SE_EVENT_BUS_SUBSCRIBE_PORT=4443 selenium/node-firefox:latest
