Gherkinize Those Behaviors!

Genius is the ability to reduce the complicated to the simple — C. W. Ceram

Many a time, we make things unnecessarily complex when we can actually think, break it down and make it simple for ourselves as well as for others.

Having decided to make test automation a priority, you plan to use BDD (Behavior-Driven Development), selecting a good language for test automation, keeping in mind a BDD tool and when you are ready to write your first Gherkin feature file, you start thinking “How am I supposed to write?”

But before we look into what good Gherkin guidelines can be followed when you write a Gherkin script, let us see what Gherkin actually is:

Gherkin is a simple, easy to read, plain text language used to verbalize BDD. It is a language formalized to make the artifacts (requirements/acceptance criteria/tests) understandable to all people — business / stakeholders/ technical/ non-technical. High level notes are provided by Product Owners which “ the three Amigos/ the Triad (business analysts, developers and testers) would collaboratively use to write feature files that give a desired product functionality involving multiple behaviors. These features are further divided into scenarios each describing one single behavior.

Gherkin scripts are written inside BDD tools (like Cucumber) using the Gherkin syntax also known as, G-W-T (Given-When-Then) syntax which allows testers and developers to continue working on the implementation of BDD. The main keywords used in Gherkin are:

Feature, Scenario, Given, When, Then, And, But

Image by David Travis from Unsplash

Just knowing the Gherkin format is not enough but using the format in the right way is important to complete the task. Good Gherkin feature files are not easy to write at first. The tough part here is thinking about all the scenarios and finally accomplishing 100% test case coverage. But with some basic pointers, and a bit of practice, Gherkin becomes easier. Let’s see some good practices to keep in mind while writing a Gherkin script.

  • The Ideal Gherkin Rule: “ Wear the hat of the user . Write Gherkin so that people who don’t even know the feature will understand it.
  • Use one feature per system functionality, making sure the feature is specific to a single functionality in particular and is as independent as possible from other functionalities.
  • The key to write a good Gherkin script is: One scenario should cover exactly one single, independent behavior to establish acceptance criteria.
  • It is suggested to write the positive scenarios first.
  • Select good titles which communicate in one concise line what the behavior is.
  • Limit the number of scenarios per feature.
  • Separate the behaviors by 3C’s: Comprehend — Communicate — Cover
  • Write behavior scenarios in a form which is readable and expressive; steps in a form that can be reusable.
  • Use as few as possible preconditions while still making the tests useful depending on the audience for your scenarios.
  • If your scenario is exceeding 5 steps, try to split it into two different scenarios — break it down to as modular as possible; even if it means to write several scenarios to make your test more complete.
  • Avoid coupling scenarios and make it as independent as possible
  • The less you have to read in each scenario, the easier to understand.
  • In a scenario, try limiting to one ‘G-W-T’ (in the exact order) step with maximum usage of two ‘And’ steps.
  • Focus on critical things and avoid duplicates.
  • Try grouping the features of a file related to one aspect of the application, thus making it better organized and easier to locate.
  • Writing scripts in third person (“the user”) perspective instead of a first person (“I, me, my”) is preferable, though there is no hard and fast rule -Consistent point of view, proper phrasing can favor clarity of what is written.
  • Write steps as a subject-predicate action phrase — always use present or present perfect tense for “Given” and present tense for “When”, “Then”.
  • The ‘But’ statement works the same as ‘Then,’ except that it is used when we want to verify that no concrete result is observed.
  • Capitalize Gherkin keywords; capitalize the first word in titles.
  • Remember Gherkin does not have an “Or” step; some programmers misinterpret Gherkin as a structured language.

Advantages:

  • More focus and less confusion
  • More visibility into team progress
  • Improved team collaboration by clarifying behaviors you want to develop
  • Each test failure points to a unique problem
  • Increased efficiency as reduced complex work and faster cycle times
  • Maintainable to reduce the costs of making changes in the test steps
  • Easy to rewrite scenarios as the products and features change

Below is an example where all the above mentioned good practices have been applied:

