Class: PageMagic::Element::Selector

Inherits:
Object
  • Object
show all
Defined in:
lib/page_magic/element/selector.rb,
lib/page_magic/element/selector/model.rb,
lib/page_magic/element/selector/methods.rb

Overview

class Selector - models the selection criteria understood by Capybara

Defined Under Namespace

Modules: Methods Classes: Model

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(selector = nil, supports_type: false, exact: false, &formatter) ⇒ Selector

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

Examples:

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

Parameters:

  • selector (Symbol) (defaults to: nil)

    the identifier for the selector

  • supports_type (Boolean) (defaults to: false)

    whether the element type being searched for can be used as part of the query

  • exact (Boolean) (defaults to: false)

    whether an exact match is required. E.g. element should include exactly the same text



38
39
40
41
42
43
44
45
# 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

Class Method Details

.find(name) ⇒ Selector

Find a Selector using it's name

Parameters:

  • name (Symbol)

    the name of the required Selector in snakecase format. See class constants for available selectors

Returns:

  • (Selector)

    returns the predefined selector with the given name

Raises:



14
15
16
17
18
19
# 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

Instance Method Details

#build(element_type, locator, options: {}) ⇒ Object

Build selector query parameters for Capybara's find method

Parameters:

  • element_type (Symbol)

    the type of browser element being found. e.g :link

  • locator (Hash<Symbol,String>)

    the selection method and its parameter. E.g. text: 'click me'



50
51
52
53
# 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