Class: RuboCop::Cop::Capybara::VisibilityMatcher

Inherits:
Base
  • Object
show all
Defined in:
lib/rubocop/cop/capybara/visibility_matcher.rb

Overview

Checks for boolean visibility in Capybara finders.

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`. Read more in www.rubydoc.info/gems/capybara/Capybara%2FNode%2FFinders:all[the documentation].

Examples:

# bad
expect(page).to have_selector('.foo', visible: false)
expect(page).to have_css('.foo', visible: true)
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)

Constant Summary collapse

MSG_FALSE =
'Use `:all` or `:hidden` instead of `false`.'
MSG_TRUE =
'Use `:visible` instead of `true`.'
CAPYBARA_MATCHER_METHODS =
%w[
  button
  checked_field
  css
  field
  link
  select
  selector
  table
  unchecked_field
  xpath
].flat_map do |element|
  ["have_#{element}".to_sym, "have_no_#{element}".to_sym]
end
RESTRICT_ON_SEND =
CAPYBARA_MATCHER_METHODS

Instance Method Summary collapse

Instance Method Details

#on_send(node) ⇒ Object



58
59
60
61
# File 'lib/rubocop/cop/capybara/visibility_matcher.rb', line 58

def on_send(node)
  visible_false?(node) { |arg| add_offense(arg, message: MSG_FALSE) }
  visible_true?(node) { |arg| add_offense(arg, message: MSG_TRUE) }
end

#visible_false?(node) ⇒ Object



54
55
56
# File 'lib/rubocop/cop/capybara/visibility_matcher.rb', line 54

def_node_matcher :visible_false?, <<~PATTERN
  (send nil? #capybara_matcher? ... (hash <$(pair (sym :visible) false) ...>))
PATTERN

#visible_true?(node) ⇒ Object



49
50
51
# File 'lib/rubocop/cop/capybara/visibility_matcher.rb', line 49

def_node_matcher :visible_true?, <<~PATTERN
  (send nil? #capybara_matcher? ... (hash <$(pair (sym :visible) true) ...>))
PATTERN