Friday, September 2, 2011

Selenium Grid with Webdriver

Recently I gone through the webdriver and then I thought to configure it with Selenium grid. On selenium grid site they have mention how to configure it but it's not in details so i though to write that on this blog.
To configure webdriver with Selenium grid you require grid2.

Prerequisites.
  • TestNG
  • Grid2
    Download the selenium-server-standalone-version.jar from the below location.
    Download Grid2

We will see two things in Selenium Grid.
  1. Run Testcase in parallel
  2. Run same testcase on different browser in parallel for browser compatibility testing.

Run Testcases in parallel.

In this example we will run testcases of GridWithWebdriver and GridWithWebdriver1 class on Internet Explorer.

GridWithWebdriver.java
/**
 * @author Gaurang Shah
 * To Demonstrate how to configure webdirver with Selenium Grid
 */
import java.net.MalformedURLException;
import java.net.URL;

import org.junit.AfterClass;
import org.openqa.selenium.*;
import org.openqa.selenium.remote.DesiredCapabilities;
import org.openqa.selenium.remote.RemoteWebDriver;
import org.testng.annotations.*;

public class GridWithWebdriver {
 
 public WebDriver driver;
 
 @Parameters({"browser"})
 @BeforeClass
 public void setup(String browser) throws MalformedURLException {
  DesiredCapabilities capability=null;
   
  if(browser.equalsIgnoreCase("firefox")){
   System.out.println("firefox");
   capability= DesiredCapabilities.firefox();
   capability.setBrowserName("firefox"); 
   capability.setPlatform(org.openqa.selenium.Platform.ANY);
   //capability.setVersion("");
  }
 
  if(browser.equalsIgnoreCase("iexplore")){
   System.out.println("iexplore");
   capability= DesiredCapabilities.internetExplorer();
   capability.setBrowserName("iexplore"); 
   capability.setPlatform(org.openqa.selenium.Platform.WINDOWS);
   //capability.setVersion("");
  }
  
  driver = new RemoteWebDriver(new URL("http://localhost:4444/wd/hub"), capability);
  driver.navigate().to("http://google.com");
  
 }
 
 @Test
 public void test_first() throws InterruptedException{
  Thread.sleep(3000);
  WebElement search_editbox = driver.findElement(By.name("q"));
  WebElement search_button = driver.findElement(By.name("btnG"));
  search_editbox.clear();
  search_editbox.sendKeys("first");
  search_button.click();
 }
 
 @Test
 public void test_second(){
  WebElement search_editbox = driver.findElement(By.name("q"));
  WebElement search_button = driver.findElement(By.name("btnG"));
  search_editbox.clear();
  search_editbox.sendKeys("second");
  search_button.click();
 }
 
 @AfterClass
 public void tearDown(){
        driver.quit();
 }
}
GridWithWebdriver1.java
/**
 * @author Gaurang Shah
 * To Demonstrate how to configure webdirver with Selenium Grid
 */
import java.net.MalformedURLException;
import java.net.URL;

import org.junit.AfterClass;
import org.openqa.selenium.*;
import org.openqa.selenium.remote.DesiredCapabilities;
import org.openqa.selenium.remote.RemoteWebDriver;
import org.testng.annotations.*;

public class GridWithWebdriver1 {
 
 public WebDriver driver;
 
 @Parameters({"browser"})
 @BeforeClass
 public void setup(String browser) throws MalformedURLException, InterruptedException {
  DesiredCapabilities capability=null;
   
  if(browser.equalsIgnoreCase("firefox")){
   System.out.println("firefox");
   capability= DesiredCapabilities.firefox();
   capability.setBrowserName("firefox"); 
   capability.setPlatform(org.openqa.selenium.Platform.ANY);
   //capability.setVersion("");
  }
 
  if(browser.equalsIgnoreCase("iexplore")){
   System.out.println("iexplore");
   capability= DesiredCapabilities.internetExplorer();
   capability.setBrowserName("iexplore"); 
   capability.setPlatform(org.openqa.selenium.Platform.WINDOWS);
   //capability.setVersion("");
  }
  
  driver = new RemoteWebDriver(new URL("http://localhost:4444/wd/hub"), capability);
  driver.navigate().to("http://google.com");
  
 }
 
 @Test
 public void test_third() throws InterruptedException{
  Thread.sleep(3000);
  WebElement search_editbox = driver.findElement(By.name("q"));
  WebElement search_button = driver.findElement(By.name("btnG"));
  search_editbox.clear();
  search_editbox.sendKeys("third");
  search_button.click();
  
 }
 
 @Test
 public void test_fourth(){
  WebElement search_editbox = driver.findElement(By.name("q"));
  WebElement search_button = driver.findElement(By.name("btnG"));
  search_editbox.clear();
  search_editbox.sendKeys("foruth");
  search_button.click();
  
 }
 
 @AfterClass
 public void tearDown(){
        //Close the browser
     //   driver.quit();

 }
}

