Posted in

How to Integrate Selenium Grid with Docker?

You must have studied about selenium grid. Selenium Grid is a component of Selenium which is used for parallel testing across different browsers and machines. You can have your system as a hub which will distribute the test cases across various other machines. Other machines on which the test will be distributed are called as nodes. A hub is a machine on which the tests are executed but they run on different nodes. This doesn’t overload the machine and also, if you have less time then you can go for parallel testing using different nodes. Docker is a container which is used for storing databases, libraries, and dependencies. These will be used to built or run any application.

Why Docker with Selenium Grid?

If you want a specific configuration of a browser like you to have to test the tests cases on Firefox version more than 46 but if you have a limitation that you can’t install that version in your system. Using 46 versions in your system can disrupt other applications. So, in this case, it is better to go for a node which will be having 46 version of Firefox so that you can easily run your test cases over there. You can easily perform parallel testing and with this, a lot of time would be solved.

Docker is a container which stores libraries and dependencies. If you want your selenium grid server to be up and running, you have to download selenium jar file in every hub and node. You have to then fire a command in the hub which makes the server up and from that, you can get the IP address. In the nodes, you can up and the server by adding the IP of the hub and port number. This process is very time consuming and takes a lot of manual efforts. Docker will help you in doing all the processes in a single go.

Prerequisites for Docker Setup in your machine

Now, we will be integrating the selenium grid with Docker, but before doing that you should have some pre-requisites to be installed in your machine. Let’s have a look at those.

  • JDK 1.7 or above version in your machine.
  • Chrome and Firefox browser installed on your machine.
  • Selenium WebDriver Jars.
  • Test Cases designed in Selenium web driver and TestNg.
  • In TestNG.xml file, the parallel flag should be true so that parallel testing would be possible.

Steps to download Docker

Now, you have all the pre-requisites to install docker in your machine. Now, the main step is to download Docker. Let’s look at the steps involved in it.

  • Download Docker from the Docker toolbox present on their official site.
  • It will pop up the installation window and there you have to click on all the check boxes.
  • Click on every Next button during installation.
  • After the installation is finished, you will get three docker options. Those are Virtual Box, Docker Quickstart and Kitematic.
  • You can open the docker Quickstart terminal and then what it will do is to give a unique IP to your machine.
  • After doing that, you will get a message as Start interactive shell .
  • After getting the above message, your docker is installed properly and you can start using it.

Now, for integration of docker with selenium grid, you have to install hub and node. You can start using hub and nodes from docker only but for the first instance, you have to configure them into the docker container. For running tests through docker, you have to get some docker images. These images will allow execution of tests from docker container.

Images to be downloaded for Selenium Grid in Docker container:

  • Selenium hub image
  • Selenium node-firefox image
  • Selenium node-chrome image
  • Selenium node-firefox-debug image
  • Selenium node-chrome-debug image

Let’s look at the ways, you can download the images. You can go to the docker repository which is available online. You have to search for these images in search box one by one. You will get results for every search but you would get so many results based on your search. You have to select an image which would be having a maximum number of pulls that means an image which is downloaded the maximum number of times. Click on that image and then you will get a docker command from there. You have to copy that and save somewhere.

Let’s have a look at one example

docker pull selenium/hub

Once, you have searched for Selenium hub image in the docker repository. You clicked on the image with a maximum of pulls. You have copied the command which you got. Now, you have to come back to the quickstart terminal. Open that and put the command in that. In this way, an image will be downloaded in your docker machine. You must allow some times as downloading images takes some time. After the image installation is done, you will get a success message. Also, don’t try to run two image installations at the same time as it would make the installation very slow. Try to go one by one.

Other commands are:

  • docker pull selenium/node-firefox
  • docker pull selenium/node-firefox
  • docker pull selenium/node-firefox-debug
  • docker pull selenium/node-chrome-debug
  • Start using Selenium Grid and Docker

The first thing which you do generally is to make the selenium server up and running on the hub. This, you will be doing from the docker. Open the quickstart terminal and you have to write the below command there.

docker run -d -p 4444:4444 “name selenium-hub selenium/hub

Once, you will hit this command the hub server will be up and running. You can verify whether it is up or not by going to url in your browser: https://192.168.99.100:4444/grid/console. You can even change the IP address of the selenium grid if your docker is installed with a different IP address.

Now, you have made the hub running. You have to then make the nodes up and running and then connect the nodes with the hub. We have already saved the names of the nodes in docker by names “ Chrome and Firefox. Let’s assume that you want to run a chrome node, you can do it easily by below command.

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

But if you want to run a Firefox node, you can do it easily by firing below command:

docker run -d “link selenium-hub:hub selenium/node-firefox

Now, again if you will go the url on the hub machine. You will see two nodes which will be connected with the hub. Now, you have to find the port numbers on which your node “ Chrome and Firefox is running. You can easily find out by firing below command in the quikstart terminal.

docker ps -a

You have to now use VNC viewer. First, download it from the internet and do all the installation steps. Once, it is downloaded and installed, you can run it in your machine. You have to then type the HUB url in it and then the port number of your chrome node. You will be able to see a pop up which says Connect . Click on connect button. If it will ask for a password, give as secret .

You can do the same for the firefox node. Now, you can test by writing a test scrIPt so that browsers will open on these nodes and we can see whether parallel testing is being successfully performed or not.

Let’s see a sample test script:

public class GridTest {

          @Test

          public void seleniumGridTest() throws MalformedURLException{

                   DesiredCapabilities cap=null;

                   if(browserType.equals("firefox")){

                   cap=DesiredCapabilities.firefox();

                   cap.setBrowserName("firefox");

                   cap.setPlatform(Platform.WINDOWS);

                   }else{

                            cap=DesiredCapabilities.internetExplorer();

                            cap.setBrowserName("iexplore");

                            cap.setPlatform(Platform.WINDOWS);

                   }

                 RemoteWebDriver driver=new RemoteWebDriver(new     URL(https://localhost:4444/wd/hub), cap);

                   driver.navigate().to("https://gmail.com");

                   driver.findElement(By.xpath("//input[@id='Email']")) .sendKeys("username");

                   driver.findElement(By.xpath("//input[@id='Passwd']")) .sendKeys("password");

                   driver.close();

}

Also, you have to set the parallel flag in testng.xml file. You can do it by writing parallel= tests tag in suite name. When you will run the test, you will see the tests being executed on both the machines. You can analyse the behaviour of the application at the same time.

Conclusion

So, you have read how to integrate selenium grid with docker. It is very easy. You can run your tests and perform Automated Software Testing on different machines and operating system. Just follow the above steps and lessen the time wastage. All the best!!

Chirag Thumar is working as a Digital marketer at Nex which is a leading Web Development Company in India and USA. He runs his own company with main focus on Java-based web application using JQuery, Struts, JSF, node.Js, spring, Ext Js, software testing, etc. He Writes about Emergency technology, Java-based frameworks & tools, Software Testing Trends, Innovative quotes, Social Media News and online marketing.

Privacy Overview

This website uses cookies so that we can provide you with the best user experience possible. Cookie information is stored in your browser and performs functions such as recognising you when you return to our website and helping our team to understand which sections of the website you find most interesting and useful.