grid001

Selenium WebDriver – How To Create A Disposable Selenium Grid Infrastructure using Docker

Selenium Grid – Overview:

Selenium Grid is used to run our tests on different OS/browsers in parallel. It basically uses master-slaves (or hub-nodes) concept – where there is one master/hub and there are few slaves/nodes registered to the master/hub. When we send our tests to the master/hub for execution, based on the browser/OS requirements of the test, master will route the request to the appropriate nodes and get them executed. Thus it minimizes the overall execution time of the tests.

 

grid001

 

Selenium Grid – Setup:

  • Install Java
  • Download  the latest selenium-server-standalone-X.XX.jar from Selenium-Downloads site.
  • Start the hub
java -jar selenium-server-standalone-3.3.1.jar -role hub
  • Register a node
java -jar selenium-server-standalone-3.3.1.jar -role node -hub http://localhost:4444/grid/register
  • Install the required browsers for your test in the node
  • Download the browser’s driver executable.
  • Register the node including the path for the executable as shown below.
java -jar selenium-server-standalone-3.3.1.jar \
    -role node -hub http://localhost:4444/grid/register \
    -Dwebdriver.chrome.driver=.\chromedriver.exe

Then, we could run our test as shown below.

DesiredCapabilities dc = DesiredCapabilities.firefox();
dc.setPlatform(Platform.WINDOWS);
 
RemoteWebDriver driver=new RemoteWebDriver(new URL("http://localhost:4444/wd/hub"), dc);
driver.get("https://www.google.com");

Challenges:

Setting up selenium grid & maintaining is not very easy.

  • Downloading and setting up all the dependencies for each node.
  • Java process – will eventually run out-of-memory.
  • Manually restarting the nodes in case of issues.
  • Maintenance is expensive.
  • Scalability issues.

Docker:

What is the use of docker here?

Docker is a bit like a virtual machine. But unlike a virtual machine, rather than creating a whole virtual operating system, Docker allows applications to use the same Linux kernel as the system that they’re running on and only requires applications be shipped with things not already running on the host computer. This gives a significant performance boost and reduces the size of the application – source: opensource.com

Docker is a manager of Infrastructure. It will be able to package a software and all its dependencies to run as a container. You can deploy the software, packaged as a docker image, in any machine where docker is installed. It, kind of, separates the software from the hardware – so the developer / test engineer can rest assured that the application will run on any machine regardless of any customized settings that machine might have that could differ from the machine used for writing and testing the code.

Please check the official docker site for installation.

Selenium Grid Setup using Docker:

Selenium official has already setup docker images for you to setup the selenium grid. Once you have installed docker in a machine,

  • Setup Hub
sudo docker run -d -p 4444:4444 --name selenium-hub selenium/hub
  • Setup nodes with Chrome & Firefox and register to the hub
sudo docker run -d --link selenium-hub:hub selenium/node-chrome
sudo docker run -d --link selenium-hub:hub selenium/node-firefox

The above commands takes care of downloading already built image (with all the dependencies like java, chrome, firefox, selenium-server-standalone jar etc… required to run your selenium tests).

You can access the selenium-grid console using http://localhost:4444/grid/console

docker-grid

If you need one more chrome node instance, run this command.

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

It creates one more container with chrome and all the dependencies and registers to the hub. Issuing sudo docker ps command will show the running containers.

grid002

To shutdown the docker-grid infrastructure, run the below commands.

sudo docker stop $(sudo docker ps -a -q)
sudo docker rm $(sudo docker ps -a -q)

Docker has greatly simplified the selenium-grid setup process. However there is still some manual work.

Challenges:

  • Need to start/stop the containers one by one to setup the hub and nodes.

Selenium Grid Setup using Docker-Compose:

As part of our Selenium Grid we need to have 1 hub and few nodes like chrome and firefox. We can define all these services in a file called docker-compose.yml file and bring the entire infrastructure up and running by using a single command. Docker provides a tool for that – Docker-Compose.

Please check the steps here for the installation of docker-compose.

Once installed, create a new directory and create a docker-compose.yml file with below content.

docker-compose file:

version: "3"
services:
  selenium-hub:
    image: selenium/hub
    container_name: selenium-hub
    ports:
      - "4444:4444"
  chrome:
    image: selenium/node-chrome
    depends_on:
      - selenium-hub
    environment:
      - HUB_PORT_4444_TCP_ADDR=selenium-hub
      - HUB_PORT_4444_TCP_PORT=4444
  firefox:
    image: selenium/node-firefox
    depends_on:
      - selenium-hub
    environment:
      - HUB_PORT_4444_TCP_ADDR=selenium-hub
      - HUB_PORT_4444_TCP_PORT=4444

Issue below command

sudo docker-compose up -d

grid003

sudo docker-compose ps

grid004

Now, do you need 5 instance of chrome? Not a big deal!! docker will take care of creating 4 more instances of chrome within few seconds. Just issue below command.

sudo docker-compose scale chrome=5

grid005

sudo docker-compose ps

grid006

The selenium grid console will look like this

docker-grid-002

Now the entire grid can be brought down by issuing a single command.

sudo docker-compose down

grid007

 

Summary:

Managing selenium grid infrastructure was a pain. By using docker, we could reduce the significant amount of effort in setting the up the selenium grid. We can bring this infrastructure up and down anytime by issuing a single command and It is highly scalable as well.

However there is a dependency that this dockerized selenium grid should be up and running before you start tests. Once we are done with our tests, we do not have to keep the grid up and running. Currently as per this approach – this is managed outside the framework – either manually or some automated script.

Is there any way for us to manage this selenium-grid as part of our framework itself? Yes, Easily!! Check below article for more information.

Managing Selenium Grid Infrastructure using Arquillian Cube

Ok, so we have simplified selenium grid setup using Docker. But what about the automated selenium tests? Can we dockerize that too?

Sure, why not? Check below article.

Running automated tests inside docker container

Happy Testing & Subscribe 🙂

 

Share This:

Categories: Arquillian, Articles, CI / CD / DevOps, Docker, Framework, Selenium, Selenium Grid

2 comments