1) To execute tests on different browsers, to test the cross browser compatibility of the application.
2) To cut down the time taken for executing tests because the whole test can be divided in to small segments and executed parallel.
How to use Selenium Grid:
Prerequisites:
1) Download the Selenium Standalone Server Jar file, click here.
2) Eclipse IDE, JDK any version, TestNG plugin installed
Procedure:
1) Make few batch files to create a hub, nodes.
2) Double click on the batch file to create hub
3) Run the batch file for node from a different machine, provided the browser version is matching
4) Once the hub and node are created, you may execute the tests.
Here are the links of batch file for creating hub and node:
- For batch file to create hub, click here
- For Firefox node, click here
- Internet explorer node, click here
Code for running the test:
public class WebDriverGrid2Test {
public WebDriver driver;
Customerfunctions cf;
@Parameters({"browser", "version", "os","loginname","password"})
@BeforeClass
public void setup(String browser, String version, String os,String username, String password) 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("url");
cf= new Customerfunctions(driver);
cf.setlogin(username,password);
try {
cf.setup();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
@Test(priority='1')
public void testlogin() throws InterruptedException{
Thread.sleep(3000);
cf.login();
}
@Test(priority='2')
public void menunavigate(){
cf.menuselection();
}
@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.internetexplorer.driver", "lib\\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;
}
}
Format of testng.xml for running Selenium node.
<?xml version="1.0" encoding="UTF-8"?>
<suite name="Test case run on Two environment" parallel="tests">
<test name="Run on Internet Explorer">
<parameter name="browser" value="iexplore"/>
<parameter name="version" value="8"/>
<parameter name="os" value="WINDOWS"/>
<parameter name="loginname" value="loginname"/>
<parameter name="password" value="password"/>
<classes>
<class name="maypackage.WebDriverGrid2Test"/>
</classes>
</test>
<test name="Run on Firefox">
<parameter name="browser" value="firefox"/>
<parameter name="version" value="29.0"/>
<parameter name="os" value="WINDOWS"/>
<parameter name="loginname" value="loginname"/>
<parameter name="password" value="password"/>
<classes>
<class name="maypackage.WebDriverGrid2Test"/>
</classes>
</test>
</suite>
0 comments:
Post a Comment