Java 如何在testng.xml中为路径指定动态值
我已经创建了一个用于读取excel值的类,该类位于一个包中,另外还有一个类用于检查登录名,它以testng的形式位于不同的类中。 我的ReadExcelFile.java是 包uatJava 如何在testng.xml中为路径指定动态值,java,testng,Java,Testng,我已经创建了一个用于读取excel值的类,该类位于一个包中,另外还有一个类用于检查登录名,它以testng的形式位于不同的类中。 我的ReadExcelFile.java是 包uat public class ReadExcelFile extends BeforeAfterSuite{ @Test @Parameters("filename") public void readXLSXFile(String fileName) { InputStr
public class ReadExcelFile extends BeforeAfterSuite{
@Test
@Parameters("filename")
public void readXLSXFile(String fileName) {
InputStream XlsxFileToRead = null;
XSSFWorkbook workbook = null;
try {
XlsxFileToRead = new FileInputStream(fileName);
//Getting the workbook instance for xlsx file
workbook = new XSSFWorkbook(XlsxFileToRead);
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
//getting the first sheet from the workbook using sheet name.
// We can also pass the index of the sheet which starts from '0'.
XSSFSheet sheet = workbook.getSheet("Sheet1");
XSSFRow row;
XSSFCell cell;
//Iterating all the rows in the sheet
Iterator rows = sheet.rowIterator();
while (rows.hasNext()) {
row = (XSSFRow) rows.next();
//Iterating all the cells of the current row
Iterator cells = row.cellIterator();
while (cells.hasNext()) {
cell = (XSSFCell) cells.next();
if (cell.getCellType() == XSSFCell.CELL_TYPE_STRING) {
System.out.print(cell.getStringCellValue() + " ");
} else if (cell.getCellType() == XSSFCell.CELL_TYPE_NUMERIC) {
System.out.print(cell.getNumericCellValue() + " ");
} else if (cell.getCellType() == XSSFCell.CELL_TYPE_BOOLEAN) {
System.out.print(cell.getBooleanCellValue() + " ");
}
}
System.out.println();
try {
XlsxFileToRead.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
在testng中,我想将参数作为文件名和路径一起传递,但它是硬编码的,我想发送动态值
因为我以后打算移动的位置位于共享路径中。以下是您的操作方法
删除对@Parameters的依赖关系
利用JVM参数,比如filename,可以选择接受实际位置,如果未提供,则为JVM参数定义默认值。
您的代码可以如下所示
@Test
public void readXLSXFile() {
//To provide a different value for the excel sheet, use the JVM argument: -Dfilename
//For e.g., -Dfilename=src/test/resources/anotherdata.xls
//If this JVM argument is not provided,
//then the file name is defaulted to src/test/resources/data.xls
String fileName = System.getProperty("filename", "src/test/resources/data.xls");
InputStream XlsxFileToRead = null;
XSSFWorkbook workbook = null;
//Rest of the code goes here
}