Sunday, July 3, 2016

Working with Selenium Grid

What is Grid:

Selenium Grid is a part of the Selenium Suite that specializes on running multiple tests across different browsers, operating systems, and machines in parallel.Selenium Grid has 2 versions - the older Grid 1 and the newer Grid 2

Selenium Grid allows the Selenium-RC/Web Driver solution to scale for test suites to be run in multiple environments.

Hub and Node Concept in Grid:

Selenium basically Provides the multiple instances of Selenium Web driver which are running on different OS with different browsers.

We have a common system which is known as a hub where Grid is running and all the Nodes are registered with this Hub.

Nodes which are basically other machine with same or different OS,Browsers. it means a machine with one OS and Browser is known as Node.

Whenever test is started they are basic send to the main HUB and hub check the capabilities that is what machine browser configuration and OS Requested. if that kind of machine or node is registered with hub then execution will start on that machine.




Lets do some exercise to create the Hub and Node:

1. We have to Create the First Hub for Grid
    Download the Server file first from the Location http://www.seleniumhq.org/download/

After downloading this save this file in a particular location , i have saved it in my E Folder.













Now open the Command Prompt and reach to that folder where this Server Jar file is placed 













and then run the below command (Note by default Hub will take 4444 Port)
java -jar selenium-server-standalone-2.53.1.jar -role hub



















Now check that Hub is configured or Not. Open the Browser and call the below URL: http://localhost:4444/grid/console ,Hub is always installed on local host at port 4444. we can change this port also if its not free.





















Click on View Config



















2. Now Next Step would be Registering the Nodes with that HUB.

Register the Node with Grid Hub with the use of below command :
java -jar selenium-server-standalone-2.4.0.jar -role node -hub http://localhost:4444/grid/register

Open the new window of command prompt and reach to that folder where server file is placed and then run the above command to register the nodes


















Now check whether node is register or not. again open the same URL in to the browser "http://localhost:4444/grid/console and you will get the below screen















In the above screen shot maximum 5 nodes has been created and execution could be done on 5 browsers parallel.

3. Configure Nodes-
Now Configure your browser with the Hub node for that you have to execute the below command and at code level you have to set the Capabilities.
Here i am going to configure FireFox Browser with Node.

Open another command window and run the below code

java -jar selenium-server-standalone-2.53.1.jar -role node -hub http://localhost:4444/grid/register -browser browserName=firefox -port5566

Sometime you will get an error while running the above command and the error would be "Failed to Start SocketListener Port 5566".

To resolve this just change the Port Number and after running you will get the below screen

































To check that Browser is configure properly or not open the given URL in Browser http://localhost:4444/grid/console and you will see below screen.












Now open the eclipse and set the capabilities for firefox browser and test application is open on Grid or not.

import java.net.URL;

import org.openqa.selenium.Platform;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.remote.DesiredCapabilities;
import org.openqa.selenium.remote.RemoteWebDriver;
import java.net.MalformedURLException;


public class Grids {

 static WebDriver driver;
 static String baseUrl = "http://google.com";
 /**
  * @param args
  * @throws MalformedURLException 
  */
 public static void main(String[] args) throws MalformedURLException {
  // TODO Auto-generated method stub
  
  DesiredCapabilities capability = DesiredCapabilities.firefox();
  capability.setBrowserName("firefox");
  capability.setPlatform(Platform.VISTA);
  driver = new RemoteWebDriver(new URL("http://localhost:4444/wd/hub"),capability);
  driver.get(baseUrl);

 }

Execute the above code and you will see that Firefox browser will launch with google.com
Output:

















No comments:

Post a Comment