Skip to content

How To Deploy Selenium Grid Using Docker.

Disposable Selenium Grid

Why We Need Docker?

Selenium Grid is a smart proxy server that makes it easy to run tests in parallel on multiple machines, which reduces execution time from days to hours. Docker is a platform that is used to develop, ship, and run applications by using containers. Docker provides virtualization at the operating system level. Docker helps a lot in setting up test labs using Grid in very easy and simple steps and helping to remove all complexities. In this article, we will try to set up a disposable selenium grid with the help of Docker-compose.

One of the biggest benefits of docker is its capability to scale. It is very helpful because running a selenium grid on virtual machines takes up a lot of unnecessary computing overhead. On the other hand, docker images share system resources so we need fewer resources than a virtual machine.

Prerequisites:

1)Install Docker on your Machine.

2)Create a Selenium Cucumber Project

If you already don’t have a BDD project, you can fork the project from Here.

3)Create disposable selenium Grid using Docker Compose.

How To Create Disposable Selenium Grid Using Docker compose:

1)Download all these images by executing below mentioned commands:

docker pull selenium/hub:3.14

docker pull selenium/node-firefox:3.14

docker pull selenium/node-chrome:3.14

2)Create docker-compose.YAML file in the project directory:

version: "3"
services:
  hub:
    image: selenium/hub:3.141.59
    shm_size: '1gb'
    ports:
      - "4444:4444"
  chrome:
    image: selenium/node-chrome:3.141.59
    depends_on:
      - hub
    environment:
      - HUB_HOST=hub
  firefox:
    image: selenium/node-firefox:3.141.59
    depends_on:
      - hub
    environment:
      - HUB_HOST=hub

The project Structure should look like this:

3)Open Terminal and go to project Directory. Start docker container with docker-compose up.

docker-compose up

Docker-compose uses a YAML file to configure the application services like a hub, chrome and Firefox will be the services in this case.

The command will create, start and attach the containers. Nodes will register and start disposable grid at localhost:4444

Disposable Selenium Grid
Selenium Grid

4)Open WebDriverFactory class and place setupGrid method inside it. This method will set the remote web driver and initiate tests on the Chrome browser of Selenium Grid.

static String completeUrl = "http://" + host + ":4444/wd/hub";
 public static WebDriver setupGrid(String browserName) throws Exception
   {
	if (browserName == null) {
	throw new Exception("Browser name is null");
    }

    DesiredCapabilities capabilities = DesiredCapabilities.chrome();
    driver.set( new RemoteWebDriver(new URL(completeUrl),capabilities));
    return driver.get();
 }

5)On Terminal Run mvn test. On Successful execution, you will see a build success message.

Execution Result
DisposabelSeleniumGrid

To stop and remove the containers use the command docker-compose down.

By default, the only things removed are:

  • Containers for services defined in the Compose file
  • Networks defined in the network section of the Compose file
  • The default network, if one is used.