chapter eleven

11 Test Automation design patterns for the UI layer

 

This chapter covers

  • The limitations of unstructured test scripts
  • Page Objects and Page Component Objects
  • Modelling User Interfaces and Modelling 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

So 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.4 WebDriver Page Factories and the @FindBy annotation

11.4.1 Finding collections

11.4.2 Page Objects in Serenity BDD

11.5 Going Beyond Page Objects

11.5.1 Action classes

11.5.2 Query classes

11.5.3 Domain Specific Language (DSL) layers and Builders

11.6 Summary