1

One of the steps in my feature file requires two parameters like login and password for multiple systems which individually works fine but Is there any way I can pass multiple examples for individual steps? The first step in the feature file is calling POST API and getting a response based on parameters. Scenario Outline: Verify search results Given I set the authorization token in Header with below feature file body payload

| Key       | Value           | 
|InstituteID| <InstitutionID> |         
|InstituteID|
|1234456    |
|1345679    |
|4564565    |

and second step is:

And User enters "<Username>", "<Password>" and click on Login button

|Username|Password |
|test    |abc      |
|test2   |abc      | 

Could you please share if there is any way to achieve this? Can I pass multiple examples in one scenario outline like mentioned above? I need a few responses from step 1 to execute step 2 so I can not break into two scenarios. Thank you.

Sourabh Choure
  • 723
  • 4
  • 15
TestUser12
  • 11
  • 1
  • 1
  • 2

2 Answers2

1

Yes, you can pass multiple examples. I'll give you an example:

Scenario Outline: I want to login
    Given I am on the login page
    Then I log in with "<Username>" and "<Password>"
    
    Examples:
        |  Username  |  Password  |
        |  User1     |  12345678  |
        |  User2     |  12345679  |
        |  User3     |  12345670  |

Now, the above scenario will run 3 times, for each of these examples.

In the step file, you can create a function like this:

@Given("^I am on the login page$")
public void navigate_to_login_page() {
    //some logic
}

@Then("^I login with "([^\"]*)\" and "([^\"]*)\"$")
public void login(String username, String pass) {
    //some logic
}
    
Dharman
  • 30,962
  • 25
  • 85
  • 135
Verma Aman
  • 149
  • 14
0

You can't write Examples for individual steps, only for whole Scenario Outlines.

But you can write as much examples as you want and mix your parameters in them

Examples:
|Username|Password|InstituteID| 
|test    |abc      |1234456    |
|test    |abc      |1345679    |
|test    |abc      |4564565    |
|test2   |abc      |1234456    |
|test2   |abc      |1345679    | 
|test2   |abc      |4564565    | 

Edit 1:

А DataTable may help you

Define the glue code like this, mind the method parameter DataTable:

    @When("A POST API is called with parameters:")
    public void callPostApi(DataTable table) {
        List<List<String>> rows = table.cells(1); // skip the header
        for (List<String> row : rows) {
           String parameter = row.get(0);
           // Call the POST API for the parameter here, save, check the results etc.
        }
    }

And use it in the .feature file

  When A POST API is called with parameters:
  | Parameter | 
  | 123       |
  | 456       |
  | 789       |
  | abc       |
  | def       |
Vitaly Roslov
  • 313
  • 1
  • 8
  • this won't work in my case. Since I want to execute first step with InstituteID to execute 5 times for each Institute and login only once for each Institute to check the records. – TestUser12 Aug 10 '20 at 19:20
  • Then the DataTable will likely suit your usecase better than Scenario Outline with Examples – Vitaly Roslov Aug 11 '20 at 07:45