Selenium Grid with Docker


What is Selenium Grid?

Selenium Grid is a part of Selenium component which supports Distributed test execution and it will let us run our tests on Multiple Operating systems against different browsers simultaneously.

Traditional Selenium grid steps are,

1.Start Hub and connect all the nodes to it using below commands
     Hub : java -jar selenium-server-standalone-3.xx.x.jar -role hub

     Node1 : java -jar selenium-server-standalone-3.xx.x.jar -role node -hub
             http://<systemip>:4444/grid/register -port 5556 -browser browserName=firefox

     Node2 : java -jar selenium-server-standalone-3.xx.x.jar -role node -hub
             http://<systemip>:4444/grid/register -port 5557 -browser browserName=chrome
2.Define the Desired capability to identify the OS platform and OS browser for execution
    DesiredCapabilities desCap = null;
    if(browser.equals("firefox")){
        desCap=DesiredCapabilities.firefox();
        desCap.setBrowserName("firefox");
        desCap.setPlatform(Platform.WINDOWS);
    }else if(browser.equals("chrome")){
        desCap=DesiredCapabilities.chrome();
        desCap.setBrowserName("chrome");
        desCap.setPlatform(Platform.WINDOWS);
    }else{
        desCap=DesiredCapabilities.internetExplorer();
        desCap.setBrowserName("iexplore");
        desCap.setPlatform(Platform.WINDOWS);
    }

3.Pass the Hub URL to the Remote Webdriver of the Test script to connect with Node system
          Webdriver driver=new RemoteWebDriver("http://localhost:4444/wd/hub", desCap)

Once the Hub found the browser version which you want to execute the script, it will identify and trigger the script.

Selenium Grid with Docker

Challenging part of Selenium Grid is, we required Node systems with the installation of the specified browsers. Which would be tough to get if we execute our Selenium test scripts in the Continuous Integration server.

So, to rectify this issue, Selenium groups itself has developed Selenium Hub and Node docker images.

Now, you may ask the question - What is docker? Is it something related to Selenium package? No, Absolutely not.

Docker is basically a container engine which uses the Linux Kernel features like namespaces and control groups to create containers on top of an operating system and automates the application deployment on the container.

In other words, Containers allow you to run an application and its dependencies in resource-isolated processes.

Selenium developers has already created a separate docker image file for Selenium hub and Selenium node with inclusion of libraries required for them to work independently.

Example: Selenium Chrome docker file will have steps to install Java, install Chrome browser and start the selenium node with browser name as chrome. When we run the Docker command, it provides an environment to run our Selenium test scripts in the resource isolated environment. 

In Selenium Automation testing, we are going to use Docker to avoid machine or browser dependencies while executing the test scripts against different platforms with multiple browsers.

Precondition : Need to install Docker in your machine or server

Steps to run selenium scripts with Docker,

1.Instead of using Jar file to start hub in one machine and nodes in different machine, we are going to start hub and    different set of nodes in same machine or server using below docker commands
            docker run -d -p 4444:4444 --name selenium-hub selenium/hub:latest
            docker run -d --link selenium-hub:hub selenium/node-chrome:latest
            docker run -d --link selenium-hub:hub selenium/node-firefox:latest

   Get the hub url for Remote webdriver from Docker
2.Define the Desired capability to identify the OS platform and OS browser for execution
            DesiredCapabilities desCap = null;
            if(browser.equals("firefox")){
              desCap=DesiredCapabilities.firefox();
              desCap.setBrowserName("firefox");
              desCap.setPlatform(Platform.WINDOWS);
            }else if(browser.equals("chrome")){
             desCap=DesiredCapabilities.chrome();
             desCap.setBrowserName("chrome");
             desCap.setPlatform(Platform.WINDOWS);
           }else{
             desCap=DesiredCapabilities.internetExplorer();
             desCap.setBrowserName("iexplore");
            desCap.setPlatform(Platform.WINDOWS);
          }
3.Pass the Hub URL to the Remote Webdriver of the Test script to connect with Node system
          Webdriver driver=new RemoteWebDriver("http://<Docker Grid host: >4444/wd/hub", desCap)

We are done. Now, the script will run in the Isolated environment without any dependencies on Machines and Browsers.

Note: Using VNC viewer, we can see the execution of test scripts in the Docker Container

Popular posts from this blog

Selenium Webdriver Interview Questions with Answer

Selenium IDE Interview Questions with Answer

DLL to Jar file conversion