skip to content

With Capybara in a Rails system test, which finders and assertions wait for the page, and which read it once and race the browser?

level: middleimportance: must knowfreq 55%

answer

  1. synchronize retries until the wait expires
  2. default_max_wait_time is 2 seconds
  3. assert_no_selector waits for absence
  4. all returns once one match appears
  5. current_path is a plain read

basics

~20 s

Capybara's find, assert_selector, assert_text, assert_no_selector and assert_current_path retry for up to default_max_wait_time, 2 seconds. Plain reads such as page.current_path, element.text compared with ==, refute has_css? or counting all(...) do not wait for the expected state.

solid answer

~40 s

Capybara wraps its queries in `synchronize`, which retries on failure until `default_max_wait_time` (2 seconds) runs out, when the driver supports waiting. So `find(".seat", text: "B7")`, `assert_selector ".seat.taken", count: 3`, `assert_text "Booked"`, `assert_no_selector ".spinner"` and `assert_current_path booking_path(booking)` wait for the state you describe. Code that reads a value and compares it in Ruby does not: `assert_equal "/bookings/9", page.current_path`, `assert_equal "Booked", find(".status").text`, or `all(".seat.taken").size` captures one moment. `all` waits only until at least one match exists, then returns, so a count taken from it can be too low. `refute page.has_css?(".spinner")` fails at once while the spinner is still there, because `has_css?` waits for presence; `assert_no_selector` waits for absence. Put the expectation inside the Capybara query, and raise the wait per call with `wait:` or `using_wait_time`, not with `sleep`.

code

ruby · 11 lines
ruby
click_on "Book seat"

# Racy: one read, compared in Ruby
assert_equal booking_path(Booking.last), page.current_path
assert_equal 3, all(".seat.taken").size
refute page.has_css?(".spinner")

# Waiting: the expectation lives inside the query
assert_current_path booking_path(Booking.last)
assert_selector ".seat.taken", count: 3
assert_no_selector ".spinner"

go deeper

for a junior

Use assert_text, assert_selector and assert_current_path after a click, and never compare page.current_path with assert_equal.

for a middle

Explain synchronize and the 2-second default, why all returns early, and why assert_no_selector differs from refute has_css?.

for a senior

Review a flaky suite for Ruby-side comparisons and sleeps, and set per-call waits where pages are genuinely slow instead of raising the global wait.

for a principal

Establish team conventions, such as a lint rule or review checklist for waiting assertions, so browser tests stay trustworthy as the suite grows.