Feature 1: Using a social media platform 
In order to use Facebook
As a user
I want a secure login
Scenario 1: User clicks on “Login” button
Given I am on the Facebook log in page
When I enter valid credentials
And click on the ‘Login’ button
Then I am logged in
Scenario 2: User presses “Enter” key
Given I am on the Facebook log in page
When I enter valid credentials
And press Enter
Then I am logged in
Scenario 3: User enters invalid credentials
Given I am on the Facebook log in page
When I enter invalid credentials
Then I am not logged in
Scenario 4: Error message when invalid user name entered
Given I am on the Facebook log in page
When I enter invalid username
Then display error message "The email or mobile number you entered isn’t connected to an account."
Scenario 5:"Find your account and log in" link when invalid username entered
Given I am on the Facebook log in page
When I enter invalid user name
Then display the "Find your account and log in" link
Scenario 6: Error message when unregistered phone number entered
Given I am on the Facebook log in page
When I enter invalid phone number
Then display error message "The email or mobile number you entered isn’t connected to an account."
Scenario 7:"Create a new Facebook account" link when unregistered phone number entered
Given I am on the Facebook log in page
When I enter invalid phone number
Then display the "Create a new Facebook account" link
Scenario 8: Error message when wrong password entered
Given I am on the Facebook log in page
When I enter invalid password
Then display error message "The password you’ve entered is incorrect."
Scenario 9: Forgot password link when wrong password entered
Given I am on the Facebook log in page
When I enter invalid password
Then display the forgot password link
Scenario 10: Open Reset password page
Given I am on the Facebook log in page
When I enter valid user name
But invalid password
And click Forgot password link
Then open Reset password page
Feature 2: Using a social media platform
In order to use Facebook
As a user
I want to edit my profile data
Scenario 1: Add/Edit account info
Given I have logged in successfully
When I click my profile
And click edit profile
Then open Edit profile page
Scenario 2: Edit Profile Pic
Given I am on Edit profile page
When I click Edit button for profile picture
Then open Update profile picture modal window
Scenario 3: Edit Cover Photo
Given I am on Edit profile page
When I click Edit button for cover photo
Then open Update cover photo modal window with select photo and upload photo links
Scenario 4: Add bio
Given I am on Edit profile page
When I click Add button for bio
Then show text box with “Describe who you are message”
Scenario 5: Customize your intro
Given I am on Edit profile page
When I click Add button for Customize your intro
Then open Edit details modal window
Scenario 6: Add hobbies
Given I am on Edit profile page
When I click Add button for hobbies
Then open Add hobbies modal window
Scenario 7: Add Featured pics
Given I am on Edit profile page
When I click Add button for Featured
Then open Edit Featured modal window
Scenario 8: Edit About info
Given I am on Edit profile page
When I click Edit your about info
Then launch profile page with about tab enabled
Scenario 9: Upload profile pic from the computer
Given I am on Update profile picture modal window
When I click Upload photo button
Then open dialog to browse file from computer
Scenario 10: Upload profile pic from mobile
Given I am on Update profile picture modal window
When I click Upload photo button
Then open dialog to browse file from mobile
Scenario 11: Upload profile pic from photos present on account’s photo section
Given I am on Update profile picture modal window
When I select photos already available from various categories of the photo section
And select one photo
Then open Update Profile Picture modal window
Scenario 12: Update cover photo from the computer
Given I am on Update cover photo modal window
When I click Upload photo button
Then open dialog to browse file from computer
Scenario 13: Update cover photo from the mobile
Given I am on Update cover photo modal window
When I click Upload photo button
Then open dialog to browse file from mobile
Scenario 14: Update cover photo from photos present on account’s photo section
Given I am on Update cover photo modal window
When I click Select photo button
Then open Select modal window with recent photos and photo albums tab
Feature 3: Using a social media platform
In order to use Facebook
As a user
I want to share on timeline
Scenario 1: Post on timeline
Given I am on profile page
When I click on “What’s on your mind?” field
Then open Create Post modal window with “What’s on your mind?” field
Scenario 2: Add to story
Given I am on profile page
When I click on “Add to story” button
Then open your story page with option to create a photo story or a text story
Scenario 3: Post text on timeline with public view
Given I am on Create Post modal window
When I type on “What’s on your mind, user name?” field
And audience dropdown remains public
And press Post button
Then text is posted in my timeline and the same displayed to public
Scenario 4: Post photo/video on timeline with public view
Given I am on Create Post modal window
When I click on add to your post photo/ video icon
And audience dropdown remains public
And press Post button
Then photo/ video is posted in my timeline and the same displayed to public
Scenario 5: Post text on timeline only friends view
Given I am on Create Post modal window
When I type on “What’s on your mind, user name?” field
And audience dropdown selected as friends
And press Post button
Then text is posted in my timeline and the same displayed to my friends
Scenario 6: Post photo/video on timeline only friends view
Given I am on Create Post modal window
When I click on add to your post photo/ video icon
And audience dropdown selected as friends
And press Post button
Then photo/ video is posted in my timeline and the same displayed to my friends
Scenario 7: Post text on timeline only me
Given I am on Create Post modal window
When I type on “What’s on your mind, user name?” field
And audience dropdown selected as me radio button
And press Post button
Then text is posted in my timeline only
Scenario 8: Post photo/video on timeline only me
Given I am on Create Post modal window
When I click on add to your post photo/ video icon
And audience dropdown selected as me radio button
And press Post button
Then photo/ video is posted in my timeline only
Scenario 9: Post text on timeline and tag friend(s)
Given I am on Create Post modal window
When I type on “What’s on your mind, user name?” field
And click on tag a friend icon
And press Post button
Then text is posted in my timeline with the respective friend(s) tagged
Scenario 10: View all posts in my timeline
Given I am on home page
When I click on my profile
Then I can view posts in list/ grid view
Scenario 11: View all comments/like/reactions in posts in my timeline
Given I am on home page
When I click on my profile
Then I can view all comments/like/reactions in posts in my timeline

Practicing this Gherkin syntax not only does expedite automation but also helps focus specifically on the expected behavior of the product under development via scenarios, resulting in less ambiguity and a good overall solution for what to develop. Thus Gherkin helps the solution to be:

Clear. Concise. Complete.

Hope you enjoyed reading!

Originally published at https://www.numpyninja.com on April 8, 2021.