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.