module Capybara::Compose::Selectors::ClassMethods

Public Instance Methods

aliases(selectors = {}) click to toggle source

Public: Light wrapper as syntax sugar for defining SELECTORS.

# File lib/capybara/compose/selectors.rb, line 43
def aliases(selectors = {})
  const_set('SELECTORS', selectors)
end
define_getters_for_selectors() click to toggle source

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

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

# File lib/capybara/compose/selectors.rb, line 62
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
selectors() click to toggle source

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

# File lib/capybara/compose/selectors.rb, line 49
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) click to toggle source

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

# File lib/capybara/compose/selectors.rb, line 72
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 Capybara::Compose::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