Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Acceptance Tests with Capybara

Acceptance Tests with Capybara

How and why we should write Acceptance Tests, with Capybara, for our Ruby/Rails web applications.

Brian V. Hughes

September 25, 2013
Tweet

More Decks by Brian V. Hughes

Other Decks in Programming

Transcript

  1. Did we really need another kind of test? “Unit” tests

    Model specs Model Layer “Functional” tests Controller specs Controller Layer
  2. Did we really need another kind of test? “Unit” tests

    Model specs Model Layer “Functional” tests Controller specs Controller Layer View tests/specs View Layer
  3. Did we really need another kind of test? “Unit” tests

    Model specs Model Layer “Functional” tests Controller specs Controller Layer Route tests/specs Route Layer View tests/specs View Layer
  4. Did we really need another kind of test? “Unit” tests

    Model specs Model Layer “Functional” tests Controller specs Controller Layer Route tests/specs Route Layer View tests/specs View Layer
  5. User Value in Web Apps? Lives in the Web Browser

    Delivered via HTML, CSS, JavaScript Rendered as pages, links, forms, buttons Derived through interactions with these elements.
  6. How do we verify that value is delivered? Hint: CMD-R

    in Safari/Chrome is not the answer.
  7. Acceptance Tests with Capybara Provides methods to automate browser interactions

    Can drive both headless and GUI browsers Integrates with RSpec, TestUnit and Cucumber Lets us test drive our web apps “outside-in”
  8. How do we write Acceptance tests? What’s the role of

    the user? What feature do they want to access? How does the user verify success? What are the browser steps that perform the feature? What’s the starting state of our app?
  9. How do we write Acceptance tests? What’s the role of

    the user? What feature do they want to access? How does the user verify success? What are the browser steps that perform the feature? What’s the starting state of our app?
  10. User Stories Describe a single feature/function of the application The

    user’s PoV. State a goal and describe the feature needed for success. “As a <user_role>”, “In order to accomplish <goal>”, “I need to perform <new_feature>”. Defines the parameters for writing and verifying an Acceptance Test
  11. User Story Example Voting for a Topic As an LVRUG

    Member In order to indicate that I want to see “Topic A” presented I want to vote for “Topic A”, so it rises higher in the Most Votes list
  12. User Story Example Suggesting a Topic As an LVRUG Member

    In order to learn cool stuff I want to suggest a topic for members to vote on
  13. User Story Example Assigning Presenter Bonus Points As an LVRUG

    Organizer In order to encourage more 1st time topic volunteers When a 1st time volunteer presents a topic, they should receive 10 extra bonus points.
  14. But what about Capybara? Drives a Browser to interact with

    your app Can visit URL paths and follow/click links Interacts with form fields and clicks buttons Can search HTML text via CSS and XPath Provides matchers to verify the presence of HTML elements and content
  15. But what about Capybara? Provides a headless Rack App HTML

    browser Also supports Selenium web driver, for JavaScript interactions Can integrate with capybara-webkit gem, to have headless HTML + JavaScript browsing. Will use Launchy gem to save HTML as a rendered page for browser viewing
  16. RSpec + Capybara Example Voting for a Topic As an

    LVRUG Member In order to indicate that I want to see “Topic A” presented I want to give my “vote” to Topic A, so that it has more votes than Topic B
  17. RSpec + Capybara Example Suggesting a Topic As an LVRUG

    Member In order to learn cool stuff I want to suggest a topic for members to vote on
  18. Capybara Resources https:/ /github.com/jnicklas/capybara https:/ /github.com/thoughtbot/capybara- webkit https:/ /github.com/copiousfreetime/launchy https:/

    /github.com/jonleighton/poltergeist http:/ /www.elabs.se/blog/51-simple-tricks-to- clean-up-your-capybara-tests