Thursday 1 August 2013

Grid with SeleniumWebdriver


import java.io.File;
 import java.net.MalformedURLException;
 import java.net.URL;

import org.junit.AfterClass;
 import org.openqa.selenium.*;
 import org.openqa.selenium.chrome.ChromeDriver;
 import org.openqa.selenium.ie.InternetExplorerDriver;
 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;
 //setting Chrome driver location
 System.setProperty("webdriver.chrome.driver",System.getProperty("user.dir")+"\\lib\\chromedriver.exe");
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("chrome")){
 System.out.println("chrome");
 capability= DesiredCapabilities.chrome();
 capability.setBrowserName("chrome");
capability.setPlatform(org.openqa.selenium.Platform.WINDOWS);
 //capability.setVersion("");
 }

 driver = new RemoteWebDriver(new URL("http://localhost:4444/wd/hub"), capability);
 driver.manage().window().maximize();
 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();
 driver.navigate().to("http://yahoo.com");
 String MYURL=driver.getCurrentUrl();
 System.out.println(MYURL);
 Thread.sleep(3000);
 }

 @Test
 public void test_second() throws InterruptedException{
 driver.navigate().to("http://CNN.com");
 String MYA=driver.getCurrentUrl();
 System.out.println(MYA);
 Thread.sleep(3000);
 driver.navigate().to("http://google.com");
 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.close();
 }
 }
 //-Then started the selenium grid as per the below.

java -jar selenium-server-standalone-2.32.0.jar -role hub
 java -jar selenium-server-standalone-2.32.0.jar -role webdriver -hub http://localhost:4444/grid/register -browser browserName=firefox,platform=WINDOWS
 java -jar selenium-server-standalone-2.32.0.jar -role webdriver -hub http://localhost:4444/grid/register -browser browserName=chrome,platform=WINDOWS -port 5556

//My testng.XML file as per the below.

<suite name="Same TestCases on Different Browser" verbose="3" parallel="tests" thread-count="2">
<test name="Run on Mozilla Firefox">
 <parameter name="browser" value="firefox"/>
 <classes>
 <class name="GridWithWebdriver"/>
 </classes>
 <test name="Run on goolge Chrome">
 <parameter name="browser" value="chrome"/>
 <classes>
 <class name="GridWithWebdriver"/>
 </classes>
 </test>
</test>
</suite>


When i ran the XML file as testng suite below error occurred.Hope any one may able to help.