尝试将自定义对象参数传递给黄瓜步骤定义时,总是出现“无法为步骤转换参数”错误?

时间:2018-06-20 03:10:31

标签: automation cucumber

我的功能文件如下:

Given User is on Home Page
    When User Navigate to LogIn Page
    And User enters Credentials to LogIn
    | Username   | Password |
    | testuser_1 | Test@153 |
    | testuser_2 | Test@154 |
    Then Message displayed Login Successfully

步骤定义为:

public void user_enters_testuser_and_Test(List<Credentials>  usercredentials) throws Throwable {

        //Write the code to handle Data Table
        for (Credentials credentials : usercredentials) {           
            driver.findElement(By.id("log")).sendKeys(credentials.getUsername()); 
            driver.findElement(By.id("pwd")).sendKeys(credentials.getPassword());
            driver.findElement(By.id("login")).click();
            }       
    }

这是自定义的Object参数:

public class Credentials {
    private String username;
    private String password;

    public String getUsername() {
        return username;
    }
    public String getPassword() {
        return password;
    }   
}

运行代码时,我总是会收到此错误:

cucumber.runtime.CucumberException: Could not convert arguments for step [^User enters Credentials to LogIn$] defined at 'stepDefinitions.ConsumerServiceEnquiry.ConsumerSteps.user_enters_testuser_and_Test(Credentials>) in file:/C:/Users/jxz36/eclipse-workspace/SDBCucumberTestng/bin/'.

我到处搜索,看到很多类似的例子。所以我认为这一定是一些基本错误或缺少配置。请有人可以帮助我吗?非常感谢!

1 个答案:

答案 0 :(得分:0)

我认为您的黄瓜等级为3以上。如果是这样,请参阅-Cucumber-JVM - io.cucumber.datatable.UndefinedDataTableTypeException

将现有转换器替换为此。

registry.defineDataTableType(new DataTableType(Credentials.class, new TableEntryTransformer<Credentials>() {
            @Override
            public Credentials transform(Map<String, String> entry) {
                return new Credentials(entry.get("Username"),entry.get("Password"));
            }
        }));

还将构造函数添加到Credentials

 public Credentials() {
    }
    public Credentials(String username, String password) {
        this.username = username;
        this.password = password;

}