2015-04-30 33 views
0

如何使用Selenium WebDriver在Excel中寫入結果?如何使用帶Selenium WebDriver的TestNG框架在Excel文件中編寫測試結果(通過/失敗)?

public static void writeData(String pathOfFile, String sheetName, int rowNum, int cellNum, String value) throws InvalidFormatException, IOException{ 
    FileInputStream fis = new FileInputStream("C:\\Users\\harini.b\\Desktop\\Copy of Login.xls"); 
    Workbook wb = WorkbookFactory.create(fis); 
    wb.getSheet(sheetName).getRow(rowNum).createCell(cellNum).setCellValue(value); 
    //wb.getSheet(sheetName).createRow(rowNum).createCell(cellNum).setCellValue(value); //use this if you are writing in new row. 
    FileOutputStream fos = new FileOutputStream(pathOfFile); 
    wb.write(fos); 
} 
+0

你得到什麼錯誤試着寫什麼時候? –

+0

我在Testng Result view中看到這個錯誤「Java.lang.NullPointerException」 –

+1

問題似乎是您正在嘗試讀取尚未創建的行。 – LittlePanda

回答

1

使用Apache POI將結果寫入excel文件。

  • 下載Apache POI jar文件
  • 所有jar文件添加到classpath

進口:

import org.apache.poi.hssf.usermodel.HSSFSheet; 
import org.apache.poi.xssf.usermodel.XSSFSheet; 

代碼:

XSSFWorkbook workbook = new XSSFWorkbook(); 
XSSFSheet sheet = workbook.createSheet("Sample.xlsx"); 
//Create a new row in current sheet 
Row row = sheet.createRow(0); 
//Create a new cell in current row 
Cell cell = row.createCell(0); 
//Set value to new value 
cell.setCellValue("Blahblah"); 

//finally write data to excel file 
FileOutputStream out = new FileOutputStream(new File("C:\\test.xls")); 
workbook.write(out); 
out.close(); 

查看更多的例子在this鏈接

0

嗨,您可以使用JXL瓶子和一些有用的類在閱讀和使用TestNG的框架寫入Excel文件。

您可以從here

否則讀取例子Write Test Case PASS/FAIL using selenium閱讀

例子:

public class Excelutility { 

    public String Testcase; 
    public WritableSheet writablesh; 
    public WritableWorkbook workbookcopy; 

@BeforeTest 
public void queryParameterization() throws BiffException,IOException,RowsExceededException,WriteException, InterruptedException{ 

FileInputStream testfile = new FileInputStream("E:\\AppiumTutorials\\Selenium_Practice\\SeleniumYoutube\\Testdata\\TestData.xls"); 
//Now get Workbook 
Workbook wbook = Workbook.getWorkbook(testfile); 
//Now get Workbook Sheet 
Sheet sheets = wbook.getSheet("Query_data"); 
int Norows = sheets.getRows(); 
//Read rows and columns and save it in String Two dimensional array 
String inputdata[][] = new String[sheets.getRows()][sheets.getColumns()]; 
System.out.println("Number of rows present in TestData xls file is -"+Norows); 

//For writing the data into excel we will use FileoutputStream class 
FileOutputStream testoutput = new FileOutputStream("E:\\AppiumTutorials\\Selenium_Practice\\SeleniumYoutube\\Testdata\\TestData_results.xls"); 
System.out.println("creating file one"); 
//To Create writable workbook 
workbookcopy = Workbook.createWorkbook(testoutput); 
System.out.println("creating file 2"); 
//To Create Writable sheet in Writable workbook 
writablesh = workbookcopy.createSheet("Query_data",0); 
System.out.println("creating file 3"); 

//Using for loop to write all the data to new sheet 
    for(int i=0;i<sheets.getRows();i++) 
     { 
     for(int k=0;k<sheets.getColumns();k++) 
     { 
     inputdata[i][k] = sheets.getCell(k, i).getContents(); 
     Label l = new Label(k, i, inputdata[i][k]); 
     Label l2 = new Label(4,0,"Results");    
     writablesh.addCell(l); 
     writablesh.addCell(l2); 
     } 
     }    
    } 

@AfterTest 
public void writeexcels(){ 
    try { 
     workbookcopy.write(); 
    } catch (IOException e) { 
     e.printStackTrace(); 
    } 
    try { 
     workbookcopy.close(); 
    } catch (WriteException e) { 
     e.printStackTrace(); 
    } catch (IOException e) { 
     e.printStackTrace(); 
    } 
} 
相關問題