Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Cucumber scenario outline and examples with generic step definitions

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();
}
like image 776
trial999 Avatar asked Mar 25 '14 16:03

trial999


People also ask

What is the purpose of scenario outline and examples keyword in Cucumber?

Cucumber supports Data Driven Testing using Scenario Outline and Examples keywords. Creating a feature file with Scenario Outline and Example keywords will help to reduce the code and testing multiple scenarios with different values.

What is the difference between scenario and scenario outline in Cucumber?

Scenario outline is similar to scenario structure; the only difference is the provision of multiple inputs. As you can see in the following example, the test case remains the same and non-repeatable. At the bottom we have provided multiple input values for the variables “Username” and “Password”.


2 Answers

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.

like image 167
Bala Avatar answered Sep 28 '22 04:09

Bala


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

like image 36
Mandy Avatar answered Sep 28 '22 05:09

Mandy