This documentation provides a comprehensive guide to using the Gherkin language template for web automation. Gherkin is a business-readable, domain-specific language that allows you to describe software's behaviour without detailing how that functionality is implemented. This makes it easier to communicate requirements and test scenarios between technical and non-technical stakeholders.
Gherkin Language Basics
Gherkin syntax uses a set of special keywords to define the structure of tests. The main keywords are:
Feature: Describes the feature under test.
Scenario: Defines a particular scenario to be tested.
Given: Sets up the initial context of the scenario.
When: Describes the action that triggers the scenario.
Then: Specifies the expected outcome of the scenario.
And: Used to combine multiple Given, When, or Then steps.
Template Overview
This template helps you to structure your web automation test cases using Gherkin. The template maps web automation actions and validations into Gherkin steps, ensuring clarity and consistency.
Key Components
PAGE_NAME: The name of the page or context.
WAIT_TYPE: The type of wait condition (e.g., visibility, presence).
SCROLL_TYPE: The type of scroll action (e.g., none, to element).
ELEMENT_NAME: The name or identifier of the web element.
ACTIONS: The action to be performed (e.g., click, send keys).
SEND_KEYS: The text or keys to be sent to an element.
VALIDATION: The type of validation (e.g., text, attribute).
VALIDATION_TYPE: The validation method or expected outcome.
Gherkin Template Structure
General Structure
Feature: <Feature Name>
<Description of the feature>
Scenario: <Scenario Name>
Given the user is on PAGE_NAME: "<PAGE_NAME>"
When the user waits for WAIT_TYPE: "<WAIT_TYPE>" and scrolls using SCROLL_TYPE: "<SCROLL_TYPE>" on ELEMENT_NAME: "<ELEMENT_NAME>" to perform ACTIONS: "<ACTIONS>" with data SEND_KEYS: "<SEND_KEYS>"
Then the user waits for WAIT_TYPE: "<WAIT_TYPE>" and scrolls using SCROLL_TYPE: "<SCROLL_TYPE>" on ELEMENT_NAME: "<ELEMENT_NAME>" to validate VALIDATION: "<VALIDATION>" with VALIDATION_TYPE: "<VALIDATION_TYPE>"
Then the user waits for WAIT_TYPE: "<WAIT_TYPE>" and scrolls using SCROLL_TYPE: "<SCROLL_TYPE>" on ELEMENT_NAME: "<ELEMENT_NAME>" and verifies VALIDATION_TYPE: "<VALIDATION_TYPE>"
Then the user scrolls using SCROLL_TYPE: "<SCROLL_TYPE>" on ELEMENT_NAME: "<ELEMENT_NAME>" and verifies VALIDATION_TYPE: "<VALIDATION_TYPE>"
When the user scrolls using SCROLL_TYPE: "<SCROLL_TYPE>" on ELEMENT_NAME: "<ELEMENT_NAME>" to perform ACTIONS: "<ACTIONS>" with data SEND_KEYS: "<SEND_KEYS>"
When the user waits for WAIT_TYPE: "<WAIT_TYPE>" on ELEMENT_NAME: "<ELEMENT_NAME>" to perform ACTIONS: "<ACTIONS>" with data SEND_KEYS: "<SEND_KEYS>"
When the user interacts with ELEMENT_NAME: "<ELEMENT_NAME>" to perform ACTIONS: "<ACTIONS>" with data SEND_KEYS: "<SEND_KEYS>"
Then the user verifies ELEMENT_NAME: "<ELEMENT_NAME>" for VALIDATION_TYPE: "<VALIDATION_TYPE>"
Then the user scrolls using SCROLL_TYPE: "<SCROLL_TYPE>" on ELEMENT_NAME: "<ELEMENT_NAME>" to validate VALIDATION: "<VALIDATION>" with VALIDATION_TYPE: "<VALIDATION_TYPE>"
Then the user interacts with ELEMENT_NAME: "<ELEMENT_NAME>" to validate VALIDATION: "<VALIDATION>" with VALIDATION_TYPE: "<VALIDATION_TYPE>"
Handling Multiple Given Steps with PAGE_NAME
In some scenarios, the PAGE_NAME needs to be persistent across multiple steps until a new PAGE_NAME is specified. The following example demonstrates how to handle multiple Given steps and ensure the PAGE_NAME persists:
Example with Persistent PAGE_NAME
Feature: Multi-Page Workflow
This feature describes a workflow involving multiple pages.
Scenario: Navigate through multiple pages
Given the user is on PAGE_NAME: "LoginPage"
When the user waits for WAIT_TYPE: "NONE" and scrolls using SCROLL_TYPE: "NONE" on ELEMENT_NAME: "NONE" to perform ACTIONS: "LOAD_URL" with data SEND_KEYS: "https://www.loginpage.com/"
Then the user waits for WAIT_TYPE: "VISIBILITY" and scrolls using SCROLL_TYPE: "NONE" on ELEMENT_NAME: "username" to perform ACTIONS: "SEND_KEYS" with data SEND_KEYS: "testuser"
Given the user is on PAGE_NAME: "HomePage"
When the user waits for WAIT_TYPE: "VISIBILITY" and scrolls using SCROLL_TYPE: "NONE" on ELEMENT_NAME: "menuButton" to perform ACTIONS: "CLICK" with data SEND_KEYS: "NONE"
Then the user waits for WAIT_TYPE: "PRESENCE" and scrolls using SCROLL_TYPE: "TO_ELEMENT" on ELEMENT_NAME: "dashboard" and verifies VALIDATION_TYPE: "IS_PRESENT"
In this example:
First Given Step:
Given the user is on PAGE_NAME: "LoginPage"
This step sets the PAGE_NAME to "LoginPage".
Subsequent Steps:
When the user waits for WAIT_TYPE: "NONE" and scrolls using SCROLL_TYPE: "NONE" on ELEMENT_NAME: "NONE" to perform ACTIONS: "LOAD_URL" with data SEND_KEYS: "https://www.loginpage.com/"
Then the user waits for WAIT_TYPE: "VISIBILITY" and scrolls using SCROLL_TYPE: "NONE" on ELEMENT_NAME: "username" to perform ACTIONS: "SEND_KEYS" with data SEND_KEYS: "testuser"
These steps apply to the "LoginPage".
New Given Step:
Given the user is on PAGE_NAME: "HomePage"
This step updates the PAGE_NAME to "HomePage".
Remaining Steps:
When the user waits for WAIT_TYPE: "VISIBILITY" and scrolls using SCROLL_TYPE: "NONE" on ELEMENT_NAME: "menuButton" to perform ACTIONS: "CLICK" with data SEND_KEYS: "NONE"
Then the user waits for WAIT_TYPE: "PRESENCE" and scrolls using SCROLL_TYPE: "TO_ELEMENT" on ELEMENT_NAME: "dashboard" and verifies VALIDATION_TYPE: "IS_PRESENT"
These steps apply to the "HomePage".
Example
Here’s how to use the Gherkin template to describe a web automation scenario for logging into Gmail and verifying the email field:
Feature: Gmail Login
This feature allows users to log in to Gmail.
Scenario: Load Gmail and enter email
Given the user performs ACTIONS: "LOAD_URL" with data SEND_KEYS: "https://www.gmail.com/"
Given the user is on PAGE_NAME: "gmail"
Then the user waits for WAIT_TYPE: "VISIBILITY" and scrolls using SCROLL_TYPE: "NONE" on ELEMENT_NAME: "email" to perform ACTIONS: "SEND_KEYS" with data SEND_KEYS: "prabhatgaddam"
Then the user waits for WAIT_TYPE: "VISIBILITY" and scrolls using SCROLL_TYPE: "NONE" on ELEMENT_NAME: "email" and verifies VALIDATION_TYPE: "ATTRIBUTE_VALUE_PRESENT"
Feature: Shopping Cart
This feature allows users to add items to the shopping cart.
Scenario: Add an item to the cart
Given the user is on PAGE_NAME: "shoppingPage"
When the user waits for WAIT_TYPE: "VISIBILITY" and scrolls using SCROLL_TYPE: "NONE" on ELEMENT_NAME: "itemButton" to perform ACTIONS: "CLICK" with data SEND_KEYS: "NONE"
Then the user waits for WAIT_TYPE: "VISIBILITY" and scrolls using SCROLL_TYPE: "NONE" on ELEMENT_NAME: "cart" to validate VALIDATION: "text" with VALIDATION_TYPE: "Item added"
Conclusion
This documentation provides a structured approach to using Gherkin language for web automation, ensuring clear and maintainable test scenarios. By following this template, you can effectively describe and automate various web interactions in a business-readable format.