Class: PageMagic::Element::Selector

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

Overview

class Selector - models the selection criteria understood by Capybara

Constant Summary collapse

XPATH =
Selector.new(:xpath, supports_type: false)
ID =
Selector.new(:id, supports_type: false)
LABEL =
Selector.new(:field, supports_type: false, exact: true)
CSS =
Selector.new(supports_type: false)
TEXT =
Selector.new(supports_type: true)
NAME =
Selector.new(supports_type: false) do |arg|
  "*[name='#{arg}']"
end

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

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

Returns a new instance of Selector.



19
20
21
22
23
24
# File 'lib/page_magic/element/selector.rb', line 19

def initialize(selector = nil, supports_type: false, exact: false, &formatter)
  @name = selector
  @formatter = formatter || proc { |arg| arg }
  @supports_type = supports_type
  @exact = exact
end

Instance Attribute Details

#exactObject (readonly)

Returns the value of attribute exact.



17
18
19
# File 'lib/page_magic/element/selector.rb', line 17

def exact
  @exact
end

#formatterObject (readonly)

Returns the value of attribute formatter.



17
18
19
# File 'lib/page_magic/element/selector.rb', line 17

def formatter
  @formatter
end

#nameObject (readonly)

Returns the value of attribute name.



17
18
19
# File 'lib/page_magic/element/selector.rb', line 17

def name
  @name
end

#supports_typeObject (readonly)

Returns the value of attribute supports_type.



17
18
19
# File 'lib/page_magic/element/selector.rb', line 17

def supports_type
  @supports_type
end

Class Method Details

.find(name) ⇒ Selector

Find a Selecor 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



10
11
12
13
14
# File 'lib/page_magic/element/selector.rb', line 10

def find(name)
  selector = constants.find { |constant| constant.to_s.downcase == name.to_s.downcase }
  fail UnsupportedCriteriaException unless selector
  const_get(selector)
end

Instance Method Details

#build(element_type, locator) ⇒ 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)

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



29
30
31
32
33
34
35
36
# File 'lib/page_magic/element/selector.rb', line 29

def build(element_type, locator)
  [].tap do |array|
    array << element_type if supports_type
    array << name if name
    array << formatter.call(locator)
    array << { exact: true } if exact
  end
end