Class: PageObject::Elements::Element

Inherits:
Object
  • Object
show all
Includes:
NestedElements
Defined in:
lib/page-object/elements/element.rb

Overview

Contains functionality that is common across all elements.

See Also:

  • for the Watir version of all common methods

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from NestedElements

included

Constructor Details

#initialize(element) ⇒ Element

Returns a new instance of Element.



15
16
17
18
# File 'lib/page-object/elements/element.rb', line 15

def initialize(element)
  @element = element
  @platform = PageObject::Platforms::Watir::PageObject.new(@element)
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

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

delegate calls to driver element



178
179
180
181
182
183
184
# File 'lib/page-object/elements/element.rb', line 178

def method_missing(method, *args, &block)
  if element.respond_to?(method)
    element.send(method, *args, &block)
  else
    super
  end
end

Instance Attribute Details

#elementObject (readonly)

Returns the value of attribute element.



13
14
15
# File 'lib/page-object/elements/element.rb', line 13

def element
  @element
end

Class Method Details

.plural_formObject

specify plural form of element



42
43
44
45
46
47
48
# File 'lib/page-object/elements/element.rb', line 42

def self.plural_form
  "#{self.to_s.split('::')[-1].
      gsub(/([A-Z]+)([A-Z][a-z])/, '\1_\2').
      gsub(/([a-z\d])([A-Z])/, '\1_\2').
      tr("-", "_").
      downcase}s"
end

Instance Method Details

#==(other) ⇒ Object

compare this element to another to determine if they are equal



75
76
77
# File 'lib/page-object/elements/element.rb', line 75

def ==(other)
  other.is_a? self.class and element == other.element
end

#check_exists(timeout = ::PageObject.default_element_wait) ⇒ Object

Keeps checking until the element exists

Parameters:

  • (defaults (Integer)

    to: 5) seconds to wait before timing out



66
67
68
69
70
# File 'lib/page-object/elements/element.rb', line 66

def check_exists(timeout=::PageObject.default_element_wait)
  timed_loop(timeout) do |element|
    element.exists?
  end
end

#check_visible(timeout = ::PageObject.default_element_wait) ⇒ Object

Keeps checking until the element is visible

Parameters:

  • (defaults (Integer)

    to: 5) seconds to wait before timing out



55
56
57
58
59
# File 'lib/page-object/elements/element.rb', line 55

def check_visible(timeout=::PageObject.default_element_wait)
  timed_loop(timeout) do |element|
    element.present?
  end
end

#children(opt = {}) ⇒ Object

Return all elements that are children of this element



116
117
118
119
# File 'lib/page-object/elements/element.rb', line 116

def children(opt={})
  children = element.children(opt)
  children.collect {|child| pageobject_wrapper(child)}
end

#disabled?Boolean

return true if the element is not enabled

Returns:

  • (Boolean)


23
24
25
# File 'lib/page-object/elements/element.rb', line 23

def disabled?
  not enabled?
end

#drag_and_drop_on(droppable) ⇒ Object



34
35
36
37
# File 'lib/page-object/elements/element.rb', line 34

def drag_and_drop_on(droppable)
  droppable_native = droppable.kind_of?(PageObject::Elements::Element) ? droppable.element : droppable
  element.drag_and_drop_on(droppable_native)
end

#following_sibling(opt = {}) ⇒ Object

Return the element that exists at the same level of the DOM immediately after this element



100
101
102
103
# File 'lib/page-object/elements/element.rb', line 100

def following_sibling(opt={})
  sibling = element.following_sibling(opt)
  pageobject_wrapper(sibling)
end

#following_siblings(opt = {}) ⇒ Object

Return all elements that exist at the same level of the DOM immediately after this element



134
135
136
137
# File 'lib/page-object/elements/element.rb', line 134

def following_siblings(opt={})
  siblings = element.following_siblings(opt)
  siblings.collect {|sibling| pageobject_wrapper(sibling)}
end

#nameObject



172
173
174
# File 'lib/page-object/elements/element.rb', line 172

def name
  element.attribute(:name)
end

#parent(opt = {}) ⇒ Object

find the parent element



82
83
84
85
# File 'lib/page-object/elements/element.rb', line 82

def parent(opt = {})
  parent = element.parent(opt)
  pageobject_wrapper(parent)
end

#preceding_sibling(opt = {}) ⇒ Object

Return the element that exists at the same level of the DOM immediately prior to this element



91
92
93
94
# File 'lib/page-object/elements/element.rb', line 91

def preceding_sibling(opt = {})
  sibling = element.preceding_sibling(opt)
  pageobject_wrapper(sibling)
end

#preceding_siblings(opt = {}) ⇒ Object

Return all elements that exist at the same level of the DOM immediately prior to this element



125
126
127
128
# File 'lib/page-object/elements/element.rb', line 125

def preceding_siblings(opt={})
  siblings = element.preceding_siblings(opt)
  siblings.collect {|sibling| pageobject_wrapper(sibling)}
end

#present?Boolean

return true if the element exists and is visible

Returns:

  • (Boolean)


30
31
32
# File 'lib/page-object/elements/element.rb', line 30

def present?
  element.present?
end

#respond_to_missing?(m, *args) ⇒ Boolean

Returns:

  • (Boolean)


186
187
188
# File 'lib/page-object/elements/element.rb', line 186

def respond_to_missing?(m,*args)
  element.respond_to?(m) || super
end

#siblings(opt = {}) ⇒ Object

Return all elements that are direct children of this element’s parent



108
109
110
111
# File 'lib/page-object/elements/element.rb', line 108

def siblings(opt={})
  siblings = element.siblings(opt)
  siblings.collect {|sibling| pageobject_wrapper(sibling)}
end

#wait_until(timeout = ::PageObject.default_element_wait, message = nil, &block) ⇒ Object

Waits until the block returns true

Parameters:

  • (defaults (Integer)

    to: 5) seconds to wait before timing out

  • the (String)

    message to display if the event timeouts

  • the

    block to execute when the event occurs



168
169
170
# File 'lib/page-object/elements/element.rb', line 168

def wait_until(timeout=::PageObject.default_element_wait, message=nil, &block)
  element.wait_until(timeout: timeout, message: message, &block)
end

#when_not_present(timeout = ::PageObject.default_element_wait) ⇒ Object Also known as: when_not_visible

Waits until the element is not present

timing out

Parameters:

  • (defaults (Integer)

    to: 5) seconds to wait before



156
157
158
# File 'lib/page-object/elements/element.rb', line 156

def when_not_present(timeout=::PageObject.default_element_wait)
  element.wait_while(timeout: timeout, message: "Element still present in #{timeout} seconds", &:present?)
end

#when_present(timeout = ::PageObject.default_element_wait) ⇒ Object Also known as: when_visible

Waits until the element is present

Parameters:

  • (defaults (Integer)

    to: 5) seconds to wait before timing out



144
145
146
147
# File 'lib/page-object/elements/element.rb', line 144

def when_present(timeout=::PageObject.default_element_wait)
  element.wait_until(timeout: timeout, message: "Element not present in #{timeout} seconds", &:present?)
  self
end