• Cucumber Data Tables Example in Java

    In our previous post, we learned how to create scenario outline that can be used to repeat the same steps with different parameters. In this tutorial, we will show you some Cucumber Data Tables Example in Java and how it differs in Scenario Outline and how you can implement it in your Test Cases.

    Cucumber Data Tables can be used to add multiple parameters in a Step Definition in a tabular form rather than putting all the parameters in the Gherkin statement. This is much easier to read and multiple rows of data can be passed in the same step. Let’s take some Cucumber Data Tables Example:

    Cucumber Data Tables Example in Java

    Data Table without Header Example

    Here’s an example on how to implement Data Tables without a Header. For example, we want to test the registration form the user is submitting in our application. We can either put all the arguments inside the Gherkin statement or use a table to list all the arguments like we used below:

    Scenario: Valid Registration Form Information
        Given User submits a valid registration form
          | John | Doe | jdoe | testPass1 | [email protected] |
        Then System proceeds with registration
    

    Notice that we used pipe delimiter (|) to create a table form. Below is the Step Definition we defined:

    private boolean isValid;
    
    @Given("User submits a valid registration form")
    public void userSubmitsAValidRegistrationForm(DataTable dataTable) {
        List<String> signUpForm = dataTable.asList();
    
        String fName = signUpForm.get(0);
        String lName = signUpForm.get(1);
        String username = signUpForm.get(2);
        String password = signUpForm.get(3);
        String email = signUpForm.get(4);
        isValid = isValid(fName, lName, username, password, email);
    }
    
    private boolean isValid(String fName, String lName, String username, String password, String email) {
        if (StringUtils.isAnyBlank(fName, lName, username, password, email)
                || !EmailValidator.getInstance().isValid(email)) {
            return false;
        }
        return true;
    }
    

    From the code above, we add the DataTable parameter in our method declaration. This object contains our arguments that we can retrieved to use in our logic. Since we don’t have a header, we can just get the List object and get the values starting from 0 index.

    Data Table with Header and Single Row Example

    Below is a cucumber data tables example with header. Adding a header in your table makes it easier to read and maintain.

    Scenario: Valid Registration Form Information with Header
      Given User submits a valid registration form header
        | FirstName | LastName | Username | Password  | Email          |
        | John      | Doe      | jdoe     | testPass1 | [email protected] |
      Then System proceeds with registration
    

    And in our Java code, we can then just get the Map object that contains the Key/Value pairs of our arguments. For example, we can retrieve the First Name by using map.get(“FirstName”).

    @Given("User submits a valid registration form header")
    public void userSubmitsAValidRegistrationFormHeader(DataTable dataTable) {
        List<Map<String, String>> signUpForms = dataTable.asMaps(String.class, String.class);
    
        String fName = signUpForms.get(0).get("FirstName");
        String lName = signUpForms.get(0).get("LastName");
        String username = signUpForms.get(0).get("Username");
        String password = signUpForms.get(0).get("Password");
        String email = signUpForms.get(0).get("Email");
        isValid = isValid(fName, lName, username, password, email);
    }
    

    Since we only have 1 row in our Gherkin, we can then just use List.get(0) to retrieve the first row. Then get the argument by using map.get(“HeaderName”).

    Data Table with Header and Multiple Rows Example

    Finally, we will show you an example that contains multiple rows of data. This is helpful when you want to test multiple combination of data in a step. Here’s an example of our Gherkin:

    Scenario: Valid Registration Form Information with Header and Multiple Rows
      Given User submits valid registration forms
        | FirstName | LastName | Username | Password  | Email              |
        | John      | Doe      | jdoe     | testPass1 | [email protected]     |
        | Anne      | Smith    | asmith   | testPass2 | [email protected]   |
        | Mike      | Stewart  | mstewart | testPass3 | [email protected] |
      Then All Forms will proceed with registration
    

    And here’s our Step Definition. Notice that in our previous example, we only retrieve the first row by using get(0) method of List. Now, we will loop all the values in the List. Our List object contains the Map object which represents a Row in a table. So in our example, we have 3 rows of data which means that our List object holds 3 objects of Map. The Map object contains the columns of our data. Our example have 5 columns which then translates that our Map object will have 5 key/value pairs inside.

    @Given("User submits valid registration forms")
        public void userSubmitsValidRegistrationForms(DataTable dataTable) {
            List<Map<String, String>> signForms = dataTable.asMaps();
            formValidationResults = new ArrayList<Boolean>();
    
            for (Map<String, String> form : signForms) {
    
                String fName = form.get("FirstName");
                String lName = form.get("LastName");
                String username = form.get("Username");
                String password = form.get("Password");
                String email = form.get("Email");
                boolean validationResult = isValid(fName, lName, username, password, email);
    
                formValidationResults.add(validationResult);
            }
        }
    

    And that’s it. Next tutorial is about how you can run your feature files automatically when building a maven project. Let us know in the comments if you have questions.

    Related Post