Search code examples
javaseleniumcucumber

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


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

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:

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

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:

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


Solution

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

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

    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 |