1

我想遍历excel表并使用@dataprovider获取数据然后希望使用@Factory和@dataProvider在以下模式中执行方法@dataProvider返回的数据类型是

这是我正在寻找的执行模式

Before Test : 1
testMethod0 : Method 0 data set 1
testMethod1 : Method 1 data set 1
testMethod2 : Method 0 data set 1
After Test : 1

Before Test : 2
testMethod0 : Method 0 data set 2
testMethod1 : Method 1 data set 2
testMethod2 : Method 0 data set 2
After Test : 2
.
.
and so on 

请帮帮我

这就是我的 excel 工作表的样子

+--------------------+--------------------+--------------------+
| Method0Data        | Method0Data        | Method0Data        |
+--------------------+--------------------+--------------------+
|Method 0 data set 1 |Method 1 data set 1 |Method 0 data set 1 |
|Method 0 data set 1 |Method 1 data set 1 |Method 0 data set 1 |
|Method 0 data set 1 |Method 1 data set 1 |Method 0 data set 1 |
+--------------------+--------------------+--------------------+

这是我正在处理的代码,请查看

package ExecutionPackage;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.apache.poi.openxml4j.exceptions.InvalidFormatException;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.DataFormatter;
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.ss.usermodel.WorkbookFactory;
import org.testng.annotations.AfterTest;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.DataProvider;
import org.testng.annotations.Factory;
import org.testng.annotations.Test;

public class MapPoweredDataProviderSample {

    private static final String FILE = "D:\\Users\\sshafee\\DataSheet\\EnviromentDataSheet.xlsx";
    private static final DataFormatter dataFormatter = new DataFormatter();
    private static final String SHEET_NAME = "Credentials";

    private Map<String, String> iteration;
    private int count=1;

    @Factory(dataProvider = "dp")
    public void getFactoryData(Map<String, String> iteration) {
        this.iteration = iteration;
        System.out.println("New :" + this.iteration);
        System.out.println("Count :" + count++);
    }

    @BeforeTest
    public void beforetest() {
        System.out.println("Before Test : " + count);
    }

    @Test
    public void testMethod0() {
        System.out.println("testMethod0 : " + iteration.get("Method0Data"));
    }

    @Test
    public void testMethod1() {
        System.out.println("testMethod1 : " + iteration.get("Method1Data"));
    }

    @Test
    public void testMethod2() {
        System.out.println("testMethod2 : " + iteration.get("Method2Data"));
    }

    @AfterTest
    public void tearDown() {
        System.out.println("After Test : " + count);
        iteration.clear();
    }

    @DataProvider(name = "dp")
    public Object[][] getData() throws IOException, InvalidFormatException {
        Workbook workbook = WorkbookFactory.create(new File(FILE));
        Sheet sheet = workbook.getSheet(SHEET_NAME);
        Iterable<Row> rows = sheet::rowIterator;
        List<Map<String, String>> results = new ArrayList<>();
        boolean header = true;
        List<String> keys = null;
        for (Row row : rows) {
            List<String> values = getValuesInEachRow(row);
            if (header) {
                header = false;
                keys = values;
                continue;
            }
            results.add(transform(keys, values));
        }
        return asTwoDimensionalArray(results);
    }

    private static Object[][] asTwoDimensionalArray(List<Map<String, String>> interimResults) {
        Object[][] results = new Object[interimResults.size()][1];
        int index = 0;
        for (Map<String, String> interimResult : interimResults) {
            results[index++] = new Object[] { interimResult };
        }
        return results;
    }

    private static Map<String, String> transform(List<String> names, List<String> values) {
        Map<String, String> results = new HashMap<>();
        for (int i = 0; i < names.size(); i++) {
            String key = names.get(i);
            String value = values.get(i);
            results.put(key, value);
        }
        return results;
    }

    private static List<String> getValuesInEachRow(Row row) {
        List<String> data = new ArrayList<>();
        Iterable<Cell> columns = row::cellIterator;
        for (Cell column : columns) {
            data.add(dataFormatter.formatCellValue(column));
        }
        return data;
    }
}

当我执行上面的代码时,我得到以下错误,不知道为什么

[RemoteTestNG] detected TestNG version 7.0.0
org.testng.TestNGException: 
ExecutionPackage.MapPoweredDataProviderSample.getFactoryData MUST return [ java.lang.Object[] or org.testng.IInstanceInfo[] ] but returns void
    at org.testng.internal.Utils.checkReturnType(Utils.java:547)
    at org.testng.internal.FactoryMethod.<init>(FactoryMethod.java:89)
    at org.testng.internal.TestNGClassFinder.processFactory(TestNGClassFinder.java:169)
    at org.testng.internal.TestNGClassFinder.processMethod(TestNGClassFinder.java:145)
    at org.testng.internal.TestNGClassFinder.processClass(TestNGClassFinder.java:136)
    at org.testng.internal.TestNGClassFinder.<init>(TestNGClassFinder.java:74)
    at org.testng.TestRunner.initMethods(TestRunner.java:443)
    at org.testng.TestRunner.init(TestRunner.java:342)
    at org.testng.TestRunner.init(TestRunner.java:295)
    at org.testng.TestRunner.<init>(TestRunner.java:226)
    at org.testng.remote.support.RemoteTestNG6_12$1.newTestRunner(RemoteTestNG6_12.java:33)
    at org.testng.remote.support.RemoteTestNG6_12$DelegatingTestRunnerFactory.newTestRunner(RemoteTestNG6_12.java:66)
    at org.testng.SuiteRunner$ProxyTestRunnerFactory.newTestRunner(SuiteRunner.java:679)
    at org.testng.SuiteRunner.init(SuiteRunner.java:196)
    at org.testng.SuiteRunner.<init>(SuiteRunner.java:127)
    at org.testng.TestNG.createSuiteRunner(TestNG.java:1265)
    at org.testng.TestNG.createSuiteRunners(TestNG.java:1244)
    at org.testng.TestNG.runSuitesLocally(TestNG.java:1093)
    at org.testng.TestNG.runSuites(TestNG.java:1032)
    at org.testng.TestNG.run(TestNG.java:1000)
    at org.testng.remote.AbstractRemoteTestNG.run(AbstractRemoteTestNG.java:115)
    at org.testng.remote.RemoteTestNG.initAndRun(RemoteTestNG.java:251)
    at org.testng.remote.RemoteTestNG.main(RemoteTestNG.java:77)

这是我的 POM.xml

<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>AncestryDNA</groupId>
    <artifactId>testAutomation</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>jar</packaging>

    <name>testAutomation</name>
    <url>http://maven.apache.org</url>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    </properties>
    <build>
        <plugins>
            <plugin>
                <inherited>true</inherited>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>2.3.2</version>
                <configuration>
                    <encoding>UTF-8</encoding>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-surefire-plugin</artifactId>
                <version>2.18.1</version>
                <configuration>
                    <suiteXmlFiles>
                        <suiteXmlFile>TestNG.xml</suiteXmlFile>
                    </suiteXmlFiles>
                </configuration>
            </plugin>
        </plugins>
    </build>

    <dependencies>
        <dependency>
            <groupId>org.testng</groupId>
            <artifactId>testng</artifactId>
            <version>7.0.0</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.seleniumhq.selenium</groupId>
            <artifactId>selenium-java</artifactId>
            <version>3.11.0</version>
        </dependency>

        <dependency>
            <groupId>org.apache.poi</groupId>
            <artifactId>poi</artifactId>
            <version>[4.1.1,)</version>
        </dependency>
        <dependency>
            <groupId>org.apache.poi</groupId>
            <artifactId>poi-ooxml</artifactId>
            <version>[4.1.0,)</version>
        </dependency>
    </dependencies>
</project>
4

0 回答 0