Class: Watir::Element
Overview
Base class for HTML elements.
Constant Summary
AttributeHelper::IGNORED_ATTRIBUTES
Instance Method Summary
collapse
attribute_list, attributes, typed_attributes
#wait_until_present, #wait_while_present, #when_present
Methods included from Container
#a, #abbr, #abbrs, #address, #addresses, #area, #areas, #article, #articles, #as, #aside, #asides, #audio, #audios, #b, #base, #bases, #bdi, #bdis, #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, #element, #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, #u, #ul, #uls, #us, #var, #vars, #video, #videos, #wbr, #wbrs
#element_by_xpath, #elements_by_xpath, escape
Constructor Details
#initialize(parent, selector) ⇒ Element
Returns a new instance of Element.
17
18
19
20
21
22
23
24
25
26
27
28
|
# File 'lib/watir-webdriver/elements/element.rb', line 17
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
298
299
300
301
302
303
304
305
|
# File 'lib/watir-webdriver/elements/element.rb', line 298
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?
46
47
48
49
50
51
|
# File 'lib/watir-webdriver/elements/element.rb', line 46
def ==(other)
return false unless other.kind_of? self.class
assert_exists
@element == other.wd
end
|
#attribute_value(attribute_name) ⇒ Object
116
117
118
119
|
# File 'lib/watir-webdriver/elements/element.rb', line 116
def attribute_value(attribute_name)
assert_exists
@element.attribute attribute_name
end
|
68
69
70
71
72
73
|
# File 'lib/watir-webdriver/elements/element.rb', line 68
def click
assert_exists
assert_enabled
@element.click
run_checkers
end
|
#double_click ⇒ Object
75
76
77
78
79
80
81
82
83
84
|
# File 'lib/watir-webdriver/elements/element.rb', line 75
def double_click
assert_exists
unless driver.respond_to?(:mouse)
raise NotImplementedError, "need support in WebDriver"
end
driver.mouse.double_click @element
run_checkers
end
|
This method is part of a private API.
You should avoid using this method if possible, as it may be removed or be changed in the future.
171
172
173
|
# File 'lib/watir-webdriver/elements/element.rb', line 171
def driver
@parent.driver
end
|
#exists? ⇒ Boolean
Also known as:
exist?
#fire_event(event_name) ⇒ Object
142
143
144
145
146
|
# File 'lib/watir-webdriver/elements/element.rb', line 142
def fire_event(event_name)
assert_exists
event_name = event_name.to_s.sub(/^on/, '').downcase
execute_atom(:fireEvent, @element, event_name)
end
|
97
98
99
100
101
102
103
104
|
# File 'lib/watir-webdriver/elements/element.rb', line 97
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
|
137
138
139
140
|
# File 'lib/watir-webdriver/elements/element.rb', line 137
def focus
assert_exists
driver.execute_script "return arguments[0].focus()", @element
end
|
54
55
56
|
# File 'lib/watir-webdriver/elements/element.rb', line 54
def hash
@element ? @element.hash : super
end
|
121
122
123
124
|
# File 'lib/watir-webdriver/elements/element.rb', line 121
def html
assert_exists
execute_atom(:getOuterHtml, @element).strip
end
|
38
39
40
41
42
43
44
|
# File 'lib/watir-webdriver/elements/element.rb', line 38
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
|
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
|
# File 'lib/watir-webdriver/elements/element.rb', line 148
def parent
assert_exists
script = <<-JS
var element = arguments[0].parentNode;
if(element.nodeType != Node.ELEMENT_NODE)
return null;
return element;
JS
e = driver.execute_script(script, @element)
if e.kind_of?(WebDriver::Element)
Watir.element_class_for(e.tag_name.downcase).new(@parent, :element => e)
end
end
|
#present? ⇒ Boolean
Returns true if the element exists and is visible on the page
199
200
201
|
# File 'lib/watir-webdriver/elements/element.rb', line 199
def present?
exists? && visible?
end
|
#right_click ⇒ Object
86
87
88
89
90
91
92
93
94
95
|
# File 'lib/watir-webdriver/elements/element.rb', line 86
def right_click
assert_exists
unless driver.respond_to?(:mouse)
raise NotImplementedError, "need support in WebDriver"
end
driver.mouse.context_click @element
run_checkers
end
|
#run_checkers ⇒ Object
212
213
214
|
# File 'lib/watir-webdriver/elements/element.rb', line 212
def run_checkers
@parent.run_checkers
end
|
#send_keys(*args) ⇒ Object
126
127
128
129
|
# File 'lib/watir-webdriver/elements/element.rb', line 126
def send_keys(*args)
assert_exists
@element.send_keys(*args)
end
|
#style(property = nil) ⇒ Object
203
204
205
206
207
208
209
210
|
# File 'lib/watir-webdriver/elements/element.rb', line 203
def style(property = nil)
if property
assert_exists
@element.style property
else
attribute_value("style").to_s.strip
end
end
|
63
64
65
66
|
# File 'lib/watir-webdriver/elements/element.rb', line 63
def tag_name
assert_exists
@element.tag_name.downcase
end
|
58
59
60
61
|
# File 'lib/watir-webdriver/elements/element.rb', line 58
def text
assert_exists
@element.text
end
|
#to_subtype ⇒ Object
Cast this Element instance to a more specific subtype.
Example:
browser.element(:xpath => "//input[@type='submit']").to_subtype
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
|
# File 'lib/watir-webdriver/elements/element.rb', line 224
def to_subtype
elem = wd()
tag_name = elem.tag_name.downcase
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
|
106
107
108
109
110
111
112
113
114
|
# File 'lib/watir-webdriver/elements/element.rb', line 106
def value
assert_exists
begin
@element.attribute('value') || ''
rescue WebDriver::Error::InvalidElementStateError
""
end
end
|
#visible? ⇒ Boolean
Returns true if this element is visible on the page
188
189
190
191
|
# File 'lib/watir-webdriver/elements/element.rb', line 188
def visible?
assert_exists
@element.displayed?
end
|
This method is part of a private API.
You should avoid using this method if possible, as it may be removed or be changed in the future.
179
180
181
182
|
# File 'lib/watir-webdriver/elements/element.rb', line 179
def wd
assert_exists
@element
end
|