Module: CapybaraTestHelpers::Selectors::ClassMethods

Defined in:
lib/capybara_test_helpers/selectors.rb

Instance Method Summary collapse

Instance Method Details

#aliases(selectors = {}) ⇒ Object

Public: Light wrapper as syntax sugar for defining SELECTORS.



44
45
46
# File 'lib/capybara_test_helpers/selectors.rb', line 44

def aliases(selectors = {})
  const_set('SELECTORS', selectors)
end

#define_getters_for_selectorsObject

Internal: Allows to “call” selectors, as a shortcut for find.

Example: table.header == table.find(:header)



63
64
65
66
67
68
69
# File 'lib/capybara_test_helpers/selectors.rb', line 63

def define_getters_for_selectors
  selectors.each_key do |selector_name|
    define_method(selector_name) { |*args, **kwargs, &block|
      find(selector_name, *args, **kwargs, &block)
    }
  end
end

#selectorsObject

Public: Returns the available selectors for the test helper, or an empty Hash if selectors are not defined.



50
51
52
53
54
55
56
57
58
# File 'lib/capybara_test_helpers/selectors.rb', line 50

def selectors
  unless defined?(@selectors)
    parent_selectors = superclass.respond_to?(:selectors) ? superclass.selectors : {}
    child_selectors = (defined?(self::SELECTORS) && self::SELECTORS || {})
      .tap { |new_selectors| validate_selectors(new_selectors) }
    @selectors = parent_selectors.merge(child_selectors).transform_values(&:freeze).freeze
  end
  @selectors
end

#validate_selectors(selectors) ⇒ Object

Internal: Validates that all the selectors defined in the class won’t cause confusion or misbehavior.



73
74
75
76
77
78
79
80
81
82
83
84
# File 'lib/capybara_test_helpers/selectors.rb', line 73

def validate_selectors(selectors)
  selectors.each_key do |name|
    if Capybara::Selector.all.key?(name)
      raise "A selector with the name #{ name.inspect } is already registered in Capybara," \
      " consider renaming the #{ name.inspect } alias in #{ self.class.name } to avoid confusion."
    end
    if CapybaraTestHelpers::RESERVED_METHODS.include?(name)
      raise "A method with the name #{ name.inspect } is part of the Capybara DSL," \
      " consider renaming the #{ name.inspect } alias in #{ self.class.name } to avoid confusion."
    end
  end
end