Black-box JavaScript tests with a real browser

Capybara's first gem release was in 2009. At that time, JavaScript was not required for many popular web pages, and a popular approach to web development was "unobtrusive JavaScript", which meant that the basic functionality of a web page was still available without JavaScript. The most popular framework for JavaScript was jQuery and single-page web app frameworks like Angular.js or React were not in wide use. In the intervening years, web pages have been steadily replaced by web apps, which require JavaScript for all functionality. The single-page web application is a very popular architecture and many frameworks exist to support it.

The reason for this history lesson is to put in context the choices we are going to make about how we use Capybara. The easiest way to use Capybara and the first thing documented in its README is to let Capybara start our web app automatically, allowing anything in the app to be mocked. To interact with the web app, a fake Ruby browser called Rack::Test is used. This "browser" ignores JavaScript completely and doesn't even make HTTP requests, as it interacts with the Ruby web server directly.

We won't spend any time going down this "easy" path since it no longer makes any sense. There is not much interest today in developing a web app in static HTML without JavaScript. To get JavaScript to run, we'll need a real browser of some kind. We'll start with Mozilla Firefox, which is the default for Selenium. Finally, we'll treat the HTTP API server as a black box, avoiding mocking. This will simplify our tests a great deal and keep us focused on end-to-end testing. There is room for mocking or lower-level tests using Capybara, but in the world of rich web apps, these use cases are now a niche, just like the static HTML website.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset