How To Write Scenarios Using Gherkin Language

Master BDD scenarios for mobile app testing with practical examples and best practices for cross-platform development.

What Is Gherkin and Why It Matters for Mobile Development

Gherkin is a business-readable, domain-specific language that enables teams to describe software behavior without getting bogged down in implementation details. Originally developed as part of the Cucumber testing framework, Gherkin has become a cornerstone of Behavior-Driven Development practices adopted by mobile development teams worldwide. As BrowserStack explains, Gherkin was created specifically to bridge the communication gap between technical and non-technical stakeholders in testing contexts.

The language uses a simple, keyword-based syntax that resembles natural English, making it accessible to non-technical stakeholders while remaining precise enough for test automation frameworks to parse and execute. For mobile development teams working on cross-platform applications, Gherkin serves as a shared vocabulary that bridges the gap between business requirements and technical implementation.

When building mobile applications--whether using React Native, Flutter, iOS native, or Android native--teams face unique testing challenges that Gherkin helps address effectively. The approach transforms how teams collaborate on quality assurance, creating specifications that everyone from product managers to developers can understand and contribute to.

Key Benefits for Mobile Development Teams

Adopting Gherkin for mobile testing brings tangible benefits

Single Source of Truth

Create shared documentation that developers, QA engineers, and product managers can all understand and maintain.

Executable Specifications

Run scenarios against your mobile apps to verify features behave as intended, catching regressions early.

Cross-Platform Consistency

Write scenarios once and run them against both iOS and Android builds for feature parity.

Better Design

Gherkin's structured approach encourages thinking through edge cases before implementation.

Core Gherkin Keywords and Syntax

Gherkin's syntax centers on a small set of keywords that define the structure of scenarios. Understanding these keywords and their proper usage is essential for writing effective Gherkin scenarios for mobile testing. Each keyword serves a specific purpose in describing user interactions and expected outcomes. As detailed in AccelQ's Gherkin syntax guide, each Gherkin keyword serves a specific purpose in describing behavior.

The keyword structure creates a clear narrative flow that mirrors how users actually interact with mobile applications--establishing context, performing actions, and verifying results. This approach aligns naturally with mobile QA workflows and supports comprehensive testing strategies.

Feature and Scenario Example
1Feature: User Authentication2 As a mobile app user3 I want to sign in securely4 So that I can access my personal data5 6Scenario: User successfully logs in with valid credentials7 Given the user has an account with email "[email protected]" and password "SecurePass123"8 And the app is on the login screen9 When the user enters "[email protected]" in the email field10 And the user enters "SecurePass123" in the password field11 And the user taps the "Sign In" button12 Then the app should display the home screen13 And the user profile should show "[email protected]"
Core Gherkin Keywords and Their Purpose
KeywordPurposeMobile Example
FeatureGroups related scenarios and provides context about functionalityUser Authentication, Payment Processing, Push Notifications
ScenarioA specific example of how a feature should behaveUser logs in successfully, User fails login with wrong password
GivenEstablishes initial context or preconditionsGiven the user has an existing account
WhenCaptures the action or event being testedWhen the user taps the "Submit" button
ThenDefines the expected outcome or verificationThen the app should display a confirmation message
And/ButChains additional steps of the same typeAnd the user should remain on the login screen

Advanced Gherkin Features for Comprehensive Testing

As mobile applications grow in complexity, basic scenarios may not suffice for thorough testing. Gherkin provides several advanced features that enable more sophisticated testing approaches without duplicating scenario definitions.

Background

The Background keyword allows you to define common steps that apply to all scenarios in a feature file, eliminating repetition when multiple scenarios share the same initial setup. For mobile testing, Background steps often handle app launch, login, or navigation to a common starting point.

Background Example for Mobile Testing
1Feature: Shopping Cart Management2 3 Background:4 Given the user has logged into the app5 And the user has added 3 items to the cart6 And the user is on the cart screen7 8 Scenario: User views cart contents9 When the user taps on the cart icon10 Then the app should display all 3 added items11 12 Scenario: User removes an item from cart13 When the user swipes left on the first item14 And the user taps the "Delete" button15 Then the cart should contain 2 items

Scenario Outline and Examples

Scenario Outline enables data-driven testing by defining a template scenario that can be executed with different sets of data. The Examples section provides the actual data values to substitute into the template. This feature is particularly valuable for mobile testing where the same workflow must be validated across different inputs, device configurations, or user types. According to AccelQ's comprehensive tutorial, Scenario Outline is one of the most powerful features for creating maintainable test suites.

