Class: Watir::Element

Inherits:
Object
  • Object
show all
Includes:
Comparable, Container, Exception
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

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



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

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

Instance Attribute Details

#containerObject

Returns the value of attribute container.



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

def container
  @container
end

Instance Method Details

#<=>(other) ⇒ Object



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

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

#activeObjectHighLightColorObject



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

def activeObjectHighLightColor
  @container.activeObjectHighLightColor
end

#after_textObject Also known as: afterText

return the text after the element



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

def after_text # label only

  assert_exists
  begin
    ole_object.getAdjacentText("beforeBegin").strip
  rescue
    ''
  end
end

#assert_enabledObject



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

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

#assert_existsObject



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

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.



345
346
347
348
# File 'lib/watir/element.rb', line 345

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



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

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


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

def click
  click!
  @container.wait
end

#click!Object



258
259
260
261
262
263
264
# File 'lib/watir/element.rb', line 258

def click!
  assert_enabled

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

#click_no_waitObject



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

def click_no_wait
  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



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

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)


314
315
316
317
# File 'lib/watir/element.rb', line 314

def enabled?
  assert_exists
  return ! disabled
end

#exists?Boolean Also known as: exist?

Returns whether this element actually exists.

Returns:

  • (Boolean)


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

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


283
284
285
286
287
288
289
290
# File 'lib/watir/element.rb', line 283

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



268
269
270
271
272
273
274
275
276
277
# File 'lib/watir/element.rb', line 268

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


295
296
297
298
# File 'lib/watir/element.rb', line 295

def focus
  assert_enabled
  ole_object.focus
end

#inspectObject



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

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.



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

def ole_object # BUG: should use an attribute reader and rename the instance variable

  return @o
end

#ole_object=(o) ⇒ Object



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

def ole_object=(o)
  @o = o
end

#parentObject

Return the element immediately containing self.



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

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



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

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


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

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

#type_keysObject



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

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

#typingspeedObject



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

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)


323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
# File 'lib/watir/element.rb', line 323

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