简体   繁体   中英

How to pass multiple examples with different number of parameters in Cucumber feature file

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.

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
}
    

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       |

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM