Module: RuboCop::Cop::Capybara::CapybaraHelp Private

Defined in:
lib/rubocop/cop/capybara/mixin/capybara_help.rb

Overview

This module is part of a private API. You should avoid using this module if possible, as it may be removed or be changed in the future.

Help methods for capybara.

Constant Summary collapse

COMMON_OPTIONS =

This constant is part of a private API. You should avoid using this constant if possible, as it may be removed or be changed in the future.

%w[
  id class style
].freeze
SPECIFIC_OPTIONS =

This constant is part of a private API. You should avoid using this constant if possible, as it may be removed or be changed in the future.

{
  'button' => (
    COMMON_OPTIONS + %w[disabled name value title type]
  ).freeze,
  'link' => (
    COMMON_OPTIONS + %w[href alt title download]
  ).freeze,
  'table' => (
    COMMON_OPTIONS + %w[cols rows]
  ).freeze,
  'select' => (
    COMMON_OPTIONS + %w[
      disabled name placeholder
      selected multiple
    ]
  ).freeze,
  'field' => (
    COMMON_OPTIONS + %w[
      checked disabled name placeholder
      readonly type multiple
    ]
  ).freeze
}.freeze
SPECIFIC_PSEUDO_CLASSES =

This constant is part of a private API. You should avoid using this constant if possible, as it may be removed or be changed in the future.

%w[
  not() disabled enabled checked unchecked
].freeze

Class Method Summary collapse

Class Method Details

.common_attributes?(selector) ⇒ Boolean

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Examples:

common_attributes?('a[focused]') # => true
common_attributes?('button[focused][visible]') # => true
common_attributes?('table[id=some-id]') # => true
common_attributes?('h1[invalid]') # => false

Parameters:

  • selector (String)

Returns:

  • (Boolean)


61
62
63
# File 'lib/rubocop/cop/capybara/mixin/capybara_help.rb', line 61

def common_attributes?(selector)
  CssSelector.attributes(selector).keys.difference(COMMON_OPTIONS).none?
end

.include_option?(node, option) ⇒ Boolean

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Parameters:

  • node (RuboCop::AST::SendNode)
  • option (Symbol)

Returns:

  • (Boolean)


125
126
127
# File 'lib/rubocop/cop/capybara/mixin/capybara_help.rb', line 125

def include_option?(node, option)
  node.each_descendant(:sym).find { |opt| opt.value == option }
end

.replaceable_attributes?(attrs) ⇒ Boolean

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Examples:

replaceable_attributes?('table[id=some-id]') # => true
replaceable_attributes?('a[focused]') # => false

Parameters:

  • attrs (Array<String>)

Returns:

  • (Boolean)


70
71
72
# File 'lib/rubocop/cop/capybara/mixin/capybara_help.rb', line 70

def replaceable_attributes?(attrs)
  attrs.values.none?(&:nil?)
end

.replaceable_element?(node, element, attrs) ⇒ Boolean

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Parameters:

  • node (RuboCop::AST::SendNode)
  • element (String)
  • attrs (Array<String>)

Returns:

  • (Boolean)


108
109
110
111
112
113
# File 'lib/rubocop/cop/capybara/mixin/capybara_help.rb', line 108

def replaceable_element?(node, element, attrs)
  case element
  when 'link' then replaceable_to_link?(node, attrs)
  else true
  end
end

.replaceable_option?(node, locator, element) ⇒ Boolean

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Parameters:

  • node (RuboCop::AST::SendNode)
  • locator (String)
  • element (String)

Returns:

  • (Boolean)


45
46
47
48
49
50
51
52
# File 'lib/rubocop/cop/capybara/mixin/capybara_help.rb', line 45

def replaceable_option?(node, locator, element)
  attrs = CssSelector.attributes(locator).keys
  return false unless replaceable_element?(node, element, attrs)

  attrs.all? do |attr|
    SPECIFIC_OPTIONS.fetch(element, []).include?(attr)
  end
end

.replaceable_pseudo_class?(pseudo_class, locator) ⇒ Boolean

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Parameters:

  • pseudo_class (String)
  • locator (String)

Returns:

  • (Boolean)


85
86
87
88
89
90
91
92
# File 'lib/rubocop/cop/capybara/mixin/capybara_help.rb', line 85

def replaceable_pseudo_class?(pseudo_class, locator)
  return false unless SPECIFIC_PSEUDO_CLASSES.include?(pseudo_class)

  case pseudo_class
  when 'not()' then replaceable_pseudo_class_not?(locator)
  else true
  end
end

.replaceable_pseudo_class_not?(locator) ⇒ Boolean

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Parameters:

  • locator (String)

Returns:

  • (Boolean)


96
97
98
99
100
101
102
# File 'lib/rubocop/cop/capybara/mixin/capybara_help.rb', line 96

def replaceable_pseudo_class_not?(locator)
  locator.scan(/not\(.*?\)/).all? do |negation|
    CssSelector.attributes(negation).values.all? do |v|
      v.is_a?(TrueClass) || v.is_a?(FalseClass)
    end
  end
end

.replaceable_pseudo_classes?(locator) ⇒ Boolean

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Parameters:

  • locator (String)

Returns:

  • (Boolean)


76
77
78
79
80
# File 'lib/rubocop/cop/capybara/mixin/capybara_help.rb', line 76

def replaceable_pseudo_classes?(locator)
  CssSelector.pseudo_classes(locator).all? do |pseudo_class|
    replaceable_pseudo_class?(pseudo_class, locator)
  end
end

.replaceable_to_link?(node, attrs) ⇒ Boolean

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Parameters:

  • node (RuboCop::AST::SendNode)
  • attrs (Array<String>)

Returns:

  • (Boolean)


118
119
120
# File 'lib/rubocop/cop/capybara/mixin/capybara_help.rb', line 118

def replaceable_to_link?(node, attrs)
  include_option?(node, :href) || attrs.include?('href')
end