Reputation: 87
What I am trying to do is onTestFailure (TestNG) I am trying to write a pass/fail to a specified column. In the below code, I just wanted to do a little test to write Fail to a specified cell. But what's happening is, instead of writing a pass/fail to the specified cell, it literally overwrites the entire file and erases all of my data and I am puzzled as to why that's happening. I have looked on here and googled but cannot find the answer. Below is the following code:
@Override
public synchronized void onTestFailure(ITestResult tr){
try {
Workbook workbook = Workbook.getWorkbook(new File(testData), workbookSettings);
WritableWorkbook wb = Workbook.createWorkbook(new File(testData), workbook);
WritableSheet ws = wb.getSheet("OrderEditQA3");
Label label = new Label(5,2, "Fail");
ws.addCell(label);
wb.write();
} catch (IOException e) {
e.printStackTrace();
} catch (BiffException b){
System.out.print("Error!");
} catch (WriteException we){
System.out.print("");
}
}
Any help would be greatly appreciated.
UPDATED...
Below is another class that I use to get test data returned to a Dataprovider and I also load all of my elements from a specified sheet. Below is the code
// Code to get all test data
protected static Object[][] loadTestData(String sheetName)throws BiffException, IOException{
Sheet sheet = null;
try{
sheet = getWorkBook().getSheet(sheetName);
rowCount = sheet.getRows();
colCount = sheet.getColumns();
data = new String[rowCount -1][colCount-1];
int ci;
int cj;
ci=0;
for(int i=1; i< rowCount; i++, ci++){
cj=0;
for(int j=1; j< colCount; j++, cj++){
Cell cell = sheet.getCell(j,i);
if(cell.getContents().isEmpty()){ continue;}
System.out.print(cell.getContents() + "\n");
data[ci][cj] = cell.getContents();
}
}
}catch (IOException io){
System.out.print(String.format("File: %s not found!", testData));
}
getWorkBook().close();
return data;
}
//Code to retrieve all element attributes:
@BeforeClass
protected static List<String> loadElements()throws BiffException, IOException {
Sheet sheet = null;
sheet = getWorkBook().getSheet("Elements");
rowCount = sheet.getRows();
colCount = sheet.getColumns();
List<String> list = new ArrayList<>(rowCount);
for (int i = 1; i < rowCount; i++) {
for (int j = 0; j < colCount; j++) {
Cell cell = sheet.getCell(j,i);
if(cell.getContents().isEmpty()) {continue;}
String cellContents = cell.getContents();
list.add(cellContents);
System.out.print(cell.getContents() + "\n");
}
}
elements = list;
getWorkBook().close();
return list;
}
Upvotes: 2
Views: 987
Reputation: 719
Just forgot that pesky close() method
@Override
public synchronized void onTestFailure(ITestResult tr){
try {
Workbook workbook = Workbook.getWorkbook(new File(testData), workbookSettings);
WritableWorkbook wb = Workbook.createWorkbook(new File(testData), workbook);
WritableSheet ws = wb.getSheet("OrderEditQA3");
Label label = new Label(5,2, "Fail");
ws.addCell(label);
wb.write();
wb.close();
} catch (IOException e) {
e.printStackTrace();
} catch (BiffException b){
System.out.print("Error!");
} catch (WriteException we){
System.out.print("");
}
}
Upvotes: 1