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

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

Instance Method Summary collapse

Methods included from Container

#area, #areas, #button, #buttons, #cell, #cells, #checkbox, #checkboxes, #dds, #divs, #dls, #dts, #element, #elements, #ems, #file_field, #file_fields, #form, #forms, #frame, #hidden, #hiddens, #image, #images, #labels, #link, #links, #lis, #locate_all_elements, #locate_input_element, #locate_tagged_element, #log, #map, #maps, #modal_dialog, #popup, #pres, #ps, #radio, #radios, #row, #rows, #select_list, #select_lists, #set_container, #show_all_objects, #spans, #strongs, #table, #tables, #text_field, #text_fields, #wait

Constructor Details

#initialize(ole_object) ⇒ Element

ole_object - the ole object for the element being wrapped



14
15
16
17
# File 'lib/watir/element.rb', line 14

def initialize(ole_object)
  @o = ole_object
  @original_color = nil
end

Instance Attribute Details

#containerObject

Returns the value of attribute container



8
9
10
# File 'lib/watir/element.rb', line 8

def container
  @container
end

Instance Method Details

#<=>(other) ⇒ Object



150
151
152
153
154
# File 'lib/watir/element.rb', line 150

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

#activeObjectHighLightColorObject



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

def activeObjectHighLightColor
  @container.activeObjectHighLightColor
end

#after_textObject Also known as: afterText

return the text after the element



112
113
114
115
116
117
118
119
# File 'lib/watir/element.rb', line 112

def after_text # label only
  assert_exists
  begin
    ole_object.getAdjacentText("beforeBegin").strip
  rescue
    ''
  end
end

#assert_enabledObject



62
63
64
65
66
# File 'lib/watir/element.rb', line 62

def assert_enabled
  unless enabled?
    raise ObjectDisabledException, "object #{@how} and #{@what} is disabled"
  end
end

#assert_existsObject



54
55
56
57
58
59
60
# File 'lib/watir/element.rb', line 54

def assert_exists
  locate if respond_to?(: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.



350
351
352
353
# File 'lib/watir/element.rb', line 350

def attribute_value(attribute_name)
  assert_exists
  return ole_object.getAttribute(attribute_name)
end

#before_textObject Also known as: beforeText

return the text before the element



102
103
104
105
106
107
108
109
# File 'lib/watir/element.rb', line 102

def before_text # label only
  assert_exists
  begin
    ole_object.getAdjacentText("afterEnd").strip
  rescue
    ''
  end
end

#clickObject

This method clicks the active element.

raises: UnknownObjectException  if the object is not found
ObjectDisabledException if the object is currently disabled


229
230
231
232
# File 'lib/watir/element.rb', line 229

def click
  click!
  @container.wait
end

#click!Object



260
261
262
263
264
265
266
267
# File 'lib/watir/element.rb', line 260

def click!
  assert_exists
  assert_enabled

  highlight(:set)
  ole_object.click
  highlight(:clear)
end

#click_no_waitObject



234
235
236
237
238
239
240
241
242
243
244
# File 'lib/watir/element.rb', line 234

def click_no_wait
  assert_exists
  assert_enabled
  highlight(:set)
  element = "#{self.class}.new(#{@page_container.attach_command}, :unique_number, #{self.unique_number})"
  ruby_code = "require 'rubygems';" <<
          "require '#{File.expand_path(File.dirname(__FILE__))}/core';" <<
          "#{element}.click!"
  system(spawned_click_no_wait_command(ruby_code))
  highlight(:clear)
end

#documentObject



135
136
137
138
# File 'lib/watir/element.rb', line 135

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)


319
320
321
322
# File 'lib/watir/element.rb', line 319

def enabled?
  assert_exists
  return ! disabled
end

#exists?Boolean Also known as: exist?

Returns whether this element actually exists.

Returns:

  • (Boolean)


306
307
308
309
310
311
312
313
# File 'lib/watir/element.rb', line 306

def exists?
  begin
    locate if defined?(locate)
  rescue WIN32OLERuntimeError
    @o = nil
  end
  @o ? true: false
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


286
287
288
289
290
291
292
293
294
# File 'lib/watir/element.rb', line 286

def fire_event(event)
  assert_exists
  assert_enabled

  highlight(:set)
  ole_object.fireEvent(event)
  @container.wait
  highlight(:clear)
end

#flash(number = 10) ⇒ Object

Flash the element the specified number of times. Defaults to 10 flashes.



271
272
273
274
275
276
277
278
279
280
# File 'lib/watir/element.rb', line 271

def flash number=10
  assert_exists
  number.times do
    highlight(:set)
    sleep 0.05
    highlight(:clear)
    sleep 0.05
  end
  nil
end

#focusObject

This method sets focus on the active element.

raises: UnknownObjectException  if the object is not found
        ObjectDisabledException if the object is currently disabled


299
300
301
302
303
# File 'lib/watir/element.rb', line 299

def focus
  assert_exists
  assert_enabled
  ole_object.focus
end

#inspectObject



28
29
30
# File 'lib/watir/element.rb', line 28

def inspect
  '#<%s:0x%x located=%s how=%s what=%s>' % [self.class, hash*2, !!ole_object, @how.inspect, @what.inspect]
end

#ole_objectObject Also known as: getOLEObject

Return the ole object, allowing any methods of the DOM that Watir doesn't support to be used.



20
21
22
# File 'lib/watir/element.rb', line 20

def ole_object # BUG: should use an attribute reader and rename the instance variable
  return @o
end

#ole_object=(o) ⇒ Object



24
25
26
# File 'lib/watir/element.rb', line 24

def ole_object=(o)
  @o = o
end

#parentObject

Return the element immediately containing self.



141
142
143
144
145
146
# File 'lib/watir/element.rb', line 141

def parent
  assert_exists
  result = Element.new(ole_object.parentelement)
  result.set_container self
  result
end

#textObject Also known as: innerText

Return the innerText of the object Raise an ObjectNotFound exception if the object cannot be found



123
124
125
126
# File 'lib/watir/element.rb', line 123

def text
  assert_exists
  return ole_object.innerText.strip
end

#to_sObject

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


194
195
196
197
# File 'lib/watir/element.rb', line 194

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

#type_keysObject



165
166
167
168
# File 'lib/watir/element.rb', line 165

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

#typingspeedObject



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

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)


328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
# File 'lib/watir/element.rb', line 328

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