简体   繁体   English

使用testNG读取硒中的Excel数据

[英]Reading excel data in selenium with testNG

I am new to Selenium and still learning. 我是Selenium的新手,仍然在学习。 I am trying to write a program in TestNG with two tests to read data from excel and to write, using Apache POI. 我试图用两个测试在TestNG中编写一个程序,以从Excel读取数据并使用Apache POI进行编写。 I have one XLS and one XLSX file in the same folder and trying to use both the files. 我在同一文件夹中有一个XLS和一个XLSX文件,并尝试使用这两个文件。 I am looking to configure path and name of the excels in testng.xml. 我正在尝试在testng.xml中配置Excel的路径和名称。 When I executed my first test to read data, the sheet name is being read as xml. 当我执行第一个测试以读取数据时,工作表名称被读取为xml。 Below is my testng.xml 下面是我的testng.xml

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">
<suite name="Suite" parallel="tests" thread-count="2">
<parameter name="filePath" value="C:\\Technologies\\Selenium\\WebDriver\\DataFiles\\ExcelFiles"></parameter>
  <test name="Xlsx">
  <parameter name="fileName" value="12142015_sx.xlsx"></parameter>
  <parameter name="sheetName" value="xlsx_1"></parameter>
    <classes>
      <class name="handlefiles.handleExcel"/>
    </classes>
  </test>
  <test name="Xls">
  <parameter name="fileName" value="12142015_s.xls"></parameter>
  <parameter name="sheetName" value="xls_1"></parameter>
    <classes>
      <class name="handlefiles.handleExcel"/>
    </classes>
  </test> <!-- Test -->
</suite> <!-- Suite -->

Below is java class code. 下面是Java类代码。

package handlefiles;

import java.io.*;

import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.testng.annotations.*;

public class handleExcel {

    @BeforeTest
    @Parameters({"filePath", "fileName", "sheetName"})
    public void readExcel(String filePath, String fileName, String sheetName) throws IOException{
        // set the file path
        File eFile = new File(filePath+"\\"+fileName);
        //print the file path
        System.out.println("File is at: "+eFile);

        FileInputStream inputstream = new FileInputStream(eFile);
        Workbook wb = null;

        // Read and publish extension
        String extensionName = fileName.substring(fileName.indexOf("."));
        System.out.println("File type is: "+extensionName);

        //Read the file
        if(extensionName.equalsIgnoreCase(".xlsx")){
            wb = new XSSFWorkbook(inputstream);
        }else if(extensionName.equalsIgnoreCase(".xls")){
            wb = new HSSFWorkbook(inputstream);
        }
        //Read the sheet name
        Sheet wbSheet = wb.getSheet(sheetName);
        System.out.println("Sheet Name is: "+wbSheet);

    }

  @Test(priority=1)
  public void readData(Sheet wbSheet) {

    // Get the row count
            int rowCount = wbSheet.getLastRowNum() - wbSheet.getFirstRowNum();

            // print data from excel
            for (int i=0; i<rowCount-1; i++){
                Row row = wbSheet.getRow(i);
                for (int j=0; j<row.getLastCellNum(); j++){
                    System.out.println(row.getCell(j).getStringCellValue()+"||");
                }
            }
  }
}

When I run this in eclipse, I see below exception- 当我在Eclipse中运行时,我看到以下异常-

org.apache.poi.poifs.filesystem.OfficeXmlFileException: The supplied data appears to be in the Office 2007+ XML. You are calling the part of POI that deals with OLE2 Office Documents. You need to call a different part of POI to process this data (eg XSSF instead of HSSF)
    at org.apache.poi.poifs.storage.HeaderBlock.<init>(HeaderBlock.java:128)
    at org.apache.poi.poifs.storage.HeaderBlock.<init>(HeaderBlock.java:112)
    at org.apache.poi.poifs.filesystem.NPOIFSFileSystem.<init>(NPOIFSFileSystem.java:300)
    at org.apache.poi.hssf.usermodel.HSSFWorkbook.<init>(HSSFWorkbook.java:400)
    at org.apache.poi.hssf.usermodel.HSSFWorkbook.<init>(HSSFWorkbook.java:381)
    at handlefiles.handleExcel.readExcel(handleExcel.java:36)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
    at java.lang.reflect.Method.invoke(Unknown Source)
    at org.testng.internal.MethodInvocationHelper.invokeMethod(MethodInvocationHelper.java:86)
    at org.testng.internal.Invoker.invokeConfigurationMethod(Invoker.java:514)
    at org.testng.internal.Invoker.invokeConfigurations(Invoker.java:215)
    at org.testng.internal.Invoker.invokeConfigurations(Invoker.java:142)
    at org.testng.TestRunner.beforeRun(TestRunner.java:656)
    at org.testng.TestRunner.run(TestRunner.java:624)
    at org.testng.SuiteRunner.runTest(SuiteRunner.java:366)
    at org.testng.SuiteRunner.access$000(SuiteRunner.java:39)
    at org.testng.SuiteRunner$SuiteWorker.run(SuiteRunner.java:400)
    at org.testng.internal.thread.ThreadUtil$2.call(ThreadUtil.java:64)
    at java.util.concurrent.FutureTask.run(Unknown Source)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
    at java.lang.Thread.run(Unknown Source)

According to the stack trace POI thinks you are attempting to open an XLSX file using POI's XLS code (not XLSX code). 根据堆栈跟踪,POI认为您正在尝试使用POI的XLS代码(不是XLSX代码)打开XLSX文件。 Perhaps the file extension is wrong for that file is ".xls" but should be ".xlsx" or the file is corrupted, etc. (or maybe you've found a bug). 也许文件扩展名错误,因为该文件是“ .xls”,但应该是“ .xlsx”,或者文件已损坏等。(或者您发现了错误)。

I suggest using POI's WorkbookFactory instead: 我建议改用POI的WorkbookFactory

Factory for creating the appropriate kind of Workbook (be it HSSFWorkbook or XSSFWorkbook ), by auto-detecting from the supplied input. 通过从提供的输入中自动检测,用于创建适当种类的工作簿( HSSFWorkbookXSSFWorkbook )的XSSFWorkbook

Remove following IF Condition and 删除以下IF条件,然后

if(extensionName.equalsIgnoreCase(".xlsx")){
            wb = new XSSFWorkbook(inputstream);
        }else if(extensionName.equalsIgnoreCase(".xls")){
            wb = new HSSFWorkbook(inputstream);
        }

Add this piece of code 添加这段代码

 wb = new XSSFWorkbook(inputstream);

I used in past and I never told which extension I am using. 我过去使用过,但从未告诉过我正在使用哪个扩展名。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 使用TestNG和Excel的Selenium WebDriver - Selenium WebDriver using TestNG and Excel 硒从Excel文件中读取数据 - Selenium reading data from Excel file Selenium 中的数据提供者与 TestNG 不匹配 - Data provider mismatch in Selenium with TestNG Selenium与Java和TestNG-使用Excel DataSheet测试登录 - Selenium with Java and TestNG - Test login with Excel DataSheet Selenium TestNG从Excel工作表中传入参数 - Selenium TestNG Passing in parameters from Excel sheet 如何在selenium Web驱动程序和testNG中使用数据提供程序读取Excel并获取多个值并将其连接 - How to read an excel and fetch multiple values and concatenate them using data-provider in selenium web driver and testNG 在现有 excel 文件中创建新单元格并从输出中导出数据 - Selenium_TestNG_JAVA - Create new Cell in existing excel file and export data from output - Selenium_TestNG_JAVA 如何使用Selenium WebDriver TestNG数据提供程序方法从Excel工作表中读取特定值? - How to read a specific value from Excel sheet using Selenium WebDriver TestNG Data Provider Method? 如何在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 Testng数据提供程序(从excel读取)未检索到另一个类最近更新的最新值 - Testng Data Provider(reading from excel) is not retrieving latest value updated recently by another class
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM