skip to content

A Rails system test that releases a held cinema seat behind a confirm dialog fails one CI run in ten; how do you diagnose and fix it?

level: seniorimportance: should knowfreq 40%

answer

  1. dialog handled inside the block
  2. assert the result, not the click
  3. failure screenshots in tmp/screenshots
  4. raise_server_errors surfaces app exceptions
  5. within the seat map, then text match

basics

~20 s

Wrap the click in accept_confirm("Release seat B7?") so Capybara handles the dialog, then assert the outcome with a waiting matcher such as assert_selector before touching the database. Read the failure screenshot and HTML in tmp/screenshots, and remove sleeps.

solid answer

~50 s

Start from evidence: Rails' system tests take a screenshot on failure into `tmp/screenshots`, and `RAILS_SYSTEM_TESTING_SCREENSHOT_HTML=1` saves the page HTML too. Typical causes in this test: the dialog is answered by code outside Capybara or not at all, so wrap the click as `accept_confirm("Release seat B7?") { click_on "Release B7" }`, which also checks the text and raises `Capybara::ModalNotFound` if no matching dialog opens; the next line reads the database with `seat.reload.status` before the Puma thread has handled the request, so assert the UI first with `assert_selector ".seat.free", text: "B7"`; the button is found ambiguously or before the seat map finished rendering, so scope with `within(".seat-map")`; and a `sleep` sized for a fast laptop. Re-run the test with the failing seed and several times in a loop to confirm. An app exception in the server thread is re-raised in the test by Capybara's `raise_server_errors`, so read that error first.

code

bash · 3 lines
bash
RAILS_SYSTEM_TESTING_SCREENSHOT_HTML=1 bin/rails test test/system/seat_releases_test.rb
ls tmp/screenshots
for i in $(seq 1 20); do bin/rails test test/system/seat_releases_test.rb || break; done

go deeper

for a junior

Use accept_confirm around the click that opens a dialog, and check the page with assert_selector instead of sleeping.

for a middle

Explain why the database read races the server thread, and how within and specific locators prevent ambiguous clicks.

for a senior

Lead the diagnosis from failure screenshots, saved HTML, re-raised server errors and repeated runs, and fix root causes rather than raising timeouts.

for a principal

Set a quarantine-and-fix policy for flaky browser tests so CI stays trusted without hiding real regressions.

## The test that flakes A cinema-booking app lets a customer hold a seat and release it. Releasing uses a button with a Turbo confirmation, which by default calls the browser's native `confirm()`: ```ruby test "releasing a held seat" do visit screening_url(screenings(:evening)) page.driver.browser.switch_to.alert.accept rescue nil click_on "Release B7" sleep 0.5 assert_equal "free", seats(:b7).reload.status end ``` It passes locally and fails roughly once in ten CI runs. Each line hides a timing assumption. ## Step 1: collect evidence Rails' system tests already help: - On failure, `take_failed_screenshot` runs in teardown and writes `tmp/screenshots/failures_<test name>.png`; the generated CI workflow uploads that folder. - `RAILS_SYSTEM_TESTING_SCREENSHOT_HTML=1` also saves the HTML at the moment of failure, which shows hidden elements and the DOM state. - `take_screenshot` can be called mid-test, numbered in sequence, to see intermediate states. - If the app raised in the Puma thread, Capybara re-raises it in the test thread, because `Capybara.raise_server_errors` is true: when a following query fails, on the next `visit`, or at the latest during session reset in teardown. The failure may already name the real exception. ## Step 2: fix the dialog handling The `switch_to.alert` line runs **before** the click, when no dialog exists, and the `rescue nil` swallows that. Then the click opens a dialog nobody answers. Capybara's modal API handles this in the right order: ```ruby accept_confirm("Release seat B7?") do click_on "Release B7" end ``` `accept_confirm` runs the block that triggers the dialog, waits for the dialog, checks its text, and accepts it. If no dialog with that text appears in time, it raises `Capybara::ModalNotFound`, which turns a silent mis-click into a clear error. `dismiss_confirm` does the opposite for the "keep my seat" path. ## Step 3: assert the outcome where it appears first `sleep 0.5` then a database read is a race between the test thread and the server thread. The click returns once the browser has dispatched the event; the request, the update and the response happen afterwards in Puma. Fix it in this order: 1. Assert what the user sees, with a waiting matcher: `assert_selector ".seat.free", text: "B7"`. 2. Only then, if still needed, check the database: by the time the seat shows as free, the server has finished the update. 3. Delete the `sleep`; the waiting matcher costs only as long as the page needs. ## Step 4: make the target unambiguous and ready - The text "B7" may appear in the seat map and in a summary panel. `within(".seat-map") { ... }` scopes the search, and `find` raises `Capybara::Ambiguous` instead of clicking the wrong one at random. - If the seat map is rendered by a Stimulus controller, finders wait for the button to exist before clicking. Problems remain only when an element exists but is covered or moving, so assert the settled state first, for example `assert_selector ".seat-map[data-ready]"` if the controller marks itself ready. ## Step 5: prove it is fixed - Run the test file repeatedly, for example in a shell loop, until a failure would have appeared several times over. - Run it with the seed from the failing CI build, since order-dependent data can look like flakiness. - Check that tests do not share state outside the database, such as class-level caches or files. ## The corrected test ```ruby test "releasing a held seat" do visit screening_url(screenings(:evening)) within ".seat-map" do accept_confirm("Release seat B7?") { click_on "Release B7" } end assert_selector ".seat.free", text: "B7" assert_equal "free", seats(:b7).reload.status end ``` ## Preventing the next one - Prefer locators that do not change with copy or layout: a button's text plus a `within` scope, or a dedicated attribute where text is ambiguous. - Make components announce readiness when they render client-side, and assert that state before interacting. - Keep side-effect checks behind a waiting assertion on the page, as a habit in code review. - Keep the number of browser tests small, so each flaky one is noticed and fixed rather than retried. ## What to take into an interview | Symptom | Likely cause | Fix | |---|---|---| | fails at the dialog | dialog answered outside Capybara | `accept_confirm` around the click | | DB still shows old value | read raced the server thread | waiting UI assertion first | | wrong element clicked | ambiguous text | `within` and specific locators | | fails only on CI | `sleep` tuned to a fast machine | remove sleeps, use waits |

  • What does accept_confirm raise when the click opens no confirm dialog?
    `Capybara::ModalNotFound`, after waiting for a dialog for at least Capybara's default wait time. Passing the expected text, as in `accept_confirm("Release seat B7?")`, also makes it raise when a different dialog appears, which catches a button that changed its wording or behaviour.
  • Why can reading seats(:b7).reload.status right after click_on see the old value?
    The click returns once the browser dispatched the event. The request is then handled by Puma in another thread of the test process, so the update may not have run yet when the test thread reads the row. A waiting assertion on the page first guarantees the response has arrived.

  • Raising default_max_wait_time globally is the proper fix for this flake
  • click_on returns only after the server has finished the request
  • A sleep of one second makes the dialog handling reliable
  • Calling switch_to.alert before the click answers the dialog it opens
  • Rails only saves failure screenshots when take_screenshot is called explicitly