Class: Capybara::Selenium::Node
Instance Attribute Summary
Attributes inherited from Driver::Node
#driver, #native
Instance Method Summary
collapse
#initialize, #inspect, #trigger
Instance Method Details
#==(other) ⇒ Object
149
150
151
|
# File 'lib/capybara/selenium/node.rb', line 149
def ==(other)
native == other.native
end
|
#[](name) ⇒ Object
13
14
15
16
17
|
# File 'lib/capybara/selenium/node.rb', line 13
def [](name)
native.attribute(name.to_s)
rescue Selenium::WebDriver::Error::WebDriverError
nil
end
|
#all_text ⇒ Object
8
9
10
11
|
# File 'lib/capybara/selenium/node.rb', line 8
def all_text
text = driver.browser.execute_script("return arguments[0].textContent", native)
Capybara::Helpers.normalize_whitespace(text)
end
|
#click ⇒ Object
97
98
99
|
# File 'lib/capybara/selenium/node.rb', line 97
def click
native.click
end
|
#disabled? ⇒ Boolean
135
136
137
|
# File 'lib/capybara/selenium/node.rb', line 135
def disabled?
!native.enabled?
end
|
#double_click ⇒ Object
105
106
107
|
# File 'lib/capybara/selenium/node.rb', line 105
def double_click
driver.browser.action.double_click(native).perform
end
|
#drag_to(element) ⇒ Object
117
118
119
|
# File 'lib/capybara/selenium/node.rb', line 117
def drag_to(element)
driver.browser.action.drag_and_drop(native, element.native).perform
end
|
#find_css(locator) ⇒ Object
145
146
147
|
# File 'lib/capybara/selenium/node.rb', line 145
def find_css(locator)
native.find_elements(:css, locator).map { |n| self.class.new(driver, n) }
end
|
#find_xpath(locator) ⇒ Object
141
142
143
|
# File 'lib/capybara/selenium/node.rb', line 141
def find_xpath(locator)
native.find_elements(:xpath, locator).map { |n| self.class.new(driver, n) }
end
|
#hover ⇒ Object
113
114
115
|
# File 'lib/capybara/selenium/node.rb', line 113
def hover
driver.browser.action.move_to(native).perform
end
|
#path ⇒ Object
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
|
# File 'lib/capybara/selenium/node.rb', line 153
def path
path = find_xpath('ancestor::*').reverse
path.unshift self
result = []
while node = path.shift
parent = path.first
if parent
siblings = parent.find_xpath(node.tag_name)
if siblings.size == 1
result.unshift node.tag_name
else
index = siblings.index(node)
result.unshift "#{node.tag_name}[#{index+1}]"
end
else
result.unshift node.tag_name
end
end
'/' + result.join('/')
end
|
#right_click ⇒ Object
101
102
103
|
# File 'lib/capybara/selenium/node.rb', line 101
def right_click
driver.browser.action.context_click(native).perform
end
|
#select_option ⇒ Object
86
87
88
|
# File 'lib/capybara/selenium/node.rb', line 86
def select_option
native.click unless selected?
end
|
#selected? ⇒ Boolean
Also known as:
checked?
130
131
132
133
|
# File 'lib/capybara/selenium/node.rb', line 130
def selected?
selected = native.selected?
selected and selected != "false"
end
|
#send_keys(*args) ⇒ Object
109
110
111
|
# File 'lib/capybara/selenium/node.rb', line 109
def send_keys(*args)
native.send_keys(*args)
end
|
#set(value, options = {}) ⇒ Object
Set the value of the form element to the given value.
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
|
# File 'lib/capybara/selenium/node.rb', line 38
def set(value, options={})
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
if options[:clear] == :backspace
backspaces = [:backspace] * self.value.to_s.length
native.send_keys(*(backspaces + [value.to_s]))
elsif options[:clear] == :none
native.send_keys(value.to_s)
elsif options[:clear].is_a? Array
native.send_keys(*options[:clear], value.to_s)
else
driver.browser.execute_script "arguments[0].value = ''", native
native.send_keys(value.to_s)
end
end
elsif native.attribute('isContentEditable')
script = <<-JS
var range = document.createRange();
arguments[0].focus();
range.selectNodeContents(arguments[0]);
window.getSelection().addRange(range);
JS
driver.browser.execute_script script, native
native.send_keys(value.to_s)
end
end
|
#tag_name ⇒ Object
121
122
123
|
# File 'lib/capybara/selenium/node.rb', line 121
def tag_name
native.tag_name.downcase
end
|
#unselect_option ⇒ Object
90
91
92
93
94
95
|
# File 'lib/capybara/selenium/node.rb', line 90
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
|
#value ⇒ Object
19
20
21
22
23
24
25
|
# File 'lib/capybara/selenium/node.rb', line 19
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
125
126
127
128
|
# File 'lib/capybara/selenium/node.rb', line 125
def visible?
displayed = native.displayed?
displayed and displayed != "false"
end
|