{{announcement.body}}
{{announcement.title}}

How to Integrate Selenium Grid With Docker

DZone 's Guide to

How to Integrate Selenium Grid With Docker

It is very easy to integrate Selenium Grid with Docker. Here's how.

· Integration Zone ·
Free Resource

You must have studied about Selenium Grid. Selenium Grid is a component of Selenium used for parallel testing across different browsers and machines. You can have your system as a hub that will distribute the test cases across various other machines. Other machines on which the test will be distributed are called 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 used to store 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, you to have to test the tests cases on Firefox v46+, but if you have a limitation, then 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 with Firefox v46+ so that you can easily run your test cases. You can easily save time performing parallel testing.

Docker is a container that stores libraries and dependencies. If you want your Selenium Grid Server to be up and running, you have to download a Selenium JAR file in every hub and node. You have it, then fire the command in the hub to get the server up. From that, you can get the IP address. In the nodes, you can get the server up by adding the IP of the hub and port number. This process is very time-consuming and takes a lot of manual effort. Docker will help you do 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 prerequisites to installed on your machine:

  • JDK v1.7+ on your machine

  • Chrome and Firefox browser installed

  • Selenium Web Driver JARs

  • Test cases designed in Selenium Web Driver and TestNg

  • In TestNG.xml, the parallel flag should be true so that parallel testing is possible.

Steps to Download Docker

Now, you have all the prerequisites to install Docker in your machine. Now, the main step is to download Docker. Let’s look at the steps.

  • Download Docker.
  • It will pop up the installation window and there you have to click on all the checkboxes.
  • Click on every Next button during installation.
  • After the installation is finished, you will get three Docker options: Virtual Box, Docker Quickstart, and Kitematic.
  • Open the Docker quickstart terminal to get a unique IP for your machine.
  • After doing that, you will get the message “Start interactive shell.”
  • After getting the above message, Docker is installed properly and you can start using it.

Now, to integrate Docker with Selenium Grid, you have to install hubs and nodes. You can start using hubs 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 that will allow tests to be executed from the 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 and search for these images in the search box one by one but you'll get countless results. You have to select an image with a maximum number of pulls (i.e. an image downloaded the maximum number of times). Click on that image and then you will get a Docker command. Copy and save it.

Let’s have a look at one example.

docker pull selenium/hub

Now, you have to come back to the quikstart terminal. Open that and put the command in it. 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 this 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 you generally do is make the Selenium server up and running on the hub from the Docker. Open the quick start terminal and type the below command.

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

Once you hit this command, the hub server will be up and running. You can verify whether it is up by going to url in your browser: http://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 get 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 the names Chrome and Firefox. Run the below command to run a Chrome node.

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

Run the below command to run a Firefox node:

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

Now, again go to the URL on the hub machine. You will see two nodes connected with the hub. Now, you have to find the port numbers on which your nodes are running. Run the bellow command in the quick start 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. Click Connect on the pop-up. If it asks for a password, put “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.

Let’s see a sample test script:

public class GridTest {
 @Test
 public void seleniumGridTest() throws MalformedURLException {
   DesiredCapabilities;
   if (browserType.equals("firefox")) {
    .firefox();
    cap.setBrowserName("firefox");
    cap.setPlatform(Platform.WINDOWS);
   } else {
    .internetExplorer();
    cap.setBrowserName("iexplore");
    cap.setPlatform(Platform.WINDOWS);
   }
   RemoteWebDriver RemoteWebDriver(new URL(http: //localhost:4444/wd/hub), cap);
      driver.navigate().to("http://gmail.com");
      driver.findElement(By.xpath("//input[@]")).sendKeys("username");
      driver.findElement(By.xpath("//input[@]")).sendKeys("password");
      driver.close();
     }

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

A video on Selenium Grid with Docker by Docker Houston Meetup:

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 systems. Just follow the above steps and save time. All the best!

Topics:
selenium grid ,integration ,tutorial ,docker

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}