Class: Watir::BaseElement

Inherits:
Object
  • Object
show all
Includes:
Selenium, Container, Exception
Defined in:
lib/watir-webdriver/base_element.rb

Direct Known Subclasses

HTMLElement

Constant Summary collapse

IGNORED_ATTRIBUTES =
[:text, :hash]

Class Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Container

add

Methods included from XpathSupport

#element_by_xpath, #elements_by_xpath

Constructor Details

#initialize(parent, *selectors) ⇒ BaseElement

class << self



159
160
161
162
163
164
165
166
# File 'lib/watir-webdriver/base_element.rb', line 159

def initialize(parent, *selectors)
  @parent   = parent
  @selector = extract_selector(selectors).merge(self.class.default_selector)

  if @selector.has_key?(:element)
    @element = @selector[:element]
  end
end

Class Attribute Details

.default_selectorObject



43
44
45
# File 'lib/watir-webdriver/base_element.rb', line 43

def default_selector
  @default_selector ||= {}
end

Class Method Details

.attribute_listObject



18
19
20
21
22
23
24
25
# File 'lib/watir-webdriver/base_element.rb', line 18

def attribute_list
  @attribute_list ||= (
    list = typed_attributes.values.flatten
    list += ancestors[1..-1].map do |e|
      e.attribute_list if e.respond_to?(:attribute_list)
    end.compact.flatten
  ).uniq
end

.attributes(attribute_map = nil) ⇒ Object



27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/watir-webdriver/base_element.rb', line 27

def attributes(attribute_map = nil)
  if attribute_map.nil?
    return attribute_list
  end

  add_attributes attribute_map

  attribute_map.each do |type, attribs|
    attribs.each do |name|
      # we don't want to override methods like :text or :hash
      next if IGNORED_ATTRIBUTES.include?(name)
      define_attribute(type, name)
    end
  end
end

.typed_attributesObject



14
15
16
# File 'lib/watir-webdriver/base_element.rb', line 14

def typed_attributes
  @typed_attributes ||= Hash.new { |hash, type| hash[type] = []  }
end

Instance Method Details

#attribute_value(attribute_name) ⇒ Object



231
232
233
234
235
# File 'lib/watir-webdriver/base_element.rb', line 231

def attribute_value(attribute_name)
  assert_exists
  # should rescue?
  @element.attribute attribute_name
end

#clickObject



194
195
196
197
198
199
# File 'lib/watir-webdriver/base_element.rb', line 194

def click
  assert_exists
  assert_enabled
  @element.click
  run_checkers
end

#double_clickObject

Raises:

  • (NotImplementedError)


201
202
203
204
205
206
207
# File 'lib/watir-webdriver/base_element.rb', line 201

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

  @element.double_click
  run_checkers
end

#driverObject



274
275
276
# File 'lib/watir-webdriver/base_element.rb', line 274

def driver
  @parent.driver
end

#elementObject Also known as: wd



278
279
280
281
# File 'lib/watir-webdriver/base_element.rb', line 278

def element
  assert_exists
  @element
end

#exists?Boolean Also known as: exist?

Returns:

  • (Boolean)


168
169
170
171
172
173
# File 'lib/watir-webdriver/base_element.rb', line 168

def exists?
  assert_exists
  true
rescue UnknownObjectException, UnknownFrameException
  false
end

#fire_event(event_name, bubble = false) ⇒ Object



258
259
260
261
262
# File 'lib/watir-webdriver/base_element.rb', line 258

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



217
218
219
220
221
222
223
224
# File 'lib/watir-webdriver/base_element.rb', line 217

def flash
  assert_exists
  original_color = driver.execute_script("arguments[0].style.backgroundColor", @element)
  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



253
254
255
256
# File 'lib/watir-webdriver/base_element.rb', line 253

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

#htmlObject



237
238
239
240
# File 'lib/watir-webdriver/base_element.rb', line 237

def html
  assert_exists
  browserbot('getOuterHTML', @element).strip
end

#inspectObject



176
177
178
179
180
181
182
# File 'lib/watir-webdriver/base_element.rb', line 176

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.inspect]
  end
end

#parentObject



264
265
266
267
268
269
270
271
272
# File 'lib/watir-webdriver/base_element.rb', line 264

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

#right_clickObject

Raises:

  • (NotImplementedError)


209
210
211
212
213
214
215
# File 'lib/watir-webdriver/base_element.rb', line 209

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

  @element.right_click
  run_checkers
end

#run_checkersObject



298
299
300
# File 'lib/watir-webdriver/base_element.rb', line 298

def run_checkers
  @parent.run_checkers
end

#send_keys(*args) ⇒ Object



242
243
244
245
# File 'lib/watir-webdriver/base_element.rb', line 242

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

#style(property = nil) ⇒ Object



289
290
291
292
293
294
295
296
# File 'lib/watir-webdriver/base_element.rb', line 289

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

#tag_nameObject



189
190
191
192
# File 'lib/watir-webdriver/base_element.rb', line 189

def tag_name
  assert_exists
  @element.tag_name
end

#textObject



184
185
186
187
# File 'lib/watir-webdriver/base_element.rb', line 184

def text
  assert_exists
  @element.text
end

#valueObject



226
227
228
229
# File 'lib/watir-webdriver/base_element.rb', line 226

def value
  assert_exists
  rescue_no_match { @element.value || "" }
end

#visible?Boolean

Returns:

  • (Boolean)


284
285
286
287
# File 'lib/watir-webdriver/base_element.rb', line 284

def visible?
  assert_exists
  @element.displayed?
end