Dockerized Selenium Grid on Cloud

Published Oct 21, 2017Last updated Feb 25, 2018
Dockerized Selenium Grid on Cloud

Problem Statement

As a test automation engineer, you should have experienced the need to run parallel tests across several browsers at the same time in order to achieve more compatibility test coverage and faster tests executions. For this to be achieved, you might have used your local machine, but this only handle few sessions and will keep your machine busy for a while. Another solution you might have considered is to use cloud services such as SauceLabs, BrowserStack, etc.. But this is also come with a cost, and a really high cost.

Solution

Today, I got your back. We will build our own cloud service to run our automated tests against cross-browsers and cross-platforms at minimum cost using Selenium Grid built within Docker. Actually, I will get your started for free 😉

This article will take you 30 minutes to read and add the following skills on your resume right away:

  • Setting up Selenium Grid for remote test executions.
  • Getting started with Docker.
  • Creating cloud machines using DigitalOcean.
  • Managing your docker containers & remote machines using Rancher.

Selenium Grid Docker Rancher DigitalOcean - Page 1.png

Let's get into steps and you will learn all of the above by examples:

What is Selenium Grid

Consider it as a network of several devices (Nodes) connected to a single machine (Hub). Every node may have a different browser and platform type. For example, you may have a hub with 4 nodes connected (Firefox, Chrome, Safari, IE). In order to run your tests on these nodes, you send an order to the hub to execute tests on Safari and it will send it to the next available node that have Safari installed on it.

Globel-networking-Converted.png

Once you have your selenium grid up and ready, you may initiate your web driver to send tests to your selenium hub. Following is an example on how to do so:

import org.openqa.selenium.WebDriver;
import org.openqa.selenium.remote.DesiredCapabilities;
import org.openqa.selenium.remote.RemoteWebDriver;
 
import java.net.MalformedURLException;
import java.net.URL;
 
public class Grid_SetUp {
  public static WebDriver driver;
 
  public static void main(String[]  args) throws MalformedURLException, InterruptedException{
 
 		String URL = "http://www.baaz.com";
 		String Node = "http://hub_IP:4444/wd/hub";
 		DesiredCapabilities cap = DesiredCapabilities.firefox();
 
 		driver = new RemoteWebDriver(new URL(Node), cap);
 
 		driver.navigate().to(URL);
 		Thread.sleep(5000);
 		driver.quit();
 	}		
}

We will get your selenium grid up and ready in few minutes. Stay focused.

What is DigitalOcean

This is a cloud computing services. It gives you the ability to create and run your own Linux machine in few seconds. We will use it to create our Ubuntu machine that will host our selenium grid. Register an account using the following link to get free $10 credit 😉

https://m.do.co/c/03740affdae2

What is Docker, and Why It's Important

This really needs a very long answer 😄 But if you don't already know it, consider it as a tool to build virtual machines for you in a very easy and fast way. One Linux machine may have several Docker containers (VMs) up and running on it, each on its own ports.

At least this is what you need to know for this tutorial. We will use it to create linux virtual machines that host our selenium grid nodes. I do highly recommend you read more about it on their official site: https://www.docker.com/what-docker

Here comes the hot part, follow these steps and pray for it to work (kidding).

#1: Create a DigitalOcean account, use the following link to get $10 USD free credit

https://m.do.co/c/03740affdae2

#2: Create a Droplet (Linux Machine) with Docker installed.

  • Click "Create Droplet".

  • Choose One-Click apps.

  • Select Docker from the list.
    01.png

  • Choose size as per screenshot below.
    02.png

  • Keep everything else as is and hit "Create".

#3: Access your new Machine using your Terminal.

  • You will get an email with the temporary password to access your droplet using ssh.
  • Open your terminal and type the following command (Mac or Linux)
ssh root@IP_Address_of_Your_Droplet_here
  • You will be asked to change the password. Do it and stay on terminal to install Rancher.

#4: Install Rancher.

  • Type the following command to install Rancher server and run it inside a container using Docker.
docker run -d -p 8080:8080 rancher/server

Wait for 5 minutes after successful installtion, and then you will be able to access your Rancher server using your droplet IP using http://Droplet_IP:8080

03.png

#5: Observe your DigitalOcean API Key

  • Go back to your DigitalOcean account and hit on API.
  • Generate a new API token and save it for the next step.
    04.png

#6: Create a Host using DigitalOcean API Key.

  • Now on your Rancher server top menu, click on "Infrastructure" and choose "Hosts"

  • Click on "Add Host"

  • Leave the default URL as it is and click Save

  • Choose DigitalOcean, paste your API token, and then click "Next".
    05.png

  • Type a name for your new host, and create.
    06.png

#7: Create a Stack.

  • Once our new host is ready, let's create our stack of services (Selenium Grid) and deploy it to the new host.
  • From top menu, click on "Stacks", choose "user" and then click on "Add Stack"
  • Give the stack a name and description and hit create.
    07.png

#8: Add your Selenium Hub & its Nodes as Services to the Stack.

  • Once your stack created, click on "Add Service"

  • First service is the selenium Hub and expose tcp port 4444 for it. Fill in exactly as shown below, then click "Create".
    08.png

  • Now we want to add 2 more services, one for Chrome node and one for Firefox node and these must be linked to the hub. Do as the following screenshot for Chrome node:
    09.png

Let's do the same but for Firefox node:
10.png

  • Our Stack now contains 3 services: Selenium Hub, Chrome Node, Firefox Node. This stack is what we call the Selenium Grid.
    11.png

#9: Access you Hub Console.

  • Go to your host IP but using port 4444
    http://ip_address:4444/grid/console

12.png

  • Now you have the grid up and running. You want more nodes? Check next step

#10: Scale Up Nodes.

  • Let's scale our nodes to have 5 Firefox and 5 Chrome.

  • Go back to your Stack, select the selenium grid stack.

  • Click on the 3 dots next to your Chrome node and hit "Edit"
    13.png

  • Popup will appear, change the number to be 5 and hit save.
    14.png

  • Do the same for the Firefox node.

  • Now access your selenium grid console to see the 10 nodes linked and ready to receive your tests.

15.png

#11: Export your Configs for later use.

  • To view your Rancher & Docker config go to Stack and hit view config or export the config for later use.
    16.png

17.png

Summary

We have learnt how to use the cloud computing service DigitalOcean to create a remote Linux machines. We also created a Rancher server using Docker in Linux. Then we used Rancher to create other remote machines on DigitalOcean and to deploy dockerized selenium hub and nodes in order to create our Selenium Grid on Cloud.

I will leave it to you to figure out how to connect more machines of different platforms to your grid such as Safari on Mac, or Chrome on Windows.

Good luck 😃

Discover and read more posts from Talal Ibdah
get started