In Cucumber, a Data Table can be converted into a Map when the data is represented as a single row with two columns (Key-Value pair). This is useful when you want to pass configuration data or form field values to a step definition in a structured way.
What is a Map in Data Table?
A Map in a data table is a key-value pair where the first column is considered the key, and the second column is the value.
Format Example:
Feature: Login Feature Scenario: Successful login with valid credentials Given the user logs in with the following details | username | testuser | | password | testpass |
Step Definition (Java):
You can access this data as a Map<String, String> using DataTable.asMap().
import io.cucumber.java.en.Given; import io.cucumber.datatable.DataTable; import java.util.Map; public class LoginStepDef { @Given("the user logs in with the following details") public void user_logs_in_with_details(DataTable dataTable) { Map<String, String> loginDetails = dataTable.asMap(String.class, String.class); String username = loginDetails.get("username"); String password = loginDetails.get("password"); System.out.println("Username: " + username); System.out.println("Password: " + password); // Add login logic here } }
Explanation:
DataTableis converted toMap<String, String>using.asMap(String.class, String.class)Each row in the table becomes a key-value pair in the map.
Very useful for form inputs or structured key-value data.
Output
Username: testuser Password: testpass