Class: Watir::Element

Inherits:
Object
  • Object
show all
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.

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

Attributes included from Container

#page_container

Class Method Summary collapse

Instance Method Summary collapse

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



441
442
443
444
445
446
447
448
449
450
451
452
# File 'lib/watir/element.rb', line 441

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



177
178
179
180
181
# File 'lib/watir/element.rb', line 177

def <=> other
  assert_exists
  other.assert_exists
  ole_object.sourceindex <=> other.ole_object.sourceindex
end

#activeObjectHighLightColor ⇒ Object



197
198
199
# File 'lib/watir/element.rb', line 197

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.message_for_unable_to_locate(@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.



436
437
438
439
# File 'lib/watir/element.rb', line 436

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



256
257
258
259
# File 'lib/watir/element.rb', line 256

def click
  click!
  @container.wait
end

#click! ⇒ Object



301
302
303
304
305
306
307
308
309
310
311
# File 'lib/watir/element.rb', line 301

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



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
378
379
380
# File 'lib/watir/element.rb', line 352

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



339
340
341
342
343
344
345
346
347
348
349
350
# File 'lib/watir/element.rb', line 339

def dispatch_event(event)
  if IE.version_parts.first.to_i >= 9
    begin
      # we're in IE9 document standards mode
      ole_object.dispatchEvent(create_event(event))
    rescue WIN32OLERuntimeError
      ole_object.fireEvent(event)
    end
  else
    ole_object.fireEvent(event)
  end
end

#document ⇒ Object



162
163
164
165
# File 'lib/watir/element.rb', line 162

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

Returns:

  • (Boolean)


405
406
407
408
# File 'lib/watir/element.rb', line 405

def enabled?
  assert_exists
  return ! disabled
end

#exists? ⇒ Boolean Also known as: exist?

Returns whether this element actually exists.

Returns:

  • (Boolean)


392
393
394
395
396
397
398
399
# File 'lib/watir/element.rb', line 392

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



330
331
332
333
334
335
336
337
# File 'lib/watir/element.rb', line 330

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.



315
316
317
318
319
320
321
322
323
324
# File 'lib/watir/element.rb', line 315

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



385
386
387
388
389
# File 'lib/watir/element.rb', line 385

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.



168
169
170
171
172
173
# File 'lib/watir/element.rb', line 168

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 button id b5 value Disabled Button disabled true



221
222
223
224
# File 'lib/watir/element.rb', line 221

def to_s
  assert_exists
  return string_creator.join("\n")
end

#type_keys ⇒ Object



192
193
194
195
# File 'lib/watir/element.rb', line 192

def type_keys
  return @container.type_keys if @type_keys.nil?
  @type_keys
end

#typingspeed ⇒ Object



188
189
190
# File 'lib/watir/element.rb', line 188

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.

Returns:

  • (Boolean)


414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
# File 'lib/watir/element.rb', line 414

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