Gherkin
Gherkin is a business readable language which helps you to describe business behavior without going into the details of implementation .it is a language that used for defining tests in Cucumber format. It uses plain language and allows users to remove logic details for behavior tests .
The text in gherkin language acts as documentation and skeleton of your automated tests.
This script serves two primary purposes
1.Document User Scenarios
2.Writing an automated tests(BDD)
Gherkin is a line oriented language. Each line starts with a step and starts with a keyword and ends of the terminal with stop.
The format looks like follows.
1.Feature : Title of the scenario
2.Given [Precondition or Initial Context]
3.When [Event or Trigger]
4.Then [Expected Output]
Important terms used in Gherkin
- Feature
- Scenario
- Given
- When
- Then
- And
- But
Some examples in Gherkin Language
Feature 1 : Login Facebook account
Scenario 1: login with valid credential
Given : I am on the login page
When : enter valid username And I enter valid password
Then : I should be on the user’s homepage.
Scenario 2: login with invalid username
Given I am on the login page
When I enter invalid email and I enter valid password and I press login
Then I see an error message.
Scenario 3: login with invalid password
Given : I am on the login page
When : I enter valid email and I enter invalid password I and press login
Then : I see an error message.
Scenario 4:login with valid phone number
Given : I am on the login page
When : I enter a valid phone number registered with my account and I enter valid password and I press login
Then : I should be on the user’s homepage.
Scenario 5:login with invalid phone number
Given : I am on the login page
When : I enter a invalid phone number and I enter a valid password and I press login
Then : I should see an error message .
Feature2: Buy Now Button on an online shopping site.
The user should be able to add one or more products to their online shopping cart by clicking on a button provided the user in on a valid product page and there are enough products in stock. The button should add the selected number of items in the cart.
Scenario 1 :User wants to add a product to his cart
Given : the user is on the product page And the product is in stock
When :he clicks the buy now button
Then : one of the products is added to the cart.
Scenario 2: User Wants to add two products to the cart
Given : the user is on product page and there are two or more products in stock
When : the user selects two products and clicks on the buy now button
Then : two products should be added to the cart.
Scenario 3 : user wants to add more than two products to the cart.
Given : the user is on the product page and there are no more then two products in the stock
When : the user selects three products and clicks on the buy now button
Then : a pop up appears warning that there are only two products in stock
Feature 3 : Making payment online for shopping.
User is shopping stuff online and making payment with a credit card.
Scenario 1 :Valid Credit card number with valid address
Given : the user has items added on to his shopping cart and items are in stock
When: the user adds valid credit card number to make payment online and valid address associated with the credit card
Then : the payment should be made successfully.
Scenario 2 : Valid Credit Card number but invalid address
Given : the user has items added onto his shopping cart and the items are available in stock
When: the user enters valid credit card number but invalid address
Then: the payment fails and error message pops up .
Originally published at https://www.numpyninja.com on April 7, 2021.