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.

Direct Known Subclasses

Form, HTMLElement, Image, InputElement, Link, NonControlElement, Table, TableBodies, TableBody, TableCell, TableRow

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

- (Element) initialize(ole_object)

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

- (Object) container

Returns the value of attribute container



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

def container
  @container
end

Instance Method Details

- (Object) <=>(other)



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

- (Object) activeObjectHighLightColor



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

def activeObjectHighLightColor
  @container.activeObjectHighLightColor
end

- (Object) after_text 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

- (Object) assert_enabled



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

- (Object) assert_exists



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

- (Object) attribute_value(attribute_name)

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

- (Object) before_text 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

- (Object) click

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

- (Object) click!



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

- (Object) click_no_wait



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

- (Object) document



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

def document
  assert_exists
  return ole_object
end

- (Boolean) enabled?

Returns true if the element is enabled, false if it isn't.

raises: UnknownObjectException  if the object is not found


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

def enabled?
  assert_exists
  return ! disabled
end

- (Boolean) exists? Also known as: exist?

Returns whether this element actually exists.



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

- (Object) fire_event(event) 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

- (Object) flash(number = 10)

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

- (Object) focus

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

- (Object) inspect



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

- (Object) ole_object 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

- (Object) ole_object=(o)



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

def ole_object=(o)
  @o = o
end

- (Object) parent

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

- (Object) text 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

- (Object) to_s

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

- (Object) type_keys



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

- (Object) typingspeed



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

def typingspeed
  @container.typingspeed
end

- (Boolean) visible?

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.



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