Created
May 13, 2013 15:19
-
-
Save jimode/5569097 to your computer and use it in GitHub Desktop.
JUnit: HSSF Workbook
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.io.File; | |
import java.io.FileInputStream; | |
import java.io.FileNotFoundException; | |
import java.io.FileOutputStream; | |
import org.apache.poi.hssf.usermodel.HSSFCell; | |
import org.apache.poi.hssf.usermodel.HSSFRow; | |
import org.apache.poi.hssf.usermodel.HSSFSheet; | |
import org.apache.poi.hssf.usermodel.HSSFWorkbook; | |
import org.junit.After; | |
import org.junit.Test; | |
import org.openqa.selenium.By; | |
import org.openqa.selenium.WebDriver; | |
import org.openqa.selenium.firefox.FirefoxDriver; | |
import org.openqa.selenium.ie.InternetExplorerDriver; | |
public class DDF40 { | |
int xRows, xCols; | |
FirefoxDriver driver; //creating an object instance. | |
@Test | |
public void myMainTest() throws Exception { | |
// Local variables to store Test Data | |
String vTDID, vURL, vSearchTerm, vEmail, vPswd, vSearchResults, vWrongEmailMsg, vWelcomeMsg, vExecute; | |
long iWait; | |
String oSearchResults; | |
// Read each set of Test Data from the Excel. | |
String xlPath = "C:/Users/Jimi/Selenium/Workspace/A16_2012/YT_Playing Around.xls"; | |
String[][] myXL = readXL(xlPath, "Test Data"); | |
// See what has been read from the Excel. | |
for (int i = 0; i < xRows; i++) { | |
for (int j = 0; j < xCols; j++) { | |
//System.out.println(myXL[i][j]); | |
} | |
} | |
// Create a driver to run automation | |
// InternetExplorerDriver driver = new InternetExplorerDriver(); | |
driver = new FirefoxDriver(); | |
iWait = 3000; | |
// Take values from each row in 2D local array 'MyXL' and load into local variables. | |
for (int k=1; k<xRows; k++ ) { | |
// Identify the Test Data to execute | |
vExecute = myXL[k][8]; | |
// Only execute if Y | |
if (vExecute.equals("Y")) { | |
vTDID = myXL[k][0]; | |
vURL = myXL[k][1]; | |
vSearchTerm = myXL[k][2]; | |
vEmail = myXL[k][3]; | |
vPswd = myXL[k][4]; | |
vSearchResults = myXL[k][5]; | |
vWrongEmailMsg = myXL[k][6]; | |
vWelcomeMsg = myXL[k][7]; | |
// System.out.println("Row is " + k); | |
// System.out.println("URL is " + vURL); | |
// Execute the Test: TC001 | |
// Go To youtube.com | |
driver.navigate().to(vURL); | |
// Enter a search term from Excel data | |
driver.findElement(By.xpath("//input[@id='masthead-search-term']")).sendKeys(vSearchTerm); | |
// Click the search button or image | |
driver.findElement(By.xpath("//button[@id='search-btn']")).click(); | |
Thread.sleep(iWait); | |
oSearchResults = driver.findElement(By.xpath("//p[@class='num-results']")).getText(); | |
System.out.println("The search numbers are: " + oSearchResults); | |
myXL[k][10] = oSearchResults; | |
// Verify that the results show as expected | |
if(vSearchResults.equals(oSearchResults)) { | |
myXL[k][9] = "Pass"; | |
System.out.println("~~~~PASS~~~~"); | |
} else { | |
myXL[k][9] = "Fail"; | |
System.out.println("~~~~FAIL~~~~"); | |
} | |
} else { | |
System.out.println("Row is " + k + " is skipped"); | |
} | |
} | |
System.out.println("Writing back into Excel"); | |
writeXL ("C:/Users/Jimi/Selenium/Workspace/A16_2012/Result2.xls", "TD Results2", myXL); | |
System.out.println("Wrote back into Excel"); | |
} | |
@After | |
public void myAfter(){ | |
driver.quit(); | |
} | |
// Custom methods are here | |
// ****Method to read XL**** | |
public String[][] readXL(String sPath, String iSheet) throws Exception { | |
String[][] xData; | |
File myxl = new File(sPath); | |
FileInputStream myStream = new FileInputStream(myxl); | |
HSSFWorkbook myWB = new HSSFWorkbook(myStream); | |
HSSFSheet mySheet = myWB.getSheet(iSheet); | |
xRows = mySheet.getLastRowNum()+1; | |
xCols = mySheet.getRow(0).getLastCellNum(); | |
xData = new String[xRows][xCols]; | |
for (int i = 0; i < xRows; i++) { | |
HSSFRow row = mySheet.getRow(i); | |
for (int j = 0; j < xCols; j++) { | |
HSSFCell cell = row.getCell(j); | |
String value = cellToString(cell); | |
xData[i][j] = value; | |
} | |
} | |
return xData; | |
} | |
// ****Method to write to XL**** | |
public void writeXL (String sPath, String iSheet, String[][] xData) throws Exception { | |
File outFile = new File(sPath); | |
HSSFWorkbook wb = new HSSFWorkbook(); // The workbook | |
HSSFSheet osheet = wb.createSheet(iSheet); // The worksheet | |
int xR_TS = xData.length; // The length or number of rows of the 2 dimensional array | |
int xC_TS = xData[0].length; // The length or number of columns of the first row | |
for (int myrow = 0; myrow < xR_TS; myrow++) { // Go through each row | |
HSSFRow row = osheet.createRow(myrow); // Create a new row | |
for (int mycol = 0; mycol < xC_TS; mycol++) { // Go through each column | |
HSSFCell cell = row.createCell(mycol); // Create a new column | |
cell.setCellType(HSSFCell.CELL_TYPE_STRING); | |
cell.setCellValue(xData[myrow][mycol]); | |
} | |
FileOutputStream fOut = new FileOutputStream(outFile); | |
wb.write(fOut); | |
fOut.flush(); | |
fOut.close(); | |
} | |
} | |
// ****Change cell type**** | |
public static String cellToString(HSSFCell cell) { | |
// This function will convert an object of type excel cell to a string value. | |
int type = cell.getCellType(); | |
Object result; | |
switch (type) { | |
case HSSFCell.CELL_TYPE_NUMERIC: //0 | |
result = cell.getNumericCellValue(); | |
break; | |
case HSSFCell.CELL_TYPE_STRING: //1 | |
result = cell.getStringCellValue(); | |
break; | |
case HSSFCell.CELL_TYPE_FORMULA: //2 | |
throw new RuntimeException("We can't evaluate formulas in Java"); | |
case HSSFCell.CELL_TYPE_BLANK: //3 | |
result = "_"; | |
break; | |
case HSSFCell.CELL_TYPE_BOOLEAN: //4 | |
result = cell.getBooleanCellValue(); | |
break; | |
case HSSFCell.CELL_TYPE_ERROR: //5 | |
throw new RuntimeException("This cell has an error"); | |
default: | |
throw new RuntimeException("We don't support this cell type: " + type); | |
} | |
return result.toString(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment