Data Tables in Cucumber for Selenium Java Automation

 In Selenium Java test automation with Cucumber, Data Tables are a powerful way to handle multiple sets of data without hardcoding values into your step definitions. They help maintain clean, readable, and scalable test cases — especially when dealing with forms, login credentials, or repetitive input fields.

πŸ₯’ What Are Data Tables in Cucumber?

Data Tables in Cucumber are tables defined directly in the Gherkin feature files. They allow you to pass structured data from a scenario to a step definition, enabling data-driven testing.

Example:

Scenario: User logs in with valid credentials

  Given the user logs in with the following credentials

    | username | password |

    | user1    | pass123  |

πŸ§‘‍πŸ’» Using Data Tables in Step Definitions

Cucumber provides the DataTable class to work with these tables in your Java step definitions. You can map them in several ways depending on how the data is structured.

1. As a List of Lists

@Given("the user logs in with the following credentials")

public void loginWithCredentials(DataTable dataTable) {

    List<List<String>> data = dataTable.asLists();

    String username = data.get(1).get(0); // user1

    String password = data.get(1).get(1); // pass123

    // Use username and password with Selenium

}

2. As a List of Maps (for multiple rows)

Scenario: Multiple users login

  Given the following users log in

    | username | password |

    | user1    | pass1    |

    | user2    | pass2    |

@Given("the following users log in")

public void multipleUsersLogin(DataTable dataTable) {

    List<Map<String, String>> users = dataTable.asMaps(String.class, String.class);

    for (Map<String, String> user : users) {

        String username = user.get("username");

        String password = user.get("password");

        // Call Selenium login method with username and password

    }

}

πŸ§ͺ Why Use Data Tables?

πŸ” Reusable test steps

🧼 Cleaner feature files

πŸ”„ Easy to scale with more data

πŸ’‘ Encourages separation of test logic and data

✅ Best Practices

Keep column headers meaningful (firstName, email, phone)

Avoid complex nested data — break it into smaller steps

Use helper classes or POJOs for mapping large tables

Final Thoughts

Cucumber Data Tables elevate your test automation from basic scripting to robust, data-driven testing. They integrate seamlessly with Selenium and Java, making your BDD tests easier to read, write, and maintain. By learning to use Data Tables effectively, you’ll improve both the flexibility and professionalism of your test framework.

Learn Selenium with Java Training

Read More: Mapping Step Definitions in Selenium Java Cucumber Framework

Read More: Writing Feature Files for Selenium Java with Cucumber

Read More: Selenium Java with Cucumber: Behavior-Driven Development (BDD) Basics

Visit Our IHUB Talent Institute Hyderabad
Get Direction 

Comments

Popular posts from this blog

How to Use Tosca's Test Configuration Parameters

Tosca Licensing: Types and Considerations

Using Hibernate ORM for Fullstack Java Data Management