DEV Community

loading...

starting to think about PageObjects

Tony Colston
wanna be gamedev ... all around nerd and dev
・1 min read

How to get started with PageObjects?

First off what is a PageObject. It is mental model/pattern that others have followed in the Selenium community to create maintainable Selenium scripts.

Usually you see it in Java but it can be done in any OO-like languge.

The idea is that a web page is represented by a single class. And the Selenium commands are safely hidden away with methods.

Here is a simple example.

public class LoginPage {
  public boolean login(String username, String password) {
    // detail omitted
  }
  public boolean logout() {
    // detail omitted
  }
}

With this type of setup you now have a Java class that you can give to anyone that can be used to login to your website. Logging into your website is still always in the context of Selenium script but another tester in your organization can/should rely that this class just works.

This is an example of how you would use the Login PageObject.

LoginPage lpage = new LoginPage();
lpage.login("fakeuser","fakepassword");
driver.get("http://example.com/app");
// more code here
lpage.logout();

The Login PageObject becomes a reusable tool. One that hides the complexity of figuring out how to login to a website. In that way PageObject is a useful tool in your testing toolbelt.

Discussion (0)