Class: OperaWatir::Element

Inherits:
Object show all
Extended by:
Forwardable
Includes:
Deprecated
Defined in:
lib/operawatir/element.rb,
lib/operawatir/compat/element.rb

Instance Method Summary collapse

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.

Parameters:

  • name (String, Symbol)

    The name of the attribute to get.

Returns:

  • (String)

    The value of the attribute.



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.

Returns:

  • (Boolean)

    True if the attribute exists on the element, false otherwise.



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_nameObject



54
55
56
# File 'lib/operawatir/element.rb', line 54

def class_name
  attr(:class)
end

#clearObject

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.

Parameters:

  • x (optional, Fixnum) (defaults to: 1)

    The offset from the left of the element

  • y (optional, Fixnum) (defaults to: 1)

    The offset from the top of the element



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_asyncObject Also known as: click_no_wait



132
133
134
# File 'lib/operawatir/element.rb', line 132

def click_async
  node.click 1
end

#colspanFixnum

Gets the colspan attribute as an integer.

Returns:

  • (Fixnum)

    The colspan.



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

Returns:

  • (Boolean)


105
106
107
# File 'lib/operawatir/element.rb', line 105

def disabled?
  !enabled?
end

#double_clickObject



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

Returns:

  • (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?

Returns:

  • (Boolean)

    True if element exists, false otherwise.



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

#focusObject

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

Returns:

  • (Boolean)


34
35
36
# File 'lib/operawatir/element.rb', line 34

def has_attribute?(name)
  !attr(name).nil?
end

#hashObject

TODO Need support for this in Webdriver



9
10
11
# File 'lib/operawatir/element.rb', line 9

def hash
  node.hashCode
end

#idObject



46
47
48
# File 'lib/operawatir/element.rb', line 46

def id
  attr(:id)
end

#locationObject



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_clickObject



152
153
154
# File 'lib/operawatir/element.rb', line 152

def middle_click
  node.middleClick
end

#quadruple_clickObject



144
145
146
# File 'lib/operawatir/element.rb', line 144

def quadruple_click
  node.click 4
end

#right_clickObject



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.

Parameters:

  • value (String) (defaults to: nil)

    (Optional.) Text the selected option should contain.

Returns:

  • (Boolean)

    True if the element is selected/selected option contains value, false otherwise.



161
162
163
164
165
166
167
# File 'lib/operawatir/compat/element.rb', line 161

def selected?(value=nil)
  if option.nil?
    selected_options.text.include?(value)
  else
    node.isSelected
  end
end

#selected_optionsCollection

Gets the selected ‘<option>` elements in a `<select>` element.

Returns:

  • (Collection)

    a collection of the selected ‘<option>`s



145
146
147
# File 'lib/operawatir/compat/element.rb', line 145

def selected_options
  options(: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

#submitObject

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

#textString Also known as: caption

Gets the text content of the element.

Returns:

  • (String)

    The text content.



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_clickObject



140
141
142
# File 'lib/operawatir/element.rb', line 140

def triple_click
  node.click 3
end

#typeObject

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

#urlString

Gets the href of an ‘<a>` element, or the url attribute of any other element.

Returns:

  • (String)

    An href or the @url attribute.



134
135
136
# File 'lib/operawatir/compat/element.rb', line 134

def url
  attr(tag_name == 'A' ? :href : :url)
end

#valueString

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`.

Returns:

  • (String)

    Value of the element.



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')

Parameters:

  • String (String)

    to search for.

  • True (Boolean)

    if the element’s text contains str, false otherwise.



60
61
62
# File 'lib/operawatir/compat/element.rb', line 60

def verify_contains(str)
  text.include?(str)
end

#visible?Boolean

Returns:

  • (Boolean)


215
216
217
# File 'lib/operawatir/element.rb', line 215

def visible?
  node.isVisible
end