使用Cucumber获取空字段的“没有匹配的胶水代码”错误

时间:2016-12-15 06:49:21

标签: java selenium cucumber

我是Cucumber的新手。我想测试两种登录方案:

  1. 使用有效凭据,用户应该能够成功登录
  2. 使用空用户名和密码,用户无法登录
  3. 我有以下代码用于上述场景:

    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_

1 个答案:

答案 0 :(得分:0)

首先,如果这是您的错误的实际复制/粘贴,看起来您在步骤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 |