Class: OperaWatir::Element
- Extended by:
- Forwardable
- Includes:
- Deprecated
- Defined in:
- lib/operawatir/element.rb,
lib/operawatir/compat/element.rb
Instance Method Summary collapse
- #==(other) ⇒ Object
-
#attr(name) ⇒ String
Gets the attribute called name.
-
#attr?(name) ⇒ Boolean
Check the existence of the attribute on the element.
- #check! ⇒ Object
- #class_name ⇒ Object
-
#clear ⇒ Object
Clears a text input or textarea of any text.
-
#click(x = 1, y = 1) ⇒ Object
Clicks on the top left of the element, or the given x, y offset.
- #click_async ⇒ Object (also: #click_no_wait)
-
#colspan ⇒ Fixnum
Gets the colspan attribute as an integer.
-
#compare_hash(other) ⇒ Object
Opera-specific.
- #disabled? ⇒ Boolean
- #double_click ⇒ Object
- #drag_and_drop_by(x, y) ⇒ Object
- #drag_and_drop_on(other) ⇒ Object
- #eql?(other) ⇒ Boolean
-
#exist? ⇒ Boolean
(also: #exists?)
Does the element exist in the DOM?.
-
#fire_event(event, x = 0, y = 0) ⇒ Object
FIXME: This should be migrated to using browserbot.js, as watir-webdriver is using.
-
#focus ⇒ Object
Focuses the element.
-
#has_attribute?(name) ⇒ Boolean
TODO Move to Webdriver Relies on getAttribute returning nil.
-
#hash ⇒ Object
TODO Need support for this in Webdriver.
- #id ⇒ Object
-
#initialize(node) ⇒ Element
constructor
A new instance of Element.
- #location ⇒ Object
- #method_missing(name, *args, &blk) ⇒ Object
- #middle_click ⇒ Object
- #quadruple_click ⇒ Object
- #right_click ⇒ Object
-
#screenshot(file_name, time_out) ⇒ Object
UI.
-
#selected?(value = nil) ⇒ Boolean
On checkboxes, radio buttons, and option elements returns whether the element is checked/selected.
-
#selected_options ⇒ Collection
Gets the selected ‘<option>` elements in a `<select>` element.
- #send_keys(*list) ⇒ Object
-
#submit ⇒ Object
Submits a form, or the form the elment is contained in.
-
#text ⇒ String
(also: #caption)
Gets the text content of the element.
- #text=(input) ⇒ Object (also: #set, #value=)
- #triple_click ⇒ Object
-
#type ⇒ Object
For ‘<select>` elements returns either ’select-one’ for ‘<select>`s where only a single `<option>` can be selected, or ’select-multiple’ otherwise.
- #uncheck! ⇒ Object
-
#url ⇒ String
Gets the href of an ‘<a>` element, or the url attribute of any other element.
-
#value ⇒ String
On elements of type ‘input`, `textarea` or `select` it will fetch the texteditable `value` attribute, on every other element type it returns the DOM attribute `value`.
-
#verify_contains(str) ⇒ Object
(also: #verify_contains?)
Checks whether the text content of the element contains the given string In the compatibility layer as the preferred way of doing this is.
- #visible? ⇒ Boolean
Constructor Details
#initialize(node) ⇒ Element
Returns a new instance of Element.
4 5 6 |
# File 'lib/operawatir/element.rb', line 4 def initialize(node) self.node = node end |
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(name, *args, &blk) ⇒ Object
38 39 40 41 42 43 44 |
# File 'lib/operawatir/element.rb', line 38 def method_missing(method, *args, &blk) if !(block_given? || !args.empty?) && has_attribute?(method) attr(method) else super end end |
Instance Method Details
#==(other) ⇒ Object
13 14 15 |
# File 'lib/operawatir/element.rb', line 13 def ==(other) node.equals other.node end |
#attr(name) ⇒ String
Gets the attribute called name.
28 29 30 |
# File 'lib/operawatir/element.rb', line 28 def attr(name) node.getAttribute(name.to_s) end |
#attr?(name) ⇒ Boolean
Check the existence of the attribute on the element.
22 23 24 |
# File 'lib/operawatir/compat/element.rb', line 22 def attr?(name) !node.getAttribute(name.to_s).nil? end |
#check! ⇒ Object
109 110 111 |
# File 'lib/operawatir/element.rb', line 109 def check! node.toggle if not selected? end |
#class_name ⇒ Object
54 55 56 |
# File 'lib/operawatir/element.rb', line 54 def class_name attr(:class) end |
#clear ⇒ Object
Clears a text input or textarea of any text.
105 106 107 108 109 |
# File 'lib/operawatir/compat/element.rb', line 105 def clear assert_enabled! click node.clear end |
#click(x = 1, y = 1) ⇒ Object
Clicks on the top left of the element, or the given x, y offset. Asserts whether element is enabled first.
128 129 130 |
# File 'lib/operawatir/element.rb', line 128 def click(x=1, y=1) node.click(x.to_i, y.to_i) end |
#click_async ⇒ Object Also known as: click_no_wait
132 133 134 |
# File 'lib/operawatir/element.rb', line 132 def click_async node.click 1 end |
#colspan ⇒ Fixnum
Gets the colspan attribute as an integer.
192 193 194 |
# File 'lib/operawatir/compat/element.rb', line 192 def colspan attr(:colspan).to_i end |
#compare_hash(other) ⇒ Object
Opera-specific
198 199 200 |
# File 'lib/operawatir/compat/element.rb', line 198 def compare_hash(other) visual_hash == other.visual_hash end |
#disabled? ⇒ Boolean
105 106 107 |
# File 'lib/operawatir/element.rb', line 105 def disabled? !enabled? end |
#double_click ⇒ Object
136 137 138 |
# File 'lib/operawatir/element.rb', line 136 def double_click node.click 2 end |
#drag_and_drop_by(x, y) ⇒ Object
160 161 162 |
# File 'lib/operawatir/element.rb', line 160 def drag_and_drop_by(x, y) node.dragAndDropBy(x.to_i, y.to_i) end |
#drag_and_drop_on(other) ⇒ Object
156 157 158 |
# File 'lib/operawatir/element.rb', line 156 def drag_and_drop_on(other) node.dragAndDropOn other.node end |
#eql?(other) ⇒ Boolean
17 18 19 |
# File 'lib/operawatir/element.rb', line 17 def eql?(other) is_a?(other.class) && self == other end |
#exist? ⇒ Boolean Also known as: exists?
Does the element exist in the DOM?
99 100 101 |
# File 'lib/operawatir/element.rb', line 99 def exist? !!!tag_name.empty? end |
#fire_event(event, x = 0, y = 0) ⇒ Object
FIXME: This should be migrated to using browserbot.js, as watir-webdriver is using.
185 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 211 212 213 |
# File 'lib/operawatir/element.rb', line 185 def fire_event(event, x = 0, y = 0) loc = location x += loc[:x] y += loc[:y] # In the case that event is given as symbol, we convert it to a # string. event = event.to_s event =~ /on(.*)/i event = $1 if $1 event = event.downcase.to_sym case event when :abort, :blur, :change, :error, :focus, :load, :reset, :resize, :scroll, :submit, :unload type = 'HTMLEvents'; init = "initEvent(\"#{event.to_s}\", true, true)" when :click, :dblclick, :mousedown, :mousemove, :mouseout, :mouseover, :mouseup type = 'MouseEvents' init = "initMouseEvent(\"#{event.to_s}\", true, true, window, 1, 0, 0, #{x}, #{y}, false, false, false, false, 0, null)" else raise Exceptions::NotImplementedException, "Event on#{event} is not a valid ECMAscript event for OperaWatir." end script = "var event = document.createEvent(\"#{type}\"); " + "event.#{init}; " + "locator.dispatchEvent(event);" node.callMethod(script) end |
#focus ⇒ Object
Focuses the element
88 89 90 |
# File 'lib/operawatir/compat/element.rb', line 88 def focus fire_event :focus end |
#has_attribute?(name) ⇒ Boolean
TODO Move to Webdriver Relies on getAttribute returning nil
34 35 36 |
# File 'lib/operawatir/element.rb', line 34 def has_attribute?(name) !attr(name).nil? end |
#hash ⇒ Object
TODO Need support for this in Webdriver
9 10 11 |
# File 'lib/operawatir/element.rb', line 9 def hash node.hashCode end |
#id ⇒ Object
46 47 48 |
# File 'lib/operawatir/element.rb', line 46 def id attr(:id) end |
#location ⇒ Object
227 228 229 230 |
# File 'lib/operawatir/element.rb', line 227 def location loc = node.getLocation {:x => loc.x.to_i, :y => loc.y.to_i} end |
#middle_click ⇒ Object
152 153 154 |
# File 'lib/operawatir/element.rb', line 152 def middle_click node.middleClick end |
#quadruple_click ⇒ Object
144 145 146 |
# File 'lib/operawatir/element.rb', line 144 def quadruple_click node.click 4 end |
#right_click ⇒ Object
148 149 150 |
# File 'lib/operawatir/element.rb', line 148 def right_click node.rightClick end |
#screenshot(file_name, time_out) ⇒ Object
UI
221 222 223 |
# File 'lib/operawatir/element.rb', line 221 def screenshot(file_name, time_out) node.saveScreenshot(file_name, time_out) end |
#selected?(value = nil) ⇒ Boolean
On checkboxes, radio buttons, and option elements returns whether the element is checked/selected. On a select element, when passed an value it checks whether the selected option contains the given text.
161 162 163 164 165 166 167 |
# File 'lib/operawatir/compat/element.rb', line 161 def selected?(value=nil) if option.nil? .text.include?(value) else node.isSelected end end |
#selected_options ⇒ Collection
Gets the selected ‘<option>` elements in a `<select>` element.
145 146 147 |
# File 'lib/operawatir/compat/element.rb', line 145 def (:selected?, true) end |
#send_keys(*list) ⇒ Object
179 180 181 |
# File 'lib/operawatir/element.rb', line 179 def send_keys(*list) raise Exceptions::NotImplementedException end |
#submit ⇒ Object
Submits a form, or the form the elment is contained in.
96 97 98 99 |
# File 'lib/operawatir/compat/element.rb', line 96 def submit assert_exists node.submit end |
#text ⇒ String Also known as: caption
Gets the text content of the element.
40 41 42 43 44 45 46 |
# File 'lib/operawatir/compat/element.rb', line 40 def text if node.tag_name =~ /input|textarea|select/i node.value.strip else node.getText.strip end end |
#text=(input) ⇒ Object Also known as: set, value=
166 167 168 169 170 171 172 173 174 |
# File 'lib/operawatir/element.rb', line 166 def text=(input) if attr(:type) =~ /checkbox/i toggle_check! unless checked? else # Focus before typing clear unless value.empty? node.sendKeys(input.to_s.split('').to_java(:string)) end end |
#triple_click ⇒ Object
140 141 142 |
# File 'lib/operawatir/element.rb', line 140 def triple_click node.click 3 end |
#type ⇒ Object
For ‘<select>` elements returns either ’select-one’ for ‘<select>`s where only a single `<option>` can be selected, or ’select-multiple’ otherwise. For non-‘<select>` elements returns the `type` attribute.
178 179 180 |
# File 'lib/operawatir/compat/element.rb', line 178 def type attr(:type) end |
#uncheck! ⇒ Object
113 114 115 |
# File 'lib/operawatir/element.rb', line 113 def uncheck! node.toggle if selected? end |
#url ⇒ String
Gets the href of an ‘<a>` element, or the url attribute of any other element.
134 135 136 |
# File 'lib/operawatir/compat/element.rb', line 134 def url attr(tag_name == 'A' ? :href : :url) end |
#value ⇒ String
On elements of type ‘input`, `textarea` or `select` it will fetch the texteditable `value` attribute, on every other element type it returns the DOM attribute `value`.
79 80 81 82 83 84 85 86 87 88 89 90 91 |
# File 'lib/operawatir/element.rb', line 79 def value =begin if tag_name =~ /input|textarea|select/i node.getValue # elsif tag_name =~ /button/i text # getVisibleContents else attr :value # attribute @value end =end node.getValue end |
#verify_contains(str) ⇒ Object Also known as: verify_contains?
Checks whether the text content of the element contains the given string In the compatibility layer as the preferred way of doing this is.
elm.text.should include('My string')
60 61 62 |
# File 'lib/operawatir/compat/element.rb', line 60 def verify_contains(str) text.include?(str) end |
#visible? ⇒ Boolean
215 216 217 |
# File 'lib/operawatir/element.rb', line 215 def visible? node.isVisible end |