2

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;
}
user2474976
  • 87
  • 3
  • 13
  • I assume you are using the jexcel api? – Michael Sep 23 '14 at 18:07
  • yes I am using the jexcel api - any thoughts as to why it would just completely overwrite the entire book with nothing? – user2474976 Sep 23 '14 at 18:09
  • I'm looking at the API and I dont see anything wrong with your code. Are you closing both workbooks? – Michael Sep 23 '14 at 18:15
  • I updated the above. I am closing the workbook after I retrieve the necessary data for the test, but I wouldn't think that would affect what's going on since I am opening the workbook again and not closing it. – user2474976 Sep 23 '14 at 18:26
  • Can you pinpoint which method blanks out the workbook? I assume you have too much code to post EVERYTHING so we should try and narrow it down – Michael Sep 23 '14 at 18:27
  • Ok so I pinpointed exactly where it blanks out the workbook. It's in the first code that I wrote, "onTestFailure" and I put a breakpoint on each line and after each line I opened the excel file to see if information was still there.. What I found was that the line: – user2474976 Sep 23 '14 at 18:33
  • WritableWorkbook wb = Workbook.createWorkbook(new File(testData), workbook); is where it deletes my entire book. So not sure why. – user2474976 Sep 23 '14 at 18:34
  • Try changing this line WritableWorkbook wb = Workbook.createWorkbook(new File(differentFileName), workbook); I have no clue whats going on but I see no reason these should/need to be the same file. Also, make sure your program is fully complete before you check the file. No looking while you debug or anything – Michael Sep 23 '14 at 18:40
  • Scratch the filename suggestion, you've got that right. I believe you should close the workbooks inside the onTestFailure method – Michael Sep 23 '14 at 18:51
  • Okay so I tried your method creating a new file but it makes a new file, but the file is blank. I'll keep looking into it and see what I can find - I appreciate it! I don't know why it's doing what it's doing. – user2474976 Sep 23 '14 at 18:53
  • Ok... let me try closing the workbook and see if that helps – user2474976 Sep 23 '14 at 18:54
  • 1
    Eureka - that worked using .close() method. But I am puzzled as to why it needs to close in order to succeed? Is the information sitting in some buffer somewhere and if you fail to close it it remains in this buffer?? - thank you I appreciate it! – user2474976 Sep 23 '14 at 18:59
  • I've learned that a lot of blank IO stuff has to do with closing. I'll let you know when I fully understand it. I've added an answer so you can close the question – Michael Sep 23 '14 at 19:02

1 Answers1

1

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("");
    }
}
Michael
  • 679
  • 7
  • 24