In a Rails system test, when would you use driven_by :rack_test instead of driven_by :selenium, using: :headless_chrome, and what stops working?
answer
- no browser, no JavaScript
- no server thread, Rack calls in-process
- wait? is false, so no retries
- NotSupportedByDriverError for modals
- Capybara's :selenium means Firefox
basics
~20 sdriven_by :rack_test drives the app through Rack in the test process, with no browser, server thread or JavaScript, so it is fast for server-rendered pages. Anything needing JavaScript, such as Stimulus, Turbo, confirm dialogs or screenshots, needs Selenium with headless Chrome.
solid answer
~40 s`:rack_test` is Capybara's in-process driver: it builds Rack requests, parses the returned HTML and follows links and forms itself. There is no browser, no Puma server thread and no JavaScript, so a test runs in milliseconds and cannot race the page. Rails registers it with `respect_data_method: true`. It suits pages whose behaviour is all server-side. `:selenium` with `using: :headless_chrome` starts real Chrome through WebDriver against Puma, so Stimulus controllers, Turbo navigation, confirm dialogs, CSS visibility and screenshots work, at the cost of seconds per test and timing races. With `rack_test`, `accept_confirm` and `save_screenshot` raise `Capybara::NotSupportedByDriverError`, failure screenshots are skipped, and Capybara does not retry finders, because the driver's `wait?` is false. A common setup is `rack_test` in `ApplicationSystemTestCase` with a JavaScript subclass using headless Chrome for the few flows that need it.
code
ruby · 17 lines# test/application_system_test_case.rb
class ApplicationSystemTestCase < ActionDispatch::SystemTestCase
driven_by :rack_test
end
# test/javascript_system_test_case.rb
class JavaScriptSystemTestCase < ApplicationSystemTestCase
driven_by :selenium, using: :headless_chrome, screen_size: [ 1400, 1400 ]
end
class ReleaseSeatTest < JavaScriptSystemTestCase
test "releasing a held seat asks for confirmation" do
visit screening_url(screenings(:evening))
accept_confirm("Release seat B7?") { click_on "Release B7" }
assert_selector ".seat.free", text: "B7"
end
endgo deeper
Know that rack_test has no browser and no JavaScript, and that headless Chrome is needed for dialogs, Stimulus and Turbo behaviour.
Explain how each driver executes a click, why only Selenium needs a server thread, and which Capybara features raise NotSupportedByDriverError under rack_test.
Design a suite that defaults to rack_test and opts into headless Chrome per class, and diagnose tests that break after switching drivers.
Weigh browser coverage against suite time across many flows, and decide when an alternative driver is worth the dependency.
## Two very different drivers Capybara separates the test vocabulary (`visit`, `click_on`, `assert_text`) from the **driver** that carries it out. Rails' `driven_by` chooses the driver for a test class. The two that matter in Rails 8.1 are: | | `driven_by :rack_test` | `driven_by :selenium, using: :headless_chrome` | |---|---|---| | Browser | none; Capybara parses HTML itself | real Chrome, no window | | App server | none; Rack calls in the test process | Puma in a thread of the test process | | JavaScript | never runs | runs: Stimulus, Turbo, inline scripts | | Speed | milliseconds per action | seconds per test, browser start-up | | Waiting and retries | none; the driver reports `wait?` as false | finders and assertions retry up to the wait time | | Confirm dialogs | `NotSupportedByDriverError` | `accept_confirm` / `dismiss_confirm` | | Screenshots | not supported; failure screenshots skipped | `take_screenshot`, automatic on failure | | CSS visibility | based on the HTML only | computed by the browser | ## How rack_test works `Capybara::RackTest::Driver` turns `visit "/screenings/4"` into a Rack request against the application object, then parses the response body. Clicking a link issues a new request for its `href`; submitting a form serialises the fields and sends the method and params. Because there is no browser, nothing in a `<script>` tag or an import map runs, and because there is no network, Capybara never starts a server: the driver's `needs_server?` is false. Rails registers its own instance with `respect_data_method: true`, so links carrying `data-method` are sent with that HTTP method even though no JavaScript runs. ## How selenium with headless Chrome works `driven_by :selenium, using: :headless_chrome` makes Rails register a `Capybara::Selenium::Driver` for Chrome with the headless flag added and the window sized from `screen_size:`. Capybara boots Puma in a separate thread, Chrome connects to it over HTTP, and every Capybara call becomes WebDriver commands. JavaScript runs, so a seat map built by a Stimulus controller or a Turbo Frame that loads lazily actually behaves as in production. The price is time and timing. Browser start-up, page loads and asynchronous updates all cost real seconds, and because the page changes after a click, Capybara must wait and retry, which is why its default wait time exists. ## What breaks when you switch to rack_test Moving a cinema-booking test from headless Chrome to `rack_test` to speed it up exposes every hidden JavaScript dependency: - `accept_confirm { click_on "Release seat" }` raises `Capybara::NotSupportedByDriverError`, because the base driver's `accept_modal` raises. - A seat map rendered by a Stimulus controller is empty, so `click_on "B7"` raises `Capybara::ElementNotFound`. - Elements hidden by JavaScript or by a CSS class are judged visible from the HTML only. - `take_screenshot` raises, and Rails' automatic failure screenshot is silently skipped because it checks the current driver first. ## A naming trap Capybara and Rails disagree about what `:selenium` means: - In plain Capybara, `Capybara.javascript_driver` and the registered `:selenium` driver default to **Firefox**. - In Rails, `driven_by :selenium` passes the browser from `using:`, which defaults to **`:chrome`**. So the same symbol opens a different browser depending on whether Rails' `driven_by` or Capybara's registry chose it. ## Choosing per test Ask three questions of each browser test: 1. Does the behaviour under test need JavaScript, a dialog, CSS visibility or a screenshot? If yes, it needs headless Chrome. 2. Is the page fully server-rendered, with links and forms doing all the work? Then `rack_test` gives the same confidence at a fraction of the time. 3. Would an integration test with `get` and `post` cover it? If the browser adds nothing, a system test is the wrong level altogether. ## A mixed setup Most suites need only a handful of JavaScript flows. A common shape: 1. `ApplicationSystemTestCase` uses `driven_by :rack_test` for server-rendered pages. 2. A subclass, for example `JavaScriptSystemTestCase`, calls `driven_by :selenium, using: :headless_chrome`. 3. Tests that need JavaScript, dialogs or screenshots inherit the subclass. Because `driven_by` is stored per class, each subclass keeps its own driver. Rails' `driven_by` also accepts `:cuprite` and `:playwright` and registers them for you once their gems are installed; the generated default is still Selenium with headless Chrome.
- Why does a rack_test system test not race the page the way a Selenium test can?There is nothing asynchronous: each Capybara action is a synchronous Rack call in the test process, and the response is fully rendered before the next line runs. The driver reports `wait?` as false, so Capybara does not retry either. Failures are deterministic, which also means a missing element fails at once rather than after the wait time.
- Why does Capybara not start Puma for a rack_test test?Capybara only boots a server when the driver's `needs_server?` is true. Selenium needs one because Chrome must connect over HTTP; `rack_test` calls the Rack application object directly, so no server thread, port or network is involved.
- rack_test runs JavaScript, just more slowly than Chrome
- accept_confirm works with every Capybara driver
- rack_test still boots Puma in a server thread
- Capybara's own :selenium driver defaults to Chrome
- Rails takes a failure screenshot under rack_test too