Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Support Capybara assertions in ActionDispatch::IntegrationTest
This is a re-submission of #41291. Both `ActionDispatch::IntegrationTest` and Capybara in `:rack_test` mode are capable of exercising Rails applications that serve HTML over HTTP. Capybara itself provides a [wide-range of selectors][selectors], which can be [extended even further][capybara_accessible_selectors]. While Capybara's JavaScript far exceeds `ActionDispatch::IntegrationTest`' HTML-only support, their overlapping capabilities makes Capybara an interesting candidate to supplant `ActionDispatch::IntegrationTest`'s assertions. For example, Capybara provides a `:button` selector, which can be invoked by `assert_selector :button, "Button Text"` or `assert_button "Button Text"`. Additionally, Capybara's `button` selector supports resolution based on ARIA attributes like `aria-label` and `[role="button"]`. The assertions provided by `rails-dom-testing` do not. It's possible to recreate those assertion characteristics in `rails-dom-testing`, Rails itself, or a consumer application. However, given the fact that `capybara` is already a Rails testing dependency (through System Test support), and the fact that they're both capable of coordinating with `Rack::Test`, there is an opportunity to unify them. [selectors]: https://github.com/teamcapybara/capybara/tree/84acc29d5ff807507fe57aafcf7f9b2acdb89fe2/lib/capybara/selector/definition [capybara_accessible_selectors]: https://github.com/citizensadvice/capybara_accessible_selectors/tree/d61971c609e3b019df6dc0ea0c9ce11433f3d0f7#documentation Action Dispatch: Use `build_rack_mock_session` in test --- This commit introduces the `ActionDispatch::Assertions::CapybaraAssertions` module to override the `ActionDispatch::Integration::Session#_mock_session` implementation by substituting the `Rack::MockSession.new` call with a memoized delgation to the an already constructed `Rack::MockSession` instance created by the `RackTest`-driven `Capybara::Session`. This commit includes a new test case that extends the `Session` instance to integrate with `Capybara`.`` Alternatives --- This is currently possible, without any implementation changes. Applications could create their own versions of `ActionDispatch::Assertions::CapybaraAssertions`by overriding `_mock_session`. That feels like a blatant violation of the public API. Having said that, if a third-party package were to provide this kind of Capybara integration, it would have to re-open the class and target the private `_mock_session` method for overriding. Similarly, [open_session][]'s documentation mentions the pattern of using `#extend` on the block's `Session` instance. However, I'm not sure if there's a way to promote the block argument to serve globally as the overridded `#integration_session` that methods are delegated to. [Rack::Test::Methods]: https://github.com/rack/rack-test/blob/v1.1.0/lib/rack/test/methods.rb#L29-L31 [Capybara::RackTest::Browser]: https://github.com/teamcapybara/capybara/blob/3.35.3/lib/capybara/rack_test/browser.rb#L128-L131 [open_session]: https://github.com/rails/rails/blob/main/actionpack/lib/action_dispatch/testing/integration.rb#L377-L386
- Loading branch information