[英]Selenium Java: How to read data from Excel with annotations
Using DataProvider annotation & gradle as Dependency Manager.使用 DataProvider 注释和 gradle 作为依赖管理器。
Have questions:有问题:
You can use the apache poi library to read the xls file.. maven dependency and jar file is here..您可以使用 apache poi 库来读取 xls 文件.. maven 依赖项和 jar 文件在这里..
https://mvnrepository.com/artifact/org.apache.poi/poi-ooxml/3.9 https://mvnrepository.com/artifact/org.apache.poi/poi-ooxml/3.9
Sample Code:示例代码:
Assumption is you have dropped the xls on the project folder.. if not give the full path of the xlsx.假设您已将 xls 放在项目文件夹中。如果没有给出 xlsx 的完整路径。
You can pass the sheetName and which Row you wanted to read you can pass to this method.. or you can create your own method by referring this.您可以传递 sheetName 和您想读取的 Row 可以传递给此方法。或者您可以通过引用此方法创建自己的方法。
public static HashMap<String, String> ReadExcel(String SheetName, int Rownumber) throws IOException {
// Define the File Folder
XSSFWorkbook sourceBook = new XSSFWorkbook("./MasterSheet.xlsx");
//Which sheet you want to read (Sheet Name)
XSSFSheet sourceSheet = sourceBook.getSheet(SheetName);
//int lastRowNumber = sourceSheet.getLastRowNum();
int lastColumnNumber = sourceSheet.getRow(0).getLastCellNum() - 1;
//Read the headerRow
XSSFRow headerRow = sourceSheet.getRow(0);
HashMap<String, String> ValuesMap = new HashMap<String, String>();
for (int i = 0; i <= lastColumnNumber; i++) {
ValuesMap.put(headerRow.getCell(i).toString(), sourceSheet.getRow(Rownumber).getCell(i).toString());
//System.out.println(sourceSheet.getRow(Rownumber).getCell(i).toString());
}
//Close the workbook
sourceBook.close();
return ValuesMap;
}
You can use the returned value by K,V pair and reading like..您可以使用 K,V 对的返回值并读取类似..
HashMap<String, String> queryset = utilities.ReadExcel("CardDetails", 1);
String urlvalue = queryset.get(url).toString();
driver.get(urlvalue)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.