Class: PageMagic::Element::Query

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

Overview

class Query - models overall queries for Capybara, queries can include:

  • requirements on element type
  • selection criteria, modeled through the Selector class
  • options

Constant Summary collapse

ELEMENT =
Query.new
TEXT_FIELD =
CHECKBOX = SELECT_LIST = RADIOS = TEXTAREA = Query.new(:field)
Query.new(:link)
BUTTON =
Query.new(:button)

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(type = nil) ⇒ Query

Returns a new instance of Query.

Parameters:

  • type (defaults to: nil)

    -



23
24
25
# File 'lib/page_magic/element/query.rb', line 23

def initialize(type = nil)
  @type = type
end

Instance Attribute Details

#typeObject (readonly)

Returns the value of attribute type.



20
21
22
# File 'lib/page_magic/element/query.rb', line 20

def type
  @type
end

Class Method Details

.find(type) ⇒ Query

Find a query using it's name

Parameters:

  • type (Symbol)

    the name of the required query in snakecase format

Returns:

  • (Query)

    returns the predefined query with the given name



13
14
15
16
17
# File 'lib/page_magic/element/query.rb', line 13

def find(type)
  query = constants.find { |constant| constant.to_s.casecmp(type.to_s).zero? }
  return ELEMENT unless query
  const_get(query)
end

Instance Method Details

#build(locator, options = {}) ⇒ Array

Build query parameters for Capybara's find method

Parameters:

  • locator (Hash)

    the location method e.g. text: 'button text'

  • options (Hash) (defaults to: {})

    additional options to be provided to Capybara. e.g. count: 3

Returns:

  • (Array)

    list of compatible capybara query parameters.



31
32
33
34
35
36
37
# File 'lib/page_magic/element/query.rb', line 31

def build(locator, options = {})
  [].tap do |array|
    selector = Selector.find(locator.keys.first)
    array << selector.build(type, locator.values.first)
    array << options unless options.empty?
  end.flatten
end