New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Define aria:
and data:
Capybara expression filters
#49665
Define aria:
and data:
Capybara expression filters
#49665
Conversation
require "rails/testing/capybara_extensions" | ||
|
||
Rails.configuration.after_initialize do | ||
Capybara::Selector.all.each do |name, _| |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ideally, this would be a configurable list that defaults to all selectors. Since this file is required from railties/lib/rails/test_help.rb
, I wasn't sure how to defer this code to be late enough to read from a configuration value. I would appreciate any guidance on how to achieve that.
if defined?(Capybara) | ||
require "rails/testing/capybara" | ||
end |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Like I mentioned in the PR description, this felt like the most global place to do this extension, since ActionDispatch::SystemTestCase
, ActionDispatch::IntegrationTest
, ActionView::TestCase
, and maybe even ActionMailer::TestCase could benefit from defining Capybara
extensions.
Having said that, it feels like there is a more appropriate place to do so, or an entirely different and more appropriate mechanism.
8e2fa66
to
cac23e1
Compare
d489e9f
to
95e9c31
Compare
When `Capybara` is available to the environment, define `aria:` and `data:` expression filters to match Action View's `aria: { ... }` and `data: { ... }` nesting: ```ruby tag.div id: "one", aria: { labelledby: "two" }, data: { nested_key: "value" } # => <div id="one" aria-labelledby="two" data-nested-key="value"></div> assert_selector :element, aria: { labelledby: "two" }, data: { nested_key: "value" } ``` Originally, these filters were proposed upstream, but ultimately, the Capybara team felt they'd be better suited for Rails, since the `aria:` and `data:` nesting are unique to Action View. This commit implements them as part of loading `railties`. The rationale for extending them from `rails/test_help` is that test cases other than `ActionDispatch::SystemTestCase` have the ability to leverage Capybara. For example: * by [extending ActionView::TestCase](https://edgeguides.rubyonrails.org/testing.html#testing-view-partials) * by [extending ActionDispatch::TestCase](rails#43361) * from third-party gems like [ViewComponent](https://viewcomponent.org/guide/testing.html) To that end, this commit includes test coverage for `ActionView::TestCase` and `ActionDispatch::IntegrationTest` to exercise those possibilities.
95e9c31
to
b9911f9
Compare
This style of extension is discouraged by Capybara, so I'm closing in favor of a gem. If there's interested in adding this support directly, I'm happy to re-open. |
Motivation / Background
When
Capybara
is available to the environment, definearia:
anddata:
expression filters to match Action View'saria: { ... }
anddata: { ... }
nesting:Detail
This commit implements them as part of loading
railties
. The rationale for extending them fromrails/test_help
is that test cases other thanActionDispatch::SystemTestCase
have the ability to leverage Capybara. For example:To that end, this commit includes test coverage for
ActionView::TestCase
andActionDispatch::IntegrationTest
to exercise those possibilities.Additional information
Originally, these filters were proposed upstream in teamcapybara/capybara#2709 and teamcapybara/capybara#2708.
Ultimately, the Capybara team felt they'd be better suited for Rails, since the
aria:
anddata:
nesting are unique to Action View.Checklist
Before submitting the PR make sure the following are checked:
[Fix #issue-number]