top of page

Page objects design pattern



Introduction:

This is the most important pattern related to software automation. It enables you to create object repository with UI elements. Elements are separated from test logic. This pattern makes code much more maintainable and reusable.


Never do this

Without this pattern what you might do is start WebDriver, navigate to some test page. Locate element you need then click it.


IWebElement element = webDriver.FindElement(By.CssSelector("div.find label"));

element.Click();


This is good when you want to test the idea or do some quick demo. In a real-life project, this element might be needed in dozens of tests. What happens if the UI changes and CSS selector is not matching anymore? Here comes the maintainability problem, you have to search and replace all of them.



The proper way

In software engineering, there is a principle called DRY for short. Its idea is to have each element or action stored only once in a system. This avoids copy/paste and reduces the overhead for code maintenance. The same idea is used in Page Object pattern.


a) Each page or re-usable part of a page (i.e. header, footer, menu) is a separate class.

b) Class constructor takes WebDriver as an argument and uses it internally to locate elements.

c) Each element is a private property (or getter in Java).

d) Actions are public and internally operate with elements.


In the code below SearchField is private property used only by SearchFor method which is exposed to available action on HomePage. An element can be located inside the action method but suggested approach gives better readability. And if an element is needed more than once then defining it separately is a must.


public class HomePage

{

private IWebDriver webDriver;

public HomePageObject(IWebDriver webDriver)

{

this.webDriver = webDriver;

}

private IWebElement SearchField

{

get { return webDriver.FindElement(By.Id("search")); }

}

public void SearchFor(string text)

{

SearchField.SendKeys(text);

}

}


Invoking Page Objects:

The page object is instantiated in the test and actions are invoked.


HomePageObject homePage = new HomePageObject(webDriver);

homePage.SearchFor("automation");


With this approach, you have one element defined in only one place. Only actions are exposed out of the page object. It is very clear what actions can be done on this page.


Conclusion

Always use page objects in your test automation. ALWAYS!

Building better for tomorrow

bottom of page