How to use Selenium Grid


Selenium Grid helped me a lot in Compatibility check (Multiple Browsers & Multiple Platforms).This is grid helps us to execute multiple instances of WebDriver in parallel which uses the same code base (code which is present in only one system), hence the code need NOT be present on the other system they execute. The selenium-server-standalone includes Hub, WebDriver, and Selenium RC to execute the scripts in grid.

Selenium Grid has a Hub and a Node. Each hub may have many nodes.

Hub – The hub can also be understood as a server which acts as the central point where the tests would be triggered. A Selenium Grid has only one Hub and it is launched on a single machine once.
How to start Hub : java -jar selenium-server-standalone-2.42.2.jar -role hub

Node – Nodes are the Selenium instances that are attached to the Hub which execute the tests. There can be one or more nodes in a grid which can be of any OS and can contain any of the Selenium supported browsers.
How to start Node : java -Dwebdriver.ie.driver=D:\IEDriverServer.exe -jar D:\JAR\selenium-server-standalone-2.42.2.jar -role webdriver -hub http://ipaddressofNode:4444/grid/register -browser browserName=ie,platform=WINDOWS -port 5558

There are few steps to be followed to use Selenium Grid:Here i was using standalone version 2.42.2

Step 1 : 

Open command prompt and navigate to folder where the server is located. Run the server by using below command.

java -jar selenium-server-standalone-2.42.2.jar -role hub

The hub will use the port 4444 by default. This port can be changed by passing the different port number in command prompt provided the port is open and has not been assigned a task.

Now open browser and type : http://localhost:4444/grid/console.You will see Grid console.

Step 2 :

Go to the other machine where you intend to setup Nodes. Open the command prompt and run the below line by navigating to the dir where the standalone jar is present.

java -jar selenium-server-standalone-2.42.2.jar -role node -hub http://192.168.xxxx:4444/grid/register -port 5556

Step 3 : Now go to other node which has IE browser and IE driver, Open command prompt and type the below code.

C:\>java -Dwebdriver.ie.driver=D:\IEDriverServer.exe -jar D:\JAR\selenium-server-standalone-2.42.2.jar -role webdriver -hub http://192.169.xxxx:4444/grid/register -browser browserName=ie,platform=WINDOWS -port 5558

Step 4 : Now go to other node which has chromedriver and chrome browser.Type the below code.

C:\>java -Dwebdriver.chrome.driver=D:\chromedriver.exe -jar D:\JAR\selenium-server-standalone-2.42.2.jar -role webdriver -hub  http://192.168.xxxx:4444/grid/register -browser browserName=chrome,platform=WINDOWS -port 5557

Now its time to write the code to execute your testcases in various nodes.Here i am using TestNG framework.Just create a class and paste the following  code.

import org.openqa.selenium.remote.RemoteWebDriver;
import org.openqa.selenium.remote.DesiredCapabilities;

import org.testng.annotations.AfterTest;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.Parameters;
import org.testng.annotations.Test;

import java.net.URL;
import java.util.concurrent.TimeUnit;
import java.net.MalformedURLException;

public class SeleniumGrid
{
public WebDriver driver;
public String URL, Node;
protected ThreadLocal<RemoteWebDriver> threadDriver = null;

@Parameters(“browser”)
@BeforeTest
public void launchapp(String browser) throws MalformedURLException
{
String URL = “http://www.xyz.com&#8221;;

if (browser.equalsIgnoreCase(“firefox”))
{
System.out.println(” Executing on FireFox”);
String Node = “http://192.168.xxxx/wd/hub&#8221;;
DesiredCapabilities cap = DesiredCapabilities.firefox();
cap.setBrowserName(“firefox”);

driver = new RemoteWebDriver(new URL(Node), cap);
// Puts an Implicit wait, Will wait for 10 seconds before throwing exception
driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);

// Launch website
driver.navigate().to(URL);
driver.manage().window().maximize();
}
else if (browser.equalsIgnoreCase(“chrome”))
{
System.out.println(” Executing on CHROME”);
DesiredCapabilities cap = DesiredCapabilities.chrome();
cap.setBrowserName(“chrome”);
// ip address of the node having chrome
String Node = “http://192.168.xxxx:5557/wd/hub&#8221;;
driver = new RemoteWebDriver(new URL(Node), cap);
driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);

// Launch website
driver.navigate().to(URL);
driver.manage().window().maximize();
}
else if (browser.equalsIgnoreCase(“ie”))
{
System.out.println(” Executing on IE”);
DesiredCapabilities cap = DesiredCapabilities.chrome();
cap.setBrowserName(“ie”);
//ip address of node having ie browser
String Node = “http://192.168.xxxx:5558/wd/hub&#8221;;
driver = new RemoteWebDriver(new URL(Node), cap);
driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);

// Launch website
driver.navigate().to(URL);
driver.manage().window().maximize();
}
else
{
throw new IllegalArgumentException(“The Browser Type is Undefined”);
}
}

@Test
public void testGrid()
{
System.out.println(” This is the test method “);

}

}

Now create a xml file and paste the following code

<suite name=”Suite” parallel=”tests”>

<test name=”FirefoxTest”>
<parameter name=”browser” value=”firefox” />
<classes>
<class name=”SeleniumGrid” />
</classes>
</test>

<test name=”ChromeTest”>
<parameter name=”browser” value=”chrome” />
<classes>
<class name=”SeleniumGrid” />
</classes>
</test>

<test name=”IETest”>
<parameter name=”browser” value=”ie” />
<classes>
<class name=”SeleniumGrid” />
</classes>
</test>

</suite>

Now just run this xml file.You will see the testcases executed in the nodes as well.

Conclusion :

  • Selenium Grid is used to run multiple tests simultaneously in different browsers and platforms.
  • To run test scripts on the Grid, you should use the DesiredCapabilities and the RemoteWebDriver objects.

 

Advertisements

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