Cucumber DataTable 错误 - io.cucumber.datatable.UndefinedDataTableTypeException:无法将 DataTable 转换为 cucu
Posted
技术标签:
【中文标题】Cucumber DataTable 错误 - io.cucumber.datatable.UndefinedDataTableTypeException:无法将 DataTable 转换为 cucumber.api.DataTable【英文标题】:Cucumber DataTable Error - io.cucumber.datatable.UndefinedDataTableTypeException: Can't convert DataTable to cucumber.api.DataTable 【发布时间】:2019-08-30 16:10:10 【问题描述】:尝试使用 cucumber/selenium/java/intelliJ 运行场景,但在其中一个步骤中收到有关 DataTable 的错误。在我开始使用测试运行器并更改了一些东西之前,dataTable 工作正常并正确转换了该步骤的参数,但我无法让它工作。
这是错误:
cucumber.runtime.CucumberException: Could not convert arguments for step [^I enter the following login details:$] defined at 'Steps.MaStepdefs.iEnterTheFollowingLoginDetails(DataTable) in file:/C:/Users/Kristian.Senior/Desktop/CukesReporting/target/test-classes/'.
It appears you did not register a data table type. The details are in the stacktrace below.
at cucumber.runner.PickleStepDefinitionMatch.registerTypeInConfiguration(PickleStepDefinitionMatch.java:59)
at cucumber.runner.PickleStepDefinitionMatch.runStep(PickleStepDefinitionMatch.java:44)
at cucumber.runner.TestStep.executeStep(TestStep.java:63)
at cucumber.runner.TestStep.run(TestStep.java:49)
at cucumber.runner.PickleStepTestStep.run(PickleStepTestStep.java:43)
at cucumber.runner.TestCase.run(TestCase.java:45)
at cucumber.runner.Runner.runPickle(Runner.java:40)
at cucumber.runtime.junit.PickleRunners$NoStepDescriptions.run(PickleRunners.java:146)
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:238)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:63)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:236)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:53)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:229)
at org.junit.runners.ParentRunner.run(ParentRunner.java:309)
at cucumber.runtime.junit.FeatureRunner.run(FeatureRunner.java:73)
at cucumber.api.junit.Cucumber.runChild(Cucumber.java:122)
at cucumber.api.junit.Cucumber.runChild(Cucumber.java:64)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:238)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:63)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:236)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:53)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:229)
at cucumber.api.junit.Cucumber$1.evaluate(Cucumber.java:131)
at org.junit.runners.ParentRunner.run(ParentRunner.java:309)
at org.junit.runner.JUnitCore.run(JUnitCore.java:160)
at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:68)
at com.intellij.rt.execution.junit.IdeaTestRunner$Repeater.startRunnerWithArgs(IdeaTestRunner.java:47)
at com.intellij.rt.execution.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:242)
at com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:70)
Caused by: io.cucumber.datatable.UndefinedDataTableTypeException: Can't convert DataTable to cucumber.api.DataTable.
Please register a DataTableType with a TableTransformer, TableEntryTransformer or TableRowTransformer for cucumber.api.DataTable.
at io.cucumber.datatable.UndefinedDataTableTypeException.singletonNoConverterDefined(UndefinedDataTableTypeException.java:15)
at io.cucumber.datatable.DataTableTypeRegistryTableConverter.toSingleton(DataTableTypeRegistryTableConverter.java:106)
at io.cucumber.datatable.DataTableTypeRegistryTableConverter.convert(DataTableTypeRegistryTableConverter.java:75)
at io.cucumber.datatable.DataTable.convert(DataTable.java:362)
at io.cucumber.stepexpression.StepExpressionFactory$3.transform(StepExpressionFactory.java:73)
at io.cucumber.stepexpression.DataTableArgument.getValue(DataTableArgument.java:19)
at cucumber.runner.PickleStepDefinitionMatch.runStep(PickleStepDefinitionMatch.java:41)
... 29 more
这是我的场景:
Feature: LoginFeature
This deals with logging in
Scenario: Log in with correct username
Given I navigate to the login page
And I enter the following login details:
| username | password |
| cukey | passwoid |
And I click the login button
Then I should land on the newest page
这是我的步骤定义:
package Steps;
import Base.BaseUtil;
import Pages.LoginPageObjeks;
import cucumber.api.DataTable;
import cucumber.api.PendingException;
import cucumber.api.java.en.And;
import cucumber.api.java.en.Given;
import cucumber.api.java.en.Then;
import io.github.bonigarcia.wdm.WebDriverManager;
import org.junit.Assert;
import org.openqa.selenium.By;
import org.openqa.selenium.chrome.ChromeDriver;
import java.util.ArrayList;
import java.util.List;
public class MaStepdefs extends BaseUtil
private BaseUtil base;
public MaStepdefs(BaseUtil base)
this.base = base;
@And("^I click the login button$")
public void iClickTheLoginButton() throws Throwable
LoginPageObjeks page = new LoginPageObjeks(base.Driver);
page.ClickLogin();
@Given("^I navigate to the login page$")
public void iNavigateToTheLoginPage() throws Throwable
base.Driver.navigate().to("http://www.executeautomation.com/demosite/Login.html");
@And("^I enter the following login details:$")
public void iEnterTheFollowingLoginDetails(DataTable table) throws Throwable
List<User> users = new ArrayList<User>();
users = table.asList(User.class);
LoginPageObjeks page = new LoginPageObjeks(base.Driver);
for (User user : users)
page.Login(user.username, user.password);
//base.Driver.findElement(By.name("UserName")).sendKeys(user.username);
//base.Driver.findElement(By.name("Password")).sendKeys(user.password);
Thread.sleep(2000);
@Then("^I should land on the newest page$")
public void iShouldLandOnTheNewestPage () throws Throwable
Assert.assertEquals("It's not displayed", base.Driver.findElement(By.id("Initial")).isDisplayed(), true);
class User
public String username;
public String password;
public User(String userName, String passWord)
username = userName;
password = passWord;
这是我的 pom:
<?xml version="1.0" encoding="UTF-8"?>
<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>groupId</groupId>
<artifactId>ownCukes</artifactId>
<version>1.0-SNAPSHOT</version>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.5.1</version>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.19</version>
</plugin>
</plugins>
</build>
<dependencies>
<!-- https://mvnrepository.com/artifact/io.cucumber/cucumber-java -->
<dependency>
<groupId>io.cucumber</groupId>
<artifactId>cucumber-java</artifactId>
<version>4.2.6</version>
</dependency>
<dependency>
<groupId>org.seleniumhq.selenium</groupId>
<artifactId>selenium-java</artifactId>
<version>3.141.59</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.11</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>io.github.bonigarcia</groupId>
<artifactId>webdrivermanager</artifactId>
<version>3.2.0</version>
<scope>test</scope>
</dependency>
<!-- https://mvnrepository.com/artifact/io.cucumber/cucumber-picocontainer -->
<dependency>
<groupId>io.cucumber</groupId>
<artifactId>cucumber-picocontainer</artifactId>
<version>4.2.6</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>io.cucumber</groupId>
<artifactId>cucumber-junit</artifactId>
<version>4.2.6</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>io.cucumber</groupId>
<artifactId>cucumber-junit</artifactId>
<version>4.2.6</version>
<scope>test</scope>
</dependency>
<!-- https://mvnrepository.com/artifact/io.cucumber/cucumber-core -->
<dependency>
<groupId>info.cukes</groupId>
<artifactId>cucumber-core</artifactId>
<version>1.2.5</version>
<scope>test</scope>
</dependency>
</dependencies>
</project>
这是我的 TestRunner:
package Runner;
import cucumber.api.CucumberOptions;
import org.junit.runner.RunWith;
import cucumber.api.junit.Cucumber;
@RunWith(Cucumber.class)
@CucumberOptions(features = "src/test/java/features", glue = "Steps")
public class TestRunner
我不确定数据表类型是什么或如何注册?我试过搞乱 DataTableType 和 TypeRegistry 但我不是专家。任何帮助将不胜感激,谢谢
【问题讨论】:
数据表导入为“import io.cucumber.datatable.DataTable;”而不是你使用的是来自旧版本的黄瓜,比如版本 2。你应该查看 maven 依赖项并清理它。 刚刚试过这个。给了我一个新的错误——io.cucumber.datatable.UndefinedDataTableTypeException: Can't convert DataTable to List您使用的是过时的版本,现在是:
import io.cucumber.datatable.DataTable;
黄瓜 jvm 见 CHANGELOG,引用:
[核心] 用 io.cucumber.datatable.DataTable 替换 DataTable(#1248 M.P. Korstanje,Björn Rasmusson,Marit van Dijk) 可以通过实现 TypeRegistryConfigurer 来定义自定义数据表类型。
现在的使用方式略有不同,例如:
小黄瓜步骤示例:
And I update ADDRESS tab data in building form
| Input | Value |
| Building name | New name |
| Ref | Some ref |
步骤实施:
@And("^some test step$")
public void someTestStep(DataTable table)
List<List<String>> data = table.asLists(String.class);
String buildingName = data.get(1).get(1);
String reference = data.get(2).get(1);
【讨论】:
好的,谢谢。您能否向我展示一个如何修复上述步骤定义的示例?用于输入登录详细信息? 我尝试了新格式,但它阻止了它下面的“for(用户用户:用户)”行 可能是因为我发布了字符串列表的示例,而不是您在问题中所做的用户。 不,我将其更改为用户,但仍然没有运气 尝试根据从表中检索到的变量“手动”创建用户,就像我一样,作为字符串,并使用这两个检索到的字符串来创建用户,并将其插入用户列表中。如果您在任何地方都使用正确的类型,那么说它破坏“for (User user : users)”是没有意义的。您现在可能想将代码粘贴到 pastebin 上,我会对其进行审核。【参考方案2】: I have created a code which will not use **DataTable** concept. You can update this below implementation so that you will not have any failures in your scripts in future.
CucumberUtil.java:
-----------------
import java.util.HashMap;
import java.util.List;
import java.util.Map;
public class CucumberUtil
// public static synchronized Map<String, String> TableDictionaryConverter(DataTable table) -- removed this concept because of version issues in DataTable
public static synchronized Map<String, String> TableDictionaryConverter(List<List<String>> data)
Map<String, String> mapTable = new HashMap<String, String>();
for(List<String> rows: data)
mapTable.put(rows.get(0), rows.get(1));
return mapTable;
feature:
-------
And I enter the following login details:
| username | cukey |
| password | passwoid |
step defn:
---------
@And("^I enter the following login details:$")
public void iEnterTheFollowingLoginDetails(List<List<String>> table) throws Throwable
Map<String, String> mapTable = CucumberUtil.TableDictionaryConverter(table);
LoginPageObjeks page = new LoginPageObjeks(base.Driver);
page.Login(mapTable.get("username"), mapTable.get("password"));
Thread.sleep(2000);
【讨论】:
请编辑您的答案,以便仅将代码格式化为代码以上是关于Cucumber DataTable 错误 - io.cucumber.datatable.UndefinedDataTableTypeException:无法将 DataTable 转换为 cucu的主要内容,如果未能解决你的问题,请参考以下文章
Cucumber / Capybara 错误:参数 [0] 未定义(Selenium::WebDriver::Error::JavascriptError)
Java:package cucumber.api.junit不存在
删除datatable的行后,出现“不能通过已删除的行访问该行的信息”的错误,即DeletedRowInaccessibleException