Capybara

Capybara/AbsoluteXPathInElementScope

Enabled by default Safe Supports autocorrection Version Added Version Changed

Pending

Yes

No

3.1

-

Checks for absolute XPath searches on Capybara elements.

An XPath starting with // searches from the document root, even when the query is called on an element. Use .// to stay within it. Direct RSpec let and named subject element definitions are recognized.

Examples

# bad
page.find('.dialog').find(:xpath, '//button')
dialog = page.find('.dialog')
dialog.find(:xpath, '//button')
RSpec.describe 'dialog' do
  let(:dialog) { find('.dialog') }
  it { dialog.find(:xpath, '//button') }
end

# good
page.find('.dialog').find(:xpath, './/button')
page.find(:xpath, '//button')

Capybara/AlwaysTrueCountQuery

Enabled by default Safe Supports autocorrection Version Added Version Changed

Pending

Yes

No

3.1

-

Checks for positive queries that succeed when there are no matches.

Examples

# bad
expect(page).to have_css('.item', minimum: 0)
page.has_css?('.item', minimum: 0)

# good
expect(page).to have_css('.item')
expect(page).to have_css('.item', minimum: 0, maximum: 2)
all('.item', minimum: 0)

Capybara/AmbiguousClick

Enabled by default Safe Supports autocorrection Version Added Version Changed

Disabled

Yes

No

2.22

-

Specify the exact target to click on.

In projects where accessibility needs to be considered, it is crucial to specify the click target precisely.

Examples

# bad
click_link_or_button('foo')
click_on('foo')

# good
click_link('foo')
click_button('foo')

Capybara/AssertStyle

Enabled by default Safe Supports autocorrection Version Added Version Changed

Enabled

Yes

Always

2.23

3.0

Checks for usage of deprecated assert style method.

Examples

# bad
page.find(:css, '#first').assert_style(display: 'block')

# good
page.find(:css, '#first').assert_matches_style(display: 'block')

Capybara/ChainedFind

Enabled by default Safe Supports autocorrection Version Added Version Changed

Pending

Yes

Always

3.1

-

Avoid chaining find methods and combine selectors.

Combining selectors into a single find call is more efficient than chaining multiple find calls, as it reduces the number of DOM queries. Only recognizable Capybara finder chains with string locators are checked. Set DefaultSelector to match Capybara.default_selector, or to null if it varies at runtime. Autocorrection requires css and literal strings.

Examples

# bad
page.find('#foo').find('.bar')
page.find('#foo').find('.bar').find('.baz')

# good
page.find('#foo .bar')
page.find('#foo .bar .baz')
find('#form').find(:button, 'Save')

Configurable attributes

Name Default value Configurable values

DefaultSelector

css

String

Capybara/FindAllFirst

Enabled by default Safe Supports autocorrection Version Added Version Changed

Enabled

Yes

Always (Unsafe)

2.22

3.0

Enforces use of first instead of all with first or [0].

Safety

With default options, all(…​).first and all(…​)[0] return nil when no element matches, while first(…​) raises an exception. Autocorrection can break code that depends on nil.

Examples

# bad
all('a').first
all('a')[0]

# good
first('a')

Capybara/IgnoredQueryOption

Enabled by default Safe Supports autocorrection Version Added Version Changed

Pending

Yes

Always

3.1

-

Checks for query options that have no effect in Capybara.

Count options passed to singular finders are not autocorrected because removing them may conceal the author’s intent.

This cop targets Capybara’s built-in selectors. Set DefaultSelector to match Capybara.default_selector, or to null if it varies at runtime. Queries using custom or unknown selectors are skipped.

Examples

# bad
find('.row', count: 3)
find(:css, '.item', exact: true)
find('.item', text: /foo/, exact_text: true)
all(:css, '.item', match: :first)
expect(page).to have_css('.item', count: 3, minimum: 1)

# good
all('.row', count: 3)
find(:css, '.item')
find('.item', text: /foo/)
all(:css, '.item')
expect(page).to have_css('.item', count: 3)

