Capybara
02 / 02

Scoping, Form Controls & the Testing Pyramid

Scoping, Form Controls & the Testing Pyramid

Scoping with within

within('#sidebar') do
  click_link 'Settings'
end

within scopes subsequent DSL calls to search only inside the given element — useful when the same text ("Settings") appears in multiple places on a page, like a sidebar and a header.

Full Form Control Support

select 'Ruby', from: 'Language'
check 'Subscribe to newsletter'
uncheck 'Show advanced options'

select, check, and uncheck extend the interaction simulation beyond plain text fields to dropdowns, checkboxes, and radio buttons — necessary for realistically exercising complete real-world forms.

Selecting by User-Facing Text

Capybara's philosophy favors finding elements by visible text/labels rather than CSS classes or IDs — tests written this way stay resilient to internal implementation/styling refactors that don't actually change user-facing behavior, the same principle behind tools like Testing Library.

Feature Tests in the Testing Pyramid

Capybara feature tests (commonly paired with RSpec in Rails system/feature specs) give high confidence a complete flow works end-to-end, but run slower and can be more brittle than narrower unit tests — flakiness from shared state (unreset database, port conflicts) is a known risk in parallelized suites specifically. A balanced suite uses feature tests for critical end-to-end flows and faster unit tests for detailed logic coverage, rather than relying on either exclusively. Headless browsers (headless Chrome via Cuprite/Selenium) let CI run full-fidelity JS tests without a display attached.

Keep your own version of these notes — editable, searchable, and organised by your stack.

Start free