16

I have a Feature file which is as below:

Scenario Outline: Create ABC

  Given I open the application

  When I enter username as <username>

  And I enter password as <password>

  Then I enter title as <title>

  And press submit


Examples:

| username | password | title |

| Rob      | xyz1      | title1 |

| Bob      | xyz1      | title2 |

This mandates me to have step definitions for each of these values. Can i instead have a

generic step definition that can be mapped for every username or password or title values in

the examples section.

i.e instead of saying

@When("^I enter username as Rob$")
public void I_enter_username_as_Rob() throws Throwable {
    // Express the Regexp above with the code you wish you had
    throw new PendingException();
}

can i enter

@When("^I enter username as <username>$")
public void I_enter_username_as_username(<something to use the value passed>) throws Throwable {
    // Express the Regexp above with the code you wish you had
    throw new PendingException();
}
trial999
  • 1,646
  • 6
  • 21
  • 36
  • how can this happen? It must be some old cucumber plugin, nowdays outline is well supported, and only produce a single generic step function in the case of . No need to change to "" – user1559625 Nov 28 '15 at 22:26
  • Still happening on CucumberJVM 1.2.5 when executed via JUnit. So use the quotes. :-) – Lance Kind May 22 '17 at 07:33

2 Answers2

35

You should use this format

Scenario Outline: Create ABC

    Given I open the application
    When I enter username as "<username>"
    And I enter password as "<password>"
    Then I enter title as "<title>"
    And press submit

Which would produce

@When("^I enter username as \"([^\"]*)\"$")
public void I_enter_username_as(String arg1) throws Throwable {
    // Express the Regexp above with the code you wish you had
    throw new PendingException();
}

arg1 will now have your username/value passed.

Bala
  • 11,068
  • 19
  • 67
  • 120
  • 3
    Thank you! I also want to explain that \"([^\"]*)\" is a regex and it looks for a string starting with quotation mark " and ending with undefined number of other characters(asterisk * provides that). So if we look at "" we can see that it starts with quotation mark ". Hope that helps someone. – hipokito Apr 28 '15 at 22:13
  • 2
    as clarified by @hipokito, as an **alternative** so will `@When("^I enter username as \"(.+)\"$")` – DaddyMoe Sep 21 '15 at 12:50
2

Cucumber would give the missing steps in console automatically. Just make a dry run and missing steps would be shown in console.

@RunWith(Cucumber.class)
@CucumberOptions(plugin = { "pretty" }, features = { "<path_to_feature>" }, 
    glue = { "<optional_steps_location_java_file>" }, dryRun = true,
    tags = { "<optional_NOT_req_for_now>" })
public class RunMyCucumberTest {

}

See for more Cucumber options

Mandy
  • 163
  • 1
  • 9