How to run java test scripts using Grid2

Prerequisites. 
  1. Java should be install and setup path in system.
  2. Eclipse should be configured TestNG plugin 
  3. Download the selenium-server-standalone-version.jar from the below location.
  4. TestNG jar file.
Steps to  create and execute test scripts:

1. Launch Grid 2 console(Hub ) and Nodes using below commands:

    Hub:
       java -jar selenium-server-standalone-2.29.0.jar -host localhost -port 4444 -role hub

    Nodes:
       java -jar selenium-server-standalone-2.29.0.jar -host localhost -port 5555 -role webdriver -hub http://localhost:4444/grid/register -browser browserName=iexplore,platform=XP,version=8

        java -jar selenium-server-standalone-2.29.0.jar -host localhost -port 5556 -role webdriver -hub http://localhost:4444/grid/register -browser browserName=firefox,platform=XP,version=13
       
2. Run test case parallel:  This “WebDriverGrid2Test “ example will be run test case on firefox and  internet explorer on version 8 and 13 respectively.

   package com.test;

    import java.net.MalformedURLException;
    import java.net.URL;
    import org.openqa.selenium.By;
    import org.openqa.selenium.WebDriver;
    import org.openqa.selenium.WebElement;
    import org.openqa.selenium.remote.DesiredCapabilities;
    import org.openqa.selenium.remote.RemoteWebDriver;
    import org.testng.annotations.AfterClass;
    import org.testng.annotations.BeforeClass;
    import org.testng.annotations.Parameters;
    import org.testng.annotations.Test;

    public class WebDriverGrid2Test {

public WebDriver driver;
@Parameters({"browser", "version", "os"})
@BeforeClass
public void setup(String browser, String version, String os) throws MalformedURLException,     InterruptedException {
DesiredCapabilities capability=null;
capability = gridStting(browser, version, os);
driver = new RemoteWebDriver(new URL("http://localhost:4444/wd/hub"), capability);
driver.navigate().to("http://google.com");
}
@Test
public void testGoogleSearch2() throws InterruptedException{
Thread.sleep(3000);
WebElement search_input = driver.findElement(By.id("gbqfq"));
search_input.clear();
search_input.sendKeys("Testing");
WebElement search_button = driver.findElement(By.id("gbqfb"));
search_button.click();
}
@Test
public void testGoogleSearch1(){
WebElement search_input = driver.findElement(By.id("gbqfq"));
search_input.clear();
search_input.sendKeys("Testing");
WebElement search_button = driver.findElement(By.id("gbqfb"));
search_button.click();
}
@AfterClass
public void tearDown(){        
driver.quit();
}
public DesiredCapabilities gridStting(String browser, String version, String os){
DesiredCapabilities capability=null;
if(browser.equals("firefox")){
System.out.println("Test scripts running on firefox");
capability= DesiredCapabilities.firefox();
capability.setBrowserName("firefox");
capability.setVersion(version);
}
if(browser.equals("iexplore")){
System.out.println("Test scripts running on iexplore");
capability= DesiredCapabilities.internetExplorer();
capability.setBrowserName("iexplore");
System.setProperty("webdriver.ie.driver", "IEDriverServer.exe");
capability.setVersion(version);
}
if(os.equals("WINDOWS")){
capability.setPlatform(org.openqa.selenium.Platform.WINDOWS);
}
else if(os.equals("XP")){
capability.setPlatform(org.openqa.selenium.Platform.XP);
}
else if(os.equals("Linux")){
capability.setPlatform(org.openqa.selenium.Platform.LINUX);
}
else{
capability.setPlatform(org.openqa.selenium.Platform.ANY);
}
return capability;
}
      }

3. We need to create TestNG file like as below “Suite.xml” file:

 <?xml version="1.0" encoding="UTF-8"?>
 <suite name="Test case run on Two environment" verbose="3"  parallel="tests" thread-count="2">   
      <test name="Run on Internet Explorer">
          <parameter name="browser"  value="iexplore"/>
           <parameter name="version"  value="8"/>
           <parameter name="os"  value="XP"/>
            <classes>
                <class name="com.test.WebDriverGrid2Test"/>
            </classes>
        </test>  
         <test name="Run on Firefox">
              <parameter name="browser"  value="firefox"/>
              <parameter name="version"  value="13"/>
              <parameter name="os"  value=" XP "/>
               <classes>
                  <class name="com.test.WebDriverGrid2Test"/>
               </classes>
          </test>  
 </suite>

4. Rigth click on Suite.xml file,  select “Run As” and click on  TestNG Suite options
5. Now test script is executed on both browser and version combination. execution log on eclipse look like as below:

.

No comments:

Post a Comment

Leave your comments, queries, suggestion I will try to provide solution