by Amy Wibowo
Life before frontend integration testing meant clicking links and buttons and testing everything by hand before launching any feature change. But as every programmer knows, insanity is doing the same thing over and over again, and not writing code to do it for you.
Though it might seem daunting to set up a frontend integration testing environment, automating these tests is worth the effort.
- Testing by hand is unreliable and time-consuming.
- Integration testing allows you to be more aggressive in refactoring frontend flows, since changes that break critical behaviors will be caught by the test.
For our frontend testing needs, we use a combination of Capybara and Selenium — Selenium because it’s a mature solution for automating browser interactions; Capybara because of its rspec-like syntax for specifying browser interactions.
Using Selenium requires installation of the selenium-webdriver gem, and Capybara requires installation of the capybara gem and the launchy gem for screengrabs. In your Gemfile:
Starting outYou can generate the test (with a template) with rails g integration_test test_name The auto-generated test will live in spec/requests look something like
Within the test, you can use Capybara to describe any user interactions on a page.
- You can access pages:
click_link “Log In” - You can interact with forms: fill_in “email”, :with => “email@example.com“
fill_in “password”, :with => “secret”
- To keep the tests from being too fragile, use text selectors that are as general as possible (i.e. If your button says “Sign up now!” just check for “Sign up” in case the text changes in the future) Further Reading http://railscasts.com/episodes/257-request-specs-and-capybara
http://rubydoc.info/github/jnicklas/capybara/master/file/README.md Stay tuned for a followup post on how we integrated Capybara tests with Jenkins, our continuous integration server!