Software Testing

Selenium Grid Setup with Docker Containers

Selenium Grid Setup with Docker Containers

In this tutorial, we will learn the steps for running Selenium Grid Setup with Docker Containers, an app containerization platform.

Let’s get started to set up the grid.

Pull Docker Images

Navigate to the https://hub.docker.com URL

Search for Selenium Hub.

We need to pull docker images from the Docker Repository Hub to get started with the Selenium grid setup.

Selenium Hub

Pull the Selenium Hub docker image.

\> docker pull selenium/hub

Docker Selenium Hub

 

Pull Selenium Nodes

Pull some Selenium Node docker images. In this example, we will use two nodes.

  • selenium/node-firefox
  • selenium/node-chrome

Pull the selenium nodes from the remote repository.

\> docker pull selenium/node-firefox

\> docker pull selenium/node-chrome

 

Docker pull selenium Node Firefox

We can check the images using the docker local repository command.

\> docker images

 

Docker Local Repository

Start Docker Containers

Start Selenium Hub

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

Start Nodes

Start nodes and connect them with the Selenium Hub.

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

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

Selenium Grid with Docker Containers Illustration

 

Verify Grid Console

Open the Browser and navigate to the Grid console URL.

  • http://localhost:4444/grid/console

 

Docker Selenium Grid Console

Sample Test

Run a sample test against the Grid Hub URL. The test just opens an URL and prints the title of the Web page. If we run multiple tests the hub would distribute the tests on the nodes.

package com.testingdocs.docker.demo;

import org.testng.annotations.Test;
import java.net.URL;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeOptions;
import org.openqa.selenium.firefox.FirefoxOptions;
import org.openqa.selenium.remote.RemoteWebDriver;

public class SeleniumGridOnDockerTest {

	@Test
	public void FBTestTestOnDocker() throws Exception {
		FirefoxOptions fOptions = new FirefoxOptions();
		URL remote = new URL("http://localhost:4444/wd/hub");
		WebDriver driver = new RemoteWebDriver(remote, fOptions);
		driver.get("https://www.facebook.com");
		System.out.println(driver.getTitle() + 
" \nwww.TestingDocs.com -Selenium Tutorials");
		System.out.println("Selenium Grid using Docker container."
				+ "Check the Docker Logs for more information!");
	}
}


 

Sample Output

Execute the test and verify the Docker container logs.

Selenium Grid on Docker Sample Test