</>

Technology

Cucumber

Difficulty

Beginner

Interview Question

Provide an example of a feature file using the Cucumber framework.

Answer

Feature File Example

Login Feature (login.feature)

Gherkin
Feature: Login to the Application Under Test

  As a registered user
  I want to log in to the application
  So that I can access my account

  Background:
    Given the user opens the Chrome browser
    And the user navigates to the application URL

  Scenario: Successful login with valid credentials
    When the user enters the username "admin@test.com"
    And the user enters the password "Admin@123"
    And the user clicks on the Login button
    Then the user should be redirected to the dashboard
    And the message "Welcome, Admin" should be displayed

  Scenario: Failed login with invalid password
    When the user enters the username "admin@test.com"
    And the user enters the password "WrongPass"
    And the user clicks on the Login button
    Then the error message "Invalid credentials" should be displayed
    And the user should remain on the login page

  Scenario: Login with empty credentials
    When the user clicks on the Login button
    Then the validation error "Username is required" should appear

E-Commerce Checkout Feature (checkout.feature)

Gherkin
Feature: Product Checkout Flow

  Scenario: User purchases a single item
    Given the user is logged in as "customer"
    And the user is on the product page for "Laptop"
    When the user clicks "Add to Cart"
    And the user proceeds to checkout
    And the user enters shipping address "123 Main St, NY"
    And the user selects payment method "Credit Card"
    And the user places the order
    Then the order confirmation page should be displayed
    And an email confirmation should be sent to "customer@test.com"

Key Points

  1. First line always starts with Feature: keyword
  2. The text after Feature: describes what is being tested
  3. Each Scenario: is a complete, independent test case
  4. Background: contains common steps shared across all scenarios
  5. Steps start with Given, When, Then, And, or But

Follow AutomateQA

Related Topics