java-Cucumber-JVM 3-使用asMap()将DataTable转换为单个对象

在cucumber-jvm 3.x.x中,我想将只有两列的数据表转换为单个对象.此功能在2.x.x中曾经是自动的,但此后已随新的主要版本删除.

码:

// Feature
Scenario: Scan Barcode
    Given I Am At The Login Page
    When I Log In As Valid User
        | group    | gpao     |
        | username | svgpao1  |
        | password | REDACTED |

// Stepdef
@When("I Log In As Valid User")
public void I_Log_In_As_Valid_User(User user) throws Throwable {
    Selenium.enterText(driver, user.getUsername(), "username");
    Selenium.enterText(driver, user.getPassword(), "password");
    Selenium.clickElement(driver, "sign.in");
    LOGGER.debug("User Group=" + user.getGroup());
}

// Configurer
registry.defineDataTableType(new DataTableType(User.class, new TableTransformer<User>() {
    @Override
    public User transform(DataTable dataTable) throws Throwable {
        Map<String, String> map = dataTable.asMap(String.class, String.class);
        return new User(map);
    }
}));

// User - constructor
public User(Map<String, String> map) {
    this.group = map.get("group");
    this.username = map.get("username");
    this.password = map.get("password");
}

堆栈跟踪:

cucumber.runtime.CucumberException: Could not convert arguments for step [I Log In As Valid User] defined at 'jcucumberng.project.stepdefs.EnterPacketDetailsSteps.I_Log_In_As_Valid_User(User) in file:/Z:/workspace/java/jcucumberng-framework/target/test-classes/'.
The details are in the stacktrace below.
        at cucumber.runtime.PickleStepDefinitionMatch.couldNotConvertArguments(PickleStepDefinitionMatch.java:69)
        at cucumber.runtime.PickleStepDefinitionMatch.runStep(PickleStepDefinitionMatch.java:46)
        at cucumber.runner.TestStep.executeStep(TestStep.java:55)
        at cucumber.runner.TestStep.run(TestStep.java:42)
        at cucumber.runner.PickleStepTestStep.run(PickleStepTestStep.java:53)
        at cucumber.runner.TestCase.run(TestCase.java:47)
        at cucumber.runner.Runner.runPickle(Runner.java:44)
        at cucumber.runtime.junit.PickleRunners$NoStepDescriptions.run(PickleRunners.java:140)
        at cucumber.runtime.junit.FeatureRunner.runChild(FeatureRunner.java:68)
        at cucumber.runtime.junit.FeatureRunner.runChild(FeatureRunner.java:23)
        at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
        at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
        at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
        at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
        at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
        at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
        at cucumber.runtime.junit.FeatureRunner.run(FeatureRunner.java:73)
        at cucumber.api.junit.Cucumber.runChild(Cucumber.java:98)
        at cucumber.api.junit.Cucumber.runChild(Cucumber.java:55)
        at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
        at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
        at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
        at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
        at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
        at cucumber.api.junit.Cucumber$1.evaluate(Cucumber.java:107)
        at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:26)
        at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
        at org.apache.maven.surefire.junit4.JUnit4Provider.execute(JUnit4Provider.java:365)
        at org.apache.maven.surefire.junit4.JUnit4Provider.executeWithRerun(JUnit4Provider.java:273)
        at org.apache.maven.surefire.junit4.JUnit4Provider.executeTestSet(JUnit4Provider.java:238)
        at org.apache.maven.surefire.junit4.JUnit4Provider.invoke(JUnit4Provider.java:159)
        at org.apache.maven.surefire.booter.ForkedBooter.invokeProviderInSameClassLoader(ForkedBooter.java:383)
        at org.apache.maven.surefire.booter.ForkedBooter.runSuitesInProcess(ForkedBooter.java:344)
        at org.apache.maven.surefire.booter.ForkedBooter.execute(ForkedBooter.java:125)
        at org.apache.maven.surefire.booter.ForkedBooter.main(ForkedBooter.java:417)
Caused by: io.cucumber.datatable.CucumberDataTableException: 'jcucumberng.project.domain.User' could not transform
      | group    | gpao     |
      | username | svgpao1  |
      | password | REDACTED |

解决方法:

除了@Transpose之外,还需要修改transform方法.

public User transform(DataTable dataTable) throws Throwable {
    return new User(dataTable.asMaps().get(0));
}

@When("I Log In As Valid User")
public void I_Log_In_As_Valid_User(@Transpose SilverUser user) throws Throwable {
    System.out.println(user);
}
上一篇:CodeGo.net>有什么容易的浪费将XML输出从sharepoint GetListItems()转换为DataTable


下一篇:封装一个redis操作类来操作hash格式