Class: Capybara::Driver::Selenium::Node

Inherits:
Node
  • Object
show all
Defined in:
lib/capybara/driver/selenium_driver.rb

Instance Attribute Summary

Attributes inherited from Node

#driver, #native

Instance Method Summary collapse

Methods inherited from Node

#initialize, #inspect, #path, #trigger

Constructor Details

This class inherits a constructor from Capybara::Driver::Node

Instance Method Details

#[](name) ⇒ Object



9
10
11
12
13
14
15
16
17
# File 'lib/capybara/driver/selenium_driver.rb', line 9

def [](name)
  if name == :value
    native.value
  else
    native.attribute(name.to_s)
  end
rescue Selenium::WebDriver::Error::WebDriverError
  nil
end

#clickObject



60
61
62
# File 'lib/capybara/driver/selenium_driver.rb', line 60

def click
  native.click
end

#drag_to(element) ⇒ Object



64
65
66
# File 'lib/capybara/driver/selenium_driver.rb', line 64

def drag_to(element)
  native.drag_and_drop_on(element.native)
end

#find(locator) ⇒ Object



76
77
78
# File 'lib/capybara/driver/selenium_driver.rb', line 76

def find(locator)
  native.find_elements(:xpath, locator).map { |n| self.class.new(driver, n) }
end

#select_option(option) ⇒ Object



38
39
40
41
42
43
44
# File 'lib/capybara/driver/selenium_driver.rb', line 38

def select_option(option)
  option_node = native.find_element(:xpath, ".//option[normalize-space(text())=#{Capybara::XPath.escape(option)}]") || native.find_element(:xpath, ".//option[contains(.,#{Capybara::XPath.escape(option)})]")
  option_node.select
rescue 
  options = native.find_elements(:xpath, ".//option").map { |o| "'#{o.text}'" }.join(', ')
  raise Capybara::OptionNotFound, "No such option '#{option}' in this select box. Available options: #{options}"
end

#set(value) ⇒ Object



27
28
29
30
31
32
33
34
35
36
# File 'lib/capybara/driver/selenium_driver.rb', line 27

def set(value)
  if tag_name == 'input' and type == 'radio'
    native.click
  elsif tag_name == 'input' and type == 'checkbox'
    native.click if native.attribute('checked') != value
  elsif tag_name == 'textarea' or tag_name == 'input'
    native.clear
    native.send_keys(value.to_s)
  end
end

#tag_nameObject



68
69
70
# File 'lib/capybara/driver/selenium_driver.rb', line 68

def tag_name
  native.tag_name
end

#textObject



5
6
7
# File 'lib/capybara/driver/selenium_driver.rb', line 5

def text
  native.text
end

#unselect_option(option) ⇒ Object



46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/capybara/driver/selenium_driver.rb', line 46

def unselect_option(option)
  if native['multiple'] != 'multiple'
    raise Capybara::UnselectNotAllowed, "Cannot unselect option '#{option}' from single select box."
  end

  begin
    option_node = native.find_element(:xpath, ".//option[normalize-space(text())=#{Capybara::XPath.escape(option)}]") || native.find_element(:xpath, ".//option[contains(.,#{Capybara::XPath.escape(option)})]")
    option_node.clear
  rescue
    options = native.find_elements(:xpath, ".//option").map { |o| "'#{o.text}'" }.join(', ')
    raise Capybara::OptionNotFound, "No such option '#{option}' in this select box. Available options: #{options}"
  end
end

#valueObject



19
20
21
22
23
24
25
# File 'lib/capybara/driver/selenium_driver.rb', line 19

def value
  if tag_name == "select" and self[:multiple]
    native.find_elements(:xpath, ".//option").select { |n| n.selected? }.map { |n| n.text }
  else
    self[:value]
  end
end

#visible?Boolean

Returns:

  • (Boolean)


72
73
74
# File 'lib/capybara/driver/selenium_driver.rb', line 72

def visible?
  native.displayed? and native.displayed? != "false"
end