What is the Example keyword in Cucumber?


We can perform data-driven testing with the help of keyword Examples. We shall also take the help of keyword Scenario Outline to execute the same Scenario over multiple values.

The data sets to be taken into consideration shall be passed below the Examples section one after another separated by | symbol. So, if there are three rows, we shall have three test cases executed from a Single scenario.

Also, the Given step has the <> delimiter. It points to the header of the Examples table. SpecFlow shall put the values within this table prior to the task of matching a step with a step definition.

To verify a Login module, we require the below steps to be executed −

  • User types the username and password.
  • Verify users should be able to log in.

We shall incorporate the above steps into the Feature File.

Feature File

Feature: User credential

Scenario Outline: Login module

Given user types <username> and <password>

Then user should be able to login

Examples

| username      | password |

| tutorialspoint1| pwd         |

| tutorialspoint2| pwd1       |

Example

Step Definition File

using System;
using TechTalk.SpecFlow;
namespace SpecFlowProject1.Features {
   [Binding]
   public class UserCredentialSteps    {
      //regular expression used to point to data
      [Given(@"user types (.*) and (.*)")]
      public void GivenUserTypesUserAndPwds(string username, string password) {
         Console.WriteLine(username);
         Console.WriteLine(password);
      }
      [Then(@"user should be able to login")]
      public void ThenUserShouldBeAbleToLogin() {
         Console.WriteLine("User should be able to login");
      }
   }
}

Output

Updated on: 18-Nov-2021

4K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements