mvn测试不适用于Apache Poi

时间:2016-12-20 10:34:51

标签: java apache selenium-webdriver webdriver apache-poi

mvn测试无法与Apache Poi一起使用。

  1. 如果我在我的框架中排除与Apache POI相关的代码: mvn test。

  2. 但是我的代码需要从Excel工作表中访问特定数据,因此我需要同时使用“mvn test”和Apache POI。

  3. 使用mvn test时似乎得到以下异常: enter image description here

  4. 例如,如果我评论以下代码'examples'mvn test成功执行了我的测试用例: enter image description here

  5. enter image description here

    <project xmlns="http://maven.apache.org/POM/4.0.0"    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
     xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    
    <groupId>uk.co.uat.pizzahut</groupId>
    <artifactId>PhFramework</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>jar</packaging>
    
    <name>PhFramework</name>
    <url>http://maven.apache.org</url>
    
    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    </properties>
    
    <build>
        <pluginManagement>
            <plugins>
                <plugin>
                    <artifactId>maven-compiler-plugin</artifactId>
                    <version>3.3</version>
                    <configuration>
                        <source>1.8</source>
                        <target>1.8</target>
                    </configuration>
                </plugin>
    
                <plugin>
                    <groupId>org.apache.maven.plugins</groupId>
                    <artifactId>maven-surefire-plugin</artifactId>
                    <version>2.19.1</version>
                    <configuration>
                        <suiteXmlFiles>
                            <!-- TestNG suite XML files -->
                            <suiteXmlFile>testng.xml</suiteXmlFile>
                        </suiteXmlFiles>
                    </configuration>
                </plugin>
    
            </plugins>
        </pluginManagement>
    </build>
    
    
    <dependencies>
        <!-- https://mvnrepository.com/artifact/org.seleniumhq.selenium/selenium-java -->
        <dependency>
            <groupId>org.seleniumhq.selenium</groupId>
            <artifactId>selenium-java</artifactId>
            <version>2.53.1</version>
        </dependency>
    
        <!-- https://mvnrepository.com/artifact/commons-io/commons-io -->
        <dependency>
            <groupId>commons-io</groupId>
            <artifactId>commons-io</artifactId>
            <version>2.4</version>
        </dependency>
    
        <!-- https://mvnrepository.com/artifact/org.apache.poi/poi -->
        <dependency>
            <groupId>org.apache.poi</groupId>
            <artifactId>poi</artifactId>
            <version>3.15-beta2</version>
        </dependency>
    
    
        <!-- https://mvnrepository.com/artifact/org.apache.poi/poi -->
        <dependency>
            <groupId>org.apache.poi</groupId>
            <artifactId>poi</artifactId>
            <version>3.15</version>
        </dependency>
    
        <!-- https://mvnrepository.com/artifact/org.apache.commons/commons-collections4 -->
        <dependency>
            <groupId>org.apache.commons</groupId>
            <artifactId>commons-collections4</artifactId>
            <version>4.0</version>
        </dependency>
    
        <dependency>
            <groupId>commons-collections</groupId>
            <artifactId>commons-collections</artifactId>
            <version>3.2.2</version>
        </dependency>
    
        <dependency>
            <groupId>com.danielflower.apprunner</groupId>
            <artifactId>app-runner</artifactId>
            <version>0.1.8</version>
        </dependency>
    
        <!-- https://mvnrepository.com/artifact/log4j/log4j -->
        <dependency>
            <groupId>log4j</groupId>
            <artifactId>log4j</artifactId>
            <version>1.2.17</version>
        </dependency>
    
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>3.8.1</version>
            <scope>test</scope>
        </dependency>
    
        <dependency>
            <groupId>org.testng</groupId>
            <artifactId>testng</artifactId>
            <version>6.8</version>
        </dependency>
    
        <dependency>
            <groupId>org.seleniumhq.selenium</groupId>
            <artifactId>selenium-server</artifactId>
            <version>3.0.1</version>
        </dependency>
    
        <dependency>
            <groupId>com.google.guava</groupId>
            <artifactId>guava</artifactId>
            <version>20.0</version>
        </dependency>
    
        <!-- https://mvnrepository.com/artifact/org.apache.xmlbeans/xmlbeans -->
        <dependency>
            <groupId>org.apache.xmlbeans</groupId>
            <artifactId>xmlbeans</artifactId>
            <version>2.6.0</version>
        </dependency>
    
    </dependencies>
    
    <!-- https://mvnrepository.com/artifact/org.seleniumhq.selenium/selenium-server -->
    

    Excel阅读器代码:

    private XSSFSheet sheet = null;
    private XSSFRow row = null;
    private XSSFCell cell = null;
    String path = null;
    
    
    public ExcelReader() throws IOException {
        path = System.getProperty("user.dir") + "\\src\\main\\java\\PhFramework\\testData\\pricebandPrices.xlsx";
        fis = new FileInputStream(path);
        workbook = new XSSFWorkbook(fis);
    
    
        sheet = workbook.getSheetAt(0);
    }
    
    
    public int getSheetRows() throws IOException {
        Properties p = new Properties();
        FileInputStream fi = new FileInputStream("C:\\Users\\gpb7642\\Desktop\\PhAutomationFramework\\PhFramework\\src\\main\\java\\PhFramework\\testData\\Setup.properties");
        p.load(fi);
        int index = workbook.getSheetIndex(p.getProperty("priceband"));
        sheet = workbook.getSheetAt(index);
        return (sheet.getLastRowNum() + 1);
    }
    
    
    public int getSheetColumns(String sheetName) throws IOException {
        Properties p = new Properties();
        FileInputStream fi = new FileInputStream("C:\\Users\\gpb7642\\Desktop\\PhAutomationFramework\\PhFramework\\src\\main\\java\\PhFramework\\testData\\Setup.properties");
        p.load(fi);
        int index = workbook.getSheetIndex(p.getProperty("priceband"));
        sheet = workbook.getSheetAt(index);
        row = sheet.getRow(0);
        return (row.getLastCellNum());
    }
    
    
    public String getCellDataString(int colNum, int rowNum) throws IOException {
        Properties p = new Properties();
        FileInputStream fi = new FileInputStream("C:\\Users\\gpb7642\\Desktop\\PhAutomationFramework\\PhFramework\\src\\main\\java\\PhFramework\\testData\\Setup.properties");
        p.load(fi);
        int index = workbook.getSheetIndex(p.getProperty("priceband"));
        sheet = workbook.getSheetAt(index);
        row = sheet.getRow(rowNum);
        cell = row.getCell(colNum);
        return (cell.getStringCellValue());
    }
    
    public double getCellDataDouble(int colNum, int rowNum) throws IOException {
        Properties p = new Properties();
        FileInputStream fi = new FileInputStream("C:\\Users\\gpb7642\\Desktop\\PhAutomationFramework\\PhFramework\\src\\main\\java\\PhFramework\\testData\\Setup.properties");
        p.load(fi);
        int index = workbook.getSheetIndex(p.getProperty("priceband"));
        sheet = workbook.getSheetAt(index);
        row = sheet.getRow(rowNum);
        cell = row.getCell(colNum);
        return (cell.getNumericCellValue());
    }
    
    public String getCellDataText(String colName, int rowNum) throws IOException {
        Properties p = new Properties();
        FileInputStream fi = new FileInputStream("C:\\Users\\gpb7642\\Desktop\\PhAutomationFramework\\PhFramework\\src\\main\\java\\PhFramework\\testData\\Setup.properties");
        p.load(fi);
        int colNum = -1;
        int index = workbook.getSheetIndex(p.getProperty("priceband"));
        sheet = workbook.getSheetAt(index);
        for (int i = 0; i < getSheetColumns(p.getProperty("priceband")); i++) {
            row = sheet.getRow(0);
            cell = row.getCell(i);
            if (cell.getStringCellValue().equals(colName)) {
                colNum = cell.getColumnIndex();
                break;
            }
        }
        row = sheet.getRow(rowNum);
        cell = row.getCell(colNum);
        return (cell.getStringCellValue());
    }
    
    public double getCellDataNumber(String colName, int rowNum) throws IOException {
        Properties p = new Properties();
        FileInputStream fi = new FileInputStream("C:\\Users\\gpb7642\\Desktop\\PhAutomationFramework\\PhFramework\\src\\main\\java\\PhFramework\\testData\\Setup.properties");
        p.load(fi);
        int colNum = -1;
        int index = workbook.getSheetIndex(p.getProperty("priceband"));
        sheet = workbook.getSheetAt(index);
        for (int i = 0; i < getSheetColumns(p.getProperty("priceband")); i++) {
            row = sheet.getRow(0);
            cell = row.getCell(i);
            if (cell.getStringCellValue().equals(colName)) {
                colNum = cell.getColumnIndex();
                break;
            }
        }
        row = sheet.getRow(rowNum);
        cell = row.getCell(colNum);
        return (cell.getNumericCellValue());
    }
    
    public void setCellData(int colNum, int rowNum, String str) throws IOException {
        Properties p = new Properties();
        FileInputStream fi = new FileInputStream("C:\\Users\\gpb7642\\Desktop\\PhAutomationFramework\\PhFramework\\src\\main\\java\\PhFramework\\testData\\Setup.properties");
        p.load(fi);
        int index = workbook.getSheetIndex(p.getProperty("priceband"));
        sheet = workbook.getSheetAt(index);
        row = sheet.getRow(rowNum);
        cell = row.createCell(colNum);
        cell.setCellValue(str);
        try {
            fileOut = new FileOutputStream(path);
    
            try {
                workbook.write(fileOut);
                fileOut.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }
     }
    
    }
    

    enter image description here

1 个答案:

答案 0 :(得分:2)

org.apache.poi.xssf...poi-ooxml工件提供,您需要将其添加到您的pom.xml:

<dependency>
    <groupId>org.apache.poi</groupId>
    <artifactId>poi-ooxml</artifactId>
    <version>3.15</version>
</dependency>

您还可能希望删除3.15-beta2版本的poi工件