Class: PageMagic::Element
- Extended by:
- Forwardable, Selector::Methods, Elements
- Includes:
- Locators, Selector::Methods, 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/not_found.rb,
lib/page_magic/element/selector/model.rb,
lib/page_magic/element/selector/methods.rb,
lib/page_magic/element/query/single_result.rb,
lib/page_magic/element/query/multiple_results.rb,
lib/page_magic/element/query/prefetched_result.rb
Overview
Capybara::Finder
Defined Under Namespace
Modules: Locators Classes: NotFound, Query, 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'
Constants included from Elements
PageMagic::Elements::INVALID_METHOD_NAME_MSG, PageMagic::Elements::TYPES
Constants included from Watchers
Constants included from Locators
Locators::ELEMENT_NOT_DEFINED_MSG
Instance Attribute Summary collapse
-
#after_events ⇒ Object
readonly
Returns the value of attribute after_events.
-
#before_events ⇒ Object
readonly
Returns the value of attribute before_events.
-
#browser_element ⇒ Object
readonly
Returns the value of attribute browser_element.
-
#name ⇒ Object
readonly
Returns the value of attribute name.
-
#parent_element ⇒ Object
readonly
Returns the value of attribute parent_element.
-
#type ⇒ Object
readonly
Returns the value of attribute type.
Class Method Summary collapse
- .==(other) ⇒ Object
-
.after_events ⇒ Array
If a block is passed in, it adds it to be run after an event is triggered on an element.
-
.before_events ⇒ Object
If a block is passed in, it adds it to be run before an event is triggered on an element.
-
.inherited(clazz) ⇒ Object
called when class inherits this one.
- .load(source) ⇒ Object
-
.parent_element(page_element = nil) ⇒ Element
Get/Sets the parent element desribed by this class.
-
.watch(name, method = nil, &block) ⇒ Object
Defines watchers to be used by instances.
Instance Method Summary collapse
-
#click ⇒ Object
calls method of the same name on the underlying Capybara element.
-
#initialize(browser_element) ⇒ Element
constructor
A new instance of Element.
- #method_missing(method, *args, &block) ⇒ Object
- #respond_to_missing?(*args) ⇒ Boolean
-
#select ⇒ Object
calls method of the same name on the underlying Capybara element.
-
#select_option ⇒ Object
calls method of the same name on the underlying Capybara element.
-
#session ⇒ Session
get the current session.
-
#set ⇒ Object
calls method of the same name on the underlying Capybara element.
-
#unselect_option ⇒ Object
calls method of the same name on the underlying Capybara element.
Methods included from Selector::Methods
Methods included from Elements
element, element_definitions, elements
Methods included from Watchers
#changed?, #watch, #watcher, #watchers
Methods included from SessionMethods
#execute_script, #page, #path, #url
Methods included from WaitMethods
Methods included from Locators
#element_by_name, #element_definitions
Constructor Details
#initialize(browser_element) ⇒ Element
Returns a new instance of Element.
79 80 81 82 83 84 85 86 |
# File 'lib/page_magic/element.rb', line 79 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
103 104 105 106 107 108 109 110 111 112 113 |
# File 'lib/page_magic/element.rb', line 103 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) parent_element.send(method, *args, &block) rescue NoMethodError super end end |
Instance Attribute Details
#after_events ⇒ Object (readonly)
Returns the value of attribute after_events.
25 26 27 |
# File 'lib/page_magic/element.rb', line 25 def after_events @after_events end |
#before_events ⇒ Object (readonly)
Returns the value of attribute before_events.
25 26 27 |
# File 'lib/page_magic/element.rb', line 25 def before_events @before_events end |
#browser_element ⇒ Object (readonly)
Returns the value of attribute browser_element.
25 26 27 |
# File 'lib/page_magic/element.rb', line 25 def browser_element @browser_element end |
#name ⇒ Object (readonly)
Returns the value of attribute name.
25 26 27 |
# File 'lib/page_magic/element.rb', line 25 def name @name end |
#parent_element ⇒ Object (readonly)
Returns the value of attribute parent_element.
25 26 27 |
# File 'lib/page_magic/element.rb', line 25 def parent_element @parent_element end |
#type ⇒ Object (readonly)
Returns the value of attribute type.
25 26 27 |
# File 'lib/page_magic/element.rb', line 25 def type @type end |
Class Method Details
.==(other) ⇒ Object
74 75 76 |
# File 'lib/page_magic/element.rb', line 74 def ==(other) other <= PageMagic::Element && element_definitions == other.element_definitions end |
.after_events ⇒ Array
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
|
# File 'lib/page_magic/element.rb', line 28
|
.before_events ⇒ Object
If a block is passed in, it adds it to be run before an event is triggered on an element.
36 37 38 39 40 41 42 43 |
# File 'lib/page_magic/element.rb', line 36 %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
56 57 58 59 60 |
# File 'lib/page_magic/element.rb', line 56 def inherited(clazz) super clazz.before_events.replace(before_events) clazz.after_events.replace(after_events) end |
.load(source) ⇒ Object
62 63 64 |
# File 'lib/page_magic/element.rb', line 62 def load(source) new(Capybara::Node::Simple.new(source)) end |
.parent_element(page_element = nil) ⇒ Element
Get/Sets the parent element desribed by this class
48 49 50 51 52 |
# File 'lib/page_magic/element.rb', line 48 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
68 69 70 71 72 |
# File 'lib/page_magic/element.rb', line 68 def watch(name, method = nil, &block) before_events do watch(name, method: method, &block) end end |
Instance Method Details
#click ⇒ Object
calls method of the same name on the underlying Capybara element
95 96 97 98 99 100 101 |
# File 'lib/page_magic/element.rb', line 95 EVENT_TYPES.each do |method| define_method method do |*args| raise NotSupportedException, EVENT_NOT_SUPPORTED_MSG % method unless browser_element.respond_to?(method) browser_element.send(method, *args) end end |
#respond_to_missing?(*args) ⇒ Boolean
115 116 117 |
# File 'lib/page_magic/element.rb', line 115 def respond_to_missing?(*args) super || contains_element?(args.first) || browser_element.respond_to?(*args) || parent_element.respond_to?(*args) end |
#select ⇒ Object
calls method of the same name on the underlying Capybara element
95 96 97 98 99 100 101 |
# File 'lib/page_magic/element.rb', line 95 EVENT_TYPES.each do |method| define_method method do |*args| raise NotSupportedException, EVENT_NOT_SUPPORTED_MSG % method unless browser_element.respond_to?(method) browser_element.send(method, *args) end end |
#select_option ⇒ Object
calls method of the same name on the underlying Capybara element
95 96 97 98 99 100 101 |
# File 'lib/page_magic/element.rb', line 95 EVENT_TYPES.each do |method| define_method method do |*args| raise NotSupportedException, EVENT_NOT_SUPPORTED_MSG % method unless browser_element.respond_to?(method) browser_element.send(method, *args) end end |
#session ⇒ Session
get the current session
127 |
# File 'lib/page_magic/element.rb', line 127 def_delegator :parent_element, :session |
#set ⇒ Object
calls method of the same name on the underlying Capybara element
95 96 97 98 99 100 101 |
# File 'lib/page_magic/element.rb', line 95 EVENT_TYPES.each do |method| define_method method do |*args| raise NotSupportedException, EVENT_NOT_SUPPORTED_MSG % method unless browser_element.respond_to?(method) browser_element.send(method, *args) end end |
#unselect_option ⇒ Object
calls method of the same name on the underlying Capybara element
95 96 97 98 99 100 101 |
# File 'lib/page_magic/element.rb', line 95 EVENT_TYPES.each do |method| define_method method do |*args| raise NotSupportedException, EVENT_NOT_SUPPORTED_MSG % method unless browser_element.respond_to?(method) browser_element.send(method, *args) end end |