Class: PageMagic::Element

Inherits:
Object show all
Extended by:
Forwardable, SelectorMethods, Elements
Includes:
Locators, SelectorMethods, SessionMethods, WaitMethods, Watchers
Defined in:
lib/page_magic/element.rb,
lib/page_magic/element/query.rb,
lib/page_magic/element/locators.rb,
lib/page_magic/element/selector.rb,
lib/page_magic/element/query_builder.rb,
lib/page_magic/element/selector_methods.rb

Overview

for the benefit of pull review :@

Defined Under Namespace

Modules: Locators, SelectorMethods Classes: Query, QueryBuilder, Selector

Constant Summary collapse

EVENT_TYPES =
%i[set select select_option unselect_option click].freeze
DEFAULT_HOOK =
proc {}.freeze
EVENT_NOT_SUPPORTED_MSG =
'%s event not supported'.freeze

Constants included from Elements

PageMagic::Elements::INVALID_METHOD_NAME_MSG, PageMagic::Elements::TYPES

Constants included from Watchers

Watchers::ELEMENT_MISSING_MSG

Constants included from Locators

Locators::ELEMENT_NOT_DEFINED_MSG

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from SelectorMethods

selector

Methods included from Elements

element, element_definitions

Methods included from Watchers

#changed?, #watch, #watcher, #watchers

Methods included from SessionMethods

#execute_script, #page, #path, #url

Methods included from WaitMethods

#wait_until

Methods included from Locators

#element_by_name, #element_definitions

Constructor Details

#initialize(browser_element) ⇒ Element

Returns a new instance of Element.



69
70
71
72
73
74
75
76
# File 'lib/page_magic/element.rb', line 69

def initialize(browser_element)
  @browser_element = browser_element
  @parent_element = self.class.parent_element
  @before_events = self.class.before_events
  @after_events = self.class.after_events
  @element_definitions = self.class.element_definitions.dup
  wrap_events(browser_element)
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method, *args, &block) ⇒ Object



95
96
97
98
99
100
101
102
103
104
# File 'lib/page_magic/element.rb', line 95

def method_missing(method, *args, &block)
  ElementContext.new(self).send(method, *args, &block)
rescue ElementMissingException
  begin
    return browser_element.send(method, *args, &block) if browser_element.respond_to?(method)
    return parent_element.send(method, *args, &block)
  rescue NoMethodError
    super
  end
end

Instance Attribute Details

#after_eventsObject (readonly)

Returns the value of attribute after_events.



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

def after_events
  @after_events
end

#before_eventsObject (readonly)

Returns the value of attribute before_events.



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

def before_events
  @before_events
end

#browser_elementObject (readonly)

Returns the value of attribute browser_element.



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

def browser_element
  @browser_element
end

#nameObject (readonly)

Returns the value of attribute name.



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

def name
  @name
end

#parent_elementObject (readonly)

Returns the value of attribute parent_element.



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

def parent_element
  @parent_element
end

#typeObject (readonly)

Returns the value of attribute type.



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

def type
  @type
end

Class Method Details

.==(other) ⇒ Object



64
65
66
# File 'lib/page_magic/element.rb', line 64

def ==(other)
  other <= PageMagic::Element && element_definitions == other.element_definitions
end

.after_eventsArray

If a block is passed in, it adds it to be run after an event is triggered on an element. See EVENT_TYPES for the

Returns:

  • (Array)

    all registered blocks



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

.before_eventsObject

If a block is passed in, it adds it to be run before an event is triggered on an element.

See Also:



33
34
35
36
37
38
39
40
# File 'lib/page_magic/element.rb', line 33

%i[after_events before_events].each do |method|
  define_method method do |&block|
    instance_variable_name = "@#{method}".to_sym
    instance_variable_value = instance_variable_get(instance_variable_name) || [DEFAULT_HOOK]
    instance_variable_value << block if block
    instance_variable_set(instance_variable_name, instance_variable_value)
  end
end

.inherited(clazz) ⇒ Object

called when class inherits this one

Parameters:

  • clazz (Class)

    inheriting class



52
53
54
55
56
# File 'lib/page_magic/element.rb', line 52

def inherited(clazz)
  super
  clazz.before_events.replace(before_events)
  clazz.after_events.replace(after_events)
end

.parent_element(page_element = nil) ⇒ Element

Get/Sets the parent element desribed by this class

Parameters:

  • page_element (Element) (defaults to: nil)

    parent page element

Returns:



45
46
47
48
# File 'lib/page_magic/element.rb', line 45

def parent_element(page_element = nil)
  return @parent_page_element unless page_element
  @parent_page_element = page_element
end

.watch(name, method = nil, &block) ⇒ Object

Defines watchers to be used by instances

See Also:



60
61
62
# File 'lib/page_magic/element.rb', line 60

def watch(name, method = nil, &block)
  before_events { watch(name, method, &block) }
end

Instance Method Details

#clickObject

calls method of the same name on the underlying Capybara element

Raises:



85
86
87
88
89
90
91
92
93
# File 'lib/page_magic/element.rb', line 85

EVENT_TYPES.each do |method|
  define_method method do |*args|
    unless browser_element.respond_to?(method)
      raise NotSupportedException, EVENT_NOT_SUPPORTED_MSG % method
    end

    browser_element.send(method, *args)
  end
end

#respond_to?(*args) ⇒ Boolean

Returns:

  • (Boolean)


106
107
108
# File 'lib/page_magic/element.rb', line 106

def respond_to?(*args)
  super || contains_element?(args.first) || browser_element.respond_to?(*args) || parent_element.respond_to?(*args)
end

#selectObject

calls method of the same name on the underlying Capybara element

Raises:



85
86
87
88
89
90
91
92
93
# File 'lib/page_magic/element.rb', line 85

EVENT_TYPES.each do |method|
  define_method method do |*args|
    unless browser_element.respond_to?(method)
      raise NotSupportedException, EVENT_NOT_SUPPORTED_MSG % method
    end

    browser_element.send(method, *args)
  end
end

#select_optionObject

calls method of the same name on the underlying Capybara element

Raises:



85
86
87
88
89
90
91
92
93
# File 'lib/page_magic/element.rb', line 85

EVENT_TYPES.each do |method|
  define_method method do |*args|
    unless browser_element.respond_to?(method)
      raise NotSupportedException, EVENT_NOT_SUPPORTED_MSG % method
    end

    browser_element.send(method, *args)
  end
end

#sessionSession

get the current session

Returns:

  • (Session)

    returns the session of the parent page element. Capybara session



114
# File 'lib/page_magic/element.rb', line 114

def_delegator :parent_element, :session

#setObject

calls method of the same name on the underlying Capybara element

Raises:



85
86
87
88
89
90
91
92
93
# File 'lib/page_magic/element.rb', line 85

EVENT_TYPES.each do |method|
  define_method method do |*args|
    unless browser_element.respond_to?(method)
      raise NotSupportedException, EVENT_NOT_SUPPORTED_MSG % method
    end

    browser_element.send(method, *args)
  end
end

#unselect_optionObject

calls method of the same name on the underlying Capybara element

Raises:



85
86
87
88
89
90
91
92
93
# File 'lib/page_magic/element.rb', line 85

EVENT_TYPES.each do |method|
  define_method method do |*args|
    unless browser_element.respond_to?(method)
      raise NotSupportedException, EVENT_NOT_SUPPORTED_MSG % method
    end

    browser_element.send(method, *args)
  end
end