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.
You 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 => “firstname.lastname@example.org”
fill_in “password”, :with => “secret”
The selectors can be id, label, or text — they’re lenient.
- You can verify that the right thing happened
page.should have_content(“Hi, Amy!”)
it “lets the user login via modal and email”, :js => true do
sample test:Running Tests
Afterwards, you can run the tests with rake spec:requests. They also run as a part of bundle exec rake spec
A Few Tips
- Start off by writing tests for your main flow, for the greatest ROI.
- 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)
Stay tuned for a followup post on how we integrated Capybara tests with Jenkins, our continuous integration server!
Originally published at nerds.airbnb.com on March 22, 2012.