【问题标题】:Getting "Does not have a matching glue code" error for empty fields using Cucumber使用 Cucumber 获取空字段的“没有匹配的胶水代码”错误
【发布时间】:2016-12-15 06:49:21
【问题描述】:

我是 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_

【问题讨论】:

  • 你用什么类来运行?

标签: java selenium cucumber


【解决方案1】:

首先,如果这是您的错误的实际复制/粘贴,那么您的步骤中I enter 之后似乎有一个额外的空间。可以肯定的是,只需复制自动建议的步骤并使用它而不是当前的步骤。

另一件事是,如果您不提供多个示例,则使用 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 |

【讨论】:

  • 谢谢。我不知道,你可以使用 may be 并给出多个输出。我有很多示例,但为了简单起见,我删除了这些示例。
  • @Ram 有不同的方法可以使功能文件更具可读性和有用性。也许你应该通过一些教程。顺便说一句,添加空间是否解决了您的问题?
【解决方案2】:

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-11-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-07-07
    相关资源
    最近更新 更多