Hi Guys,
Take a look at this .feature
file.
Feature: Verify successful user login As a valid user I go to website and Login Scenario: Login by entering username and password Given I am on website Then I enter username with emp code Then I enter password When I hit enter Then I successfully logged in
In this .feature
file only one user can be verified in the test. What if you want to run test with multiple test users or you have different role types.
For that there are two ways you can do that, let’s see in steps below.
When you are done your
First: Using “Scenario Outline:” with “Examples” table
- Here we need to use “Scenario Outline:”
- We need to write a table at the end of
.feature
file and name it as “Examples:” - Whatever data we want to fetch from table we need to put in this format in our
.feature
file.- when we want to pass strings “”, and then inside quotes we need give opening and closing tags, that will tell cucumber to take that as table column headers
- and if we want to pass numbers then we will simply use tags without quotes
![dataDrivenFeature](https://mintqa.com/wp-content/uploads/2023/04/dataDrivenFeature.gif)
.feature
file should look like this. All steps will run in a loop for all the rows present in the Examples table.
Feature: Verify successful user login As a valid user I go to website and Login Scenario Outline: Login by entering username and password Given I am on website Then I enter username as "<username>" with emp code <empCode> Then I enter password as "<password>" When I hit enter Then I successfully logged in Examples: | username | empCode | password | | admin | 123 | admin123 | | owner | 321 | owner123 |
Second: Using “Scenario:” use of inline tables
Inline table means a data present in form of table for that particular step. Test will not loop for the rows of this table. It only provides data. However you can loop using for loop, to extract all data from that inline table. But as soon as you come out of this step this data will not be available for next step. For such scenarios when data is specifically required for particular step, we use “Scenario:” and not “Scenario Outline:” So let’s imagine that password is same as “admin123!” for all users, and we have inline table only for single step.- Here we wouldn’t have the “Examples:” table at the end of
.feature
file - This time we know that table is required for particular step i.e. it doesn’t contain the data for other steps of
.feature
file. - So, just provide the table below the step where it is needed.
And now new
.feature
file would look like this:Scenario: Login by entering username and password Given I am on website Then I enter username with emp code empCode | admin | 123 | | owner | 321 | Then I enter password as "admin123" When I hit enter Then I successfully logged in
Run the above feature file with the below step definition.
Also, you can get this piece of code from here
Download
package org.example.steps; import io.cucumber.datatable.DataTable; import io.cucumber.java.en.Given; import io.cucumber.java.en.Then; import io.cucumber.java.en.When; import java.util.List; public class DataDrivenTestsSteps { @Given("I am on website") public void i_am_on_website() { } @Then("I enter username as {string} with emp code {int}") public void iEnterUsername(String username, int num) { System.out.println("Employee with emp code \"" + num + "\" has username as \"" + username + "\""); } @Then("I enter password as {string}") public void iEnterPassword(String password) { System.out.println("Password is " + password); } @When("I hit enter") public void iHitEnterReturnKey() { } @Then("I successfully logged in") public void iSuccessfullyLoggedIn() { } @Then("I enter username with emp code empCode") public void iEnterUsernameWithEmpCodeEmpCode(DataTable userData) { List<List<String>> listData = userData.asLists(String.class); String username = userData.cell(0, 0); int num = Integer.parseInt(userData.cell(0, 1)); // Without loop on the data System.out.println("Without loop: Employee with emp code >> \"" + num + "\" has username as >> \"" + username + "\""); // Print all values using For loop for (int i = 0; i < listData.size(); i++) { username = userData.cell(i, 0); num = Integer.parseInt(userData.cell(i, 1)); System.out.println("On loop: Employee with emp code >> \"" + num + "\" has username as >> \"" + username + "\""); } } }
Third way is
You can simple use quotes and then read that as Strings in your feature file.
Scenario: Three - Login by entering username and password Given I am on website Then I enter username "admin" with emp code 321 Then I enter password as "admin123" When I hit enter Then I successfully logged in
@Then("I enter username {string} with emp code {int}")
public void iEnterUsernameWithEmpCode(String str, int empCode) {
}