Class: Watir::Element

Inherits:
Object show all
Extended by:
AttributeHelper
Includes:
Selenium, Container, Exception
Defined in:
lib/watir-webdriver/extensions/wait.rb,
lib/watir-webdriver/elements/element.rb

Overview

Base class for HTML elements.

Direct Known Subclasses

HTMLElement

Constant Summary

Constants included from AttributeHelper

AttributeHelper::IGNORED_ATTRIBUTES

Instance Method Summary collapse

Methods included from AttributeHelper

attribute_list, attributes, typed_attributes

Methods included from Container

#a, #abbr, #abbrs, #address, #addresses, #area, #areas, #article, #articles, #as, #aside, #asides, #audio, #audios, #b, #base, #bases, #bdo, #bdos, #blockquote, #blockquotes, #body, #bodys, #br, #brs, #bs, #button, #buttons, #canvas, #canvases, #caption, #captions, #cell, #cells, #checkbox, #checkboxes, #cite, #cites, #code, #codes, #col, #colgroup, #colgroups, #cols, #command, #commands, #datalist, #datalists, #dd, #dds, #del, #dels, #details, #dfn, #dfns, #div, #divs, #dl, #dls, #dt, #dts, #em, #embed, #embeds, #ems, #fieldset, #fieldsets, #figcaption, #figcaptions, #figure, #figures, #file_field, #file_fields, #font, #fonts, #footer, #footers, #form, #forms, #frame, #frames, #h1, #h1s, #h2, #h2s, #h3, #h3s, #h4, #h4s, #h5, #h5s, #h6, #h6s, #head, #header, #headers, #heads, #hgroup, #hgroups, #hidden, #hiddens, #hr, #hrs, #htmls, #i, #iframe, #iframes, #image, #images, #img, #imgs, #input, #inputs, #ins, #inses, #is, #kbd, #kbds, #keygen, #keygens, #label, #labels, #legend, #legends, #li, #link, #links, #lis, #map, #maps, #mark, #marks, #menu, #menus, #meta, #metas, #meter, #meters, #nav, #navs, #noscript, #noscripts, #object, #objects, #ol, #ols, #optgroup, #optgroups, #option, #options, #output, #outputs, #p, #param, #params, #pre, #pres, #progress, #progresses, #ps, #q, #qs, #radio, #radios, #row, #rows, #rp, #rps, #rt, #rts, #rubies, #ruby, #samp, #samps, #script, #scripts, #section, #sections, #select, #select_list, #select_lists, #selects, #small, #smalls, #source, #sources, #span, #spans, #strong, #strongs, #styles, #sub, #subs, #summaries, #summary, #sup, #sups, #table, #tables, #tbody, #tbodys, #td, #tds, #text_field, #text_fields, #textarea, #textareas, #tfoot, #tfoots, #th, #thead, #theads, #ths, #time, #times, #title, #titles, #tr, #track, #tracks, #trs, #ul, #uls, #var, #vars, #video, #videos, #wbr, #wbrs

Methods included from XpathSupport

#element_by_xpath, #elements_by_xpath

Constructor Details

#initialize(parent, selector) ⇒ Element

Returns a new instance of Element


15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/watir-webdriver/elements/element.rb', line 15

def initialize(parent, selector)
  @parent   = parent
  @selector = selector

  unless @selector.kind_of? Hash
    raise ArgumentError, "invalid argument: #{selector.inspect}"
  end

  if @selector.has_key?(:element)
    @element = @selector[:element]
  end
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(meth, *args, &blk) ⇒ Object (private)


215
216
217
218
219
220
221
222
# File 'lib/watir-webdriver/elements/element.rb', line 215

def method_missing(meth, *args, &blk)
  method = meth.to_s
  if method =~ /^data_(.+)$/
    attribute_value(method.gsub(/_/, '-'), *args)
  else
    super
  end
end

Instance Method Details

#==(other) ⇒ Object Also known as: eql?


44
45
46
47
# File 'lib/watir-webdriver/elements/element.rb', line 44

def ==(other)
  assert_exists
  @element == other.element
end

#attribute_value(attribute_name) ⇒ Object


106
107
108
109
# File 'lib/watir-webdriver/elements/element.rb', line 106

def attribute_value(attribute_name)
  assert_exists
  @element.attribute attribute_name
end

#clickObject


64
65
66
67
68
69
# File 'lib/watir-webdriver/elements/element.rb', line 64

def click
  assert_exists
  assert_enabled
  @element.click
  run_checkers
end

#double_clickObject

Raises:

  • (NotImplementedError)

71
72
73
74
75
76
77
# File 'lib/watir-webdriver/elements/element.rb', line 71

def double_click
  assert_exists
  raise NotImplementedError, "need support in WebDriver"

  @element.double_click
  run_checkers
end

#driverObject


148
149
150
# File 'lib/watir-webdriver/elements/element.rb', line 148

def driver
  @parent.driver
end

#elementObject Also known as: wd


152
153
154
155
# File 'lib/watir-webdriver/elements/element.rb', line 152

def element
  assert_exists
  @element
end

#exists?Boolean Also known as: exist?

Returns:

  • (Boolean)

28
29
30
31
32
33
# File 'lib/watir-webdriver/elements/element.rb', line 28

def exists?
  assert_exists
  true
rescue UnknownObjectException, UnknownFrameException
  false
end

#fire_event(event_name, bubble = false) ⇒ Object


132
133
134
135
136
# File 'lib/watir-webdriver/elements/element.rb', line 132

