Testing beyond limits!

Search This Blog

Wednesday, September 27, 2017

Gherkin Keywords : Keywords used in Gherkin language (For Cucumber)

Keywords in Gherkin Language majorly includes: Feature, Background, Scenario, Given, When, Then, And, But, *

It's not necessary to write just automated tests using Gherkin. Gherkin can also be used to create project documentation in a structured manner. This structured format gives us the ability to automate them easily.

Sample .feature file:

Feature: Test Login Functionality for Amazon.com
Scenario: Login using valid username and password.
Given User is on Amazon.com
When User navigates to Login window.
And User enters valid usename and password combination
Then User should be able to Login successfully on Amazon.com website successfully.

Once we have installed Cucumber plugin in Eclipse and start writing our feature files in Cucumber editor, we notice that few words get colored. These words are the keywords namely,
  • Feature
  • Background
  • Scenario
  • Given
  • When
  • Then
  • And
  • But
  • *

Let's discuss these keywords in detail:

1. Feature: Feature keyword is the very first word in every feature file. It indicates what feature is being referred to in entire feature file. 

For Example: 
Feature: Test Login Functionality for Amazon.com

2. Background: Background keyword is used to define all common steps to be executed for every scenario. It helps us reduce redundancy of common steps for all scenarios. Background keyword can be used in Gherkin as in example below:

For Example: 
Feature: Test Login Functionality for Amazon.com
Background: Given User is on Amazon.com
And User navigates to Login window.

Scenario: Login using valid username and password.
When User enters valid username and password combination
Then User should be able to Login on Amazon.com website successfully.

Scenario: Login using Invalid username and password.
When User enters Invalid username and password combination
Then User should be not able to Login on Amazon.com website.

3. Scenario: Scenarios are various scenarios that can exist against above feature. 
For Example: Login using Valid credentials, Login using Invalid credentials, Login using blank/null credentials etc.

For Example: 
Feature: Test Login Functionality for Amazon.com
Scenario: Login using valid username and password.
When User navigates to Login window.
And User enters valid username and password combination
Then User should be able to Login successfully on Amazon.com website successfully.

Each scenario can be broken down to Precondition (using Given keyword), Test step (using When keyword), Verification (using Then keyword)

4. Given Keyword: It is used to set the precondition to the test Scenario.

For Example:
Scenario: Login using valid username and password.
Given User is on Amazon.com

5. When Keyword: It is used to set the test steps to the test Scenario.

For Example:
Scenario: Login using valid username and password.
Given User is on Amazon.com
When User navigates to Login window.

6. Then Keyword: It is used to set the verification to the test Scenario.

For Example:
Scenario: Login using valid username and password.
Given User is on Amazon.com
When User navigates to Login window.
And User enters valid usename and password combination
Then User should be able to Login successfully on Amazon.com website successfully.

7. And Keyword: It is used to Add conditions to the test steps.

8. But Keyword: It is used to Add Negative conditions to the test steps.

For Example:
Scenario: Login using valid username and password.
Given User is on Amazon.com
When User navigates to Login window.
And User enters username and password combination
But Credentials are wrong
Then User should be not able to on Amazon.com website.

9. * Keyword: * keyword holds the veto power and can be replaced with any of the keywords. Cucumber care about what code it needs to execute for each step irrespective of the keyword used. For example below mentioned example would execute perfectly fine as long as the code is glued to every step:

For Example:
Scenario: Login using valid username and password.
* User is on Amazon.com
* User navigates to Login window.
* User enters usename and password combination
* Credentials are wrong
* User should be not able to on Amazon.com website.

That's all about the keywords used in Gherkin (for Cucumber). Happy Testing!