Module 3 - Selenium WebDriver - Example 2 - Data Driven - Test Class
Jump to navigation
Jump to search
/**
*
*/
package com.nobleprog.selenium.regression.Tests;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Vector;
import java.util.concurrent.TimeUnit;
import jxl.Sheet;
import jxl.Workbook;
import jxl.read.biff.BiffException;
import jxl.write.Label;
import jxl.write.WritableSheet;
import jxl.write.WritableWorkbook;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.openqa.selenium.By;
import org.openqa.selenium.Keys;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxBinary;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.firefox.FirefoxProfile;
/**
* @author vaseemzia
*
*/
public class ExerciseThree {
@SuppressWarnings("rawtypes")
public static Vector paramData=new Vector();
@SuppressWarnings("rawtypes")
public static Vector testMethod=new Vector();
TestData td = new TestData("C:\\Selenium\\readdata.xls");
private WebDriver driver;
@Before
public void setUp() throws Exception {
//Create a binary object by feeding in the location
//of the firefox binary
FirefoxBinary binary = new FirefoxBinary(new File("C:\\Program Files (x86)\\Mozilla Firefox\\firefox.exe"));
//create an empty firefox profile object
FirefoxProfile profile = new FirefoxProfile();
//create a new driver object by passing it the binary and
// the profile object
driver = new FirefoxDriver(binary, profile);
// driver = new FirefoxDriver();
driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
td.getDatafromXL();
paramData=td.getParamData();
testMethod=td.getTestMethods();
}
@Test
public void test() throws Exception {
driver.get((String)paramData.get(0));
driver.findElement(By.id("gbqfq")).clear();
System.out.println("Read Data : "+(String) paramData.get(1));
driver.findElement(By.id("gbqfq")).sendKeys((String) paramData.get(1));
driver.findElement(By.id("gbqfq")).sendKeys(Keys.ENTER);
driver.findElement(By.linkText((String)paramData.get(2))).click();
driver.manage().timeouts().implicitlyWait(60,TimeUnit.SECONDS);
String s = driver.getTitle();
System.out.println("Get Title :" + s);
writereport(s);
}
@After
public void tearDown() throws Exception {
driver.quit();
}
/*
* Write data to excel sheet
*
*/
public void writereport(String text)
{
try
{
FileOutputStream f = new FileOutputStream("C:\\Selenium\\output4.xls",true);
WritableWorkbook book = Workbook.createWorkbook(f);
WritableSheet sheet = book.createSheet("output",0);
Label l = new Label(2, 1, text);
sheet.addCell(l);
book.write();
book.close();
}
catch (Exception e)
{
e.printStackTrace();
}
}
/*
* Read data from excel sheet
*
*
*/
public String[][] getXLData(String location, String sheetname)
{
Workbook w = null;
try {
w = Workbook.getWorkbook(new File(location));
} catch (BiffException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
Sheet s = w.getSheet(sheetname);
String a[][] = new String[10][10];
try
{
for (int j=0;j<s.getColumns();j++)
{
for (int i=0;i<s.getRows();i++)
{
a[j][i] = s.getCell(j, i).getContents();
System.out.println(j+" and "+i+" "+a[j][i]);
}
}
}
catch(Exception e)
{
e.printStackTrace();
}
return a;
}
}