class PageMagic::Element::Selector

class Selector - models the selection criteria understood by Capybara

Constants

CSS
ID
LABEL
NAME
TEXT
XPATH

Attributes

formatter[R]
options[R]
selector[R]
supports_type[R]

Public Class Methods

find(name) click to toggle source

Find a Selector using it’s name @param [Symbol] name the name of the required Selector in snakecase format. See class constants for available

selectors

@return [Selector] returns the predefined selector with the given name

# File lib/page_magic/element/selector.rb, line 14
def find(name)
  selector_name = selector_constant_name(name)
  raise UnsupportedCriteriaException unless selector_name

  const_get(selector_name)
end
new(selector = nil, supports_type: false, exact: false, &formatter) click to toggle source

Initialize a new selector a block can be supplied to decorate the query. E.g. @example

Selector.new(supports_type: false) do |arg|
  "*[name='#{arg}']"
end

@param [Symbol] selector the identifier for the selector @param [Boolean] supports_type whether the element type being searched for can be used as part of the query @param [Boolean] exact whether an exact match is required. E.g. element should include exactly the same text

# File lib/page_magic/element/selector.rb, line 38
def initialize(selector = nil, supports_type: false, exact: false, &formatter)
  @selector = selector
  @formatter = formatter || proc { |arg| arg }
  @supports_type = supports_type
  @options = {}.tap do |hash|
    hash[:exact] = true if exact
  end
end

Private Class Methods

selector_constant_name(name) click to toggle source
# File lib/page_magic/element/selector.rb, line 23
def selector_constant_name(name)
  constants.find { |constant| constant.to_s.casecmp(name.to_s).zero? }
end

Public Instance Methods

build(element_type, locator, options: {}) click to toggle source

Build selector query parameters for Capybara’s find method @param [Symbol] element_type the type of browser element being found. e.g :link @param [Hash<Symbol,String>] locator the selection method and its parameter. E.g. text: ‘click me’

# File lib/page_magic/element/selector.rb, line 50
def build(element_type, locator, options: {})
  array = [type(element_type), selector, formatter.call(locator)].compact
  Model.new(array, self.options.merge(options))
end

Private Instance Methods

type(element_type) click to toggle source
# File lib/page_magic/element/selector.rb, line 57
def type(element_type)
  supports_type ? element_type : nil
end