使用 Cucumber 获取空字段的 "Does not have a matching glue code" 错误

Getting "Does not have a matching glue code" error for empty fields using Cucumber

我是 Cucumber 新手。我想测试两种登录场景:

  1. 凭有效凭据,用户应该能够成功登录
  2. 用户名和密码为空,用户应该无法登录

对于上述场景,我有以下代码:

Scenario Outline: Test login with valid credentials
Given Open firefox and start application
When I enter "<username>" and "<password>"
Then User should be able to login successfully

Examples: 
  | username | password |
  | ro       | mech     |

Scenario Outline: Test login with empty credentials
Given Open firefox and start application
When I enter  "<username>" and "<password>" (this shows a warning)
Then User should be not able to login 

Examples: 
  | username | password |
  |          |          |

在 java 文件中,我有以下代码:

@When("^I enter \"([^\"]*)\" and \"([^\"]*)\"$")
public void I_enter_and_(String username, String password) throws Throwable {
    driver.findElement(By.id("ember629")).sendKeys(username);
    driver.findElement(By.id("ember640")).sendKeys(password);

}

我的问题是场景 2 显示一条警告消息:

Step 'I enter  "<username>" and "<password>"' does not have a matching glue code

这是否意味着对于有效、空和无效凭据等每种情况,我都需要编写一个单独的 java 函数?我不能使用相同的 java 函数吗:

public void I_enter_and_(String username, String password) 

所有场景?如果我 运行 特征文件我得到这个输出:

You can implement missing steps with the snippets below:

@When("^I enter  \"([^\"]*)\" and \"([^\"]*)\"$")
public void i_enter_and(String arg1, String arg2) throws Throwable {
    // Write code here that turns the phrase above into concrete actions
    throw new PendingException();
}

有没有办法重用java函数I_enter_and_

首先,如果这是您的实际 copy/paste 错误,您的步骤中 I enter 之后似乎有一个额外的 space。可以肯定的是,只需复制自动建议的步骤并使用它来代替当前步骤。

另一件事是,如果您不提供多个示例,那么使用 Scenario Outline 就没有意义。它应该看起来更像:

Scenario Outline: Test login with valid credentials
  Given Open firefox and start application
  When I enter "<username>" and "<password>"
  Then User "<maybe>" be able to login successfully

  Examples: 
    | username | password | maybe     | 
    | ro       | mech     | shall     | 
    |          |          | shall not |

胶水路径将是正确的并与 stepdef 路径匹配如下: