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. πŸ™‚

Advertisements

3 comments

  1. Hi Sudeep,

    Tried with the above written codes. I use TestNg Framework instead of Junit.. Do i need to do any modification to run the code in multiple browsers.
    I am getting the below error :

    [TestNG] Running:
    E:\Documents and Settings\Anil\Local Settings\Temp\testng-eclipse-1431710759\testng-customsuite.xml

    FAILED CONFIGURATION: @BeforeTest StartServer
    java.lang.IllegalArgumentException: You must specify a remote address to connect to
    at org.openqa.selenium.remote.HttpCommandExecutor.(HttpCommandExecutor.java:76)
    at org.openqa.selenium.remote.RemoteWebDriver.(RemoteWebDriver.java:69)
    at org.openqa.selenium.remote.RemoteWebDriver.(RemoteWebDriver.java:65)
    at Selenium2Example.StartServer(Selenium2Example.java:22)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
    at java.lang.reflect.Method.invoke(Unknown Source)
    at org.testng.internal.MethodInvocationHelper.invokeMethod(MethodInvocationHelper.java:80)
    at org.testng.internal.Invoker.invokeConfigurationMethod(Invoker.java:551)
    at org.testng.internal.Invoker.invokeConfigurations(Invoker.java:213)
    at org.testng.internal.Invoker.invokeConfigurations(Invoker.java:138)
    at org.testng.TestRunner.beforeRun(TestRunner.java:641)
    at org.testng.TestRunner.run(TestRunner.java:609)
    at org.testng.SuiteRunner.runTest(SuiteRunner.java:334)
    at org.testng.SuiteRunner.runSequentially(SuiteRunner.java:329)
    at org.testng.SuiteRunner.privateRun(SuiteRunner.java:291)
    at org.testng.SuiteRunner.run(SuiteRunner.java:240)
    at org.testng.SuiteRunnerWorker.runSuite(SuiteRunnerWorker.java:53)
    at org.testng.SuiteRunnerWorker.run(SuiteRunnerWorker.java:87)
    at org.testng.TestNG.runSuitesSequentially(TestNG.java:1185)
    at org.testng.TestNG.runSuitesLocally(TestNG.java:1110)
    at org.testng.TestNG.run(TestNG.java:1022)
    at org.testng.remote.RemoteTestNG.run(RemoteTestNG.java:109)
    at org.testng.remote.RemoteTestNG.initAndRun(RemoteTestNG.java:202)
    at org.testng.remote.RemoteTestNG.main(RemoteTestNG.java:173)

    SKIPPED: googleSelenium

    ===============================================
    Default test
    Tests run: 1, Failures: 0, Skips: 1
    Configuration Failures: 1, Skips: 0
    ===============================================

    ===============================================
    Default suite
    Total tests run: 1, Failures: 0, Skips: 1
    Configuration Failures: 1, Skips: 0
    ===============================================

    [TestNG] Time taken by org.testng.reporters.JUnitReportReporter@1a679b7: 31 ms
    [TestNG] Time taken by org.testng.reporters.jq.Main@1430b5c: 313 ms
    [TestNG] Time taken by org.testng.reporters.SuiteHTMLReporter@16a9d42: 265 ms
    [TestNG] Time taken by org.testng.reporters.EmailableReporter@e102dc: 16 ms
    [TestNG] Time taken by [TestListenerAdapter] Passed:0 Failed:0 Skipped:0]: 31 ms
    [TestNG] Time taken by org.testng.reporters.XMLReporter@a1807c: 31 ms

    • Hi Anil,

      I guess it’s some configuration issue. Are you sure u have added exception to the ports 4444 and 5555 in the firewall? Also check if you are using RemoteWebDriver() in the code. And also check if the testNG methods for Before, After and Test are imported.


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s