DefaultSelector: custom

# bad
find(:css, '.item', text: /foo/, exact_text: true)

# good
find('.item', text: /foo/, exact_text: true)

Configurable attributes

Name Default value Configurable values

DefaultSelector

css

String

Capybara/ModalMethodWithoutBlock

Enabled by default Safe Supports autocorrection Version Added Version Changed

Pending

Yes

No

3.1

-

Checks that modal handling methods are called with a block.

Examples

# bad
accept_confirm
page.accept_prompt(with: 'answer')

# good
accept_confirm { click_button 'Delete' }
page.accept_prompt(with: 'answer', &trigger_modal)

Capybara/NativeAttribute

Enabled by default Safe Supports autocorrection Version Added Version Changed

Pending

Yes

Always (Unsafe)

3.1

-

Enforces use of Element#[] instead of native.attribute.

Only calls on Capybara finder chains are checked.

Safety

This cop’s autocorrection is unsafe because attribute values may differ between the driver API and Capybara’s Element#[].

Examples

# bad
find('.item').native.attribute(:id)
page.find('.item').native.attribute('aria-label')

# good
find('.item')[:id]
page.find('.item')['aria-label']

Capybara/RedundantWithinFind

Enabled by default Safe Supports autocorrection Version Added Version Changed

Enabled

Yes

Always

2.20

3.0

Checks for redundant within find(…​) calls.

Examples

# bad
within find('foo.bar') do
  # ...
end

# good
within 'foo.bar' do
  # ...
end

# bad
within find_by_id('foo') do
  # ...
end

# good
within :id, 'foo' do
  # ...
end

Capybara/SpecificActions

Enabled by default Safe Supports autocorrection Version Added Version Changed

Enabled

Yes

No

2.14

3.0

Checks for there is a more specific actions offered by Capybara.

Examples

# bad
find('a[href="/home"]').click
find('button.cls').click
find('a[href="/home"]', exact_text: 'foo').click
find('div button').click

# good
click_link(href: '/home')
click_button(class: 'cls')
click_link(href: '/home', exact_text: 'foo')
find('div').click_button

Capybara/SpecificFinders

Enabled by default Safe Supports autocorrection Version Added Version Changed

Enabled

Yes

Always

2.13

3.0

Checks if there is a more specific finder offered by Capybara.

Examples

# bad
find('#some-id')
find('[id=some-id]')
find(:css, '#some-id')
find(:id, 'some-id')
find(:link, 'Home')
find(:field, 'Name')
find('input[placeholder="Email"]')
find(:css, 'input[type="checkbox"]')

# good
find_by_id('some-id')
find_link('Home')
find_field('Name')
find_field(placeholder: 'Email')
find_field(type: 'checkbox')

Capybara/UnorderedWindowAccess

Enabled by default Safe Supports autocorrection Version Added Version Changed

Pending

Yes

No

3.1

-

Checks positional access to Capybara windows, whose order is undefined.

Examples

# bad
popup = windows.last
original = page.windows.first

# good
popup = window_opened_by { click_link 'Open' }
original = current_window

Capybara/VisibilityOption

Enabled by default Safe Supports autocorrection Version Added Version Changed

Pending

Yes

Always

3.1

-

Checks for boolean values passed to Capybara’s visible option.

visible: false finds both visible and hidden elements, rather than only hidden elements. Use :all, :hidden or :visible instead. Only true is autocorrected, to :visible.

This cop targets Capybara’s built-in selectors. Set DefaultSelector to match Capybara.default_selector, or to null if it varies at runtime. Queries using custom or unknown selectors are skipped.

Examples

# bad
find('.menu', visible: true)
expect(page).to have_css('.menu', visible: false)

# good
find('.menu', visible: :visible)
find('.menu', visible: :all)
find('.menu', visible: :hidden)

DefaultSelector: custom

# bad
find(:css, '.menu', visible: true)

# good
find('.menu', visible: true)

Configurable attributes

Name Default value Configurable values

DefaultSelector

css

String