Starting with Selenium GRID

Want to take best advantage of Selenium, GRID is the answer. It acts as a load balancer, but for tests. It’s primary job is to distribute the tests to different nodes (machines) registered to the server which has the hub. Let’s get started with the same.

Software Requirements:

  1. Latest JDK and JRE
  2. Latest Selenium Server

Setting up the GRID environment:

Set up of the GRID environment is a two step process.
Step 1: Set up the Hub which acts as the Server
Step 2: Set up the Node which acts as the Client
The test is initiated at the Hub and Nodes run the test and send the results back to Hub (server).
Advantages of using GRID: Distributed tests mean saving time. Proper usage ensures 80% reduction in execution time.

Set up Hub:

Assuming that the Java environment is set and we have the Selenium server jar file in a folder, below are the steps to perform to start the hub.

Navigate to the folder which contains the selenium server in command prompt or terminal depending on the OS you use.

Issue the command “java -jar selenium-server-standalone-2.25.0.jar -role hub“. This command starts the selenium server as a GRID hub server. The role defines how the server is to started. Once the server is started, it looks like the following image.

Grid started and running

Once the server is running you can verify the state by navigating to http://localhost:4444 in a browser. In that case the screen will like as below.

GRID verification browser view

Note: If you have a firewall installed, add an exception for the port 4444 in firewall for http connections.

Set up node:

Every node should have java installed. It should also have the same selenium server jar file. Assuming we have the same, the command to register a machine as a node to already running hub is as below.

Navigate to the folder where the selenium server jar file is present and issue the following command.

java -jar selenium-server-standalone-2.25.0.jar -role node -hub http://ip-address-of-hub:4444/grid/register

Make sure you enter the ip address and do not copy-paste the url given here blindly. If you wish to register the same machine where node is running, instead of ip-address, localhost can be used.

Once the hub is running the command prompt looks something like the image below.

Node running state

Now to verify if a node is active, in the Hub machine open the GRID browser view (show in earlier screensho) and click on console link. It will show you the node detail and the browser it supports. The screen looks like as the below image.

With this the GRID set up is done. Let’s dive into a simple script which we can run through GRID. (The example is on WebDriver)

Sample Script:

import java.util.regex.Pattern;
import java.util.concurrent.TimeUnit;
import org.junit.*;
import static org.junit.Assert.*;
import static org.hamcrest.CoreMatchers.*;
import org.openqa.selenium.*;
import org.openqa.selenium.remote.DesiredCapabilities;
import org.openqa.selenium.remote.RemoteWebDriver;
import org.openqa.selenium.support.ui.Select;

public class SampleTest {
private WebDriver driver;
private String baseUrl;
private StringBuffer verificationErrors = new StringBuffer();
@Before
public void setUp() throws Exception {
DesiredCapabilities capability = DesiredCapabilities.internetExplorer();
driver = new RemoteWebDriver(capability);
baseUrl = "https://www.google.co.in/";
driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
}

@Test
public void testUntitled() throws Exception {
driver.get(baseUrl + "/");
driver.findElement(By.id("gbqfq")).clear();
driver.findElement(By.id("gbqfq")).sendKeys("cheese");
driver.findElement(By.id("gbqfb")).click();
}

@After
public void tearDown() throws Exception {
driver.quit();
String verificationErrorString = verificationErrors.toString();
if (!"".equals(verificationErrorString)) {
fail(verificationErrorString);
}
}

private boolean isElementPresent(By by) {
try {
driver.findElement(by);
return true;
} catch (NoSuchElementException e) {
return false;
}
}
}

If you see the script carefully, there is a bit of modification. Instead of directly invoking FirefoxDriver, we are using DesiredCapabilities.firefox() and instantiating the driver object through new RemoteWebDriver() which tell the script executor that this script is to be executed through selenium server. The server then allocates a node to the script where the script gets executed. Now explore GRID and happy testing. 🙂