Class: Watir::Element
- Inherits:
-
Object
- Object
- Watir::Element
- Extended by:
- AttributeHelper
- Includes:
- Container, EventuallyPresent, Exception
- Defined in:
- lib/watir-webdriver/elements/element.rb,
lib/watir-webdriver/extensions/select_text.rb
Overview
Base class for HTML elements.
Direct Known Subclasses
Constant Summary
Constants included from AttributeHelper
AttributeHelper::IGNORED_ATTRIBUTES
Constants included from Atoms
Instance Method Summary collapse
-
#==(other) ⇒ Object
(also: #eql?)
Returns true if two elements are equal.
-
#attribute_value(attribute_name) ⇒ String
Returns given attribute value of element.
-
#browser ⇒ Watir::Browser
Returns browser.
-
#class_name ⇒ Object
temporarily add :id and :class_name manually since they’re no longer specified in the HTML spec.
-
#click(*modifiers) ⇒ Object
Clicks the element, optionally while pressing the given modifier keys.
-
#double_click ⇒ Object
Double clicks the element.
-
#drag_and_drop_by(right_by, down_by) ⇒ Object
Drag and drop this element by the given offsets.
-
#drag_and_drop_on(other) ⇒ Object
Drag and drop this element on to another element instance.
- #driver ⇒ Object private
-
#exists? ⇒ Boolean
(also: #exist?)
Returns true if element exists.
-
#fire_event(event_name) ⇒ Object
Simulates JavaScript events on element.
-
#flash ⇒ Object
Flashes (change background color far a moment) element.
-
#focus ⇒ Object
Focuses element.
-
#focused? ⇒ Boolean
Returns true if this element is focused.
- #hash ⇒ Object
-
#hover ⇒ Object
Moves the mouse to the middle of this element.
-
#html ⇒ String
Returns outer (inner + element itself) HTML code of element.
-
#id ⇒ Object
temporarily add :id and :class_name manually since they’re no longer specified in the HTML spec.
-
#initialize(parent, selector) ⇒ Element
constructor
A new instance of Element.
- #inspect ⇒ Object
-
#parent ⇒ Object
Returns parent element of current element.
-
#present? ⇒ Boolean
Returns true if the element exists and is visible on the page.
-
#right_click ⇒ Object
Right clicks the element.
-
#run_checkers ⇒ Object
Runs checkers.
- #select_text(str) ⇒ Object
-
#send_keys(*args) ⇒ Object
Sends sequence of keystrokes to element.
-
#style(property = nil) ⇒ String
Returns given style property of this element.
-
#tag_name ⇒ String
Returns tag name of the element.
-
#text ⇒ String
Returns the text of the element.
-
#to_subtype ⇒ Object
Cast this Element instance to a more specific subtype.
-
#value ⇒ String
Returns value of the element.
-
#visible? ⇒ Boolean
Returns true if this element is visible on the page.
- #wd ⇒ Object private
Methods included from AttributeHelper
attribute_list, attributes, typed_attributes
Methods included from EventuallyPresent
#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, #data, #datalist, #datalists, #datas, #dd, #dds, #del, #dels, #details, #detailses, #dfn, #dfns, #dialog, #dialogs, #div, #divs, #dl, #dls, #dt, #dts, #element, #elements, #em, #embed, #embeds, #ems, #extract_selector, #field_set, #field_sets, #fieldset, #fieldsets, #figcaption, #figcaptions, #figure, #figures, #file_field, #file_fields, #font, #fonts, #footer, #footers, #form, #forms, #frame, #frames, #frameset, #framesets, #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, #main, #mains, #map, #maps, #mark, #marks, #menu, #menuitem, #menuitems, #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, #template, #templates, #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
Methods included from Atoms
Methods included from XpathSupport
Constructor Details
#initialize(parent, selector) ⇒ Element
Returns a new instance of Element.
26 27 28 29 30 31 32 33 34 |
# File 'lib/watir-webdriver/elements/element.rb', line 26 def initialize(parent, selector) @parent = parent @selector = selector @element = nil unless @selector.kind_of? Hash raise ArgumentError, "invalid argument: #{selector.inspect}" end end |
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(meth, *args, &blk) ⇒ Object (private)
548 549 550 551 552 553 554 555 |
# File 'lib/watir-webdriver/elements/element.rb', line 548 def method_missing(meth, *args, &blk) method = meth.to_s if method =~ ElementLocator::WILDCARD_ATTRIBUTE attribute_value(method.gsub(/_/, '-'), *args) else super end end |
Instance Method Details
#==(other) ⇒ Object Also known as: eql?
Returns true if two elements are equal.
66 67 68 69 70 71 |
# File 'lib/watir-webdriver/elements/element.rb', line 66 def ==(other) return false unless other.kind_of? self.class assert_exists @element == other.wd end |
#attribute_value(attribute_name) ⇒ String
Returns given attribute value of element.
272 273 274 275 |
# File 'lib/watir-webdriver/elements/element.rb', line 272 def attribute_value(attribute_name) assert_exists @element.attribute attribute_name end |
#browser ⇒ Watir::Browser
Returns browser.
475 476 477 |
# File 'lib/watir-webdriver/elements/element.rb', line 475 def browser @parent.browser end |
#class_name ⇒ Object
temporarily add :id and :class_name manually since they’re no longer specified in the HTML spec.
TODO: use IDL from DOM core - dvcs.w3.org/hg/domcore/raw-file/tip/Overview.html
24 |
# File 'lib/watir-webdriver/elements/element.rb', line 24 attributes :string => [:id, :class_name] |
#click(*modifiers) ⇒ Object
Clicks the element, optionally while pressing the given modifier keys. Note that support for holding a modifier key is currently experimental, and may not work at all.
117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 |
# File 'lib/watir-webdriver/elements/element.rb', line 117 def click(*modifiers) assert_exists assert_enabled if modifiers.any? assert_has_input_devices_for "click(#{modifiers.join ', '})" action = driver.action modifiers.each { |mod| action.key_down mod } action.click @element modifiers.each { |mod| action.key_up mod } action.perform else @element.click end run_checkers end |
#double_click ⇒ Object
Double clicks the element. Note that browser support may vary.
145 146 147 148 149 150 151 |
# File 'lib/watir-webdriver/elements/element.rb', line 145 def double_click assert_exists assert_has_input_devices_for :double_click driver.action.double_click(@element).perform run_checkers end |
#drag_and_drop_by(right_by, down_by) ⇒ Object
Drag and drop this element by the given offsets. Note that browser support may vary.
215 216 217 218 219 220 221 222 |
# File 'lib/watir-webdriver/elements/element.rb', line 215 def drag_and_drop_by(right_by, down_by) assert_exists assert_has_input_devices_for :drag_and_drop_by driver.action. drag_and_drop_by(@element, right_by, down_by). perform end |
#drag_and_drop_on(other) ⇒ Object
Drag and drop this element on to another element instance. Note that browser support may vary.
194 195 196 197 198 199 200 201 202 |
# File 'lib/watir-webdriver/elements/element.rb', line 194 def drag_and_drop_on(other) assert_is_element other assert_exists assert_has_input_devices_for :drag_and_drop_on driver.action. drag_and_drop(@element, other.wd). perform end |
#driver ⇒ Object
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.
366 367 368 |
# File 'lib/watir-webdriver/elements/element.rb', line 366 def driver @parent.driver end |
#exists? ⇒ Boolean Also known as: exist?
Returns true if element exists.
42 43 44 45 46 47 |
# File 'lib/watir-webdriver/elements/element.rb', line 42 def exists? assert_exists true rescue UnknownObjectException, UnknownFrameException false end |
#fire_event(event_name) ⇒ Object
Simulates JavaScript events on element. Note that you may omit “on” from event name.
341 342 343 344 345 346 |
# File 'lib/watir-webdriver/elements/element.rb', line 341 def fire_event(event_name) assert_exists event_name = event_name.to_s.sub(/^on/, '').downcase execute_atom :fireEvent, @element, event_name end |
#flash ⇒ Object
Flashes (change background color far a moment) element.
231 232 233 234 235 236 237 238 239 240 241 242 243 |
# File 'lib/watir-webdriver/elements/element.rb', line 231 def flash background_color = style("backgroundColor") element_color = driver.execute_script("arguments[0].style.backgroundColor", @element) 10.times do |n| color = (n % 2 == 0) ? "red" : background_color driver.execute_script("arguments[0].style.backgroundColor = '#{color}'", @element) end driver.execute_script("arguments[0].style.backgroundColor = arguments[1]", @element, element_color) self end |
#focus ⇒ Object
Focuses element. Note that Firefox queues focus events until the window actually has focus.
313 314 315 316 |
# File 'lib/watir-webdriver/elements/element.rb', line 313 def focus assert_exists driver.execute_script "return arguments[0].focus()", @element end |
#focused? ⇒ Boolean
Returns true if this element is focused.
324 325 326 327 |
# File 'lib/watir-webdriver/elements/element.rb', line 324 def focused? assert_exists @element == driver.switch_to.active_element end |
#hash ⇒ Object
74 75 76 |
# File 'lib/watir-webdriver/elements/element.rb', line 74 def hash @element ? @element.hash : super end |
#hover ⇒ Object
Moves the mouse to the middle of this element. Note that browser support may vary.
177 178 179 180 181 182 |
# File 'lib/watir-webdriver/elements/element.rb', line 177 def hover assert_exists assert_has_input_devices_for :hover driver.action.move_to(@element).perform end |
#html ⇒ String
Returns outer (inner + element itself) HTML code of element.
287 288 289 290 |
# File 'lib/watir-webdriver/elements/element.rb', line 287 def html assert_exists execute_atom(:getOuterHtml, @element).strip end |
#id ⇒ Object
temporarily add :id and :class_name manually since they’re no longer specified in the HTML spec.
TODO: use IDL from DOM core - dvcs.w3.org/hg/domcore/raw-file/tip/Overview.html
24 |
# File 'lib/watir-webdriver/elements/element.rb', line 24 attributes :string => [:id, :class_name] |
#inspect ⇒ Object
50 51 52 53 54 55 56 |
# File 'lib/watir-webdriver/elements/element.rb', line 50 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 |
#parent ⇒ Object
Returns parent element of current element.
352 353 354 355 356 357 358 359 360 |
# File 'lib/watir-webdriver/elements/element.rb', line 352 def parent assert_exists e = execute_atom :getParentElement, @element if e.kind_of?(Selenium::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.
397 398 399 400 401 402 403 |
# File 'lib/watir-webdriver/elements/element.rb', line 397 def present? exists? && visible? rescue Selenium::WebDriver::Error::ObsoleteElementError, UnknownObjectException # if the element disappears between the exists? and visible? calls, # consider it not present. false end |
#right_click ⇒ Object
Right clicks the element. Note that browser support may vary.
161 162 163 164 165 166 167 |
# File 'lib/watir-webdriver/elements/element.rb', line 161 def right_click assert_exists assert_has_input_devices_for :right_click driver.action.context_click(@element).perform run_checkers end |
#run_checkers ⇒ Object
Runs checkers.
431 432 433 |
# File 'lib/watir-webdriver/elements/element.rb', line 431 def run_checkers @parent.run_checkers end |
#select_text(str) ⇒ Object
5 6 7 8 |
# File 'lib/watir-webdriver/extensions/select_text.rb', line 5 def select_text(str) assert_exists execute_atom :selectText, @element, str end |
#send_keys(*args) ⇒ Object
Sends sequence of keystrokes to element.
301 302 303 304 |
# File 'lib/watir-webdriver/elements/element.rb', line 301 def send_keys(*args) assert_exists @element.send_keys(*args) end |
#style(property = nil) ⇒ String
Returns given style property of this element.
418 419 420 421 422 423 424 425 |
# File 'lib/watir-webdriver/elements/element.rb', line 418 def style(property = nil) if property assert_exists @element.style property else attribute_value("style").to_s.strip end end |
#tag_name ⇒ String
Returns tag name of the element.
95 96 97 98 |
# File 'lib/watir-webdriver/elements/element.rb', line 95 def tag_name assert_exists @element.tag_name.downcase end |
#text ⇒ String
Returns the text of the element.
84 85 86 87 |
# File 'lib/watir-webdriver/elements/element.rb', line 84 def text assert_exists @element.text end |
#to_subtype ⇒ Object
Cast this Element instance to a more specific subtype.
443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 |
# File 'lib/watir-webdriver/elements/element.rb', line 443 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 |
#value ⇒ String
Returns value of the element.
251 252 253 254 255 256 257 258 259 |
# File 'lib/watir-webdriver/elements/element.rb', line 251 def value assert_exists begin @element.attribute('value') || '' rescue Selenium::WebDriver::Error::InvalidElementStateError "" end end |
#visible? ⇒ Boolean
Returns true if this element is visible on the page.
385 386 387 388 |
# File 'lib/watir-webdriver/elements/element.rb', line 385 def visible? assert_exists @element.displayed? end |
#wd ⇒ Object
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.
374 375 376 377 |
# File 'lib/watir-webdriver/elements/element.rb', line 374 def wd assert_exists @element end |