Book Image

Application Testing with Capybara

By : Matthew Robbins
Book Image

Application Testing with Capybara

By: Matthew Robbins

Overview of this book

<p>Everybody understands why implementing automated tests is important but at the same time developing them can be costly and time consuming, and tests can be also be fragile and prone to false positives. By using Capybara, you can develop robust tests quickly and run them in multiple drivers ensuring greater re-use; Capybara’s API also extends the human readable style made popular by frameworks such as Cucumber and RSpec.</p> <p>"Application Testing with Capybara" takes you from installing the gem to getting up and running with a YouTube search scenario within the first two chapters. We then look deeper into the API, using Rack-Test for applications built with Rails or Sinatra and see how to test handle Asynchronous JavaScript and “black box” components such as Flash. Finally, we consider some advanced topics such as looking at alternatives to Selenium and accessing the native driver directly.</p> <p>This book takes you from the basics of installing Capybara, through its API and onto advanced topics. You will learn how to use Capybara’s extensive API to interact with your application, covering topics such as navigation, filling in forms, and querying your page for expected content. Beyond this we will consider why Capybara is so well suited to testing applications written in frameworks such as Rails and Sinatra. We will look at strategies for validating seemingly “untestable” components such as HTML5 or Flash by building out a testable API. Finally we will turn you into a Capybara ninja by covering advanced topics such as accessing functionality in the base driver, advanced driver configuration, and alternative flavours of drivers outside Selenium and Rack-Test.</p>
Table of Contents (12 chapters)

Chapter 4. Dealing with Ajax, JavaScript, and Flash

It has been a long time since web applications consisted of mainly static HTML. Most modern web applications have huge amounts of JavaScript that modify the DOM on the client (in the browser). Automating such applications requires an additional level of awareness on the part of the developer authoring the tests. JavaScript in the DOM is not synchronous; event handlers are used to alter and modify the page as it loads and as users interact with it, and as such, our tests need to be robust enough to handle this asynchronous behavior.

Thankfully, Capybara was built with this principle at its core, so that makes things pretty easy for us. In this chapter, we will work through examples that demonstrate this.

We will also consider elements on the page that appear at first sight to be impossible to automate, such as flash components or HTML5 elements, for example, the canvas tag.