Testng.xml
<suite name="Selenium Grid with webdriver" verbose="3"  parallel="classes" thread-count="2">   
  <test name="Selenium Grid Demo">
  <parameter name="browser" value="iexplore"/>
    <classes>
      <class name="tests.GridWithWebdriver"/>
   <class name="tests.GridWithWebdriver1"/>
    </classes>
 </test>
 </suite>

Now in order to run using testng.xml we need to start the Grid hub and two remote control with Iexplore as browser.

Now to start the Grid and remote control open CMD and navigate to folder where Grid2 jar (selenium-server-standalone-x.x.x.jar) file is located and use the following commands

To start the Grid Hub
java -jar selenium-server-standalone-2.5.0.jar -role hub
To start the remote control supporting Internet explore
java -jar selenium-server-standalone-2.5.0.jar -role webdriver -hub http://localhost:4444/grid/register -browser browserName=iexplore,platform=WINDOWS
To start another the remote control supporting Internet explore
java -jar selenium-server-standalone-2.5.0.jar -role webdriver -hub http://localhost:4444/grid/register -browser browserName=iexplore,platform=WINDOWS -port 5556

After this open http://localhost:4444/grid/console in you browser. You should be able to see something like below image.



Now if you will run using testng.xml you will see two Internet explorer will open and one will execute test_first() and test_second() while another will execute test_third() and test_fourth() in parallel.

Run same testcases on different browser in parallel.

In this example we will execute GridWithWebdriver tests on Firefox and Internet Explorer in parallel.

To do so you need to modify you testng.xml as below.
Testng.xml
<suite name="Same TestCases on Different Browser" verbose="3"  parallel="tests" thread-count="2">   
  <test name="Run on Internet Explorer">
 <parameter name="browser"  value="iexplore"/>
    <classes>
      <class name="Tests.GridWithWebdriver"/>
    </classes>
 </test>  

  <test name="Run on Firefox">
 <parameter name="browser"  value="firefox"/>
    <classes>
      <class name="Tests.GridWithWebdriver"/>
    </classes>
 </test>  
 </suite>
Now to run testcases we need to start grid hub and one remote control supporting Firefox and another supporting Internet Explorer.

Now to start the Grid and remote control open CMD and navigate to folder where Grid2 jar (selenium-server-standalone-x.x.x.jar) file is located and use the following commands
To start the Grid Hub
java -jar selenium-server-standalone-2.5.0.jar -role hub
To start the remote control supporting Firefox
java -jar selenium-server-standalone-2.5.0.jar -role webdriver -hub http://localhost:4444/grid/register -browser browserName=firefox,platform=WINDOWS
To start another the remote control supporting Internet explore
java -jar selenium-server-standalone-2.5.0.jar -role webdriver -hub http://localhost:4444/grid/register -browser browserName=iexplore,platform=WINDOWS -port 5556
After this open http://localhost:4444/grid/console in you browser. You should be able to see something like below image.

Now if you will run above test case one Firefox and one IE will launch and both test_first() and test_second() will execute in both browser simultaneously.

30 comments:

qaielearning said...

nice sharing..!

Sneha said...

Great Going Gaurang! Have a question.. Is the selenium-server-standalone jar(RC) different from the selenium-grid-hub Jar? They have separate links for download ...

Chetan Parmar said...

Hi Gauring

my 2 instances for mozilla is working nicely but while i am trying with IE it's give me the following excpetion


org.openqa.selenium.ElementNotVisibleException: You may only interact with visible elements
Build info: version: '2.8.0', revision: '14056', time: '2011-10-06 12:41:26'
System info: os.name: 'Windows XP', os.arch: 'x86', os.version: '5.1', java.version: '1.6.0_21'
Driver info: driver.version: unknown; duration or timeout: 422 milliseconds
Build info: version: '2.8.0', revision: '14056', time: '2011-10-06 12:41:26'
System info: os.name: 'Windows XP', os.arch: 'x86', os.version: '5.1', java.version: '1.6.0_21'
Driver info: driver.version: RemoteWebDriver

501 said...

Hi Gaurang,

Is there any possible way to run test cases parallel by using webdriver and junit 4.

Gaurang Shah said...

hey 501,

I guess you can, check out following blogspot, it is for selenium RC but that might help you.
Selenium Grid with JUnit

Varun Kumar Rao Ponugoti said...

Is there any way to capture results Browser wise when we are using selenium grid 2 for parallel testing.

EX:I am running the whole suit on windows Firefox and on Linux Firefox, Is it possible to get the results Browser wise ....

The explanation of the way of doing it also sufficient.

Any help regarding this is appreciated

mathew said...

Hi Gaurang,

What is the advantage of passing the Browser/OS details along with RC node like this "-browser browserName=iexplore,platform=WINDOWS". Won't is work properly if we avoid this Browser/OS details

Gaurang Shah said...

hi madhew,

That's the only way to identify selenium RC.

SKumar said...

Hi Gaurang,

