lmpgdn
lmpgdn

Reputation: 19

How can I improve my tests using PageObject pattern?

I think I already get the gist of the PageObject pattern but there are some parts of it that I didn't get.

So I was following this tutorial and now I have this Page class:

public class SignUpPage extends AbstractPage {

    AndroidDriver<MobileElement> driver;

    public SignUpPage(AndroidDriver<MobileElement> driver) {
        super(driver);
    }

    public void signUpAllFieldsEmpty() {
        signup_button.click();
    }

    public void signUpOnlyFirstName() {
        first_name_edittext.sendKeys("First name");
        hideKeyboard();
        signup_button.click();
    }


    @AndroidFindBy(id="signup_button")
    private MobileElement signup_button;

    @AndroidFindBy(id="first_name_edittext")
    private MobileElement first_name_edittext;

    @AndroidFindBy(id="last_name_edittext")
    private MobileElement last_name_edittext;

    @AndroidFindBy(id="signup_email_edittext")
    private MobileElement signup_email_edittext;

    @AndroidFindBy(id="signup_password_edittext")
    private MobileElement signup_password_edittext;

    @AndroidFindBy(id="confirm_password_edittext")
    private MobileElement confirm_password_edittext;

}

Then I have this Test class:

public class Test_001_SignUp extends AbstractTest {

    public Test_001_SignUp() {}

    @Test
    public void signUp_allFieldsEmpty() {

        app.landingPage().goToSignUpPage();        
        app.signUpPage().signUpAllFieldsEmpty();

    }

    @Test
    public void signUp_onlyFirstName() {

        app.signUpPage().signUpOnlyFirstName();

    }

}

I am not sure if I am doing it right because it looks like the method in the test class becomes redundant if I create another @Test method that will be relevant to the test.

How could I improve this? Or am I doing something wrong with this test?

Upvotes: 0

Views: 81

Answers (2)

Guy
Guy

Reputation: 50809

If all your test have the same start point you can do it in a set up method with @Before annotation and clean method with @After annotation. They will run before and after every test method and will perform the action necessary to bring the test to its starting point

public class Test_001_SignUp extends AbstractTest {

    public Test_001_SignUp() {}

    @Before
    public void setUp() {
        app
            .landingPage()
            .goToSignUpPage();
    }

    @Test
    public void signUp_allFieldsEmpty() {
        app
            .signUpPage()
            .signUpAllFieldsEmpty();
    }

    @Test
    public void signUp_onlyFirstName() {
        app
            .signUpPage()
            .signUpOnlyFirstName();
    }

    @After
    public void cleanUp() {
        // roll back to initial state 
    }
}

As a side note, lets take as example signUp_allFieldsEmpty() test. goToSignUpPage() should return SignUpPage instance, so you can chain the next step

@Test
public void signUp_allFieldsEmpty() {
    app
        .landingPage()
        .goToSignUpPage()        
        .signUpAllFieldsEmpty();
}

Upvotes: 0

David Lavender
David Lavender

Reputation: 8301

Your Page class should be stupid - it simply controls the page.

public void signup() {
    signup_button.click();
}

public void enterFirstName(String firstName) {
    first_name_edittext.sendKeys(firstName);
}

public String getFirstName() {
   etc...

Your Test class has the logic of which of those methods to call for each test.

A few other points:

  • You aren't asserting anything in your tests. You should add a getSuccessMessage method, or getPageTitle to your page (or whatever happens on your page once the signup is complete - something for your test to check that it worked!). Then your test can assert that text is present and correct.
  • Some frameworks use StepDef classes in between the Test and the Pages too.

Upvotes: 1

Related Questions