selenium grid

A brief Selenium Grid
Selenium Grid is part of the Selenium project. It lets you distribute test execution across several machines. You can connect to it with Selenium Remote by specifying the browser, browser version, and operating system you want. You specify these values through Selenium Remote's Capabilities.

There are two main elements to Selenium Grid -- a hub, and nodes. First you need to stand up a hub. Then you can connect (or "register") nodes to that hub. Nodes are where your tests will run, and the hub is responsible for making sure your tests end up on the right one (e.g., the machine with the operating system and browser you specified in your test).

Part 1: Grid Setup
Selenium Grid comes built into the Selenium Standalone Server. So to get started we'll need to download the latest version of it from (Seleniumhq:http://www.seleniumhq.org/download/)
Then we need to start the hub.
1)      You want start HUB we should use the Command Prompt
2)      Navigate where jar get download (Ex: if your download in D drive navigate to D Drive)
D:> java -jar selenium-server-standalone-2.47.1.jar -role hub
The hub successfully launched and your command prompt look like this




By default hub will take 4444 port, if you want to change the port you should use
 –port port number
Ex: :> java -jar selenium-server-standalone-2.47.1.jar -role hub –port 1234

Status can be checked by using the web interface:

Screen is look like this



Go to the other machine where you want to register Nodes. Open the command prompt and run the below line.

D:> java -jar selenium-server-standalone-2.47.1.jar -role webdriver –hub http://192.168.0.3:4444/grid/register
here IP Address is where your Hub is running
by default node will take 5555 port , if you want to change you provide port number
D:> java -jar selenium-server-standalone-2.47.1.jar -role webdriver –hub http://192.168.0.3:4444/grid/register –port 4321
Now that the grid is running we can view the available browsers by visiting our Grid's console at http://192.168.0.3:4444/grid/console.





This indicates that by default you can use 5 Chrome, 5 Firefox and 1 IE browser.
For Instance if you want to use only IE you can start the node by using below command:
D:> java -jar selenium-server-standalone-2.47.1.jar -role webdriver –hub http://192.168.0.3:4444/grid/register-browser browserName=iexplore
Verify the browser Type along with other details in GRID Console by clicking on view config.



Simple Grid Code
Package com.gtn.grid;
importorg.openqa.selenium.*;
Import org.openqa.selenium.remote.DesiredCapabilities;
importjava.net.MalformedURLException;
import java.net.URL;
importorg.openqa.selenium.remote.RemoteWebDriver;
importorg.testng.Assert;
importorg.testng.annotations.*;
importjava.net.MalformedURLException;
import java.net.URL;
importorg.openqa.selenium.WebDriver;
importorg.openqa.selenium.remote.DesiredCapabilities;
importorg.openqa.selenium.remote.RemoteWebDriver;
importorg.testng.Assert;
importorg.testng.annotations.AfterMethod;
importorg.testng.annotations.BeforeMethod;
importorg.testng.annotations.Test;

public class gridexample {
                WebDriver driver;
                String baseUrl,nodeUrl;
                @BeforeTest
                public void setup() throws MalformedURLException {
                                URL nodeUrl=new URL("http://192.168.0.3:1234/wd/hub");
                                DesiredCapabilities cap=new DesiredCapabilities();
                                cap.setBrowserName("firefox");
                                //cap.setPlatform(Platform.VISTA);
                                //WebDriver driver=new RemoteWebDriver(nodeUrl,cap);       
                                driver=new RemoteWebDriver(nodeUrl,cap);
                }
               
                @AfterTest
                public void afterTest(){
                                driver.quit();
                }
               
                @Test
                public void simpleTest(){
                                baseUrl="http://google.com";
                                driver.get(baseUrl);
                                //Assert.assertEquals("Login",driver.getTitle());               
                               
                }
}





For Instance you need to run this test serially in multiple browsers you have to configure your testng.xml .Below is the testng.xml suite for above test to run your test serially.
<?xml version="1.0" encoding="UTF-8"?>
<suite name="GRID SAMPLE TEST"  thread-count="3">
<test name="GRID TEST WITH SERIAL EXECTION WITH BROWSER FF">
<parameter name ="browserType" value="firefox"/>
<classes>
<class name ="GridExample"/>
</classes>
</test>
<test name="GRID TEST WITH SERIAL EXECTION WITH BROWSER IE">
<parameter name ="browserType" value="IE"/>
<classes>
<class name ="GridExample"/>
</classes>
</test>
</suite>

To run the test parallel, you have to change your testng.xml like below.
<?xml version="1.0" encoding="UTF-8"?>
<suite name="GRID SAMPLE TEST" thread-count="4"parllel="tests" >
<test name="GRID TEST WITH SERIAL EXECTION WITH BROWSER FF">
<parameter name ="browserType" value="firefox"/>
<classes>
<class name ="GridExample"/>
</classes>
</test>
<test name="GRID TEST WITH SERIAL EXECTION WITH BROWSER IE">
<parameter name ="browserType" value="IE"/>
<classes>
<class name ="GridExample"/>
</classes>
</test>

</suite>

Comments