Is there a way to pass Webdriver object from one test to another?
For example: I have two test cases. One does Login to the application and 2nd test case tests the home page after log-in. If I create webdriver object in my Login test, how can I pass it to the second test? I want to create a TestNG suite to run these test cases one after other.

Sam said...

Hi Gaurang,

Can you please tell me how can i handle double click in WebDriver with Grid 2..

Thank you in Advance.

sathya said...

This is a great article. I am pretty much impressed with your good work.You put really very helpful information. Keep it up.
Ecommerce developer

Ravindra Kumaar said...

Hello Gaurang,

Great article. I have similar query as SKumar. I have written lot of TCs using WebDriver. Now I want to run all the tests at once using a Test Suite....i.e., Can I use TestNG with WebDriver ? Please share your valuable ideas.

Thanks
Ravindra

balakrishna said...

Hi Gaurang,

I am beginner of selenium grid. but i following as per examples you mentioned. But i need clarification on testng.xml. I created testng.xml file, but i dont know where to copy it. how to start executing the above examples. Pls help. looking for your reply

Innamuri007 said...

Hi ,

I have been facing the problem to open IE9 with Selenium Grid with webdriver. Grid2 is working fine with Firefox and Chrome. i am using OS is Windows 7 64 Bit.

Grid Hub:-

java -jar selenium-server-standalone-2.5.0.jar -role hub -browser browserName=chrome,platform=WINDOWS -port 555

Internet Explorer:-

java -Dwebdriver.iexplore.driver=C:\Users\Reva\Desktop\SeleniumGrid\InternetExplorerDriver.exe-jar selenium-server-standalone-2.5.0.jar -role webdriver -hub http://localhost:4444/grid/register -browser browserName=iexplorer,platform=WINDOWS -port 5557

java file i used Desired capabilities as below

public static void intselenium(String browser) throws MalformedURLException, InterruptedException
{
DesiredCapabilities capability=null;
if(browser.equalsIgnoreCase("iexplore")){
System.out.println("iexplore");
capability= DesiredCapabilities.internetExplorer();
capability.setBrowserName("iexplore");
capability.setPlatform(org.openqa.selenium.Platform.WINDOWS);
//capability.setVersion("");
}

My XML File configuration is below:-













and it is visible on the console : http://localhost:4444/grid/console

when i run the .XML File using the TestNg. getting the ERROR as below

"org.openqa.selenium.remote.UnreachableBrowserException: Could not start a new session. Possible causes are invalid address of the remote server or browser start-up failure"

if any one know ,how resolve this . plz let me know

siva said...

Hi,

I couldn't able to start multi instances on Grid2.0 on firefox. I followed all your instruction but only one instances start executing from Hub. Please help me on that.

Naveen's Love said...

Hi Gaurang,

Thanks a ton to your post. This really helped me a lot.
We are working on a framework that takes input from an excel file. I would really appreciate if you can provide your valuable suggestion to do the same by passing parameters from excel sheet? Is it possible?

Anna said...

Thank You very much for the information! it's amazing title about Grid . its help me to understand what i may do and how
thanks!
Anna

Deepak Jangid said...

Hello gaurang,

I am looking for some help through which i can wrote my test scripts which do the test and i can export the result to excel in for of fail pass.

I want to use selenium python only.

please help.

Deepak

JK said...

this is very very helpful. i just run through parallel test executing using webdriver grid. thnx much !!!

pietro said...

Hi Guarang,

is there any way how to store or way how to get results into a one file, e.g. output.xml or it has other type ? Or does it produce any results ?

Thanks.

sivaprasad kandula said...

Hi Guarang,

does selenium works for latest browsers ?

Thanks,

vtr said...

Thanks, nice post!

Lalit said...

Hi Gaurang,

Is there any way we can run selenium grid scripts at some time interval automatically(without human interference) in multiple systems.

Alagar Samy said...

i have one doubt ................when we connect multiple system
for the concurrent test
if any one close the connection i am getting error and
the next connection also getting not connected
i am using selenium webdriver through the grid

pls help me

Nishu Verma said...

script gives an error org.testng.TestNGException:
Parameter 'browser' is required by @Configuration on method setup but has not been marked @Optional or defined

karthik holla said...

Hi,

Can you please share info on how to connect android emulator as node to hub.

Thanks in advance

Akanksha Agrawal said...

Hi gaurand have u ever implemented the above with Csharp.
I have been recently moved from Java to CSharp whereas not able to find the TestNG equivalent tool in dotnet.
I want to implement grid with Csharp

Anite Sridhar said...

Hi Gaurang,

I am using Cucumber -JVM with JUnit and Selenium Grid, testing parallel excecutions using more than one JUnit runner class and getting cucumber json output files. I want to have a method which will get executed after all the JSON files are created as I am doing post processing by concatenating all the json files.

Please suggest any solution.

Thanks in advance,
Anite S

Anonymous said...

thanks for the nice post

Anonymous said...

nice post

Post a Comment