java 如何使用带有 Selenium WebDriver 的 TestNG 框架在 Excel 文件中写入测试结果(通过/失败)?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/29964639/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How can I write Test Result (Pass/Fail) in Excel file using TestNG Framework with Selenium WebDriver?
提问by srinuvasareddy marri
How can I write result in Excel using Selenium WebDriver?
如何使用 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);
}
回答by LittlePanda
Use Apache POIto write your results to an excel file.
使用Apache POI将结果写入 excel 文件。
- Download Apache POI jar files
- Add all jar files to the classpath
- 下载 Apache POI jar 文件
- 将所有 jar 文件添加到类路径
Imports:
进口:
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFSheet;
Code:
代码:
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();
See more examples at thislink
在此链接中查看更多示例
回答by Rajesh Om
Hi You can use JXL jar and some useful classes in that to read and write into excel file using testng framework.
嗨,您可以使用 JXL jar 和一些有用的类来使用 testng 框架读取和写入 excel 文件。
You can read from HERE
你可以从这里阅读
Otherwise read with examples Write Test Case PASS/FAIL using selenium
否则阅读示例使用 selenium 编写测试用例通过/失败
Example:
例子:
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();
}
}