11 Test automation design patterns for the UI layer

 

This chapter covers

  • The limitations of unstructured test scripts
  • Page Objects and Page Component Objects
  • Modeling user interfaces and user behavior
  • Action classes and DSLs

In the previous chapter, you learned how to automate a web page using basic Selenium WebDriver code. In this chapter, you will learn how to organize and structure your UI test code to make it easier to extend and easier to maintain.

11.1 The limitations of unstructured test scripts

Thus far we’ve explored the WebDriver API using very simple code samples. These examples work well to illustrate the various WebDriver methods, but you wouldn’t want to write code like this for real-world automated tests. For example, to log on to the Frequent Flyer website, you used the following code:

@When("he/she logs on with a valid username and password")
public void logsOnWithAValidUsernameAndPassword() {
    WebDriver driver = WebTestSupport.currentDriver();
 
    driver.get("http://localhost:3000");
    driver.findElement(By.linkText("Login")).click();
    driver.findElement(By.id("email")).sendKeys(frequentFlyer.email);
    driver.findElement(By.id("password")).sendKeys(frequentFlyer.password);
    driver.findElement(By.id("login-button")).click();
}

This code wouldn’t scale well. You’d need to duplicate the same or similar lines for every scenario involving a user logging on, and any change to this logic would need to be updated at every place that it’s used.

11.2 Separating location logic from test logic

11.3 Introducing the Page Objects pattern

11.3.1 Page Objects are responsible for locating elements on a page

11.3.2 Page Objects represent objects on a page, not an entire page

11.3.3 Page Objects tell you about the state of a page

11.3.4 Page Objects perform business tasks or simulate user behavior

11.3.5 Page Objects present state in business terms

11.3.6 Page Objects hide wait conditions and other incidental implementation details

11.3.7 Page Objects do not contain assertions

11.3.8 WebDriver Page Factories and the @FindBy annotation