Module: PageObject::Platforms::SeleniumWebDriver::Element

Includes:
Watir::Atoms
Defined in:
lib/page-object/platforms/selenium_webdriver/element.rb

Instance Method Summary collapse

Instance Method Details

#==(other) ⇒ Object

compare this element to another to determine if they are equal



47
48
49
# File 'lib/page-object/platforms/selenium_webdriver/element.rb', line 47

def ==(other)
  element == other.element
end

#attribute(attribute_name) ⇒ String?

Get the value of a the given attribute of the element. Will return the current value, even if this has been modified after the page has been loaded. More exactly, this method will return the value of the given attribute, unless that attribute is not present, in which case the value of the property with the same name is returned. If neither value is set, nil is returned. The “style” attribute is converted as best can be to a text representation with a trailing semi-colon. The following are deemed to be “boolean” attributes, and will return either “true” or “false”:

async, autofocus, autoplay, checked, compact, complete, controls, declare, defaultchecked, defaultselected, defer, disabled, draggable, ended, formnovalidate, hidden, indeterminate, iscontenteditable, ismap, itemscope, loop, multiple, muted, nohref, noresize, noshade, novalidate, nowrap, open, paused, pubdate, readonly, required, reversed, scoped, seamless, seeking, selected, spellcheck, truespeed, willvalidate

Finally, the following commonly mis-capitalized attribute/property names are evaluated as expected:

class, readonly

Parameters:

Returns:

  • (String, nil)

    attribute value



90
91
92
# File 'lib/page-object/platforms/selenium_webdriver/element.rb', line 90

def attribute(attribute_name)
  element.attribute attribute_name
end

#clearObject

clear the contents of the element



202
203
204
# File 'lib/page-object/platforms/selenium_webdriver/element.rb', line 202

def clear
  element.clear
end

#exists?Boolean

return true if an element exists

Returns:

  • (Boolean)


22
23
24
# File 'lib/page-object/platforms/selenium_webdriver/element.rb', line 22

def exists?
  not element.nil?
end

#fire_event(event_name) ⇒ Object

Fire the provided event on the current element



97
98
99
100
101
102
# File 'lib/page-object/platforms/selenium_webdriver/element.rb', line 97

def fire_event(event_name)
  event_name = event_name.to_s.sub(/^on/, '').downcase
  script = "return (%s).apply(null, arguments)" % ATOMS.fetch(:fireEvent)
  bridge = element.instance_variable_get(:@bridge)
  bridge.executeScript(script, element, event_name)
end

#focusObject

Set the focus to the current element



119
120
121
122
# File 'lib/page-object/platforms/selenium_webdriver/element.rb', line 119

def focus
  bridge = element.instance_variable_get(:@bridge)
  bridge.executeScript("return arguments[0].focus()", element)
end

#parentObject

find the parent element



107
108
109
110
111
112
113
114
# File 'lib/page-object/platforms/selenium_webdriver/element.rb', line 107

def parent
  script = "return (%s).apply(null, arguments)" % ATOMS.fetch(:getParentElement)
  bridge = element.instance_variable_get(:@bridge)
  parent = bridge.executeScript(script, element)
  type = element.attribute(:type).to_s.downcase if parent.tag_name.to_sym == :input
  cls = ::PageObject::Elements.element_class_for(parent.tag_name, type)
  cls.new(parent, :platform => :selenium_webdriver)
end

#right_clickObject

Click this element



127
128
129
# File 'lib/page-object/platforms/selenium_webdriver/element.rb', line 127

def right_click
  element.context_click
end

#send_keys(*args) ⇒ Object

Send keystrokes to this element

Examples:

element.send_keys "foo"                     #=> value: 'foo'
element.send_keys "tet", :arrow_left, "s"   #=> value: 'test'
element.send_keys [:control, 'a'], :space   #=> value: ' '

Parameters:

See Also:

  • Selenium::WebDriver::Keys::KEYS


195
196
197
# File 'lib/page-object/platforms/selenium_webdriver/element.rb', line 195

def send_keys(*args)
  element.send_keys(*args)
end

#tag_nameString

Get the tag name of this element

Returns:



56
57
58
# File 'lib/page-object/platforms/selenium_webdriver/element.rb', line 56

def tag_name
  element.tag_name
end

#textString

Get the text for the element

Returns:



31
32
33
# File 'lib/page-object/platforms/selenium_webdriver/element.rb', line 31

def text
  element.text
end

#valueString

Get the value of this element

Returns:



40
41
42
# File 'lib/page-object/platforms/selenium_webdriver/element.rb', line 40

def value
  element.attribute('value')
end

#visible?Boolean

return true if an element is visible

Returns:

  • (Boolean)


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

def visible?
  element.displayed?
end

#wait_until(timeout = 5, 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 occurrs



177
178
179
180
# File 'lib/page-object/platforms/selenium_webdriver/element.rb', line 177

def wait_until(timeout=5, message=nil, &block)
  wait = Object::Selenium::WebDriver::Wait.new({:timeout => timeout, :message => message})
  wait.until &block
end

#when_not_visible(timeout = 5) ⇒ Object

Waits until the element is not visible

Parameters:

  • (defaults (Integer)

    to: 5) seconds to wait before timing out



162
163
164
165
166
167
168
# File 'lib/page-object/platforms/selenium_webdriver/element.rb', line 162

def when_not_visible(timeout=5)
  wait = Object::Selenium::WebDriver::Wait.new({:timeout => timeout, :message => "Element not present in #{timeout} seconds"})
  wait.until do
    not self.visible?
  end
  self
end

#when_present(timeout = 5) ⇒ Object

Waits until the element is present

Parameters:

  • (defaults (Integer)

    to: 5) seconds to wait before timing out



136
137
138
139
140
141
142
# File 'lib/page-object/platforms/selenium_webdriver/element.rb', line 136

def when_present(timeout=5)
  wait = Object::Selenium::WebDriver::Wait.new({:timeout => timeout, :message => "Element not present in #{timeout} seconds"})
  wait.until do
    self.exists?
  end
  self
end

#when_visible(timeout = 5) ⇒ Object

Waits until the element is visible

Parameters:

  • (defaults (Integer)

    to: 5) seconds to wait before timing out



149
150
151
152
153
154
155
# File 'lib/page-object/platforms/selenium_webdriver/element.rb', line 149

def when_visible(timeout=5)
  wait = Object::Selenium::WebDriver::Wait.new({:timeout => timeout, :message => "Element not present in #{timeout} seconds"})
  wait.until do
    self.visible?
  end
  self
end