def fire_event(event_name, bubble = false)
  assert_exists
  event_name = event_name.to_s.sub(/^on/, '')
  browserbot('triggerEvent', @element, event_name, bubble)
end

#flashObject


87
88
89
90
91
92
93
94
# File 'lib/watir-webdriver/elements/element.rb', line 87

def flash
  original_color = style("backgroundColor")

  10.times do |n|
    color = (n % 2 == 0) ? "red" : original_color
    driver.execute_script("arguments[0].style.backgroundColor = '#{color}'", @element)
  end
end

#focusObject

Note: Firefox queues focus events until the window actually has focus.

See code.google.com/p/selenium/issues/detail?id=157


127
128
129
130
# File 'lib/watir-webdriver/elements/element.rb', line 127

def focus
  assert_exists
  driver.execute_script "return arguments[0].focus()", @element
end

#hashObject


50
51
52
# File 'lib/watir-webdriver/elements/element.rb', line 50

def hash
  @element ? @element.hash : super
end

#htmlObject


111
112
113
114
# File 'lib/watir-webdriver/elements/element.rb', line 111

def html
  assert_exists
  browserbot('getOuterHTML', @element).strip
end

#inspectObject


36
37
38
39
40
41
42
# File 'lib/watir-webdriver/elements/element.rb', line 36

def inspect
  if @selector.has_key?(:element)
    '#<%s:0x%x located=%s selector=%s>' % [self.class, hash*2, !!@element, '{:element=>(webdriver element)}']
  else
    '#<%s:0x%x located=%s selector=%s>' % [self.class, hash*2, !!@element, selector_string]
  end
end

#parentObject


138
139
140
141
142
143
144
145
146
# File 'lib/watir-webdriver/elements/element.rb', line 138

def parent
  assert_exists

  e = driver.execute_script "return arguments[0].parentNode", @element

  if e.kind_of?(WebDriver::Element)
    Watir.element_class_for(e.tag_name).new(@parent, :element => e)
  end
end

#present?Boolean

Returns true if the element exists and is visible on the page

This method is provided by an optional require.

Returns:

  • (Boolean)

See Also:


80
81
82
# File 'lib/watir-webdriver/extensions/wait.rb', line 80

def present?
  exists? && visible?
end

#right_clickObject

Raises:

  • (NotImplementedError)

79
80
81
82
83
84
85
# File 'lib/watir-webdriver/elements/element.rb', line 79

def right_click
  assert_exists
  raise NotImplementedError, "need support in WebDriver"

  @element.right_click
  run_checkers
end

#run_checkersObject


172
173
174
# File 'lib/watir-webdriver/elements/element.rb', line 172

def run_checkers
  @parent.run_checkers
end

#send_keys(*args) ⇒ Object


116
117
118
119
# File 'lib/watir-webdriver/elements/element.rb', line 116

def send_keys(*args)
  assert_exists
  @element.send_keys(*args)
end

#style(property = nil) ⇒ Object


163
164
165
166
167
168
169
170
# File 'lib/watir-webdriver/elements/element.rb', line 163

def style(property = nil)
  if property
    assert_exists
    @element.style property
  else
    attribute_value("style") || ''
  end
end

#tag_nameObject


59
60
61
62
# File 'lib/watir-webdriver/elements/element.rb', line 59

def tag_name
  assert_exists
  @element.tag_name
end

#textObject


54
55
56
57
# File 'lib/watir-webdriver/elements/element.rb', line 54

def text
  assert_exists
  @element.text
end

#valueObject


96
97
98
99
100
101
102
103
104
# File 'lib/watir-webdriver/elements/element.rb', line 96

def value
  assert_exists

  begin
    @element.value || ''
  rescue WebDriver::Error::ElementNotEnabledError
    ""
  end
end

#visible?Boolean

Returns:

  • (Boolean)

158
159
160
161
# File 'lib/watir-webdriver/elements/element.rb', line 158

def visible?
  assert_exists
  @element.displayed?
end

#wait_until_present(timeout = 30) ⇒ Object

Waits until the element is present.

This method is provided by an optional require.

Parameters:

  • timeout (Integer) (defaults to: 30)

    seconds to wait before timing out

See Also:


118
119
120
# File 'lib/watir-webdriver/extensions/wait.rb', line 118

def wait_until_present(timeout = 30)
  Watir::Wait.until(timeout) { self.present? }
end

#wait_while_present(timeout = 30) ⇒ Object

Waits while the element is present.

This method is provided by an optional require.

Parameters:

  • timeout (Integer) (defaults to: 30)

    seconds to wait before timing out

See Also:


133
134
135
136
137
# File 'lib/watir-webdriver/extensions/wait.rb', line 133

def wait_while_present(timeout = 30)
  Watir::Wait.while(timeout) { self.present? }
rescue Selenium::WebDriver::Error::ObsoleteElementError
  # it's not present
end

#when_present(timeout = 30) ⇒ Object

Waits until the element is present.

This method is provided by an optional require. Example:

browser.button(:id, 'foo').when_present.click
browser.div(:id, 'bar').when_present { |div| ... }
browser.p(:id, 'baz').when_present(60).text

Parameters:

  • timeout (Integer) (defaults to: 30)

    seconds to wait before timing out

See Also:


98
99
100
101
102
103
104
105
# File 'lib/watir-webdriver/extensions/wait.rb', line 98

def when_present(timeout = 30)
  if block_given?
    Watir::Wait.until(timeout) { self.present? }
    yield self
  else
    WhenPresentDecorator.new(self, timeout)
  end
end