For mobile applications, Scenario Outline works excellently for testing input validation, form submissions across different device configurations, and verifying behavior across various user roles and permissions.

Scenario Outline with Examples
1Scenario Outline: User login with different credentials2 Given the user has an account with email "<email>" and password "<password>"3 And the app is on the login screen4 When the user enters "<email>" in the email field5 And the user enters "<password>" in the password field6 And the user taps the "Sign In" button7 Then the login result should be "<result>"8 9 Examples:10 | email | password | result |11 | [email protected] | validPass123 | successful_login |12 | [email protected] | validPass123 | email_error |13 | [email protected] | wrongPassword | password_error |

Best Practices for Writing Effective Gherkin Scenarios

Writing effective Gherkin scenarios requires discipline and attention to style. Well-written scenarios serve as living documentation that team members at all levels can understand and maintain. Whether you're building native iOS applications or cross-platform solutions, these principles apply universally.

Keep Scenarios Short and Focused

Each scenario should test one specific behavior. When scenarios attempt to cover too much functionality, they become harder to understand and maintain.

Use Consistent Language

Establish terminology conventions and use them consistently. If you call a UI element a 'Sign In button' in one scenario, use the same term everywhere.

Write from User's Perspective

Frame scenarios in terms of user actions and observable outcomes rather than technical implementation details.

Avoid Implementation Details

Keep scenarios readable for non-technical stakeholders while step definitions handle technical specifics.

Common Patterns for Mobile App Testing

Mobile applications have testing patterns that differ significantly from web applications. Understanding how to express common mobile interactions in Gherkin helps teams write scenarios that accurately reflect user experience. These patterns apply whether you're testing Android applications or iOS apps built with Swift or SwiftUI.

Navigation Testing Example
1Scenario: User navigates through main tabs2 Given the app is on the home screen3 When the user taps the "Messages" tab4 Then the messages list screen should be displayed5 When the user taps the "Profile" tab6 Then the profile screen should be displayed7 When the user taps the back button8 Then the messages list screen should be displayed again
Form Validation Example
1Scenario: Email field validates format on blur2 Given the user is on the signup screen3 When the user enters "invalid-email" in the email field4 And the user taps outside the email field5 Then the app should display validation message "Please enter a valid email address"6 And the email field should show a red border
Gesture Testing Example
1Scenario: User deletes item by swiping2 Given the user is on the items list screen3 And the list contains an item "Old Item"4 When the user swipes left on "Old Item"5 And the user taps the "Delete" button that appears6 Then the list should not contain "Old Item"7 8Scenario: User zooms on image9 Given the user has opened a photo in full-screen view10 When the user performs a pinch-out gesture on the image11 Then the image should zoom to 200%

Writing Gherkin Scenarios for Cross-Platform Apps

Cross-platform mobile development introduces specific considerations for Gherkin testing. Whether your team builds with React Native, Flutter, or uses platform-specific native development, effective Gherkin scenarios share common principles while accommodating platform differences.

The key insight is that well-written Gherkin focuses on user behavior rather than implementation specifics. This approach ensures your test scenarios remain valuable even as technology choices evolve, supporting long-term maintainability of your mobile testing strategy.

Platform-Agnostic Scenario
1# Platform-agnostic (preferred)2Scenario: User completes checkout successfully3 Given the user has added items worth $50 to the cart4 And the user is on the checkout screen5 When the user enters valid shipping address6 And the user selects "Express Shipping"7 And the user taps "Place Order"8 Then the order should be confirmed9 And a confirmation email should be sent
Platform-Specific Testing with Tags
1@ios2Scenario: iOS swipe to delete confirmation3 Given the user is on the list screen4 When the user swipes left on an item on iOS5 Then the "Delete" button should appear from the right6 7@android8Scenario: Android long press delete option9 Given the user is on the list screen10 When the user long presses on an item on Android11 Then the contextual action bar should display "Delete"

Frequently Asked Questions

Sources

  1. BrowserStack - Gherkin and its role in BDD Scenarios - Comprehensive guide to Gherkin as a domain-specific language for BDD, emphasizing its role in creating human-readable test scenarios.
  2. AccelQ - Gherkin Syntax Guide - Detailed tutorial covering Gherkin syntax, keywords, practical examples, and best practices for writing maintainable BDD test cases.

Ready to Implement Gherkin in Your Mobile Projects?

Our team specializes in building high-quality mobile applications with comprehensive testing strategies. From BDD implementation to automated test suites, we help teams deliver reliable cross-platform apps.