如何使用带有Selenium WebDriver的TestNG Framework在Excel文件中编写测试结果(通过/失败)?

时间:2015-04-30 09:59:17

标签: java selenium selenium-webdriver webdriver testng

如何使用Selenium WebDriver在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);
}

2 个答案:

答案 0 :(得分:1)

使用Apache POI将结果写入Excel文件。

  • 下载Apache POI jar文件
  • 将所有jar文件添加到类路径

<强>进口:

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链接

上查看更多示例

答案 1 :(得分:0)

您好您可以使用JXL jar和一些有用的类来使用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();
    }   
}