Site icon Vinsguru

Selenium WebDriver – How To Create A Disposable Selenium Grid Infrastructure using Docker

Selenium Grid – Overview:

Selenium Grid is used to run our tests on different OS/browsers in parallel. It basically uses master-slaves (or hub-nodes) concept – where there is one master/hub and there are few slaves/nodes registered to the master/hub. When we send our tests to the master/hub for execution, based on the browser/OS requirements of the test, master will route the request to the appropriate nodes and get them executed. Thus it minimizes the overall execution time of the tests.

 

 

Before using Docker, lets first see how we normally setup the Selenium Grid.

Udemy – Selenium WebDriver With Docker:

TestAutomationGuru has released a brand new course in Udemy on Selenium WebDriver with Docker. 14 hours course which starts with installing docker from scratch and goes all the way up to running dockerized selenium tests on AWS cloud. Please access the above link which gives you the special discount.  You can also get your money back if you do not like the course within 30 days.

Selenium Grid – Setup:

java -jar selenium-server-standalone-3.3.1.jar -role hub
java -jar selenium-server-standalone-3.3.1.jar -role node -hub http://localhost:4444/grid/register
java -jar selenium-server-standalone-3.3.1.jar \
    -role node -hub http://localhost:4444/grid/register \
    -Dwebdriver.chrome.driver=.\chromedriver.exe

Then, we could run our test as shown below.

DesiredCapabilities dc = DesiredCapabilities.firefox();
dc.setPlatform(Platform.WINDOWS);
 
RemoteWebDriver driver=new RemoteWebDriver(new URL("http://localhost:4444/wd/hub"), dc);
driver.get("https://www.google.com");

Challenges:

Setting up selenium grid & maintaining is not very easy.

Docker:

What is the use of docker here?

Docker is a bit like a virtual machine. But unlike a virtual machine, rather than creating a whole virtual operating system, Docker allows applications to use the same Linux kernel as the system that they’re running on and only requires applications be shipped with things not already running on the host computer. This gives a significant performance boost and reduces the size of the application – source: opensource.com

Docker is a manager of Infrastructure. It will be able to package a software and all its dependencies to run as a container. You can deploy the software, packaged as a docker image, in any machine where docker is installed. It, kind of, separates the software from the hardware – so the developer / test engineer can rest assured that the application will run on any machine regardless of any customized settings that machine might have that could differ from the machine used for writing and testing the code.

Please check the official docker site for installation.

Selenium Grid Setup using Docker:

Selenium official has already setup docker images for you to setup the selenium grid. Once you have installed docker in a machine,

sudo docker run -d -p 4444:4444 --name selenium-hub selenium/hub
sudo docker run -d --link selenium-hub:hub selenium/node-chrome
sudo docker run -d --link selenium-hub:hub selenium/node-firefox

The above commands takes care of downloading already built image (with all the dependencies like java, chrome, firefox, selenium-server-standalone jar etc… required to run your selenium tests).

You can access the selenium-grid console using http://localhost:4444/grid/console

If you need one more chrome node instance, run this command.

sudo docker run -d --link selenium-hub:hub selenium/node-chrome

It creates one more container with chrome and all the dependencies and registers to the hub. Issuing sudo docker ps command will show the running containers.

To shutdown the docker-grid infrastructure, run the below commands.

sudo docker stop $(sudo docker ps -a -q)
sudo docker rm $(sudo docker ps -a -q)

Docker has greatly simplified the selenium-grid setup process. However there is still some manual work.

Challenges:

Selenium Grid Setup using Docker-Compose:

As part of our Selenium Grid we need to have 1 hub and few nodes like chrome and firefox. We can define all these services in a file called docker-compose.yml file and bring the entire infrastructure up and running by using a single command. Docker provides a tool for that – Docker-Compose.

Please check the steps here for the installation of docker-compose.

Once installed, create a new directory and create a docker-compose.yml file with below content.

docker-compose file:

version: "3"
services:
  selenium-hub:
    image: selenium/hub
    container_name: selenium-hub
    ports:
      - "4444:4444"
  chrome:
    image: selenium/node-chrome
    depends_on:
      - selenium-hub
    environment:
      - HUB_PORT_4444_TCP_ADDR=selenium-hub
      - HUB_PORT_4444_TCP_PORT=4444
  firefox:
    image: selenium/node-firefox
    depends_on:
      - selenium-hub
    environment:
      - HUB_PORT_4444_TCP_ADDR=selenium-hub
      - HUB_PORT_4444_TCP_PORT=4444

Issue below command

sudo docker-compose up -d

sudo docker-compose ps

Now, do you need 5 instance of chrome? Not a big deal!! docker will take care of creating 4 more instances of chrome within few seconds. Just issue below command.

sudo docker-compose scale chrome=5

sudo docker-compose ps

The selenium grid console will look like this

Now the entire grid can be brought down by issuing a single command.

sudo docker-compose down

 

Summary:

Managing selenium grid infrastructure was a pain. By using docker, we could reduce the significant amount of effort in setting the up the selenium grid. We can bring this infrastructure up and down anytime by issuing a single command and It is highly scalable as well.

However there is a dependency that this dockerized selenium grid should be up and running before you start tests. Once we are done with our tests, we do not have to keep the grid up and running. Currently as per this approach – this is managed outside the framework – either manually or some automated script.

Is there any way for us to manage this selenium-grid as part of our framework itself? Yes, Easily!! Check below article for more information.

Managing Selenium Grid Infrastructure using Arquillian Cube

Ok, so we have simplified selenium grid setup using Docker. But what about the automated selenium tests? Can we dockerize that too?

Sure, why not? Check below article.

Running automated tests inside docker container

Happy Testing & Subscribe 🙂

 

Share This:

Exit mobile version