Class: Capybara::Selenium::Node

Inherits:
Driver::Node show all
Defined in:
lib/capybara/selenium/node.rb

Instance Attribute Summary

Attributes inherited from Driver::Node

#driver, #native

Instance Method Summary collapse

Methods inherited from Driver::Node

#initialize, #inspect, #path, #trigger

Constructor Details

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

Instance Method Details

#==(other) ⇒ Object



120
121
122
# File 'lib/capybara/selenium/node.rb', line 120

def ==(other)
  native == other.native
end

#[](name) ⇒ Object



12
13
14
15
16
# File 'lib/capybara/selenium/node.rb', line 12

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

#all_textObject



7
8
9
10
# File 'lib/capybara/selenium/node.rb', line 7

def all_text
  text = driver.browser.execute_script("return arguments[0].textContent", native)
  Capybara::Helpers.normalize_whitespace(text)
end

#clickObject



72
73
74
# File 'lib/capybara/selenium/node.rb', line 72

def click
  native.click
end

#disabled?Boolean

Returns:

  • (Boolean)


106
107
108
# File 'lib/capybara/selenium/node.rb', line 106

def disabled?
  !native.enabled?
end

#double_clickObject



80
81
82
# File 'lib/capybara/selenium/node.rb', line 80

def double_click
  driver.browser.action.double_click(native).perform
end

#drag_to(element) ⇒ Object



88
89
90
# File 'lib/capybara/selenium/node.rb', line 88

def drag_to(element)
  driver.browser.action.drag_and_drop(native, element.native).perform
end

#find_css(locator) ⇒ Object



116
117
118
# File 'lib/capybara/selenium/node.rb', line 116

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

#find_xpath(locator) ⇒ Object



112
113
114
# File 'lib/capybara/selenium/node.rb', line 112

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

#hoverObject



84
85
86
# File 'lib/capybara/selenium/node.rb', line 84

def hover
  driver.browser.action.move_to(native).perform
end

#right_clickObject



76
77
78
# File 'lib/capybara/selenium/node.rb', line 76

def right_click
  driver.browser.action.context_click(native).perform
end

#select_optionObject



61
62
63
# File 'lib/capybara/selenium/node.rb', line 61

def select_option
  native.click unless selected?
end

#selected?Boolean Also known as: checked?

Returns:

  • (Boolean)


101
102
103
104
# File 'lib/capybara/selenium/node.rb', line 101

def selected?
  selected = native.selected?
  selected and selected != "false"
end

#set(value) ⇒ Object



26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/capybara/selenium/node.rb', line 26

def set(value)
  tag_name = self.tag_name
  type = self[:type]
  if (Array === value) && !self[:multiple]
    raise ArgumentError.new "Value cannot be an Array when 'multiple' attribute is not present. Not a #{value.class}"
  end
  if tag_name == 'input' and type == 'radio'
    click
  elsif tag_name == 'input' and type == 'checkbox'
    click if value ^ native.attribute('checked').to_s.eql?("true")
  elsif tag_name == 'input' and type == 'file'
    path_names = value.to_s.empty? ? [] : value
    native.send_keys(*path_names)
  elsif tag_name == 'textarea' or tag_name == 'input'
    if self[:readonly]
      warn "Attempt to set readonly element with value: #{value} \n *This will raise an exception in a future version of Capybara"
    elsif value.to_s.empty?
      native.clear
    else
      #script can change a readonly element which user input cannot, so dont execute if readonly
      driver.browser.execute_script "arguments[0].value = ''", native
      native.send_keys(value.to_s)
    end
  elsif native.attribute('isContentEditable')
    #ensure we are focused on the element
    script = <<-JS
      var range = document.createRange();
      range.selectNodeContents(arguments[0]);
      window.getSelection().addRange(range);
    JS
    driver.browser.execute_script script, native
    native.send_keys(value.to_s)
  end
end

#tag_nameObject



92
93
94
# File 'lib/capybara/selenium/node.rb', line 92

def tag_name
  native.tag_name.downcase
end

#unselect_optionObject



65
66
67
68
69
70
# File 'lib/capybara/selenium/node.rb', line 65

def unselect_option
  if select_node['multiple'] != 'multiple' and select_node['multiple'] != 'true'
    raise Capybara::UnselectNotAllowed, "Cannot unselect option from single select box."
  end
  native.click if selected?
end

#valueObject



18
19
20
21
22
23
24
# File 'lib/capybara/selenium/node.rb', line 18

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

#visible?Boolean

Returns:

  • (Boolean)


96
97
98
99
# File 'lib/capybara/selenium/node.rb', line 96

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

#visible_textObject



2
3
4
5
# File 'lib/capybara/selenium/node.rb', line 2

def visible_text
  # Selenium doesn't normalize Unicode whitespace.
  Capybara::Helpers.normalize_whitespace(native.text)
end