Parsing DataTables - Type Transformation

Datatables when passed as arguements from a cucumber step can be parsed in different ways. In this section we will understand how they can be parsed to a java object.

Also, this is the most recommended practice for parsing datatables.

Cucumber can understand a table as a list of certain type. e.g. in the current context what we are trying to pass from the step is user details (name, email, password). So the datatable can be transformed into a list of userdetail objects. The underlying assumption here is the fact that userdetail is a separate class with attributes like firstName, lastName, email, password.

Consider the following code to understand the step implementation. We can put this in the SignUpSteps class.

@And("^she provides the her details as follows:$")
    public void she_provides_the_her_details_as_follows(List<UserDetails> users) throws Throwable {
        UserDetails userToBeSignedUp = users.get(0);

        new SignupPage(driver).she_provides_the_first_name_as(userToBeSignedUp.firstName);
        new SignupPage(driver).she_provides_the_last_name_as(userToBeSignedUp.lastName);
        new SignupPage(driver).she_provides_the_email_as(userToBeSignedUp.email);
        new SignupPage(driver).she_provides_the_password_as(userToBeSignedUp.password);
        new SignupPage(driver).she_provides_the_confirm_password_again_as(userToBeSignedUp.password);

    }

Notice the fact that it needs the UserDetails class as well. For now let's create an inner class with that name in the same SignupStepsClass.

private class UserDetails {
        public String firstName;
        public String lastName;
        public String email;
        public String password;
    }

This is a plain old java class that will facilitate the transformation of the data table to an object of this class. Also mark the fact that the variables in this class and the column header in the step should match.

Likewise, whenever we need to pass a large set of related data in our steps, we can create similar domain object classes and use the same to do a data table transformation.

Now that we have understood the concept, let's move the class UserDetails.java to a separate package. We will create one called domain inside src and move the class inside that domain package.

results matching ""

    No results matching ""