We ended Lesson 9 by using RestAssured methods to run database queries during the tests.

I wanted to refactor the helper methods to:

  • reduce the number of dependencies (e.g. theActorInTheSpotlight())
  • remove hard-coded strings
  • separate Tasks and Questions from interactions

❗Note that I started running the app on a separate machine for later load testing, so the URLs have also changed.

I removed the references to LandingPage from UserManagementStepDefinitions:

    @Given("{actor} creates a user account")
    public void createsAUserAccount(Actor actor) {
        actor.wasAbleTo(UserManagement.openNewUserForm());
    }

The expected message when a user is created is now a property of CreateUserPage:

    public static String EXPECTED_SUCCESS_MESSAGE = "Congratulations! Your account has been created!";

And the UserManagement helper methods were refactored to use the internal Actor parameter available to Tasks:

public class UserManagement {

    public static Performable openNewUserForm() {
        return Task.where("User opens Create User form",
                Open.browserOn().the(LandingPage.class),
                Click.on(LandingPage.NAVBAR_LINK.of("Create User"))
        );
    }

    public static Performable completeNewUserForm(String username, String password) {
        return Task.where("User completes Create User form",
                SendKeys.of("q").into(CreateUserPage.EMAIL),
                DoubleClick.on(CreateUserPage.EMAIL),
                Enter.keyValues(username).into(CreateUserPage.EMAIL),

                Click.on(CreateUserPage.PASSWORD),
                SendKeys.of("q").into(CreateUserPage.PASSWORD),
                DoubleClick.on(CreateUserPage.PASSWORD),
                Enter.keyValues(password).into(CreateUserPage.PASSWORD),

                Click.on(CreateUserPage.SIGN_UP)
        );
    }

    public static Performable createUser(String username, String password) {
        deleteUserByName(username);
        return Task.where("Create a user with " + username + " and " + password,
                actor -> {
                    actor.remember("username", username);
                    actor.attemptsTo(completeNewUserForm(username, password));
                }
        );
    }

    public static Question<Boolean> userWasCreated() {
        return Question.about("User was created").answeredBy(
                actor -> {
                    String username = actor.recall("username");
                    return getUserIdFromUsername(username) > 0
                            && CreateUserPage.SUCCESS_MESSAGE.resolveFor(actor).getText()
                            .equals(CreateUserPage.EXPECTED_SUCCESS_MESSAGE);
                }
        );
    }

    public static Performable checkUserWasCreated() {
        return Ensure.that("User sees congratulations message and gets an id",
                userWasCreated()).isTrue();
    }


// API methods

    public static int getUserIdFromUsername(String username) {
        int userId = 0;
        try {
            userId = when().get("http://192.168.1.242:8080/api/customer/username=" + username)
                    .then().contentType(ContentType.JSON)
                    .extract().path("customerIf"); //sic
        } catch (Exception ignored) {
        }
        return userId;
    }

    public static void deleteUserWithId(int userId) {
        when().delete("http://192.168.1.242:8080/api/customer/" + userId)
                .then().statusCode(204);
    }

    public static void deleteUserByName(String username) {
        int userId = getUserIdFromUsername(username);
        if (userId == 0) return;
        deleteUserWithId(userId);
    }

}