如何在 POM maven 项目中使用数据提供程序从 excel sheet 读取数据
How to read data from excel sheet using data provider in POM maven project
我无法使用数据提供程序从 excel 读取数据..
我正在创建一个 class 来从 excel 读取数据,但出现错误。 [附截图]
现在如何在测试中使用它们 class 来调用它们。
public class DataProvider extends Base {
// ExcelApiTest eat = null;
//String xlfilePath = "";
//String SheetName = "";
@DataProvider(name = "UserData")
public Object[][] userFormData() throws Exception {
Object[][] data = testData(xlfilePath, SheetName);
return data;
}
public Object[][] testData(String xlfilePath, String SheetName) throws Exception {
Object[][] excelData = null;
eat = new ExcelApiTest(xlfilePath);
int rows = eat.getRowCount(SheetName);
int columns = eat.getColumnCount(SheetName);
excelData = new Object[rows - 1][columns];
for (int i = 1; i < rows; i++) {
for (int j = 0; j < columns; j++) {
excelData[i - 1][j] = eat.getcellData(SheetName, j, i);
}
}
return excelData;
}
}
您可以使用以下代码作为用户名和密码,并修改它以获得更多列或字段:
import java.io.FileInputStream;
import java.io.IOException;
import org.apache.poi.openxml4j.exceptions.InvalidFormatException;
import org.apache.poi.ss.usermodel.DataFormatter;
import org.apache.poi.xssf.usermodel.XSSFCell;
import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
public class ExcelRedaer {
/**
* @param filePath excel file path
* @param sheetName sheet name in xlsx file
* @return excel data
* @throws InvalidFormatException
* @throws IOException
*/
public static Object[][] readExcel(String filePath, String sheetName) throws InvalidFormatException, IOException {
FileInputStream file= new FileInputStream(filePath);
XSSFWorkbook wb = new XSSFWorkbook(file);
XSSFSheet sheet = wb.getSheet(sheetName);
int rowCount = sheet.getLastRowNum();
int column = sheet.getRow(0).getLastCellNum();
Object[][] data = new Object[rowCount][column];
for (int i = 1; i <= rowCount; i++) {
XSSFRow row = sheet.getRow(i);
for (int j = 0; j < column; j++) {
XSSFCell cell = row.getCell(j);
DataFormatter formatter = new DataFormatter();
String val = formatter.formatCellValue(cell);
data[i - 1][j] = val;
}
}
return data;
}
}
并在你的测试中像这样使用:
import java.io.IOException;
import org.apache.poi.openxml4j.exceptions.InvalidFormatException;
import org.testng.annotations.DataProvider;
import org.testng.annotations.Test;
public class DataProviderDemo {
private String filePath = "D:\ECLIPSE-WORKSPACE\playground\src\main\resources\demo.xlsx";
private String sheetName = "demo";
@Test(dataProvider = "excelData")
public void read(String username, String password) {
System.out.println(username + ":" + password);
}
@DataProvider(name="excelData")
public Object[][] readExcel() throws InvalidFormatException, IOException {
return ExcelRedaer.readExcel(filePath, sheetName);
}
}
你的代码出错是因为你没有 class ExcelApiTest 的所有方法,可能是当你从你得到这个的地方复制代码时,应该有一个 class 名为 ExcelApiTest 其中包含一些方法,如 getRowCount(SheetName) and getColumnCount(SheetName)
等
您可以将此代码用于用户名和密码字段的演示目的。
您的 class 名称也是 DataProvider,这将再次给您一个错误,因为 testng 也有此 class:
org.testng.annotations.DataProvider
。
所以会有冲突,你也应该更改你的 class 名称。
我的excel:
希望对您有所帮助:)
我无法使用数据提供程序从 excel 读取数据.. 我正在创建一个 class 来从 excel 读取数据,但出现错误。 [附截图] 现在如何在测试中使用它们 class 来调用它们。
public class DataProvider extends Base {
// ExcelApiTest eat = null;
//String xlfilePath = "";
//String SheetName = "";
@DataProvider(name = "UserData")
public Object[][] userFormData() throws Exception {
Object[][] data = testData(xlfilePath, SheetName);
return data;
}
public Object[][] testData(String xlfilePath, String SheetName) throws Exception {
Object[][] excelData = null;
eat = new ExcelApiTest(xlfilePath);
int rows = eat.getRowCount(SheetName);
int columns = eat.getColumnCount(SheetName);
excelData = new Object[rows - 1][columns];
for (int i = 1; i < rows; i++) {
for (int j = 0; j < columns; j++) {
excelData[i - 1][j] = eat.getcellData(SheetName, j, i);
}
}
return excelData;
}
}
您可以使用以下代码作为用户名和密码,并修改它以获得更多列或字段:
import java.io.FileInputStream;
import java.io.IOException;
import org.apache.poi.openxml4j.exceptions.InvalidFormatException;
import org.apache.poi.ss.usermodel.DataFormatter;
import org.apache.poi.xssf.usermodel.XSSFCell;
import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
public class ExcelRedaer {
/**
* @param filePath excel file path
* @param sheetName sheet name in xlsx file
* @return excel data
* @throws InvalidFormatException
* @throws IOException
*/
public static Object[][] readExcel(String filePath, String sheetName) throws InvalidFormatException, IOException {
FileInputStream file= new FileInputStream(filePath);
XSSFWorkbook wb = new XSSFWorkbook(file);
XSSFSheet sheet = wb.getSheet(sheetName);
int rowCount = sheet.getLastRowNum();
int column = sheet.getRow(0).getLastCellNum();
Object[][] data = new Object[rowCount][column];
for (int i = 1; i <= rowCount; i++) {
XSSFRow row = sheet.getRow(i);
for (int j = 0; j < column; j++) {
XSSFCell cell = row.getCell(j);
DataFormatter formatter = new DataFormatter();
String val = formatter.formatCellValue(cell);
data[i - 1][j] = val;
}
}
return data;
}
}
并在你的测试中像这样使用:
import java.io.IOException;
import org.apache.poi.openxml4j.exceptions.InvalidFormatException;
import org.testng.annotations.DataProvider;
import org.testng.annotations.Test;
public class DataProviderDemo {
private String filePath = "D:\ECLIPSE-WORKSPACE\playground\src\main\resources\demo.xlsx";
private String sheetName = "demo";
@Test(dataProvider = "excelData")
public void read(String username, String password) {
System.out.println(username + ":" + password);
}
@DataProvider(name="excelData")
public Object[][] readExcel() throws InvalidFormatException, IOException {
return ExcelRedaer.readExcel(filePath, sheetName);
}
}
你的代码出错是因为你没有 class ExcelApiTest 的所有方法,可能是当你从你得到这个的地方复制代码时,应该有一个 class 名为 ExcelApiTest 其中包含一些方法,如 getRowCount(SheetName) and getColumnCount(SheetName)
等
您可以将此代码用于用户名和密码字段的演示目的。
您的 class 名称也是 DataProvider,这将再次给您一个错误,因为 testng 也有此 class:
org.testng.annotations.DataProvider
。
所以会有冲突,你也应该更改你的 class 名称。
我的excel:
希望对您有所帮助:)