## Why waiting exists In a system test driven by Selenium, the browser and the test run concurrently. After `click_on "Book seat"`, the request goes to the Puma server thread, the response comes back, Turbo or a Stimulus controller updates the DOM, and only then is the new state visible. The test thread has moved on to its next line long before that. Capybara handles this with **synchronize**: a query that fails is retried every 0.01 seconds until it passes or `Capybara.default_max_wait_time` expires. The default wait time is **2 seconds**. Retries only happen when the driver supports waiting; Selenium does, `rack_test` does not because nothing there is asynchronous. ## What waits | Call | Waits for | |---|---| | `find(".seat", text: "B7")` | exactly one matching element, then returns it | | `first(".seat.free")` | at least one match, then returns the first | | `assert_selector ".seat.taken", count: 3` | the count to be exactly 3 | | `assert_text "Seat B7 booked"` | the text to appear | | `assert_no_selector ".spinner"` / `assert_no_text` | the element or text to be gone | | `assert_current_path booking_path(@booking)` | the URL to change | | `has_css?` / `has_no_css?` | presence, or absence, then return a boolean | | `click_on`, `fill_in` | the target element to exist before acting | `find` raises `Capybara::ElementNotFound` when nothing matches in time and `Capybara::Ambiguous` when more than one element matches under the default `match: :smart`. `Capybara::Ambiguous` and `Capybara::ExpectationNotMet`, which failed assertions raise, are both subclasses of `Capybara::ElementNotFound`. ## What does not wait These read the page once and hand a value to Ruby: - `page.current_path` and `page.current_url`, compared with `assert_equal`. - `find(".status").text` compared with `assert_equal`: the `find` waits for the element, but not for its text to change. - `all(".seat.taken").size`: `all` waits until **at least one** element matches, then returns whatever is there, so three seats being marked one after another can count as one. - `page.html.include?("Booked")`. ## Negative checks The subtle case is asserting that something has gone: 1. `refute page.has_css?(".spinner")` calls `has_css?`, which waits for the spinner to be **present**. While it is still visible, `has_css?` returns true at once and the refutation fails. 2. `assert_no_selector ".spinner"` or `assert page.has_no_css?(".spinner")` waits for it to be **absent** and passes as soon as it disappears. The same applies to text: use `assert_no_text`, never `refute page.has_text?`. ## Tuning the wait - Per call: `assert_text "Payment confirmed", wait: 10`. - Per block: `using_wait_time(10) { ... }`. - Globally: `Capybara.default_max_wait_time = 5` in the base class, which slows every failing check. `sleep 1` is the wrong tool: it always costs the full second, and on a slow CI machine it is still sometimes too short. A waiting assertion costs only as long as the browser needs. ## Scoping with within `within(".seat-map") { click_on "B7" }` scopes finders to one element. The scope element is itself found with waiting, and every query inside retries against it. Scoping prevents `Capybara::Ambiguous` when the same label, such as "B7", appears in both the seat map and a booking summary. ## The same rule in RSpec system specs rspec-rails system specs use Capybara's RSpec matchers, which follow the same waiting rules: - `expect(page).to have_text("Booked")` (alias `have_content`) and `expect(page).to have_css(".seat.taken", count: 3)` wait. - `expect(page).not_to have_css(".spinner")` waits for absence, because the negated matcher calls `assert_no_selector`. - `expect(page).to have_current_path(booking_path(booking))` waits; `expect(page.current_path).to eq(...)` does not, since `eq` only sees the string read at that instant. ## Stale elements An element found before a Turbo update may be replaced in the DOM a moment later. Capybara's `automatic_reload` setting, true by default, lets a waiting query re-find such an element inside `synchronize` instead of failing on a stale reference. Holding an element in a Ruby variable across several page updates still invites trouble; find it again close to where it is used. ## The rule of thumb Describe the state you expect **inside** a Capybara call, never by pulling a value out and comparing it in Ruby. Capybara can retry its own queries; it cannot retry an `assert_equal` that has already run.

  • Why does assert_equal 3, all(".seat.taken").size sometimes report 1?
    `all` adds an implicit `minimum: 1` and returns as soon as that is met, so it waits only until the first seat turns taken. If the other two update a moment later, the size read is 1. `assert_selector ".seat.taken", count: 3` puts the count inside the query, so Capybara retries until there are exactly three.
  • When does Capybara not wait at all, even with a waiting matcher?
    When the driver's `wait?` is false, as with `rack_test`, where nothing is asynchronous, and when you pass `wait: 0` or `wait: false`. In those cases the query is evaluated once and fails immediately if the state is not already there.
  • Does Capybara.default_max_wait_time make passing tests slower?
    No. A waiting query returns as soon as it passes, so the wait time only bounds how long a failing check retries. Raising it globally slows down failures and hides slow pages; raising it per call with `wait:` keeps the extra patience where it is needed.

Asserting with Capybara is like asking a waiter to tell you when the soup arrives; reading page.current_path is glancing at the table once and declaring there is no soup.

  • assert_equal on page.current_path waits for the redirect
  • refute page.has_css? waits for the spinner to disappear
  • all(...).size waits until every matching element has appeared
  • A sleep before each assertion is the reliable fix for timing
  • default_max_wait_time makes every passing assertion take 2 seconds