Skip to content

Instantly share code, notes, and snippets.

@baweaver
Created March 14, 2023 20:40
RailsConf 2023 talk

Teaching Capybara Testing - An Illustrated Adventure

Description

100%! Red the lemur was so excited to have 100% test coverage, finally, after unit testing all of his Ruby code. With that, he was done, the code was tested, and he could call it a day.

That was until a magical Capybara entered through his window and asked a question that changed his perceptions forever more:

"But what about your end to end tests?"

...and when he did, everything broke, and he was enlightened.

Join us on an illustrated adventure through the basics of end to end testing with Capybara.

Session format: Talk Level: Introductory and Overview Track: Tech Skills

Notes and comments

Theme: Tech Skills / Getting Started

Intended Audience

Introductory level content on end to end testing with Capybara

Expected Outcomes

  • Ability to run basic acceptance and end to end tests
  • Ability to avoid common flakes
  • Ability to debug and live-develop specs

Outline

(Dashes because Sessionize removes indentation)

  • Introduction
    • 100% test coverage! Celebrate
    • Capybara arrives - What about E2E?
      • Where did you come from!?
      • Snuck in through a gap in your unit tests
      • Here to help
    • Red riding Capybara into the tests
  • Types of Testing
    • Unit, Integration, E2E for now. Isolated pieces, combination of pieces, entire flows.
    • E2E proves things work from your user's perspective
    • Unit tests might stub too much, integration may not have wide enough range
    • Testing pyramid - How much of each?
    • Costs - Brittle, but we can talk about that
  • Our First E2E Spec
    • Library manager - How do we check out a book? How do we return one?
    • "Live" building by visiting site with binding.pry (No live code, I've learned better than that)
    • Screenshots from failures, saving page to inspect locally
    • We stubbed all the API calls to our book repository, it's actually broken here
    • Always assumed 3rd parties will break, prepare for them
    • Graceful degradation, testing for it, failing well
  • Brittleness
    • Waiting and waiting - Make sure to use waiting helpers
    • Well scoped - Describe where on your page something should be
    • Accessible - Accessibility is testability, makes it easier to work with, mention React Testing Library order or priority on searching for testable areas
      • ...or, more simply, don't test tightly coupled to page structure
      • Focus instead on roles and accessibility elements
      • Bonus points, site is more accessible as well as more testable
  • Outro
    • Recap on learnings
    • Capybara flies away

Pitch

We ran a survey at work for testing experience. People rated themselves fairly well in RSpec, sure, but Capybara? That was something like 2.3/5.0! The last few months I've been working to improve that number, to create company-level guidance on testing patterns in Capybara, and to create educational materials for folks.

This talk is distilling all of that knowledge into a merry little jaunt through the world of Capybara testing with a magical Capybara that acts as a literal test runner.

Also there are lemurs. So many lemurs.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment