capybara 3.8.1 → 3.33.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (242) hide show
  1. checksums.yaml +4 -4
  2. data/.yardopts +1 -0
  3. data/History.md +465 -0
  4. data/License.txt +1 -1
  5. data/README.md +58 -57
  6. data/lib/capybara/config.rb +10 -4
  7. data/lib/capybara/cucumber.rb +1 -1
  8. data/lib/capybara/driver/base.rb +2 -2
  9. data/lib/capybara/driver/node.rb +26 -5
  10. data/lib/capybara/dsl.rb +12 -4
  11. data/lib/capybara/helpers.rb +8 -4
  12. data/lib/capybara/minitest/spec.rb +162 -85
  13. data/lib/capybara/minitest.rb +248 -148
  14. data/lib/capybara/node/actions.rb +149 -96
  15. data/lib/capybara/node/base.rb +27 -10
  16. data/lib/capybara/node/document.rb +12 -0
  17. data/lib/capybara/node/document_matchers.rb +9 -5
  18. data/lib/capybara/node/element.rb +254 -109
  19. data/lib/capybara/node/finders.rb +83 -76
  20. data/lib/capybara/node/matchers.rb +279 -141
  21. data/lib/capybara/node/simple.rb +25 -6
  22. data/lib/capybara/queries/ancestor_query.rb +5 -7
  23. data/lib/capybara/queries/base_query.rb +11 -5
  24. data/lib/capybara/queries/current_path_query.rb +3 -3
  25. data/lib/capybara/queries/match_query.rb +1 -0
  26. data/lib/capybara/queries/selector_query.rb +467 -103
  27. data/lib/capybara/queries/sibling_query.rb +5 -4
  28. data/lib/capybara/queries/style_query.rb +6 -2
  29. data/lib/capybara/queries/text_query.rb +17 -3
  30. data/lib/capybara/queries/title_query.rb +2 -2
  31. data/lib/capybara/rack_test/browser.rb +22 -15
  32. data/lib/capybara/rack_test/driver.rb +10 -1
  33. data/lib/capybara/rack_test/errors.rb +6 -0
  34. data/lib/capybara/rack_test/form.rb +33 -28
  35. data/lib/capybara/rack_test/node.rb +74 -6
  36. data/lib/capybara/registration_container.rb +44 -0
  37. data/lib/capybara/registrations/drivers.rb +36 -0
  38. data/lib/capybara/registrations/patches/puma_ssl.rb +27 -0
  39. data/lib/capybara/registrations/servers.rb +44 -0
  40. data/lib/capybara/result.rb +55 -23
  41. data/lib/capybara/rspec/features.rb +4 -4
  42. data/lib/capybara/rspec/matcher_proxies.rb +36 -15
  43. data/lib/capybara/rspec/matchers/base.rb +111 -0
  44. data/lib/capybara/rspec/matchers/become_closed.rb +33 -0
  45. data/lib/capybara/rspec/matchers/compound.rb +88 -0
  46. data/lib/capybara/rspec/matchers/count_sugar.rb +37 -0
  47. data/lib/capybara/rspec/matchers/have_ancestor.rb +28 -0
  48. data/lib/capybara/rspec/matchers/have_current_path.rb +29 -0
  49. data/lib/capybara/rspec/matchers/have_selector.rb +77 -0
  50. data/lib/capybara/rspec/matchers/have_sibling.rb +27 -0
  51. data/lib/capybara/rspec/matchers/have_text.rb +33 -0
  52. data/lib/capybara/rspec/matchers/have_title.rb +29 -0
  53. data/lib/capybara/rspec/matchers/match_selector.rb +27 -0
  54. data/lib/capybara/rspec/matchers/match_style.rb +38 -0
  55. data/lib/capybara/rspec/matchers/spatial_sugar.rb +39 -0
  56. data/lib/capybara/rspec/matchers.rb +117 -311
  57. data/lib/capybara/selector/builders/css_builder.rb +84 -0
  58. data/lib/capybara/selector/builders/xpath_builder.rb +69 -0
  59. data/lib/capybara/selector/css.rb +17 -15
  60. data/lib/capybara/selector/definition/button.rb +52 -0
  61. data/lib/capybara/selector/definition/checkbox.rb +26 -0
  62. data/lib/capybara/selector/definition/css.rb +10 -0
  63. data/lib/capybara/selector/definition/datalist_input.rb +35 -0
  64. data/lib/capybara/selector/definition/datalist_option.rb +25 -0
  65. data/lib/capybara/selector/definition/element.rb +27 -0
  66. data/lib/capybara/selector/definition/field.rb +40 -0
  67. data/lib/capybara/selector/definition/fieldset.rb +14 -0
  68. data/lib/capybara/selector/definition/file_field.rb +13 -0
  69. data/lib/capybara/selector/definition/fillable_field.rb +33 -0
  70. data/lib/capybara/selector/definition/frame.rb +17 -0
  71. data/lib/capybara/selector/definition/id.rb +6 -0
  72. data/lib/capybara/selector/definition/label.rb +62 -0
  73. data/lib/capybara/selector/definition/link.rb +54 -0
  74. data/lib/capybara/selector/definition/link_or_button.rb +16 -0
  75. data/lib/capybara/selector/definition/option.rb +27 -0
  76. data/lib/capybara/selector/definition/radio_button.rb +27 -0
  77. data/lib/capybara/selector/definition/select.rb +81 -0
  78. data/lib/capybara/selector/definition/table.rb +109 -0
  79. data/lib/capybara/selector/definition/table_row.rb +21 -0
  80. data/lib/capybara/selector/definition/xpath.rb +5 -0
  81. data/lib/capybara/selector/definition.rb +277 -0
  82. data/lib/capybara/selector/filter.rb +1 -0
  83. data/lib/capybara/selector/filter_set.rb +26 -19
  84. data/lib/capybara/selector/filters/base.rb +24 -5
  85. data/lib/capybara/selector/filters/expression_filter.rb +3 -3
  86. data/lib/capybara/selector/filters/locator_filter.rb +29 -0
  87. data/lib/capybara/selector/filters/node_filter.rb +16 -2
  88. data/lib/capybara/selector/regexp_disassembler.rb +214 -0
  89. data/lib/capybara/selector/selector.rb +73 -367
  90. data/lib/capybara/selector/xpath_extensions.rb +17 -0
  91. data/lib/capybara/selector.rb +221 -480
  92. data/lib/capybara/selenium/atoms/getAttribute.min.js +1 -0
  93. data/lib/capybara/selenium/atoms/isDisplayed.min.js +1 -0
  94. data/lib/capybara/selenium/atoms/src/getAttribute.js +161 -0
  95. data/lib/capybara/selenium/atoms/src/isDisplayed.js +454 -0
  96. data/lib/capybara/selenium/driver.rb +203 -86
  97. data/lib/capybara/selenium/driver_specializations/chrome_driver.rb +88 -14
  98. data/lib/capybara/selenium/driver_specializations/edge_driver.rb +124 -0
  99. data/lib/capybara/selenium/driver_specializations/firefox_driver.rb +89 -0
  100. data/lib/capybara/selenium/driver_specializations/internet_explorer_driver.rb +26 -0
  101. data/lib/capybara/selenium/driver_specializations/safari_driver.rb +24 -0
  102. data/lib/capybara/selenium/extensions/file_input_click_emulation.rb +34 -0
  103. data/lib/capybara/selenium/extensions/find.rb +110 -0
  104. data/lib/capybara/selenium/extensions/html5_drag.rb +191 -22
  105. data/lib/capybara/selenium/extensions/modifier_keys_stack.rb +28 -0
  106. data/lib/capybara/selenium/extensions/scroll.rb +78 -0
  107. data/lib/capybara/selenium/logger_suppressor.rb +34 -0
  108. data/lib/capybara/selenium/node.rb +298 -93
  109. data/lib/capybara/selenium/nodes/chrome_node.rb +100 -8
  110. data/lib/capybara/selenium/nodes/edge_node.rb +104 -0
  111. data/lib/capybara/selenium/nodes/firefox_node.rb +131 -0
  112. data/lib/capybara/selenium/nodes/ie_node.rb +22 -0
  113. data/lib/capybara/selenium/nodes/safari_node.rb +118 -0
  114. data/lib/capybara/selenium/patches/action_pauser.rb +26 -0
  115. data/lib/capybara/selenium/patches/atoms.rb +18 -0
  116. data/lib/capybara/selenium/patches/is_displayed.rb +16 -0
  117. data/lib/capybara/selenium/patches/logs.rb +45 -0
  118. data/lib/capybara/selenium/patches/pause_duration_fix.rb +1 -3
  119. data/lib/capybara/selenium/patches/persistent_client.rb +20 -0
  120. data/lib/capybara/server/animation_disabler.rb +4 -3
  121. data/lib/capybara/server/checker.rb +6 -2
  122. data/lib/capybara/server/middleware.rb +23 -13
  123. data/lib/capybara/server.rb +30 -7
  124. data/lib/capybara/session/config.rb +14 -10
  125. data/lib/capybara/session/matchers.rb +11 -7
  126. data/lib/capybara/session.rb +152 -111
  127. data/lib/capybara/spec/public/offset.js +6 -0
  128. data/lib/capybara/spec/public/test.js +101 -10
  129. data/lib/capybara/spec/session/all_spec.rb +96 -6
  130. data/lib/capybara/spec/session/ancestor_spec.rb +5 -0
  131. data/lib/capybara/spec/session/assert_all_of_selectors_spec.rb +29 -0
  132. data/lib/capybara/spec/session/assert_current_path_spec.rb +5 -2
  133. data/lib/capybara/spec/session/assert_selector_spec.rb +0 -10
  134. data/lib/capybara/spec/session/assert_style_spec.rb +4 -4
  135. data/lib/capybara/spec/session/assert_text_spec.rb +9 -5
  136. data/lib/capybara/spec/session/attach_file_spec.rb +63 -36
  137. data/lib/capybara/spec/session/check_spec.rb +10 -4
  138. data/lib/capybara/spec/session/choose_spec.rb +8 -2
  139. data/lib/capybara/spec/session/click_button_spec.rb +117 -61
  140. data/lib/capybara/spec/session/click_link_or_button_spec.rb +16 -0
  141. data/lib/capybara/spec/session/click_link_spec.rb +17 -6
  142. data/lib/capybara/spec/session/element/matches_selector_spec.rb +40 -39
  143. data/lib/capybara/spec/session/evaluate_script_spec.rb +13 -0
  144. data/lib/capybara/spec/session/execute_script_spec.rb +1 -0
  145. data/lib/capybara/spec/session/fill_in_spec.rb +47 -6
  146. data/lib/capybara/spec/session/find_field_spec.rb +1 -1
  147. data/lib/capybara/spec/session/find_spec.rb +74 -4
  148. data/lib/capybara/spec/session/first_spec.rb +1 -1
  149. data/lib/capybara/spec/session/frame/switch_to_frame_spec.rb +13 -1
  150. data/lib/capybara/spec/session/frame/within_frame_spec.rb +12 -1
  151. data/lib/capybara/spec/session/has_all_selectors_spec.rb +1 -1
  152. data/lib/capybara/spec/session/has_ancestor_spec.rb +46 -0
  153. data/lib/capybara/spec/session/has_any_selectors_spec.rb +25 -0
  154. data/lib/capybara/spec/session/has_button_spec.rb +16 -0
  155. data/lib/capybara/spec/session/has_css_spec.rb +122 -12
  156. data/lib/capybara/spec/session/has_current_path_spec.rb +6 -4
  157. data/lib/capybara/spec/session/has_field_spec.rb +55 -0
  158. data/lib/capybara/spec/session/has_select_spec.rb +34 -6
  159. data/lib/capybara/spec/session/has_selector_spec.rb +11 -4
  160. data/lib/capybara/spec/session/has_sibling_spec.rb +50 -0
  161. data/lib/capybara/spec/session/has_table_spec.rb +166 -0
  162. data/lib/capybara/spec/session/has_text_spec.rb +48 -1
  163. data/lib/capybara/spec/session/has_xpath_spec.rb +17 -0
  164. data/lib/capybara/spec/session/html_spec.rb +7 -0
  165. data/lib/capybara/spec/session/matches_style_spec.rb +35 -0
  166. data/lib/capybara/spec/session/node_spec.rb +643 -18
  167. data/lib/capybara/spec/session/node_wrapper_spec.rb +1 -1
  168. data/lib/capybara/spec/session/refresh_spec.rb +4 -0
  169. data/lib/capybara/spec/session/reset_session_spec.rb +23 -8
  170. data/lib/capybara/spec/session/save_and_open_screenshot_spec.rb +2 -2
  171. data/lib/capybara/spec/session/save_screenshot_spec.rb +4 -4
  172. data/lib/capybara/spec/session/scroll_spec.rb +117 -0
  173. data/lib/capybara/spec/session/select_spec.rb +10 -10
  174. data/lib/capybara/spec/session/selectors_spec.rb +36 -5
  175. data/lib/capybara/spec/session/uncheck_spec.rb +2 -2
  176. data/lib/capybara/spec/session/unselect_spec.rb +1 -1
  177. data/lib/capybara/spec/session/window/become_closed_spec.rb +20 -17
  178. data/lib/capybara/spec/session/window/switch_to_window_spec.rb +4 -0
  179. data/lib/capybara/spec/session/window/window_opened_by_spec.rb +4 -0
  180. data/lib/capybara/spec/session/window/window_spec.rb +59 -58
  181. data/lib/capybara/spec/session/window/windows_spec.rb +4 -0
  182. data/lib/capybara/spec/session/within_spec.rb +23 -0
  183. data/lib/capybara/spec/spec_helper.rb +16 -6
  184. data/lib/capybara/spec/test_app.rb +28 -23
  185. data/lib/capybara/spec/views/animated.erb +49 -0
  186. data/lib/capybara/spec/views/form.erb +48 -7
  187. data/lib/capybara/spec/views/frame_child.erb +3 -2
  188. data/lib/capybara/spec/views/frame_one.erb +1 -0
  189. data/lib/capybara/spec/views/obscured.erb +47 -0
  190. data/lib/capybara/spec/views/offset.erb +32 -0
  191. data/lib/capybara/spec/views/react.erb +45 -0
  192. data/lib/capybara/spec/views/scroll.erb +20 -0
  193. data/lib/capybara/spec/views/spatial.erb +31 -0
  194. data/lib/capybara/spec/views/tables.erb +67 -0
  195. data/lib/capybara/spec/views/with_animation.erb +29 -1
  196. data/lib/capybara/spec/views/with_dragula.erb +24 -0
  197. data/lib/capybara/spec/views/with_hover.erb +1 -0
  198. data/lib/capybara/spec/views/with_hover1.erb +10 -0
  199. data/lib/capybara/spec/views/with_html.erb +32 -6
  200. data/lib/capybara/spec/views/with_js.erb +3 -1
  201. data/lib/capybara/spec/views/with_jstree.erb +26 -0
  202. data/lib/capybara/spec/views/with_scope_other.erb +6 -0
  203. data/lib/capybara/spec/views/with_sortable_js.erb +21 -0
  204. data/lib/capybara/version.rb +1 -1
  205. data/lib/capybara/window.rb +11 -11
  206. data/lib/capybara.rb +118 -111
  207. data/spec/basic_node_spec.rb +14 -3
  208. data/spec/capybara_spec.rb +29 -29
  209. data/spec/css_builder_spec.rb +101 -0
  210. data/spec/dsl_spec.rb +46 -21
  211. data/spec/filter_set_spec.rb +5 -5
  212. data/spec/fixtures/selenium_driver_rspec_failure.rb +1 -1
  213. data/spec/fixtures/selenium_driver_rspec_success.rb +1 -1
  214. data/spec/minitest_spec.rb +18 -4
  215. data/spec/minitest_spec_spec.rb +59 -44
  216. data/spec/rack_test_spec.rb +117 -89
  217. data/spec/regexp_dissassembler_spec.rb +250 -0
  218. data/spec/result_spec.rb +51 -49
  219. data/spec/rspec/features_spec.rb +3 -0
  220. data/spec/rspec/shared_spec_matchers.rb +112 -97
  221. data/spec/rspec_spec.rb +35 -17
  222. data/spec/sauce_spec_chrome.rb +43 -0
  223. data/spec/selector_spec.rb +244 -28
  224. data/spec/selenium_spec_chrome.rb +125 -54
  225. data/spec/selenium_spec_chrome_remote.rb +26 -12
  226. data/spec/selenium_spec_edge.rb +23 -8
  227. data/spec/selenium_spec_firefox.rb +208 -0
  228. data/spec/selenium_spec_firefox_remote.rb +15 -18
  229. data/spec/selenium_spec_ie.rb +82 -13
  230. data/spec/selenium_spec_safari.rb +148 -0
  231. data/spec/server_spec.rb +118 -77
  232. data/spec/session_spec.rb +19 -3
  233. data/spec/shared_selenium_node.rb +83 -0
  234. data/spec/shared_selenium_session.rb +110 -65
  235. data/spec/spec_helper.rb +57 -9
  236. data/spec/xpath_builder_spec.rb +93 -0
  237. metadata +257 -17
  238. data/lib/capybara/rspec/compound.rb +0 -94
  239. data/lib/capybara/selenium/driver_specializations/marionette_driver.rb +0 -49
  240. data/lib/capybara/selenium/nodes/marionette_node.rb +0 -121
  241. data/lib/capybara/spec/session/has_style_spec.rb +0 -25
  242. data/spec/selenium_spec_marionette.rb +0 -172
@@ -0,0 +1,78 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Capybara
4
+ module Selenium
5
+ module Scroll
6
+ def scroll_by(x, y)
7
+ driver.execute_script <<~JS, self, x, y
8
+ var el = arguments[0];
9
+ if (el.scrollBy){
10
+ el.scrollBy(arguments[1], arguments[2]);
11
+ } else {
12
+ el.scrollTop = el.scrollTop + arguments[2];
13
+ el.scrollLeft = el.scrollLeft + arguments[1];
14
+ }
15
+ JS
16
+ end
17
+
18
+ def scroll_to(element, location, position = nil)
19
+ # location, element = element, nil if element.is_a? Symbol
20
+ if element.is_a? Capybara::Selenium::Node
21
+ scroll_element_to_location(element, location)
22
+ elsif location.is_a? Symbol
23
+ scroll_to_location(location)
24
+ else
25
+ scroll_to_coords(*position)
26
+ end
27
+ self
28
+ end
29
+
30
+ private
31
+
32
+ def scroll_element_to_location(element, location)
33
+ scroll_opts = case location
34
+ when :top
35
+ 'true'
36
+ when :bottom
37
+ 'false'
38
+ when :center
39
+ "{behavior: 'instant', block: 'center'}"
40
+ else
41
+ raise ArgumentError, "Invalid scroll_to location: #{location}"
42
+ end
43
+ driver.execute_script <<~JS, element
44
+ arguments[0].scrollIntoView(#{scroll_opts})
45
+ JS
46
+ end
47
+
48
+ def scroll_to_location(location)
49
+ scroll_y = case location
50
+ when :top
51
+ '0'
52
+ when :bottom
53
+ 'arguments[0].scrollHeight'
54
+ when :center
55
+ '(arguments[0].scrollHeight - arguments[0].clientHeight)/2'
56
+ end
57
+ driver.execute_script <<~JS, self
58
+ if (arguments[0].scrollTo){
59
+ arguments[0].scrollTo(0, #{scroll_y});
60
+ } else {
61
+ arguments[0].scrollTop = #{scroll_y};
62
+ }
63
+ JS
64
+ end
65
+
66
+ def scroll_to_coords(x, y)
67
+ driver.execute_script <<~JS, self, x, y
68
+ if (arguments[0].scrollTo){
69
+ arguments[0].scrollTo(arguments[1], arguments[2]);
70
+ } else {
71
+ arguments[0].scrollTop = arguments[2];
72
+ arguments[0].scrollLeft = arguments[1];
73
+ }
74
+ JS
75
+ end
76
+ end
77
+ end
78
+ end
@@ -0,0 +1,34 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Capybara
4
+ module Selenium
5
+ module DeprecationSuppressor
6
+ def initialize(*)
7
+ @suppress_for_capybara = false
8
+ super
9
+ end
10
+
11
+ def deprecate(*)
12
+ super unless @suppress_for_capybara
13
+ end
14
+
15
+ def suppress_deprecations
16
+ prev_suppress_for_capybara, @suppress_for_capybara = @suppress_for_capybara, true
17
+ yield
18
+ ensure
19
+ @suppress_for_capybara = prev_suppress_for_capybara
20
+ end
21
+ end
22
+
23
+ module ErrorSuppressor
24
+ def for_code(*)
25
+ ::Selenium::WebDriver.logger.suppress_deprecations do
26
+ super
27
+ end
28
+ end
29
+ end
30
+ end
31
+ end
32
+
33
+ Selenium::WebDriver::Logger.prepend Capybara::Selenium::DeprecationSuppressor
34
+ Selenium::WebDriver::Error.singleton_class.prepend Capybara::Selenium::ErrorSuppressor
@@ -1,13 +1,20 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  # Selenium specific implementation of the Capybara::Driver::Node API
4
+
5
+ require 'capybara/selenium/extensions/find'
6
+ require 'capybara/selenium/extensions/scroll'
7
+
4
8
  class Capybara::Selenium::Node < Capybara::Driver::Node
9
+ include Capybara::Selenium::Find
10
+ include Capybara::Selenium::Scroll
11
+
5
12
  def visible_text
6
13
  native.text
7
14
  end
8
15
 
9
16
  def all_text
10
- text = driver.execute_script('return arguments[0].textContent', self)
17
+ text = driver.evaluate_script('arguments[0].textContent', self)
11
18
  text.gsub(/[\u200b\u200e\u200f]/, '')
12
19
  .gsub(/[\ \n\f\t\v\u2028\u2029]+/, ' ')
13
20
  .gsub(/\A[[:space:]&&[^\u00a0]]+/, '')
@@ -47,10 +54,16 @@ class Capybara::Selenium::Node < Capybara::Driver::Node
47
54
  # :backspace => send backspace keystrokes to clear the field <br/>
48
55
  # Array => an array of keys to send before the value being set, e.g. [[:command, 'a'], :backspace]
49
56
  def set(value, **options)
50
- raise ArgumentError, "Value cannot be an Array when 'multiple' attribute is not present. Not a #{value.class}" if value.is_a?(Array) && !multiple?
57
+ if value.is_a?(Array) && !multiple?
58
+ raise ArgumentError, "Value cannot be an Array when 'multiple' attribute is not present. Not a #{value.class}"
59
+ end
60
+
61
+ tag_name, type = attrs(:tagName, :type).map { |val| val&.downcase }
62
+ @tag_name ||= tag_name
63
+
51
64
  case tag_name
52
65
  when 'input'
53
- case self[:type]
66
+ case type
54
67
  when 'radio'
55
68
  click
56
69
  when 'checkbox'
@@ -63,13 +76,17 @@ class Capybara::Selenium::Node < Capybara::Driver::Node
63
76
  set_time(value)
64
77
  when 'datetime-local'
65
78
  set_datetime_local(value)
79
+ when 'color'
80
+ set_color(value)
81
+ when 'range'
82
+ set_range(value)
66
83
  else
67
- set_text(value, options)
84
+ set_text(value, **options)
68
85
  end
69
86
  when 'textarea'
70
- set_text(value, options)
87
+ set_text(value, **options)
71
88
  else
72
- set_content_editable(value) if content_editable?
89
+ set_content_editable(value)
73
90
  end
74
91
  end
75
92
 
@@ -79,32 +96,59 @@ class Capybara::Selenium::Node < Capybara::Driver::Node
79
96
 
80
97
  def unselect_option
81
98
  raise Capybara::UnselectNotAllowed, 'Cannot unselect option from single select box.' unless select_node.multiple?
99
+
82
100
  click if selected?
83
101
  end
84
102
 
85
103
  def click(keys = [], **options)
86
104
  click_options = ClickOptions.new(keys, options)
87
105
  return native.click if click_options.empty?
88
- click_with_options(click_options)
89
- rescue StandardError => err
90
- if err.is_a?(::Selenium::WebDriver::Error::ElementClickInterceptedError) ||
91
- err.message =~ /Other element would receive the click/
106
+
107
+ perform_with_options(click_options) do |action|
108
+ target = click_options.coords? ? nil : native
109
+ if click_options.delay.zero?
110
+ action.click(target)
111
+ else
112
+ action.click_and_hold(target)
113
+ if w3c?
114
+ action.pause(action.pointer_inputs.first, click_options.delay)
115
+ else
116
+ action.pause(click_options.delay)
117
+ end
118
+ action.release
119
+ end
120
+ end
121
+ rescue StandardError => e
122
+ if e.is_a?(::Selenium::WebDriver::Error::ElementClickInterceptedError) ||
123
+ e.message.match?(/Other element would receive the click/)
92
124
  scroll_to_center
93
125
  end
94
126
 
95
- raise err
127
+ raise e
96
128
  end
97
129
 
98
130
  def right_click(keys = [], **options)
99
131
  click_options = ClickOptions.new(keys, options)
100
- click_with_options(click_options) do |action|
101
- click_options.coords? ? action.context_click : action.context_click(native)
132
+ perform_with_options(click_options) do |action|
133
+ target = click_options.coords? ? nil : native
134
+ if click_options.delay.zero?
135
+ action.context_click(target)
136
+ elsif w3c?
137
+ action.move_to(target) if target
138
+ action.pointer_down(:right)
139
+ .pause(action.pointer_inputs.first, click_options.delay)
140
+ .pointer_up(:right)
141
+ else
142
+ raise ArgumentError, 'Delay is not supported when right clicking with legacy (non-w3c) selenium driver'
143
+ end
102
144
  end
103
145
  end
104
146
 
105
147
  def double_click(keys = [], **options)
106
148
  click_options = ClickOptions.new(keys, options)
107
- click_with_options(click_options) do |action|
149
+ raise ArgumentError, "double_click doesn't support a delay option" unless click_options.delay.zero?
150
+
151
+ perform_with_options(click_options) do |action|
108
152
  click_options.coords? ? action.double_click : action.double_click(native)
109
153
  end
110
154
  end
@@ -117,15 +161,25 @@ class Capybara::Selenium::Node < Capybara::Driver::Node
117
161
  scroll_if_needed { browser_action.move_to(native).perform }
118
162
  end
119
163
 
120
- def drag_to(element)
164
+ def drag_to(element, drop_modifiers: [], **)
165
+ drop_modifiers = Array(drop_modifiers)
121
166
  # Due to W3C spec compliance - The Actions API no longer scrolls to elements when necessary
122
167
  # which means Seleniums `drag_and_drop` is now broken - do it manually
123
168
  scroll_if_needed { browser_action.click_and_hold(native).perform }
124
- element.scroll_if_needed { browser_action.move_to(element.native).release.perform }
169
+ # element.scroll_if_needed { browser_action.move_to(element.native).release.perform }
170
+ element.scroll_if_needed do
171
+ keys_down = modifiers_down(browser_action, drop_modifiers)
172
+ keys_up = modifiers_up(keys_down.move_to(element.native).release, drop_modifiers)
173
+ keys_up.perform
174
+ end
175
+ end
176
+
177
+ def drop(*_)
178
+ raise NotImplementedError, 'Out of browser drop emulation is not implemented for the current browser'
125
179
  end
126
180
 
127
181
  def tag_name
128
- native.tag_name.downcase
182
+ @tag_name ||= native.tag_name.downcase
129
183
  end
130
184
 
131
185
  def visible?; boolean_attr(native.displayed?); end
@@ -136,20 +190,13 @@ class Capybara::Selenium::Node < Capybara::Driver::Node
136
190
 
137
191
  def disabled?
138
192
  return true unless native.enabled?
193
+
139
194
  # WebDriver only defines `disabled?` for form controls but fieldset makes sense too
140
- tag_name == 'fieldset' && find_xpath('ancestor-or-self::fieldset[@disabled]').any?
195
+ find_xpath('self::fieldset/ancestor-or-self::fieldset[@disabled]').any?
141
196
  end
142
197
 
143
198
  def content_editable?
144
- native.attribute('isContentEditable')
145
- end
146
-
147
- def find_xpath(locator)
148
- native.find_elements(:xpath, locator).map { |el| self.class.new(driver, el) }
149
- end
150
-
151
- def find_css(locator)
152
- native.find_elements(:css, locator).map { |el| self.class.new(driver, el) }
199
+ native.attribute('isContentEditable') == 'true'
153
200
  end
154
201
 
155
202
  def ==(other)
@@ -157,33 +204,18 @@ class Capybara::Selenium::Node < Capybara::Driver::Node
157
204
  end
158
205
 
159
206
  def path
160
- path = find_xpath(XPath.ancestor_or_self).reverse
161
-
162
- result = []
163
- default_ns = path.last[:namespaceURI]
164
- while (node = path.shift)
165
- parent = path.first
166
- selector = node[:tagName]
167
- if node[:namespaceURI] != default_ns
168
- selector = XPath.child.where((XPath.local_name == selector) & (XPath.namespace_uri == node[:namespaceURI])).to_s
169
- selector
170
- end
207
+ driver.evaluate_script GET_XPATH_SCRIPT, self
208
+ end
171
209
 
172
- if parent
173
- siblings = parent.find_xpath(selector)
174
- selector += case siblings.size
175
- when 0
176
- '[ERROR]' # IE doesn't support full XPath (namespace-uri, etc)
177
- when 1
178
- '' # index not necessary when only one matching element
179
- else
180
- "[#{siblings.index(node) + 1}]"
181
- end
182
- end
183
- result.push selector
184
- end
210
+ def obscured?(x: nil, y: nil)
211
+ res = driver.evaluate_script(OBSCURED_OR_OFFSET_SCRIPT, self, x, y)
212
+ return true if res == true
213
+
214
+ driver.frame_obscured_at?(x: res['x'], y: res['y'])
215
+ end
185
216
 
186
- '/' + result.reverse.join('/')
217
+ def rect
218
+ native.rect
187
219
  end
188
220
 
189
221
  protected
@@ -195,8 +227,37 @@ protected
195
227
  yield
196
228
  end
197
229
 
230
+ def scroll_to_center
231
+ script = <<-'JS'
232
+ try {
233
+ arguments[0].scrollIntoView({behavior: 'instant', block: 'center', inline: 'center'});
234
+ } catch(e) {
235
+ arguments[0].scrollIntoView(true);
236
+ }
237
+ JS
238
+ begin
239
+ driver.execute_script(script, self)
240
+ rescue StandardError
241
+ # Swallow error if scrollIntoView with options isn't supported
242
+ end
243
+ end
244
+
198
245
  private
199
246
 
247
+ def sibling_index(parent, node, selector)
248
+ siblings = parent.find_xpath(selector)
249
+ case siblings.size
250
+ when 0
251
+ '[ERROR]' # IE doesn't support full XPath (namespace-uri, etc)
252
+ when 1
253
+ '' # index not necessary when only one matching element
254
+ else
255
+ idx = siblings.index(node)
256
+ # Element may not be found in the siblings if it has gone away
257
+ idx.nil? ? '[ERROR]' : "[#{idx + 1}]"
258
+ end
259
+ end
260
+
200
261
  def boolean_attr(val)
201
262
  val && (val != 'false')
202
263
  end
@@ -206,7 +267,7 @@ private
206
267
  find_xpath(XPath.ancestor(:select)[1]).first
207
268
  end
208
269
 
209
- def set_text(value, clear: nil, **_unused)
270
+ def set_text(value, clear: nil, rapid: nil, **_unused)
210
271
  value = value.to_s
211
272
  if value.empty? && clear.nil?
212
273
  native.clear
@@ -217,15 +278,24 @@ private
217
278
  elsif clear.is_a? Array
218
279
  send_keys(*clear, value)
219
280
  else
220
- # Clear field by JavaScript assignment of the value property.
221
- # Script can change a readonly element which user input cannot, so
222
- # don't execute if readonly.
223
- driver.execute_script "arguments[0].value = ''", self unless clear == :none
224
- send_keys(value)
281
+ driver.execute_script 'arguments[0].select()', self unless clear == :none
282
+ if rapid == true || ((value.length > auto_rapid_set_length) && rapid != false)
283
+ send_keys(value[0..3])
284
+ driver.execute_script RAPID_APPEND_TEXT, self, value[4...-3]
285
+ send_keys(value[-3..-1])
286
+ else
287
+ send_keys(value)
288
+ end
225
289
  end
226
290
  end
227
291
 
228
- def click_with_options(click_options)
292
+ def auto_rapid_set_length
293
+ 30
294
+ end
295
+
296
+ def perform_with_options(click_options, &block)
297
+ raise ArgumentError, 'A block must be provided' unless block
298
+
229
299
  scroll_if_needed do
230
300
  action_with_modifiers(click_options) do |action|
231
301
  if block_given?
@@ -237,24 +307,10 @@ private
237
307
  end
238
308
  end
239
309
 
240
- def scroll_to_center
241
- script = <<-'JS'
242
- try {
243
- arguments[0].scrollIntoView({behavior: 'instant', block: 'center', inline: 'center'});
244
- } catch(e) {
245
- arguments[0].scrollIntoView(true);
246
- }
247
- JS
248
- begin
249
- driver.execute_script(script, self)
250
- rescue StandardError # rubocop:disable Lint/HandleExceptions
251
- # Swallow error if scrollIntoView with options isn't supported
252
- end
253
- end
254
-
255
310
  def set_date(value) # rubocop:disable Naming/AccessorMethodName
256
311
  value = SettableValue.new(value)
257
312
  return set_text(value) unless value.dateable?
313
+
258
314
  # TODO: this would be better if locale can be detected and correct keystrokes sent
259
315
  update_value_js(value.to_date_str)
260
316
  end
@@ -262,6 +318,7 @@ private
262
318
  def set_time(value) # rubocop:disable Naming/AccessorMethodName
263
319
  value = SettableValue.new(value)
264
320
  return set_text(value) unless value.timeable?
321
+
265
322
  # TODO: this would be better if locale can be detected and correct keystrokes sent
266
323
  update_value_js(value.to_time_str)
267
324
  end
@@ -269,12 +326,22 @@ private
269
326
  def set_datetime_local(value) # rubocop:disable Naming/AccessorMethodName
270
327
  value = SettableValue.new(value)
271
328
  return set_text(value) unless value.timeable?
329
+
272
330
  # TODO: this would be better if locale can be detected and correct keystrokes sent
273
331
  update_value_js(value.to_datetime_str)
274
332
  end
275
333
 
334
+ def set_color(value) # rubocop:disable Naming/AccessorMethodName
335
+ update_value_js(value)
336
+ end
337
+
338
+ def set_range(value) # rubocop:disable Naming/AccessorMethodName
339
+ update_value_js(value)
340
+ end
341
+
276
342
  def update_value_js(value)
277
343
  driver.execute_script(<<-JS, self, value)
344
+ if (arguments[0].readOnly) { return };
278
345
  if (document.activeElement !== arguments[0]){
279
346
  arguments[0].focus();
280
347
  }
@@ -287,33 +354,65 @@ private
287
354
  end
288
355
 
289
356
  def set_file(value) # rubocop:disable Naming/AccessorMethodName
290
- path_names = value.to_s.empty? ? [] : value
291
- native.send_keys(Array(path_names).join("\n"))
357
+ with_file_detector do
358
+ path_names = value.to_s.empty? ? [] : value
359
+ file_names = Array(path_names).map do |pn|
360
+ Pathname.new(pn).absolute? ? pn : File.expand_path(pn)
361
+ end.join("\n")
362
+ native.send_keys(file_names)
363
+ end
364
+ end
365
+
366
+ def with_file_detector
367
+ if driver.options[:browser] == :remote &&
368
+ bridge.respond_to?(:file_detector) &&
369
+ bridge.file_detector.nil?
370
+ begin
371
+ bridge.file_detector = lambda do |(fn, *)|
372
+ str = fn.to_s
373
+ str if File.exist?(str)
374
+ end
375
+ yield
376
+ ensure
377
+ bridge.file_detector = nil
378
+ end
379
+ else
380
+ yield
381
+ end
292
382
  end
293
383
 
294
384
  def set_content_editable(value) # rubocop:disable Naming/AccessorMethodName
295
385
  # Ensure we are focused on the element
296
386
  click
297
387
 
298
- script = <<-JS
299
- var range = document.createRange();
300
- var sel = window.getSelection();
301
- arguments[0].focus();
302
- range.selectNodeContents(arguments[0]);
303
- sel.removeAllRanges();
304
- sel.addRange(range);
388
+ editable = driver.execute_script <<-JS, self
389
+ if (arguments[0].isContentEditable) {
390
+ var range = document.createRange();
391
+ var sel = window.getSelection();
392
+ arguments[0].focus();
393
+ range.selectNodeContents(arguments[0]);
394
+ sel.removeAllRanges();
395
+ sel.addRange(range);
396
+ return true;
397
+ }
398
+ return false;
305
399
  JS
306
- driver.execute_script script, self
307
400
 
308
401
  # The action api has a speed problem but both chrome and firefox 58 raise errors
309
402
  # if we use the faster direct send_keys. For now just send_keys to the element
310
403
  # we've already focused.
311
404
  # native.send_keys(value.to_s)
312
- browser_action.send_keys(value.to_s).perform
405
+ browser_action.send_keys(value.to_s).perform if editable
313
406
  end
314
407
 
315
408
  def action_with_modifiers(click_options)
316
- actions = browser_action.move_to(native, *click_options.coords)
409
+ actions = browser_action.tap do |acts|
410
+ if click_options.center_offset? && click_options.coords?
411
+ acts.move_to(native).move_by(*click_options.coords)
412
+ else
413
+ acts.move_to(native, *click_options.coords)
414
+ end
415
+ end
317
416
  modifiers_down(actions, click_options.keys)
318
417
  yield actions
319
418
  modifiers_up(actions, click_options.keys)
@@ -325,28 +424,126 @@ private
325
424
 
326
425
  def modifiers_down(actions, keys)
327
426
  each_key(keys) { |key| actions.key_down(key) }
427
+ actions
328
428
  end
329
429
 
330
430
  def modifiers_up(actions, keys)
331
431
  each_key(keys) { |key| actions.key_up(key) }
432
+ actions
433
+ end
434
+
435
+ def browser
436
+ driver.browser
437
+ end
438
+
439
+ def bridge
440
+ browser.send(:bridge)
332
441
  end
333
442
 
334
443
  def browser_action
335
- driver.browser.action
444
+ browser.action
336
445
  end
337
446
 
338
- def each_key(keys)
339
- keys.each do |key|
340
- key = case key
447
+ def capabilities
448
+ browser.capabilities
449
+ end
450
+
451
+ def w3c?
452
+ (defined?(Selenium::WebDriver::VERSION) && (Selenium::WebDriver::VERSION.to_f >= 4)) ||
453
+ capabilities.is_a?(::Selenium::WebDriver::Remote::W3C::Capabilities)
454
+ end
455
+
456
+ def normalize_keys(keys)
457
+ keys.map do |key|
458
+ case key
341
459
  when :ctrl then :control
342
460
  when :command, :cmd then :meta
343
461
  else
344
462
  key
345
463
  end
346
- yield key
347
464
  end
348
465
  end
349
466
 
467
+ def each_key(keys)
468
+ normalize_keys(keys).each { |key| yield(key) }
469
+ end
470
+
471
+ def find_context
472
+ native
473
+ end
474
+
475
+ def build_node(native_node, initial_cache = {})
476
+ self.class.new(driver, native_node, initial_cache)
477
+ end
478
+
479
+ def attrs(*attr_names)
480
+ return attr_names.map { |name| self[name.to_s] } if ENV['CAPYBARA_THOROUGH']
481
+
482
+ driver.evaluate_script <<~'JS', self, attr_names.map(&:to_s)
483
+ (function(el, names){
484
+ return names.map(function(name){
485
+ return el[name]
486
+ });
487
+ })(arguments[0], arguments[1]);
488
+ JS
489
+ end
490
+
491
+ GET_XPATH_SCRIPT = <<~'JS'
492
+ (function(el, xml){
493
+ var xpath = '';
494
+ var pos, tempitem2;
495
+
496
+ while(el !== xml.documentElement) {
497
+ pos = 0;
498
+ tempitem2 = el;
499
+ while(tempitem2) {
500
+ if (tempitem2.nodeType === 1 && tempitem2.nodeName === el.nodeName) { // If it is ELEMENT_NODE of the same name
501
+ pos += 1;
502
+ }
503
+ tempitem2 = tempitem2.previousSibling;
504
+ }
505
+
506
+ if (el.namespaceURI != xml.documentElement.namespaceURI) {
507
+ xpath = "*[local-name()='"+el.nodeName+"' and namespace-uri()='"+(el.namespaceURI===null?'':el.namespaceURI)+"']["+pos+']'+'/'+xpath;
508
+ } else {
509
+ xpath = el.nodeName.toUpperCase()+"["+pos+"]/"+xpath;
510
+ }
511
+
512
+ el = el.parentNode;
513
+ }
514
+ xpath = '/'+xml.documentElement.nodeName.toUpperCase()+'/'+xpath;
515
+ xpath = xpath.replace(/\/$/, '');
516
+ return xpath;
517
+ })(arguments[0], document)
518
+ JS
519
+
520
+ OBSCURED_OR_OFFSET_SCRIPT = <<~'JS'
521
+ (function(el, x, y) {
522
+ var box = el.getBoundingClientRect();
523
+ if (x == null) x = box.width/2;
524
+ if (y == null) y = box.height/2 ;
525
+
526
+ var px = box.left + x,
527
+ py = box.top + y,
528
+ e = document.elementFromPoint(px, py);
529
+
530
+ if (!el.contains(e))
531
+ return true;
532
+
533
+ return { x: px, y: py };
534
+ })(arguments[0], arguments[1], arguments[2])
535
+ JS
536
+
537
+ RAPID_APPEND_TEXT = <<~'JS'
538
+ (function(el, value) {
539
+ value = el.value + value;
540
+ if (el.maxLength && el.maxLength != -1){
541
+ value = value.slice(0, el.maxLength);
542
+ }
543
+ el.value = value;
544
+ })(arguments[0], arguments[1])
545
+ JS
546
+
350
547
  # SettableValue encapsulates time/date field formatting
351
548
  class SettableValue
352
549
  attr_reader :value
@@ -364,7 +561,7 @@ private
364
561
  end
365
562
 
366
563
  def to_date_str
367
- value.to_date.strftime('%Y-%m-%d')
564
+ value.to_date.iso8601
368
565
  end
369
566
 
370
567
  def timeable?
@@ -398,8 +595,16 @@ private
398
595
  [options[:x], options[:y]]
399
596
  end
400
597
 
598
+ def center_offset?
599
+ options[:offset] == :center
600
+ end
601
+
401
602
  def empty?
402
- keys.empty? && !coords?
603
+ keys.empty? && !coords? && delay.zero?
604
+ end
605
+
606
+ def delay
607
+ options[:delay] || 0
403
608
  end
404
609
  end
405
610
  private_constant :ClickOptions