简体   繁体   English

Selenium Java:如何使用注释从 Excel 读取数据

[英]Selenium Java: How to read data from Excel with annotations

Using DataProvider annotation & gradle as Dependency Manager.使用 DataProvider 注释和 gradle 作为依赖管理器。

Have questions:有问题:

  1. To read data from excel file, do jxl needs to be downloaded?从excel文件中读取数据,需要下载jxl吗?
  2. If I try to read the url (which is in some cell) from excel file, will the url be opened using driver.get method?如果我尝试从 excel 文件中读取 url(在某个单元格中),是否会使用 driver.get 方法打开 url?

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.

相关问题 如何使用Java for Selenium从Excel读取和存储数据? - How to read and store data from excel using java for selenium? 从Selenium Java中的Excel文件读取数据 - Read data from Excel file in Selenium Java 如何在Java,硒测试中读取Excel数据? - How to read excel data in java, selenium testing? 如何从硒中读取excel的测试数据 - How to read test data from excel in selenium 如何在Java中使用TestNG使用Selenium Web驱动程序从2张相同的Excel文档中读取数据 - How to Read the data from 2 sheets of same Excel document using Selenium Web driver using TestNG in Java 如何从Java和Selenium中的Excel中读取浮点值 - How to read floating values from excel in java and selenium 如何从Selenium Webdriver中的Excel工作表中读取数据 - How to read Data from Excel sheet in selenium webdriver Java从Excel读取数据 - Java read data from Excel I want to read the data from the Excel in selenium using java, but it is throwing the exception as “”main“ java.lang.ExceptionInInitializerError” - I want to read the data from the Excel in selenium using java, but it is throwing the exception as “”main“ java.lang.ExceptionInInitializerError” 数据驱动框架——如何使用Selenium WebDriver和java在excel表中读写 - Data Driven Framework — how to read and write in excel sheet using Selenium WebDriver with java
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM