Selenium 在TestNG中使用@Factory和@DataProvider并将其映射到迭代器

Selenium 在TestNG中使用@Factory和@DataProvider并将其映射到迭代器,selenium,testng,testng-dataprovider,testng-annotation-test,Selenium,Testng,Testng Dataprovider,Testng Annotation Test,我想遍历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 Te

我想遍历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;
    }
}
包执行包;
导入java.io.File;
导入java.io.IOException;
导入java.util.ArrayList;
导入java.util.HashMap;
导入java.util.List;
导入java.util.Map;
导入org.apache.poi.openxml4j.exceptions.InvalidFormatException;
导入org.apache.poi.ss.usermodel.Cell;
导入org.apache.poi.ss.usermodel.DataFormatter;
导入org.apache.poi.ss.usermodel.Row;
导入org.apache.poi.ss.usermodel.Sheet;
导入org.apache.poi.ss.usermodel.工作簿;
导入org.apache.poi.ss.usermodel.WorkbookFactory;
导入org.testng.annotations.postest;
导入org.testng.annotations.BeforeTest;
导入org.testng.annotations.DataProvider;
导入org.testng.annotations.Factory;
导入org.testng.annotations.Test;
公共类MapPoweredDataProviderSample{
私有静态最终字符串文件=“D:\\Users\\sshafee\\DataSheet\\environmentdatasheet.xlsx”;
私有静态最终DataFormatter DataFormatter=新DataFormatter();
私有静态最终字符串表\u NAME=“凭证”;
私有映射迭代;
私有整数计数=1;
@工厂(dataProvider=“dp”)
public void getFactoryData(映射迭代){
this.iteration=迭代;
System.out.println(“New:+this.iteration”);
System.out.println(“计数:“+Count++”);
}
@试验前
测试前公共无效(){
System.out.println(“测试前:+计数”);
}
@试验
公共void testMethod0(){
System.out.println(“testMethod0:+iteration.get”(“Method0Data”);
}
@试验
公共void testMethod1(){
System.out.println(“testMethod1:+iteration.get(“Method1Data”);
}
@试验
公共void testMethod2(){
System.out.println(“testMethod2:+iteration.get”(“Method2Data”);
}
@事后
公共无效拆卸(){
System.out.println(“测试后:+计数);
迭代。清除();
}
@数据提供者(name=“dp”)
公共对象[][]getData()引发IOException,InvalidFormatException{
工作簿=WorkbookFactory.create(新文件);
工作表=工作簿.getSheet(工作表名称);
Iterable rows=工作表::行迭代器;
列表结果=新建ArrayList();
布尔头=真;
列表键=空;
用于(行:行){
列表值=getValuesInEachRow(行);
如果(标题){
标题=假;
键=值;
继续;
}
结果。添加(转换(键、值));
}
返回asTwoDimensionalArray(结果);
}
私有静态对象[][]asTwoDimensionalArray(列出interimResults){
Object[]results=新对象[interimResults.size()][1];
int指数=0;
用于(映射interimResult:interimResults){
结果[index++]=新对象[]{interimResult};
}
返回结果;
}
私有静态映射转换(列表名称、列表值){
映射结果=新的HashMap();
对于(int i=0;i
当我执行上面的代码时,我得到了以下错误,不知道为什么

[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)
[RemoteTestNG]检测到TestNG版本7.0.0
org.testng.TestNGException:
ExecutionPackage.MapPoweredDataProviderSample.getFactoryData必须返回[java.lang.Object[]或org.testng.IInstanceInfo[],但返回void
位于org.testng.internal.Utils.checkReturnType(Utils.java:547)
位于org.testng.internal.FactoryMethod(FactoryMethod.java:89)
位于org.testng.internal.TestNGClassFinder.processFactory(TestNGClassFinder.java:169)
位于org.testng.internal.TestNGClassFinder.processMethod(TestNGClassFinder.java:145)
位于org.testng.internal.TestNGClassFinder.processClass(TestNGClassFinder.java:136)
位于org.testng.internal.TestNGClassFinder(TestNGClassFinder.java:74)
位于org.testng.TestRunner.initMethods(TestRunner.java:443)
位于org.testng.TestRunner.init(TestRunner.java:342)
位于org.testng.TestRunner.init(TestRunner.java:295)
位于org.testng.TestRunner.(TestRunner.java:226)
位于org.testng.remote.support.RemoteTestNG6_12$1.newTestRunner(RemoteTestNG6_12.java:33)
位于org.testng.remote.support.RemoteTestNG6_12$delegatingestrunnerfactory.newestrunner(RemoteTestNG6_12.java:66)
位于org.testng.SuiteRunner$ProxyTestRunnerFactory.newTestRunner(SuiteRunner.java:679)
位于org.testng.SuiteRunner.init(SuiteRunner.java:196)
位于org.testng.SuiteRunner(SuiteRunner.java:127)
位于org.testng.testng.createSuiteRunner(testng.java:1265)
位于org.testng.testng.createSuiteRunners(testng.java:1244)
位于org.testng.testng.runSuitesLocally(testng.java:1093)
位于org.testng.testng.runSuites(testng.java:1032)
运行(testng.java:1000)
位于org.testng.remote.AbstractRemoteTestNG.run(AbstractRemoteTestNG.java:115)
位于org.testng.remote.RemoteTestNG.initAndRun(RemoteTestNG.java:251)
位于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
祖宗
遗嘱自动化
0.0.1-快照
罐子
遗嘱自动化