Module: PageObject::Platforms::SeleniumWebDriver::Element
- Includes:
- Watir::Atoms
- Defined in:
- lib/page-object/platforms/selenium_webdriver/element.rb
Instance Method Summary collapse
-
#==(other) ⇒ Object
compare this element to another to determine if they are equal.
-
#attribute(attribute_name) ⇒ String?
Get the value of a the given attribute of the element.
-
#clear ⇒ Object
clear the contents of the element.
-
#double_click ⇒ Object
hover over the element.
-
#exists? ⇒ Boolean
return true if an element exists.
-
#fire_event(event_name) ⇒ Object
Fire the provided event on the current element.
-
#flash ⇒ Object
flash the element by temporarily changing the background color.
-
#focus ⇒ Object
Set the focus to the current element.
-
#hover ⇒ Object
hover over the element.
-
#html ⇒ String
Get the html for the element.
-
#id ⇒ Object
get the id of the element.
-
#parent ⇒ Object
find the parent element.
-
#right_click ⇒ Object
Click this element.
-
#scroll_into_view ⇒ Object
Scroll until the element is viewable.
-
#select_text(text) ⇒ Object
Select the provided text.
-
#send_keys(*args) ⇒ Object
Send keystrokes to this element.
-
#tag_name ⇒ String
Get the tag name of this element.
-
#text ⇒ String
Get the text for the element.
-
#value ⇒ String
Get the value of this element.
-
#visible? ⇒ Boolean
return true if an element is visible.
-
#wait_until(timeout = ::PageObject.default_element_wait, message = nil, &block) ⇒ Object
Waits until the block returns true.
-
#when_not_present(timeout = ::PageObject.default_element_wait) ⇒ Object
Waits until the element is not present.
-
#when_not_visible(timeout = ::PageObject.default_element_wait) ⇒ Object
Waits until the element is not visible.
-
#when_present(timeout = ::PageObject.default_element_wait) ⇒ Object
Waits until the element is present.
-
#when_visible(timeout = ::PageObject.default_element_wait) ⇒ Object
Waits until the element is visible.
Instance Method Details
#==(other) ⇒ Object
compare this element to another to determine if they are equal
69 70 71 |
# File 'lib/page-object/platforms/selenium_webdriver/element.rb', line 69 def ==(other) other.is_a? self.class and 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
112 113 114 |
# File 'lib/page-object/platforms/selenium_webdriver/element.rb', line 112 def attribute(attribute_name) element.attribute attribute_name end |
#clear ⇒ Object
clear the contents of the element
265 266 267 |
# File 'lib/page-object/platforms/selenium_webdriver/element.rb', line 265 def clear element.clear end |
#double_click ⇒ Object
hover over the element
136 137 138 139 |
# File 'lib/page-object/platforms/selenium_webdriver/element.rb', line 136 def double_click mouse = Selenium::WebDriver::Mouse.new(bridge) mouse.double_click(element) end |
#exists? ⇒ Boolean
return true if an element exists
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
119 120 121 122 123 |
# File 'lib/page-object/platforms/selenium_webdriver/element.rb', line 119 def fire_event(event_name) event_name = event_name.to_s.sub(/^on/, '').downcase script = "return (%s).apply(null, arguments)" % ATOMS.fetch(:fireEvent) bridge.executeScript(script, element, event_name) end |
#flash ⇒ Object
flash the element by temporarily changing the background color
29 30 31 32 33 34 35 36 |
# File 'lib/page-object/platforms/selenium_webdriver/element.rb', line 29 def flash original_color = attribute('backgroundColor') the_bridge = bridge 10.times do |n| color = (n % 2 == 0) ? 'red' : original_color the_bridge.executeScript("arguments[0].style.backgroundColor = '#{color}'", element) end end |
#focus ⇒ Object
Set the focus to the current element
155 156 157 |
# File 'lib/page-object/platforms/selenium_webdriver/element.rb', line 155 def focus bridge.executeScript("return arguments[0].focus()", element) end |
#hover ⇒ Object
hover over the element
128 129 130 131 |
# File 'lib/page-object/platforms/selenium_webdriver/element.rb', line 128 def hover mouse = Selenium::WebDriver::Mouse.new(bridge) mouse.move_to(element) end |
#html ⇒ String
Get the html for the element
52 53 54 55 |
# File 'lib/page-object/platforms/selenium_webdriver/element.rb', line 52 def html script = "return (%s).apply(null, arguments)" % ATOMS.fetch(:getOuterHtml) bridge.executeScript(script, element).strip end |
#id ⇒ Object
get the id of the element
272 273 274 |
# File 'lib/page-object/platforms/selenium_webdriver/element.rb', line 272 def id attribute(:id) end |
#parent ⇒ Object
find the parent element
144 145 146 147 148 149 150 |
# File 'lib/page-object/platforms/selenium_webdriver/element.rb', line 144 def parent script = "return (%s).apply(null, arguments)" % ATOMS.fetch(:getParentElement) 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_click ⇒ Object
Click this element
171 172 173 |
# File 'lib/page-object/platforms/selenium_webdriver/element.rb', line 171 def right_click element.context_click end |
#scroll_into_view ⇒ Object
Scroll until the element is viewable
279 280 281 |
# File 'lib/page-object/platforms/selenium_webdriver/element.rb', line 279 def scroll_into_view element.location_once_scrolled_into_view end |
#select_text(text) ⇒ Object
Select the provided text
162 163 164 165 166 |
# File 'lib/page-object/platforms/selenium_webdriver/element.rb', line 162 def select_text(text) Watir::Atoms.load(:selectText) script = "return (%s).apply(null, arguments)" % ATOMS.fetch(:selectText) bridge.executeScript(script, element, text) 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: ' '
258 259 260 |
# File 'lib/page-object/platforms/selenium_webdriver/element.rb', line 258 def send_keys(*args) element.send_keys(*args) end |
#tag_name ⇒ String
Get the tag name of this element
78 79 80 |
# File 'lib/page-object/platforms/selenium_webdriver/element.rb', line 78 def tag_name element.tag_name end |
#text ⇒ String
Get the text for the element
43 44 45 |
# File 'lib/page-object/platforms/selenium_webdriver/element.rb', line 43 def text element.text end |
#value ⇒ String
Get the value of this element
62 63 64 |
# File 'lib/page-object/platforms/selenium_webdriver/element.rb', line 62 def value element.attribute('value') end |
#visible? ⇒ Boolean
return true if an element is visible
15 16 17 |
# File 'lib/page-object/platforms/selenium_webdriver/element.rb', line 15 def visible? element.displayed? end |
#wait_until(timeout = ::PageObject.default_element_wait, message = nil, &block) ⇒ Object
Waits until the block returns true
240 241 242 243 |
# File 'lib/page-object/platforms/selenium_webdriver/element.rb', line 240 def wait_until(timeout=::PageObject.default_element_wait, =nil, &block) wait = Object::Selenium::WebDriver::Wait.new({:timeout => timeout, :message => }) wait.until &block end |
#when_not_present(timeout = ::PageObject.default_element_wait) ⇒ Object
Waits until the element is not present
timing out
194 195 196 197 198 199 200 201 202 203 204 205 |
# File 'lib/page-object/platforms/selenium_webdriver/element.rb', line 194 def when_not_present(timeout=::PageObject.default_element_wait) wait = Object::Selenium::WebDriver::Wait.new({:timeout => timeout, :message => "Element still present in #{timeout} seconds"}) wait.until do not_present = false begin not_present = false if element and element.displayed? rescue Selenium::WebDriver::Error::ObsoleteElementError not_present = true end not_present end end |
#when_not_visible(timeout = ::PageObject.default_element_wait) ⇒ Object
Waits until the element is not visible
225 226 227 228 229 230 231 |
# File 'lib/page-object/platforms/selenium_webdriver/element.rb', line 225 def when_not_visible(timeout=::PageObject.default_element_wait) wait = Object::Selenium::WebDriver::Wait.new({:timeout => timeout, :message => "Element still visible in #{timeout} seconds"}) wait.until do not self.visible? end self end |
#when_present(timeout = ::PageObject.default_element_wait) ⇒ Object
Waits until the element is present
180 181 182 183 184 185 186 |
# File 'lib/page-object/platforms/selenium_webdriver/element.rb', line 180 def when_present(timeout=::PageObject.default_element_wait) 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 = ::PageObject.default_element_wait) ⇒ Object
Waits until the element is visible
212 213 214 215 216 217 218 |
# File 'lib/page-object/platforms/selenium_webdriver/element.rb', line 212 def when_visible(timeout=::PageObject.default_element_wait) wait = Object::Selenium::WebDriver::Wait.new({:timeout => timeout, :message => "Element not visible in #{timeout} seconds"}) wait.until do self.visible? end self end |