Tuesday 14 May 2013

Execute WebDriver script paralally on multiple browsers using TestNG anntations

Download the IE x86/x64 jar files from https://code.google.com/p/selenium/downloads/list
Give the jar file path in  System.setProperty("webdriver.ie.driver", "C:\\Jarfiles\\IEDriverServer.exe");
Download the Chrome driver from https://code.google.com/p/chromedriver/downloads/list
Give the jar file path in System.setProperty("webdriver.chrome.driver", "C:\\Jarfiles\\chromedriver_win_26.0.1383.0\\chromedriver.exe");

Write the script in the following manner


import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.ie.InternetExplorerDriver;
import org.openqa.selenium.remote.DesiredCapabilities;
import org.testng.annotations.AfterMethod;
import org.testng.annotations.Parameters;
import org.testng.annotations.Test;
//import org.testng.annotations.AfterTest;
import org.testng.annotations.BeforeMethod;

public class Browser {

static WebDriver driver;
@BeforeMethod

@Parameters({"browser"})

public void openBroswer(String browser){

if (browser.equalsIgnoreCase("FF"))
{
System.out.println("Firefox webdriver would be used");
    driver = new FirefoxDriver();
}
else if(browser.equalsIgnoreCase("IE"))
{
System.out.println("IE webdriver would be used");
DesiredCapabilities capabilities = DesiredCapabilities.internetExplorer();
capabilities.setCapability(InternetExplorerDriver.INTRODUCE_FLAKINESS_BY_IGNORING_SECURITY_DOMAINS, true);
System.setProperty("webdriver.ie.driver", "C:\\Jarfiles\\IEDriverServer.exe");
    driver = new InternetExplorerDriver();
}
else
{
System.setProperty("webdriver.chrome.driver", "C:\\Jarfiles\\chromedriver_win_26.0.1383.0\\chromedriver.exe");
driver = new ChromeDriver();
}
}

@AfterMethod

public void closeBrowser()
{try{
driver.wait(15000);
}
catch(Exception e){}
driver.close();
driver.quit();
}

@Test

public void test() throws Exception{
driver.get("http://gmail.com");
driver.findElement(By.xpath(".//*[@id='Email']")).sendKeys("administrator");
driver.findElement(By.xpath(".//*[@id='Passwd']")).sendKeys("desk@123");
driver.findElement(By.xpath(".//*[@id='signIn']")).click();
}
}



Write the XML file as:


<suite name="DDTNG" verbose="1" parallel="tests">

<test name="Generic test" >
<parameter name="browser" value="FF"></parameter>

<classes>
<class name="NG.Browser" />
</classes>
</test>

<test name="Generic test_ie" >
<parameter name="browser" value="IE"></parameter>
<classes>
<class name="NG.Browser" />
</classes>
</test>
</suite>


If u want to run the scripts on multiple browsers in sequential way ->you can use "linear" in place of "parallel" in xml file


Note:You can run the scripts in IE without jar files also.


1 comment:

  1. Hi Kalyan,
    Thanks for posting all the information regarding parallel execution of web driver script.It's very easy to understand.

    ReplyDelete