Datadriven testing using selenium webdriver


Datadriven or Parameterization in Selenium Webdriver


package selenium_examples;


import java.io.FileInputStream;
import java.io.IOException;
import jxl.Sheet;
import jxl.Workbook;
import jxl.read.biff.BiffException;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;


public class Sample {
           
                 WebDriver driver = new FirefoxDriver();

public void search() throws BiffException, IOException, InterruptedException
{


driver.get("http://www.google.com");
FileInputStream fi=new FileInputStream("F:\\Sel.xls");
Workbook WB = Workbook.getWorkbook(fi);


Sheet sheet = WB.getSheet("Sheet1");
System.out.println("Number of Rows in Excel Sheet =" +sheet.getRows());

for (int i=0;i {

String s=sheet.getCell(0,i).getContents();
System.out.println(s);
driver.findElement(By.id("gbqfq")).sendKeys(s);
driver.findElement(By.id("gbqfb")).click();
Thread.sleep(3000);
driver.findElement(By.id("gbqfq")).clear();
}
fi.close();
}

public static void main(String[] args) throws BiffException, IOException, InterruptedException
{
Sample s1=new Sample();
s1.search();

}

}

Output:-
Number of Rows in Excel Sheet =4
Selenium
Selenium Ide
Selenium RC
Selenium Webdriver


The above script gets values from excel sheet and uses in google search.


Important things to note in this script is Excel Sheet format should be in .Xls.If we use .xlsx format excel sheet,we get error : "Exception in thread "main" jxl.read.biff.BiffException: Unable to recognize OLE stream".
So save the excel sheet as "Excel 97-2003 Workbook".


 

No comments:

Related Posts Plugin for WordPress, Blogger...

Fun and Knowledge