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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/41158051/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-03 05:43:21  来源:igfitidea点击:

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

javaseleniumcucumber

提问by Ram

I'm new to Cucumber. I want to test two login scenarios:

我是黄瓜的新手。我想测试两个登录场景:

  1. With valid credentials where user should be able to login successfully
  2. With empty username and password where the user should not be able to login
  1. 使用有效凭据,用户应该能够成功登录
  2. 使用空的用户名和密码,用户应该无法登录

I have the following code for the above scenario:

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

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 |
  |          |          |

In the java file I have the following code:

在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);

}

My question is scenario 2 shows a warning message:

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

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

Does this mean that for every scenario like valid, empty and invalid credentials I need to write a separate java function? Can't I use the same java function:

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

public void I_enter_and_(String username, String password) 

for all the scenarios? If I run the feature file I get this output:

对于所有场景?如果我运行功能文件,我会得到以下输出:

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();
}

Is there any way to reuse the java function I_enter_and_?

有没有办法重用java函数I_enter_and_

采纳答案by Eugene S

First of all, if that's an actual copy/paste of your error, it looks like you have an extra space after I enterin your step. To be sure, just copy the automatically suggested step and use it instead of the current one.

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

Another thing is that tat does not make sense to use Scenario Outlineif you do not provide more than one example. It should look more like:

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

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 |