Beginner Chapter 6 · 10 min read

Data Tables

Pass structured data in Gherkin steps using data tables — lists, maps, and multi-column tables for complex inputs and verifications.

Data Tables in Gherkin

Data Tables pass structured data directly inside a step — unlike Examples (which repeat scenarios), tables provide data to a single step. They're perfect for filling forms, verifying table contents, or providing lists of items.

data_tables.feature
Feature: User Registration
  Scenario: Register a new user with full details
    Given the user fills the registration form with:
      | field     | value              |
      | firstName | Prasanna           |
      | lastName  | Venkatesh          |
      | email     | pv@example.com     |
      | password  | SecurePass123      |
      | role      | QA Engineer        |
    When the user submits the form
    Then the account should be created

  Scenario: Verify user list in admin panel
    Given the following users exist:
      | name     | role       | status |
      | Alice    | Admin      | Active |
      | Bob      | Developer  | Active |
      | Charlie  | Tester     | Locked |
    When the admin opens the user management page
    Then the user table should display 3 users

  Scenario: Add items to cart
    When the user adds the following items to the cart:
      | item        | quantity | price  |
      | Laptop      | 1        | 999.99 |
      | Mouse       | 2        | 29.99  |
      | Keyboard    | 1        | 79.99  |
    Then the cart total should be "1139.96" 
DataTableSteps.java
package com.bdd.course.stepdefinitions;

import io.cucumber.java.en.*;
import io.cucumber.datatable.DataTable;
import java.util.List;
import java.util.Map;

public class DataTableSteps {

    // DataTable as List of Maps (column headers as keys)
    @Given("the user fills the registration form with:")
    public void fillRegistrationForm(DataTable dataTable) {
        List<Map<String, String>> rows = dataTable.asMaps();
        for (Map<String, String> row : rows) {
            String field = row.get("field");
            String value = row.get("value");
            System.out.println("Setting " + field + " = " + value);
        }
    }

    @Given("the following users exist:")
    public void usersExist(DataTable dataTable) {
        List<Map<String, String>> users = dataTable.asMaps();
        for (Map<String, String> user : users) {
            System.out.println("User: " + user.get("name")
                + " (" + user.get("role") + ")");
        }
    }

    @When("the user adds the following items to the cart:")
    public void addItemsToCart(DataTable dataTable) {
        List<Map<String, String>> items = dataTable.asMaps();
        double total = 0;
        for (Map<String, String> item : items) {
            int qty = Integer.parseInt(item.get("quantity"));
            double price = Double.parseDouble(item.get("price"));
            total += qty * price;
            System.out.println(item.get("item") + " x" + qty);
        }
        System.out.println("Calculated total: " + total);
    }

    @When("the user submits the form")
    public void submitForm() { System.out.println("Form submitted"); }

    @Then("the account should be created")
    public void accountCreated() { System.out.println("Account created"); }

    @When("the admin opens the user management page")
    public void openUserManagement() {}

    @Then("the user table should display {int} users")
    public void userTableCount(int count) {
        System.out.println("Expected users: " + count);
    }

    @Then("the cart total should be {string}")
    public void cartTotal(String expected) {
        System.out.println("Expected total: " + expected);
    }
}
data_tables.feature
Feature: User Registration
  Scenario: Register a new user with full details
    Given the user fills the registration form with:
      | field     | value              |
      | firstName | Prasanna           |
      | lastName  | Venkatesh          |
      | email     | pv@example.com     |
      | password  | SecurePass123      |
      | role      | QA Engineer        |
    When the user submits the form
    Then the account should be created

  Scenario: Verify user list in admin panel
    Given the following users exist:
      | name     | role       | status |
      | Alice    | Admin      | Active |
      | Bob      | Developer  | Active |
      | Charlie  | Tester     | Locked |
    When the admin opens the user management page
    Then the user table should display 3 users
data_table_steps.py
# features/steps/data_table_steps.py
from behave import given, when, then

@given('the user fills the registration form with')
def step_fill_form(context):
    # context.table holds the data table
    for row in context.table:
        field = row['field']
        value = row['value']
        print(f"Setting {field} = {value}")

@given('the following users exist')
def step_users_exist(context):
    for row in context.table:
        print(f"User: {row['name']} ({row['role']})")

@when('the user adds the following items to the cart')
def step_add_items(context):
    total = 0
    for row in context.table:
        qty = int(row['quantity'])
        price = float(row['price'])
        total += qty * price
        print(f"{row['item']} x{qty}")
    context.cart_total = total

@when('the user submits the form')
def step_submit(context):
    print("Form submitted")

@then('the account should be created')
def step_account_created(context):
    print("Account created")

@when('the admin opens the user management page')
def step_open_admin(context):
    pass

@then('the user table should display {count:d} users')
def step_user_count(context, count):
    print(f"Expected users: {count}")

@then('the cart total should be "{expected}"')
def step_cart_total(context, expected):
    print(f"Expected total: {expected}")

Cucumber BDD Beginner Data Tables

Written by PV

© 2026 All Rights Reserved