Class: Watir::Element
- Inherits:
-
Object
- Object
- Watir::Element
- Includes:
- Comparable, ElementExtensions, Exception, Container
- Defined in:
- lib/watir/element.rb,
lib/watir/camel_case.rb
Overview
Base class for html elements. This is not a class that users would normally access.
Direct Known Subclasses
Form, Frame, HTMLElement, Image, InputElement, Link, NonControlElement, Table, TableBodies, TableBody, TableCell, TableRow
Constant Summary collapse
- TO_S_SIZE =
number of spaces that separate the property from the value in the to_s method
14
Instance Attribute Summary collapse
-
#container ⇒ Object
Returns the value of attribute container.
Attributes included from Container
Class Method Summary collapse
Instance Method Summary collapse
- #<=>(other) ⇒ Object
- #__ole_inner_elements ⇒ Object
- #activeObjectHighLightColor ⇒ Object
-
#after_text ⇒ Object
(also: #afterText)
return the text after the element.
- #assert_enabled ⇒ Object
- #assert_exists ⇒ Object
-
#attribute_value(attribute_name) ⇒ Object
Get attribute value for any attribute of the element.
-
#before_text ⇒ Object
(also: #beforeText)
return the text before the element.
-
#click ⇒ Object
This method clicks the active element.
- #click! ⇒ Object
- #create_event(event) ⇒ Object
- #dispatch_event(event) ⇒ Object
- #document ⇒ Object
-
#enabled? ⇒ Boolean
Returns true if the element is enabled, false if it isn’t.
-
#exists? ⇒ Boolean
(also: #exist?)
Returns whether this element actually exists.
-
#fire_event(event) ⇒ Object
(also: #fireEvent)
Executes a user defined “fireEvent” for objects with JavaScript events tied to them such as DHTML menus.
-
#flash(number = 10) ⇒ Object
Flash the element the specified number of times.
-
#focus ⇒ Object
This method sets focus on the active element.
-
#initialize(ole_object) ⇒ Element
constructor
ole_object - the ole object for the element being wrapped.
- #inspect ⇒ Object
- #locate ⇒ Object
- #method_missing(method_name, *args, &block) ⇒ Object
-
#name ⇒ Object
IE9 only returns empty string for ole_object.name for non-input elements so get at it through the attribute which will make the matchers work.
-
#ole_object ⇒ Object
(also: #getOLEObject)
Return the ole object, allowing any methods of the DOM that Watir doesn’t support to be used.
- #ole_object=(o) ⇒ Object
-
#parent ⇒ Object
Return the element immediately containing self.
-
#text ⇒ Object
(also: #innerText)
Return the innerText of the object Raise an ObjectNotFound exception if the object cannot be found.
-
#to_s ⇒ Object
Display basic details about the object.
- #type_keys ⇒ Object
- #typingspeed ⇒ Object
-
#visible? ⇒ Boolean
If any parent element isn’t visible then we cannot write to the element.
Methods included from Container
#input_element_locator, #locator_for, #log, #set_container, #show_all_objects, #tagged_element_locator, #wait
Constructor Details
#initialize(ole_object) ⇒ Element
ole_object - the ole object for the element being wrapped
28 29 30 31 |
# File 'lib/watir/element.rb', line 28 def initialize(ole_object) @o = ole_object @original_color = nil end |
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(method_name, *args, &block) ⇒ Object
438 439 440 441 442 443 444 445 446 447 448 449 |
# File 'lib/watir/element.rb', line 438 def method_missing(method_name, *args, &block) if method_name.to_s =~ /(.*)_no_wait/ && self.respond_to?($1) assert_exists assert_enabled highlight(:set) ruby_code = generate_ruby_code(self, $1, *args) system(spawned_no_wait_command(ruby_code)) highlight(:clear) else super end end |
Instance Attribute Details
#container ⇒ Object
Returns the value of attribute container.
8 9 10 |
# File 'lib/watir/element.rb', line 8 def container @container end |
Class Method Details
.inherited(subclass) ⇒ Object
13 14 15 16 17 18 19 20 21 22 23 24 25 |
# File 'lib/watir/element.rb', line 13 def self.inherited subclass class_name = Watir::Util.demodulize(subclass.to_s) method_name = Watir::Util.underscore(class_name) Watir::Container.module_eval <<-RUBY def #{method_name}(how={}, what=nil) #{class_name}.new(self, how, what) end def #{method_name}s(how={}, what=nil) #{class_name}s.new(self, how, what) end RUBY end |
Instance Method Details
#<=>(other) ⇒ Object
175 176 177 178 179 |
# File 'lib/watir/element.rb', line 175 def <=> other assert_exists other.assert_exists ole_object.sourceindex <=> other.ole_object.sourceindex end |
#__ole_inner_elements ⇒ Object
155 156 157 158 |
# File 'lib/watir/element.rb', line 155 def __ole_inner_elements assert_exists return ole_object.all end |
#activeObjectHighLightColor ⇒ Object
195 196 197 |
# File 'lib/watir/element.rb', line 195 def activeObjectHighLightColor @container.activeObjectHighLightColor end |
#after_text ⇒ Object Also known as: afterText
return the text after the element
132 133 134 135 136 137 138 139 |
# File 'lib/watir/element.rb', line 132 def after_text # label only assert_exists begin ole_object.getAdjacentText("beforeBegin").strip rescue '' end end |
#assert_enabled ⇒ Object
82 83 84 85 86 |
# File 'lib/watir/element.rb', line 82 def assert_enabled unless enabled? raise ObjectDisabledException, "object #{@how} and #{@what} is disabled" end end |
#assert_exists ⇒ Object
74 75 76 77 78 79 80 |
# File 'lib/watir/element.rb', line 74 def assert_exists locate unless ole_object raise UnknownObjectException.new( Watir::Exception.(@how, @what)) end end |
#attribute_value(attribute_name) ⇒ Object
Get attribute value for any attribute of the element. Returns null if attribute doesn’t exist.
433 434 435 436 |
# File 'lib/watir/element.rb', line 433 def attribute_value(attribute_name) assert_exists return ole_object.getAttribute(attribute_name) end |
#before_text ⇒ Object Also known as: beforeText
return the text before the element
122 123 124 125 126 127 128 129 |
# File 'lib/watir/element.rb', line 122 def before_text # label only assert_exists begin ole_object.getAdjacentText("afterEnd").strip rescue '' end end |
#click ⇒ Object
This method clicks the active element.
raises: UnknownObjectException if the object is not found
ObjectDisabledException if the object is currently disabled
254 255 256 257 |
# File 'lib/watir/element.rb', line 254 def click click! @container.wait end |
#click! ⇒ Object
299 300 301 302 303 304 305 306 307 308 309 |
# File 'lib/watir/element.rb', line 299 def click! assert_exists assert_enabled highlight(:set) # Not sure why but in IE9 Document mode, passing a parameter # to click seems to work. Firing the onClick event breaks other tests # so this seems to be the safest change and also works fine in IE8 ole_object.click(0) highlight(:clear) end |
#create_event(event) ⇒ Object
349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 |
# File 'lib/watir/element.rb', line 349 def create_event(event) event =~ /on(.*)/i event = $1 if $1 event.downcase! # See http://www.howtocreate.co.uk/tutorials/javascript/domevents case event when 'abort', 'blur', 'change', 'error', 'focus', 'load', 'reset', 'resize', 'scroll', 'select', 'submit', 'unload' event_name = :initEvent event_type = 'HTMLEvents' event_args = [event, true, true] when 'keydown', 'keypress', 'keyup' event_name = :initKeyboardEvent event_type = 'KeyboardEvent' # 'type', bubbles, cancelable, windowObject, ctrlKey, altKey, shiftKey, metaKey, keyCode, charCode event_args = [event, true, true, @container.page_container.document.parentWindow.window, false, false, false, false, 0, 0] when 'click', 'dblclick', 'mousedown', 'mousemove', 'mouseout', 'mouseover', 'mouseup', 'contextmenu', 'drag', 'dragstart', 'dragenter', 'dragover', 'dragleave', 'dragend', 'drop', 'selectstart' event_name = :initMouseEvent event_type = 'MouseEvents' # 'type', bubbles, cancelable, windowObject, detail, screenX, screenY, clientX, clientY, ctrlKey, altKey, shiftKey, metaKey, button, relatedTarget event_args = [event, true, true, @container.page_container.document.parentWindow.window, 1, 0, 0, 0, 0, false, false, false, false, 0, @container.page_container.document] else raise UnhandledEventException, "Don't know how to trigger event '#{event}'" end event = @container.page_container.document.createEvent(event_type) event.send event_name, *event_args event end |
#dispatch_event(event) ⇒ Object
337 338 339 340 341 342 343 344 345 346 347 |
# File 'lib/watir/element.rb', line 337 def dispatch_event(event) if IE.version_parts.first.to_i >= 9 if @container.page_container.document_mode.to_i >= 9 ole_object.dispatchEvent(create_event(event)) else ole_object.fireEvent(event) end else ole_object.fireEvent(event) end end |
#document ⇒ Object
160 161 162 163 |
# File 'lib/watir/element.rb', line 160 def document assert_exists return ole_object end |
#enabled? ⇒ Boolean
Returns true if the element is enabled, false if it isn’t.
raises: UnknownObjectException if the object is not found
402 403 404 405 |
# File 'lib/watir/element.rb', line 402 def enabled? assert_exists return ! disabled end |
#exists? ⇒ Boolean Also known as: exist?
Returns whether this element actually exists.
389 390 391 392 393 394 395 396 |
# File 'lib/watir/element.rb', line 389 def exists? begin locate rescue WIN32OLERuntimeError, UnknownObjectException @o = nil end !!@o end |
#fire_event(event) ⇒ Object Also known as: fireEvent
Executes a user defined “fireEvent” for objects with JavaScript events tied to them such as DHTML menus.
usage: allows a generic way to fire javascript events on page objects such as "onMouseOver", "onClick", etc.
raises: UnknownObjectException if the object is not found
ObjectDisabledException if the object is currently disabled
328 329 330 331 332 333 334 335 |
# File 'lib/watir/element.rb', line 328 def fire_event(event) assert_exists assert_enabled highlight(:set) dispatch_event(event) @container.wait highlight(:clear) end |
#flash(number = 10) ⇒ Object
Flash the element the specified number of times. Defaults to 10 flashes.
313 314 315 316 317 318 319 320 321 322 |
# File 'lib/watir/element.rb', line 313 def flash number=10 assert_exists number.times do highlight(:set) sleep 0.05 highlight(:clear) sleep 0.05 end nil end |
#focus ⇒ Object
This method sets focus on the active element.
raises: UnknownObjectException if the object is not found
ObjectDisabledException if the object is currently disabled
382 383 384 385 386 |
# File 'lib/watir/element.rb', line 382 def focus assert_exists assert_enabled ole_object.focus end |
#inspect ⇒ Object
48 49 50 |
# File 'lib/watir/element.rb', line 48 def inspect '#<%s:0x%x located=%s how=%s what=%s>' % [self.class, hash*2, !!ole_object, @how.inspect, @what.inspect] end |
#locate ⇒ Object
33 34 35 36 37 |
# File 'lib/watir/element.rb', line 33 def locate return if [Element, TableBodies].include? self.class tag = self.class.const_defined?(:TAG) ? self.class::TAG : self.class.name.split("::").last @o = @container.tagged_element_locator(tag, @how, @what).locate end |
#name ⇒ Object
IE9 only returns empty string for ole_object.name for non-input elements so get at it through the attribute which will make the matchers work
150 151 152 153 |
# File 'lib/watir/element.rb', line 150 def name assert_exists ole_object.getAttribute('name') || '' end |
#ole_object ⇒ Object Also known as: getOLEObject
Return the ole object, allowing any methods of the DOM that Watir doesn’t support to be used.
40 41 42 |
# File 'lib/watir/element.rb', line 40 def ole_object @o end |
#ole_object=(o) ⇒ Object
44 45 46 |
# File 'lib/watir/element.rb', line 44 def ole_object=(o) @o = o end |
#parent ⇒ Object
Return the element immediately containing self.
166 167 168 169 170 171 |
# File 'lib/watir/element.rb', line 166 def parent assert_exists result = Element.new(ole_object.parentelement) result.set_container self result end |
#text ⇒ Object Also known as: innerText
Return the innerText of the object Raise an ObjectNotFound exception if the object cannot be found
143 144 145 146 |
# File 'lib/watir/element.rb', line 143 def text assert_exists return ole_object.innerText.strip end |
#to_s ⇒ Object
Display basic details about the object. Sample output for a button is shown. Raises UnknownObjectException if the object is not found.
name b4
type
id b5
value Disabled Button
disabled true
219 220 221 222 |
# File 'lib/watir/element.rb', line 219 def to_s assert_exists return string_creator.join("\n") end |
#type_keys ⇒ Object
190 191 192 193 |
# File 'lib/watir/element.rb', line 190 def type_keys return @container.type_keys if @type_keys.nil? @type_keys end |
#typingspeed ⇒ Object
186 187 188 |
# File 'lib/watir/element.rb', line 186 def typingspeed @container.typingspeed end |
#visible? ⇒ Boolean
If any parent element isn’t visible then we cannot write to the element. The only realiable way to determine this is to iterate up the DOM element tree checking every element to make sure it’s visible.
411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 |
# File 'lib/watir/element.rb', line 411 def visible? # Now iterate up the DOM element tree and return false if any # parent element isn't visible assert_exists object = @o while object begin if object.currentstyle.invoke('visibility') =~ /^hidden$/i return false end if object.currentstyle.invoke('display') =~ /^none$/i return false end rescue WIN32OLERuntimeError end object = object.parentElement end true end |