Multiple accounts

As a blogger I want to restrict admin access to specific users to secure my blog.

Let us now use various username, password combinations to verify that our login functionality works as expected. A Business Rules Table must be used to test such cases, where we want to test some functionality with different sets of data.

The following Business Rules Table tests login using different username/password combinations.

Ensure that only valid users can login
usernamepasswordlogged in?
usernamepasswordtrue
some usersecretfalse
usernamefalse
passwordfalse

It is not necessary to use Selenium scripts in the underlying code for this table. Let us instead try to use the Login fixture in our Login Table implementation. We could refactor Login.java from
    public void loginAsWith(String user, String pass) {
        selenium.type("j_username", user);
        selenium.type("j_password", pass);
        selenium.submit("loginForm");
    }

    public void verifyThatTheHasLoggedIn(String user) {
        assertTrue(selenium.isTextPresent("Logged in as " + user));
        assertTrue(selenium.isTextPresent("Logout"));
    }
    
    ... code omitted for clarity ...
 
to
    public void loginAsWith(String user, String pass) {
        setUserName(user);
        setPassword(pass);
        submit();
    }

    public void setPassword(String pass) {
        selenium.type("j_password", pass);
    }

    public void setUserName(String user) {
        selenium.type("j_username", user);
    }

    public void verifyThatTheHasLoggedIn(String user) {
        assertTrue(isLoggedIn(user));
        assertTrue(selenium.isTextPresent("Logout"));
    }

    public boolean isLoggedIn(String user) {
        return selenium.isTextPresent("Logged in as " + user);
    }
    
    ... code omitted for clarity...

We could then implement the underlying code for our table as
public class LoginTable {

    // Transient data, reset for each row.
    private Login loginFixture = null;
    private String username;

    /**
     * Initializer before each row in a table is invoked. Note that we intercept 
     {@link #setUsername(String)} call to invoke this.
     */
    private void beginNewTest(String username) {
        loginFixture = new Login(selenium);
        this.username = username;
    }

    public void setUsername(String username) {
        beginNewTest(username);
        loginFixture.setUserName(username);
    }

    public void setPassword(String password) {
        loginFixture.setPassword(password);
    }

    public boolean loggedIn() throws InterruptedException {
        loginFixture.submit();
        boolean loggedIn = loginFixture.isLoggedIn(username);

        if (loggedIn) {
            loginFixture.logoutFromTheBlog();
        }

        return loggedIn;
    }
}

You can see that using existing abstractions to drive your tests help your tests be more maintainable.

To open the class Login, press Ctrl+Shift+T and type Login

Proceed to Testing blog settings.