Capybara/RSpec
Capybara/RSpec/CurrentPathExpectation
| Enabled by default | Safe | Supports autocorrection | Version Added | Version Changed |
|---|---|---|---|---|
Enabled |
Yes |
Always |
1.18 |
3.0 |
Checks that no expectations are set on Capybara’s current_path.
The
have_current_path matcher
should be used on page to set expectations on Capybara’s
current path, since it uses
Capybara’s waiting functionality
which ensures that preceding actions (like click_link) have
completed.
This cop does not support autocorrection in some cases.
Capybara/RSpec/CurrentUrlExpectation
| Enabled by default | Safe | Supports autocorrection | Version Added | Version Changed |
|---|---|---|---|---|
Pending |
Yes |
Always (Unsafe) |
3.1 |
- |
Checks expectations on current_url that bypass Capybara’s waiting.
have_current_path can match full URLs and waits for navigation.
A regexp needs url: true so it matches the full URL, not just the
path. Autocorrection is unsafe because URL parsing and waiting change
comparison behavior.
Dynamic match patterns and include, start_with, and end_with
are reported without autocorrection.
Examples
# bad
expect(page.current_url).to eq('https://example.com/login')
expect(current_url).to match(%r{/login})
expect(current_url).to match('login')
expect(page.current_url).to include('/login')
# good
expect(page).to have_current_path('https://example.com/login')
expect(page).to have_current_path(%r{/login}, url: true)
expect(page).to have_current_path(/login/, url: true)
Capybara/RSpec/FindElementValueEq
| Enabled by default | Safe | Supports autocorrection | Version Added | Version Changed |
|---|---|---|---|---|
Pending |
Yes |
Always (Unsafe) |
3.1 |
- |
Checks for expectations on the value of an element found by Capybara.
Safety
This cop’s autocorrection is unsafe because find('input').value
checks the value of the first matched element, while have_field
checks that any field on the page has the expected value.
Capybara/RSpec/HaveContent
| Enabled by default | Safe | Supports autocorrection | Version Added | Version Changed |
|---|---|---|---|---|
Enabled |
Yes |
Always |
2.23 |
3.0 |
Checks for usage of have_content and have_no_content.
Capybara provides have_text and have_no_text matchers that are
more concise and preferred over their aliases have_content and
have_no_content.
Capybara/RSpec/HaveSelector
| Enabled by default | Safe | Supports autocorrection | Version Added | Version Changed |
|---|---|---|---|---|
Enabled |
Yes |
Always |
2.19 |
3.0 |
Use CSS or XPath matchers instead of generic selector matchers.
Examples
# bad
expect(foo).to have_selector(:css, 'bar')
expect(foo).to have_no_selector(:css, 'bar')
# good
expect(foo).to have_css('bar')
expect(foo).to have_no_css('bar')
# bad
expect(foo).to have_selector(:xpath, 'bar')
expect(foo).to have_no_selector(:xpath, 'bar')
# good
expect(foo).to have_xpath('bar')
expect(foo).to have_no_xpath('bar')
Capybara/RSpec/MatchStyle
| Enabled by default | Safe | Supports autocorrection | Version Added | Version Changed |
|---|---|---|---|---|
Enabled |
Yes |
Always |
2.23 |
3.0 |
Checks for usage of deprecated style methods in RSpec matchers.
Capybara/RSpec/NegationMatcher
| Enabled by default | Safe | Supports autocorrection | Version Added | Version Changed |
|---|---|---|---|---|
Enabled |
Yes |
Always |
2.14 |
3.0 |
Enforces use of have_no_* or not_to for negated expectations.
Examples
Capybara/RSpec/NegationMatcherAfterVisit
| Enabled by default | Safe | Supports autocorrection | Version Added | Version Changed |
|---|---|---|---|---|
Enabled |
Yes |
No |
2.22 |
3.0 |
Do not allow negative matchers to be used immediately after visit.
Examples
# bad
visit foo_path
expect(page).to have_no_link('bar')
expect(page).to have_css('a')
# good
visit foo_path
expect(page).to have_css('a')
expect(page).to have_no_link('bar')
# bad
visit foo_path
expect(page).not_to have_link('bar')
expect(page).to have_css('a')
# good
visit foo_path
expect(page).to have_css('a')
expect(page).not_to have_link('bar')
Capybara/RSpec/PredicateMatcher
| Enabled by default | Safe | Supports autocorrection | Version Added | Version Changed |
|---|---|---|---|---|
Enabled |
Yes |
Always |
2.19 |
3.0 |
Prefer using predicate matcher over using predicate method directly.
Capybara defines magic matchers for predicate methods. This cop recommends to use the predicate matcher instead of using predicate method directly.
Examples
Strict: true, EnforcedStyle: inflected (default)
# bad
expect(foo.matches_css?(bar: 'baz')).to be_truthy
expect(foo.matches_selector?(bar: 'baz')).to be_truthy
expect(foo.matches_style?(bar: 'baz')).to be_truthy
expect(foo.matches_xpath?(bar: 'baz')).to be_truthy
# good
expect(foo).to match_css(bar: 'baz')
expect(foo).to match_selector(bar: 'baz')
expect(foo).to match_style(bar: 'baz')
expect(foo).to match_xpath(bar: 'baz')
# also good - It checks "true" strictly.
expect(foo.matches_style?(bar: 'baz')).to be(true)
Strict: false, EnforcedStyle: inflected
# bad
expect(foo.matches_style?(bar: 'baz')).to be_truthy
expect(foo.matches_style?(bar: 'baz')).to be(true)
# good
expect(foo).to match_style(bar: 'baz')
Capybara/RSpec/SpecificMatcher
| Enabled by default | Safe | Supports autocorrection | Version Added | Version Changed |
|---|---|---|---|---|
Enabled |
Yes |
No |
2.12 |
3.0 |
Checks for there is a more specific matcher offered by Capybara.
Examples
# bad
expect(page).to have_selector('button')
expect(page).to have_no_selector('button.cls')
expect(page).to have_css('button')
expect(page).to have_no_css('a.cls', href: 'http://example.com')
expect(page).to have_css('table.cls')
expect(page).to have_css('select')
expect(page).to have_css('input', exact_text: 'foo')
# good
expect(page).to have_button
expect(page).to have_no_button(class: 'cls')
expect(page).to have_button
expect(page).to have_no_link('foo', class: 'cls', href: 'http://example.com')
expect(page).to have_table(class: 'cls')
expect(page).to have_select
expect(page).to have_field(with: 'foo')
Capybara/RSpec/VisibilityMatcher
| Enabled by default | Safe | Supports autocorrection | Version Added | Version Changed |
|---|---|---|---|---|
Enabled |
Yes |
Always (Unsafe) |
1.39 |
Checks for boolean visibility in Capybara RSpec matchers.
Capybara lets you find elements that match a certain visibility using
the :visible option. :visible accepts both boolean and symbols as
values, however using booleans can have unwanted effects. visible:
false does not find just invisible elements, but both visible and
invisible elements. For expressiveness and clarity, use one of the
symbol values, :all, :hidden or :visible.
Only true is autocorrected, to :visible.
Set DefaultSelector to match Capybara.default_selector, or to
null if it varies at runtime.
Read more at: https://www.rubydoc.info/gems/capybara/Capybara%2FNode%2FFinders:all
Safety
This cop’s autocorrection is unsafe because custom filters on
built-in selectors may treat true and :visible differently.
Examples
# bad
expect(page).to have_selector('.foo', visible: true)
expect(page).to have_css('.foo', visible: false)
expect(page).to have_link('my link', visible: false)
# good
expect(page).to have_selector('.foo', visible: :visible)
expect(page).to have_css('.foo', visible: :all)
expect(page).to have_link('my link', visible: :hidden)