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)

Preface

One of my colleagues once described the Ruby community as "Test Infected" and if any library epitomizes this it's Capybara, which has gained popularity exponentially since it was first released. The Ruby community certainly owes its creator Jonas Nicklas a great deal of thanks for bringing peace and harmony to many test automation code bases around the globe.

The proof of Capybara's success is the way in which its use has spread far beyond just testing Rails applications and now supports testing of many web applications written in a wide variety of languages and frameworks. Capybara's functionality has also been replicated in languages other than Ruby again highlighting just how powerful the concept is.

So what is Capybara?

Capybara provides a domain-specific language for test automation; this DSL extends the human-readable BDD style of frameworks such as Cucumber and RSpec into the automation code itself. For example, opening a browser and navigating to a URL is as simple as visit http://google.com. This is a vast improvement over typical test APIs.

Additionally Capybara allows us to write tests once and run them in any compatible driver. The driver ecosystem is vibrant and switching libraries is as simple as adding an additional gem and making a one-line change to your code.

Finally, you can do away with writing bespoke methods that wait for content to become visible or adding sleep statements to your tests; Capybara handles asynchronous JavaScript without the user even noticing.

Capybara is quite literally your one-stop shop for test automation.

What this book covers

Chapter 1, Your First Scenario with Capybara, covers installation and configuration of your first scenario using Capybara.

Chapter 2, Mastering the API, provides a deep dive into Capybara's API for interacting with web pages.

Chapter 3, Testing Rails and Sinatra Applications, helps us explore how Capybara is particularly suited to testing applications implemented using Rails or Sinatra.

Chapter 4, Dealing with Ajax, JavaScript, and Flash, covers how to handle asynchronous JavaScript and how to use Capybara to test black box components such as Flash or HTML5 Canvas, Audio, and Video.

Chapter 5, Ninja Topics, helps us in using Capybara outside Cucumber in bespoke frameworks, within popular test frameworks such as RSpec and explores some alternatives to Capybara's built-in drivers.

What you need for this book

This book and the examples were developed using Ruby-1.9.3p237, RubyGems 1.8.23, and most importantly Capybara 2.1.0, which introduced some significant changes. All other dependencies will be downloaded by either RubyGems or Bundler when you install Capybara. We will also use Cucumber and RSpec, the latest versions of which should all be compatible with Capybara 2.1.0 and above.

Who this book is for

This book is for developers and testers who, with some exposure to Ruby, want to know how to test their applications using Capybara and its compatible drivers such as Selenium WebDriver and Rack::Test. The examples are deliberately kept simple and example HTML markup is always included so that readers can copy the examples to practice and experiment on their own machine.

Conventions

In this book, you will find a number of styles of text that distinguish between different kinds of information. Here are some examples of these styles, and an explanation of their meaning.

Code words in text, database table names, folder names, filenames, file extensions, pathnames, dummy URLs, user input, and Twitter handles are shown as follows: "The only source of confusion here might be the use of the string literal search_query in the fill_in method."

A block of code is set as follows:

<div id="main">
    <div class="section">
        <a id="myanchor" title="myanchortitle" href="#">Click thisAnchor</a>
     </div>
</div>

Any command-line input or output is written as follows:

$ ruby -v
ruby 1.9.3p327 (2012-11-10 revision 37606) [x86_64-darwin11.4.2]

New terms and important words are shown in bold. Words that you see on the screen, in menus or dialog boxes for example, appear in the text like this: "After this we need to enter our search terms and click on the Search button."

Note

Warnings or important notes appear in a box like this.

Tip

Tips and tricks appear like this.

Reader feedback

Feedback from our readers is always welcome. Let us know what you think about this book—what you liked or may have disliked. Reader feedback is important for us to develop titles that you really get the most out of.

To send us general feedback, simply send an e-mail to , and mention the book title via the subject of your message.

If there is a topic that you have expertise in and you are interested in either writing or contributing to a book, see our author guide on www.packtpub.com/authors.

Customer support

Now that you are the proud owner of a Packt book, we have a number of things to help you to get the most from your purchase.

Downloading the example code

You can download the example code files for all Packt books you have purchased from your account at http://www.packtpub.com. If you purchased this book elsewhere, you can visit http://www.packtpub.com/support and register to have the files e-mailed directly to you.

Errata

Although we have taken every care to ensure the accuracy of our content, mistakes do happen. If you find a mistake in one of our books—maybe a mistake in the text or the code—we would be grateful if you would report this to us. By doing so, you can save other readers from frustration and help us improve subsequent versions of this book. If you find any errata, please report them by visiting http://www.packtpub.com/submit-errata, selecting your book, clicking on the errata submission form link, and entering the details of your errata. Once your errata are verified, your submission will be accepted and the errata will be uploaded on our website, or added to any list of existing errata, under the Errata section of that title. Any existing errata can be viewed by selecting your title from http://www.packtpub.com/support.

Piracy

Piracy of copyright material on the Internet is an ongoing problem across all media. At Packt, we take the protection of our copyright and licenses very seriously. If you come across any illegal copies of our works, in any form, on the Internet, please provide us with the location address or website name immediately so that we can pursue a remedy.

Please contact us at with a link to the suspected pirated material.

We appreciate your help in protecting our authors, and our ability to bring you valuable content.

Questions

You can contact us at if you are having a problem with any aspect of the book, and we will do our best to address it.