Chapter 8. Automating acceptance criteria for the UI layer

 

This chapter covers

  • Why and when you should write automated UI tests
  • Using Selenium WebDriver for web tests
  • Finding and interacting with page elements in your tests
  • Using the Page Objects pattern to make your tests cleaner
  • Libraries that extend Selenium WebDriver

In the previous chapter, you learned how using a layered approach to automated acceptance testing helps make your tests clearer, more robust, and more maintainable. We discussed the three broad layers used in well-designed automated acceptance tests: the Business Rules layer, the Business Flow layer, and the Technical layer. In the following few chapters, we’ll focus on approaches and tools that can be used to implement the Technical layer, starting with the user interface.

In this chapter we’ll discuss techniques to automate UI tests for web-based applications (see figure 8.1). Users interact with an application through its user interface, and in modern web applications, the UI implementation plays a major role in the overall user experience. The screenshots from automated web tests can be a valuable aid for testers, and they’re also a great way to provide illustrated documentation describing how the application behaves.

Figure 8.1. In this chapter you’ll learn how to automate acceptance criteria in order to exercise the UI of web-based applications.

We’ll look at automated web testing from several perspectives:

8.1. When and how should you test the UI?

Web tests have some significant advantages over other types of testing:

A web test, by definition, is designed to verify UI behavior. But web tests, as end-to-end tests, can also be an effective way to illustrate and check how all of the components in the system work together. Used as living documentation, a web test also often does a great job of documenting how a user will use the system to achieve a particular goal. Web tests can also help give business analysts, testers, and stakeholders more confidence in the automated acceptance tests.

8.2. Automating web-based acceptance criteria using Selenium WebDriver

In this section, we’ll look at automating web tests using Selenium WebDriver. Selenium WebDriver is a popular open source web browser automation library that can be used to write effective, automated web tests. It also forms the basis for many higher-level web-testing tools. The examples will focus on working with WebDriver in Java, but the principles and techniques we’ll discuss will be generally applicable to any WebDriver-based testing.

The WebDriver API is powerful and flexible, but there are several open source libraries for different platforms that can help you build on WebDriver to write web tests more efficiently and more expressively, including Thucydides, Watir, WatiN, and Geb. We’ll look at some of these libraries in action in section 8.3.2. For most of this chapter, you’ll use the WebDriver API with Java.

8.3. Using page objects to make your tests cleaner

Up until now we’ve explored the WebDriver API using 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:

Now your test code can focus on the test data and the intent of the actions, rather than on how the individual web elements are found or manipulated:

8.4. Summary

In this chapter you learned to automate UI tests for web-based applications:

In the next chapter we’ll focus on techniques for testing the non-UI layers of the application.