Tuesday 14 May 2013

Data driven testing with WebDriver


Download the jxl jar file from http://www.java2s.com/Code/Jar/j/Downloadjxljar.htm
Add the jxl jar file into project

import java.io.FileInputStream;
import jxl.Sheet;
import jxl.Workbook;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.testng.annotations.AfterMethod;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.Test;


public class dd{

WebDriver driver;
@BeforeMethod
public void setUp()
{
driver = new FirefoxDriver();
}
@Test
public void searchGoogle() throws Exception
{
FileInputStream fi = new FileInputStream("C:\\poi-3.9\\excel.xls");
Workbook w = Workbook.getWorkbook(fi);
Sheet s = w.getSheet(0);
for(int row=1; row <=s.getRows();row++)
{
String username = s.getCell(0, row).getContents();
System.out.println("Username "+username);
driver.get("http://fb.com");
driver.findElement(By.xpath(".//*[@id='email']")).sendKeys(username);
String password= s.getCell(1, row).getContents();
System.out.println("Password "+password);
driver.findElement(By.xpath(".//*[@id='pass']")).sendKeys(password);
driver.findElement(By.xpath(".//*[@id='u_0_b']")).click();
}
}
@AfterMethod
public void tearDown()
{
driver.close();
driver.quit();
}
}


2 comments:

  1. Hi Kalyan,

    Simple example easy to understand Data Driven Framework,Thanks.

    ReplyDelete