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, #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, #elements, #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, #rp, #rps, #rt, #rts, #rubies, #ruby, #s, #samp, #samps, #script, #scripts, #section, #sections, #select, #select_list, #select_lists, #selects, #small, #smalls, #source, #sources, #span, #spans, #ss, #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)



255
256
257
258
259
260
261
262
# File 'lib/watir-webdriver/elements/element.rb', line 255

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
48
49
# File 'lib/watir-webdriver/elements/element.rb', line 44

def ==(other)
  return false unless other.kind_of? self.class

  assert_exists
  @element == other.element
end

#attribute_value(attribute_name) ⇒ Object



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

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

#clickObject



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

def click
  assert_exists
  assert_enabled
  @element.click
  run_checkers
end

#double_clickObject

Raises:

  • (NotImplementedError)


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

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

  @element.double_click
  run_checkers
end

#driverObject



150
151
152
# File 'lib/watir-webdriver/elements/element.rb', line 150

def driver
  @parent.driver
end

#elementObject Also known as: wd



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

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



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

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



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

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



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

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

#hashObject



52
53
54
# File 'lib/watir-webdriver/elements/element.rb', line 52

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

#htmlObject



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

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



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

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)


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

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

  @element.right_click
  run_checkers
end

#run_checkersObject



174
175
176
# File 'lib/watir-webdriver/elements/element.rb', line 174

def run_checkers
  @parent.run_checkers
end

#send_keys(*args) ⇒ Object



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

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

#style(property = nil) ⇒ Object



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

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

#tag_nameObject



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

def tag_name
  assert_exists
  @element.tag_name
end

#textObject



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

def text
  assert_exists
  @element.text
end

#to_subtypeObject

Cast this Element instance to a more specific subtype.

Example:

browser.element(:xpath => "//input[@type='submit']").to_subtype #=> #<Watir::Button>


186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
# File 'lib/watir-webdriver/elements/element.rb', line 186

def to_subtype
  elem = element()
  tag_name = elem.tag_name

  klass = nil

  if tag_name == "input"
    klass = case elem.attribute(:type)
      when *Button::VALID_TYPES
        Button
      when 'checkbox'
        CheckBox
      when 'radio'
        Radio
      when 'file'
        FileField
      else
        TextField
      end
  else
    klass = Watir.element_class_for(tag_name)
  end

  klass.new(@parent, :element => elem)
end

#valueObject



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

def value
  assert_exists

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

#visible?Boolean

Returns:

  • (Boolean)


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

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