When I run my Selenium script, the assertion method I use sorta works. When the assertion is met (element is present), the script writes to an xls file. When it is not met, the script does not write to the xls file, and it it stops the test right there.
try {
assertEquals(driver.findElement(By.xpath(prop.getProperty("failedlogin"))).getText(), "LOG IN");
//add pass entry to the excel sheet
testresultdata.put("4", new Object[] {
3d, "User should not be able to login with no password", "Login failed", "Pass"
});
} catch (Exception e) {
//add fail entry to the excel sheet
testresultdata.put("4", new Object[] {
3d, "User should not be able to login with no password", "Login failed", "Fail"
});
}
Here are my excel writer methods:
@BeforeClass(alwaysRun = true)
public void setupBeforeSuite(ITestContext context) throws IOException {
//create a new work book
workbook = new HSSFWorkbook();
//create a new work sheet
sheet = workbook.createSheet("Test Result");
testresultdata = new LinkedHashMap < String, Object[] > ();
//add test result excel file column header
//write the header in the first row
testresultdata.put("1", new Object[] {
"Test Step Id", "Action", "Expected Result", "Actual Result"
});
}
And:
@AfterClass
public void setupAfterSuite(ITestContext context) {
//write excel file and file name is TestResult.xls
Set < String > keyset = testresultdata.keySet();
int rownum = 0;
for (String key: keyset) {
Row row = sheet.createRow(rownum++);
Object[] objArr = testresultdata.get(key);
int cellnum = 0;
for (Object obj: objArr) {
Cell cell = row.createCell(cellnum++);
if (obj instanceof Date) cell.setCellValue((Date) obj);
else if (obj instanceof Boolean) cell.setCellValue((Boolean) obj);
else if (obj instanceof String) cell.setCellValue((String) obj);
else if (obj instanceof Double) cell.setCellValue((Double) obj);
}
}
try {
FileOutputStream out = new FileOutputStream(new File("C:/Users/matt_damon/workspace/project/passfail.xls"));
workbook.write(out);
out.close();
System.out.println("Excel written successfully..");
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
So why, even when I purposely make my test fail, does the excel writer not write the fail entry?