capybara 3.32.2 → 3.39.2

Sign up to get free protection for your applications and to get access to all the features.
Files changed (205) hide show
  1. checksums.yaml +4 -4
  2. data/History.md +227 -19
  3. data/README.md +36 -15
  4. data/lib/capybara/config.rb +18 -8
  5. data/lib/capybara/cucumber.rb +1 -1
  6. data/lib/capybara/driver/base.rb +8 -0
  7. data/lib/capybara/driver/node.rb +5 -1
  8. data/lib/capybara/dsl.rb +4 -10
  9. data/lib/capybara/helpers.rb +21 -2
  10. data/lib/capybara/minitest/spec.rb +14 -11
  11. data/lib/capybara/minitest.rb +2 -3
  12. data/lib/capybara/node/actions.rb +27 -27
  13. data/lib/capybara/node/base.rb +8 -7
  14. data/lib/capybara/node/document.rb +2 -2
  15. data/lib/capybara/node/element.rb +14 -7
  16. data/lib/capybara/node/finders.rb +18 -8
  17. data/lib/capybara/node/matchers.rb +12 -12
  18. data/lib/capybara/node/simple.rb +10 -2
  19. data/lib/capybara/node/whitespace_normalizer.rb +81 -0
  20. data/lib/capybara/queries/active_element_query.rb +18 -0
  21. data/lib/capybara/queries/ancestor_query.rb +3 -2
  22. data/lib/capybara/queries/base_query.rb +2 -2
  23. data/lib/capybara/queries/current_path_query.rb +14 -4
  24. data/lib/capybara/queries/selector_query.rb +78 -28
  25. data/lib/capybara/queries/sibling_query.rb +3 -2
  26. data/lib/capybara/queries/style_query.rb +1 -1
  27. data/lib/capybara/queries/text_query.rb +8 -2
  28. data/lib/capybara/rack_test/browser.rb +70 -11
  29. data/lib/capybara/rack_test/driver.rb +5 -4
  30. data/lib/capybara/rack_test/form.rb +30 -8
  31. data/lib/capybara/rack_test/node.rb +28 -22
  32. data/lib/capybara/registration_container.rb +41 -0
  33. data/lib/capybara/registrations/drivers.rb +20 -14
  34. data/lib/capybara/registrations/patches/puma_ssl.rb +3 -1
  35. data/lib/capybara/registrations/servers.rb +32 -11
  36. data/lib/capybara/result.rb +6 -10
  37. data/lib/capybara/rspec/matcher_proxies.rb +7 -7
  38. data/lib/capybara/rspec/matchers/base.rb +8 -6
  39. data/lib/capybara/rspec/matchers/compound.rb +1 -1
  40. data/lib/capybara/rspec/matchers/have_current_path.rb +2 -2
  41. data/lib/capybara/rspec/matchers/have_selector.rb +5 -5
  42. data/lib/capybara/rspec/matchers/match_style.rb +5 -0
  43. data/lib/capybara/rspec/matchers.rb +21 -20
  44. data/lib/capybara/rspec.rb +2 -0
  45. data/lib/capybara/selector/builders/css_builder.rb +2 -2
  46. data/lib/capybara/selector/builders/xpath_builder.rb +4 -2
  47. data/lib/capybara/selector/css.rb +1 -1
  48. data/lib/capybara/selector/definition/button.rb +29 -12
  49. data/lib/capybara/selector/definition/checkbox.rb +1 -1
  50. data/lib/capybara/selector/definition/css.rb +1 -1
  51. data/lib/capybara/selector/definition/datalist_input.rb +1 -1
  52. data/lib/capybara/selector/definition/element.rb +2 -1
  53. data/lib/capybara/selector/definition/file_field.rb +1 -1
  54. data/lib/capybara/selector/definition/fillable_field.rb +2 -2
  55. data/lib/capybara/selector/definition/label.rb +1 -1
  56. data/lib/capybara/selector/definition/link.rb +10 -1
  57. data/lib/capybara/selector/definition/radio_button.rb +1 -1
  58. data/lib/capybara/selector/definition/select.rb +1 -1
  59. data/lib/capybara/selector/definition/table.rb +1 -1
  60. data/lib/capybara/selector/definition/table_row.rb +2 -2
  61. data/lib/capybara/selector/definition.rb +14 -10
  62. data/lib/capybara/selector/filter_set.rb +6 -9
  63. data/lib/capybara/selector/regexp_disassembler.rb +2 -5
  64. data/lib/capybara/selector/selector.rb +14 -2
  65. data/lib/capybara/selector.rb +13 -3
  66. data/lib/capybara/selenium/atoms/src/isDisplayed.js +1 -1
  67. data/lib/capybara/selenium/driver.rb +71 -10
  68. data/lib/capybara/selenium/driver_specializations/chrome_driver.rb +10 -12
  69. data/lib/capybara/selenium/driver_specializations/edge_driver.rb +18 -16
  70. data/lib/capybara/selenium/driver_specializations/firefox_driver.rb +3 -3
  71. data/lib/capybara/selenium/extensions/find.rb +4 -4
  72. data/lib/capybara/selenium/extensions/html5_drag.rb +5 -4
  73. data/lib/capybara/selenium/extensions/scroll.rb +8 -10
  74. data/lib/capybara/selenium/logger_suppressor.rb +13 -3
  75. data/lib/capybara/selenium/node.rb +90 -37
  76. data/lib/capybara/selenium/nodes/chrome_node.rb +29 -7
  77. data/lib/capybara/selenium/nodes/edge_node.rb +25 -3
  78. data/lib/capybara/selenium/nodes/firefox_node.rb +10 -5
  79. data/lib/capybara/selenium/nodes/safari_node.rb +5 -5
  80. data/lib/capybara/selenium/patches/action_pauser.rb +3 -3
  81. data/lib/capybara/selenium/patches/atoms.rb +5 -5
  82. data/lib/capybara/selenium/patches/logs.rb +7 -9
  83. data/lib/capybara/selenium/patches/pause_duration_fix.rb +1 -1
  84. data/lib/capybara/server/animation_disabler.rb +43 -21
  85. data/lib/capybara/server/middleware.rb +5 -3
  86. data/lib/capybara/session/config.rb +6 -2
  87. data/lib/capybara/session/matchers.rb +11 -11
  88. data/lib/capybara/session.rb +52 -44
  89. data/lib/capybara/spec/public/test.js +17 -1
  90. data/lib/capybara/spec/session/accept_alert_spec.rb +1 -1
  91. data/lib/capybara/spec/session/active_element_spec.rb +31 -0
  92. data/lib/capybara/spec/session/all_spec.rb +10 -14
  93. data/lib/capybara/spec/session/assert_text_spec.rb +17 -17
  94. data/lib/capybara/spec/session/attach_file_spec.rb +6 -0
  95. data/lib/capybara/spec/session/check_spec.rb +16 -0
  96. data/lib/capybara/spec/session/choose_spec.rb +6 -0
  97. data/lib/capybara/spec/session/click_button_spec.rb +11 -0
  98. data/lib/capybara/spec/session/click_link_or_button_spec.rb +9 -0
  99. data/lib/capybara/spec/session/click_link_spec.rb +11 -0
  100. data/lib/capybara/spec/session/current_scope_spec.rb +1 -1
  101. data/lib/capybara/spec/session/current_url_spec.rb +11 -1
  102. data/lib/capybara/spec/session/fill_in_spec.rb +6 -0
  103. data/lib/capybara/spec/session/find_link_spec.rb +10 -0
  104. data/lib/capybara/spec/session/find_spec.rb +7 -1
  105. data/lib/capybara/spec/session/first_spec.rb +1 -1
  106. data/lib/capybara/spec/session/frame/within_frame_spec.rb +2 -0
  107. data/lib/capybara/spec/session/has_all_selectors_spec.rb +5 -5
  108. data/lib/capybara/spec/session/has_ancestor_spec.rb +2 -2
  109. data/lib/capybara/spec/session/has_any_selectors_spec.rb +6 -2
  110. data/lib/capybara/spec/session/has_button_spec.rb +81 -0
  111. data/lib/capybara/spec/session/has_css_spec.rb +2 -1
  112. data/lib/capybara/spec/session/has_current_path_spec.rb +18 -5
  113. data/lib/capybara/spec/session/has_field_spec.rb +41 -1
  114. data/lib/capybara/spec/session/has_link_spec.rb +40 -0
  115. data/lib/capybara/spec/session/has_none_selectors_spec.rb +7 -7
  116. data/lib/capybara/spec/session/has_select_spec.rb +14 -8
  117. data/lib/capybara/spec/session/has_selector_spec.rb +19 -4
  118. data/lib/capybara/spec/session/has_text_spec.rb +6 -25
  119. data/lib/capybara/spec/session/html_spec.rb +1 -1
  120. data/lib/capybara/spec/session/matches_style_spec.rb +4 -2
  121. data/lib/capybara/spec/session/node_spec.rb +111 -10
  122. data/lib/capybara/spec/session/refresh_spec.rb +2 -1
  123. data/lib/capybara/spec/session/reset_session_spec.rb +13 -0
  124. data/lib/capybara/spec/session/save_page_spec.rb +4 -4
  125. data/lib/capybara/spec/session/scroll_spec.rb +7 -5
  126. data/lib/capybara/spec/session/visit_spec.rb +20 -0
  127. data/lib/capybara/spec/session/window/switch_to_window_spec.rb +1 -1
  128. data/lib/capybara/spec/session/window/window_opened_by_spec.rb +1 -1
  129. data/lib/capybara/spec/session/window/window_spec.rb +2 -2
  130. data/lib/capybara/spec/session/window/windows_spec.rb +2 -2
  131. data/lib/capybara/spec/session/within_spec.rb +13 -0
  132. data/lib/capybara/spec/spec_helper.rb +23 -16
  133. data/lib/capybara/spec/test_app.rb +113 -34
  134. data/lib/capybara/spec/views/animated.erb +1 -1
  135. data/lib/capybara/spec/views/form.erb +53 -5
  136. data/lib/capybara/spec/views/frame_child.erb +1 -1
  137. data/lib/capybara/spec/views/frame_one.erb +1 -1
  138. data/lib/capybara/spec/views/frame_parent.erb +1 -1
  139. data/lib/capybara/spec/views/frame_two.erb +1 -1
  140. data/lib/capybara/spec/views/initial_alert.erb +2 -1
  141. data/lib/capybara/spec/views/layout.erb +10 -0
  142. data/lib/capybara/spec/views/obscured.erb +1 -1
  143. data/lib/capybara/spec/views/offset.erb +2 -1
  144. data/lib/capybara/spec/views/path.erb +2 -2
  145. data/lib/capybara/spec/views/popup_one.erb +1 -1
  146. data/lib/capybara/spec/views/popup_two.erb +1 -1
  147. data/lib/capybara/spec/views/react.erb +2 -2
  148. data/lib/capybara/spec/views/scroll.erb +2 -1
  149. data/lib/capybara/spec/views/spatial.erb +1 -1
  150. data/lib/capybara/spec/views/with_animation.erb +10 -3
  151. data/lib/capybara/spec/views/with_base_tag.erb +2 -2
  152. data/lib/capybara/spec/views/with_dragula.erb +5 -3
  153. data/lib/capybara/spec/views/with_fixed_header_footer.erb +2 -1
  154. data/lib/capybara/spec/views/with_hover.erb +2 -2
  155. data/lib/capybara/spec/views/with_html.erb +3 -3
  156. data/lib/capybara/spec/views/with_jquery_animation.erb +24 -0
  157. data/lib/capybara/spec/views/with_js.erb +5 -3
  158. data/lib/capybara/spec/views/with_jstree.erb +1 -1
  159. data/lib/capybara/spec/views/with_namespace.erb +1 -0
  160. data/lib/capybara/spec/views/with_scope.erb +2 -2
  161. data/lib/capybara/spec/views/with_shadow.erb +31 -0
  162. data/lib/capybara/spec/views/with_slow_unload.erb +2 -1
  163. data/lib/capybara/spec/views/with_sortable_js.erb +3 -3
  164. data/lib/capybara/spec/views/with_unload_alert.erb +1 -0
  165. data/lib/capybara/spec/views/with_windows.erb +1 -1
  166. data/lib/capybara/spec/views/within_frames.erb +1 -1
  167. data/lib/capybara/version.rb +1 -1
  168. data/lib/capybara/window.rb +4 -8
  169. data/lib/capybara.rb +40 -31
  170. data/spec/basic_node_spec.rb +25 -11
  171. data/spec/capybara_spec.rb +13 -1
  172. data/spec/counter_spec.rb +35 -0
  173. data/spec/css_builder_spec.rb +1 -1
  174. data/spec/css_splitter_spec.rb +1 -1
  175. data/spec/dsl_spec.rb +18 -3
  176. data/spec/fixtures/selenium_driver_rspec_failure.rb +2 -2
  177. data/spec/fixtures/selenium_driver_rspec_success.rb +3 -3
  178. data/spec/minitest_spec.rb +7 -2
  179. data/spec/minitest_spec_spec.rb +4 -0
  180. data/spec/per_session_config_spec.rb +1 -1
  181. data/spec/rack_test_spec.rb +41 -12
  182. data/spec/result_spec.rb +32 -35
  183. data/spec/rspec/features_spec.rb +6 -4
  184. data/spec/rspec/scenarios_spec.rb +6 -2
  185. data/spec/rspec/shared_spec_matchers.rb +64 -52
  186. data/spec/rspec_matchers_spec.rb +25 -0
  187. data/spec/rspec_spec.rb +6 -2
  188. data/spec/sauce_spec_chrome.rb +4 -4
  189. data/spec/selector_spec.rb +21 -6
  190. data/spec/selenium_spec_chrome.rb +50 -31
  191. data/spec/selenium_spec_chrome_remote.rb +16 -11
  192. data/spec/selenium_spec_edge.rb +12 -6
  193. data/spec/selenium_spec_firefox.rb +39 -20
  194. data/spec/selenium_spec_firefox_remote.rb +19 -4
  195. data/spec/selenium_spec_ie.rb +7 -8
  196. data/spec/selenium_spec_safari.rb +34 -20
  197. data/spec/server_spec.rb +65 -54
  198. data/spec/shared_selenium_node.rb +0 -4
  199. data/spec/shared_selenium_session.rb +104 -12
  200. data/spec/spec_helper.rb +36 -3
  201. data/spec/whitespace_normalizer_spec.rb +54 -0
  202. data/spec/xpath_builder_spec.rb +1 -1
  203. metadata +82 -21
  204. data/lib/capybara/spec/session/source_spec.rb +0 -0
  205. data/lib/capybara/spec/views/with_title.erb +0 -5
@@ -209,13 +209,15 @@ module Capybara
209
209
  # @!method wont_have_xpath
210
210
  # See {Capybara::Node::Matchers#has_no_xpath?}
211
211
 
212
- %w[text content title current_path].each do |assertion|
213
- infect_an_assertion "assert_#{assertion}", "must_have_#{assertion}", :reverse
214
- infect_an_assertion "refute_#{assertion}", "wont_have_#{assertion}", :reverse
215
- end
212
+ # This currently doesn't work for Ruby 2.8 due to Minitest not forwarding keyword args separately
213
+ # %w[text content title current_path].each do |assertion|
214
+ # infect_an_assertion "assert_#{assertion}", "must_have_#{assertion}", :reverse
215
+ # infect_an_assertion "refute_#{assertion}", "wont_have_#{assertion}", :reverse
216
+ # end
216
217
 
217
218
  # rubocop:disable Style/MultilineBlockChain
218
- (%w[selector xpath css link button field select table checked_field unchecked_field
219
+ (%w[text content title current_path
220
+ selector xpath css link button field select table checked_field unchecked_field
219
221
  ancestor sibling].flat_map do |assertion|
220
222
  [%W[assert_#{assertion} must_have_#{assertion}],
221
223
  %W[refute_#{assertion} wont_have_#{assertion}]]
@@ -228,14 +230,15 @@ module Capybara
228
230
  %W[refute_matches_#{assertion} wont_match_#{assertion}]]
229
231
  end).each do |(meth, new_name)|
230
232
  class_eval <<-ASSERTION, __FILE__, __LINE__ + 1
231
- def #{new_name} *args, &block
232
- ::Minitest::Expectation.new(self, ::Minitest::Spec.current).#{new_name}(*args, &block)
233
+ def #{new_name} *args, **kw_args, &block
234
+ ::Minitest::Expectation.new(self, ::Minitest::Spec.current).#{new_name}(*args, **kw_args, &block)
233
235
  end
234
236
  ASSERTION
235
237
 
236
238
  ::Minitest::Expectation.class_eval <<-ASSERTION, __FILE__, __LINE__ + 1
237
- def #{new_name} *args, &block
238
- ctx.#{meth}(target, *args, &block)
239
+ def #{new_name} *args, **kw_args, &block
240
+ raise "Calling ##{new_name} outside of test." unless ctx
241
+ ctx.#{meth}(target, *args, **kw_args, &block)
239
242
  end
240
243
  ASSERTION
241
244
  end
@@ -243,9 +246,9 @@ module Capybara
243
246
 
244
247
  ##
245
248
  # @deprecated
246
- def must_have_style(*args, &block)
249
+ def must_have_style(...)
247
250
  warn 'must_have_style is deprecated, please use must_match_style'
248
- must_match_style(*args, &block)
251
+ must_match_style(...)
249
252
  end
250
253
  end
251
254
  end
@@ -50,15 +50,14 @@ module Capybara
50
50
 
51
51
  %w[text no_text title no_title current_path no_current_path].each do |assertion_name|
52
52
  class_eval <<-ASSERTION, __FILE__, __LINE__ + 1
53
- def assert_#{assertion_name} *args
53
+ def assert_#{assertion_name}(*args, **kwargs, &optional_filter_block)
54
54
  self.assertions +=1
55
55
  subject, args = determine_subject(args)
56
- subject.assert_#{assertion_name}(*args)
56
+ subject.assert_#{assertion_name}(*args, **kwargs, &optional_filter_block)
57
57
  rescue Capybara::ExpectationNotMet => e
58
58
  raise ::Minitest::Assertion, e.message
59
59
  end
60
60
  ASSERTION
61
- ruby2_keywords "assert_#{assertion_name}" if respond_to?(:ruby2_keywords)
62
61
  end
63
62
 
64
63
  alias_method :refute_title, :assert_no_title
@@ -92,8 +92,9 @@ module Capybara
92
92
  end
93
93
 
94
94
  # @!macro label_click
95
- # @option options [Boolean] allow_label_click
95
+ # @option options [Boolean, Hash] allow_label_click
96
96
  # Attempt to click the label to toggle state if element is non-visible. Defaults to {Capybara.configure automatic_label_click}.
97
+ # If set to a Hash it is passed as options to the `click` on the label
97
98
 
98
99
  ##
99
100
  #
@@ -277,7 +278,7 @@ module Capybara
277
278
  # @return [Capybara::Node::Element] The file field element
278
279
  def attach_file(locator = nil, paths, make_visible: nil, **options) # rubocop:disable Style/OptionalArguments
279
280
  if locator && block_given?
280
- raise ArgumentError, '``#attach_file` does not support passing both a locator and a block'
281
+ raise ArgumentError, '`#attach_file` does not support passing both a locator and a block'
281
282
  end
282
283
 
283
284
  Array(paths).each do |path|
@@ -308,16 +309,14 @@ module Capybara
308
309
 
309
310
  def find_select_or_datalist_input(from, options)
310
311
  synchronize(Capybara::Queries::BaseQuery.wait(options, session_options.default_max_wait_time)) do
312
+ find(:select, from, **options)
313
+ rescue Capybara::ElementNotFound => select_error # rubocop:disable Naming/RescuedExceptionsVariableName
314
+ raise if %i[selected with_selected multiple].any? { |option| options.key?(option) }
315
+
311
316
  begin
312
- find(:select, from, **options)
313
- rescue Capybara::ElementNotFound => select_error # rubocop:disable Naming/RescuedExceptionsVariableName
314
- raise if %i[selected with_selected multiple].any? { |option| options.key?(option) }
315
-
316
- begin
317
- find(:datalist_input, from, **options)
318
- rescue Capybara::ElementNotFound => dlinput_error # rubocop:disable Naming/RescuedExceptionsVariableName
319
- raise Capybara::ElementNotFound, "#{select_error.message} and #{dlinput_error.message}"
320
- end
317
+ find(:datalist_input, from, **options)
318
+ rescue Capybara::ElementNotFound => dlinput_error
319
+ raise Capybara::ElementNotFound, "#{select_error.message} and #{dlinput_error.message}"
321
320
  end
322
321
  end
323
322
  end
@@ -365,25 +364,26 @@ module Capybara
365
364
  def _check_with_label(selector, checked, locator,
366
365
  allow_label_click: session_options.automatic_label_click, **options)
367
366
  options[:allow_self] = true if locator.nil?
368
-
369
367
  synchronize(Capybara::Queries::BaseQuery.wait(options, session_options.default_max_wait_time)) do
368
+ el = find(selector, locator, **options)
369
+ el.set(checked)
370
+ rescue StandardError => e
371
+ raise unless allow_label_click && catch_error?(e)
372
+
370
373
  begin
371
- el = find(selector, locator, **options)
372
- el.set(checked)
373
- rescue StandardError => e
374
- raise unless allow_label_click && catch_error?(e)
375
-
376
- begin
377
- el ||= find(selector, locator, **options.merge(visible: :all))
378
- el.session.find(:label, for: el, visible: true).click unless el.checked? == checked
379
- rescue StandardError # swallow extra errors - raise original
380
- raise e
374
+ el ||= find(selector, locator, **options.merge(visible: :all))
375
+ unless el.checked? == checked
376
+ el.session
377
+ .find(:label, for: el, visible: true, match: :first)
378
+ .click(**(Hash.try_convert(allow_label_click) || {}))
381
379
  end
380
+ rescue StandardError # swallow extra errors - raise original
381
+ raise e
382
382
  end
383
383
  end
384
384
  end
385
385
 
386
- UPDATE_STYLE_SCRIPT = <<~'JS'
386
+ UPDATE_STYLE_SCRIPT = <<~JS
387
387
  this.capybara_style_cache = this.style.cssText;
388
388
  var css = arguments[0];
389
389
  for (var prop in css){
@@ -393,27 +393,27 @@ module Capybara
393
393
  }
394
394
  JS
395
395
 
396
- RESET_STYLE_SCRIPT = <<~'JS'
396
+ RESET_STYLE_SCRIPT = <<~JS
397
397
  if (this.hasOwnProperty('capybara_style_cache')) {
398
398
  this.style.cssText = this.capybara_style_cache;
399
399
  delete this.capybara_style_cache;
400
400
  }
401
401
  JS
402
402
 
403
- DATALIST_OPTIONS_SCRIPT = <<~'JS'
403
+ DATALIST_OPTIONS_SCRIPT = <<~JS
404
404
  Array.prototype.slice.call((this.list||{}).options || []).
405
405
  filter(function(el){ return !el.disabled }).
406
406
  map(function(el){ return { "value": el.value, "label": el.label} })
407
407
  JS
408
408
 
409
- CAPTURE_FILE_ELEMENT_SCRIPT = <<~'JS'
409
+ CAPTURE_FILE_ELEMENT_SCRIPT = <<~JS
410
410
  document.addEventListener('click', function file_catcher(e){
411
411
  if (e.target.matches("input[type='file']")) {
412
412
  window._capybara_clicked_file_input = e.target;
413
413
  this.removeEventListener('click', file_catcher);
414
414
  e.preventDefault();
415
415
  }
416
- })
416
+ }, {capture: true})
417
417
  JS
418
418
  end
419
419
  end
@@ -77,6 +77,7 @@ module Capybara
77
77
  return yield if session.synchronized
78
78
 
79
79
  seconds = session_options.default_max_wait_time if [nil, true].include? seconds
80
+ interval = session_options.default_retry_interval
80
81
  session.synchronized = true
81
82
  timer = Capybara::Helpers.timer(expire_in: seconds)
82
83
  begin
@@ -88,7 +89,7 @@ module Capybara
88
89
  if driver.wait?
89
90
  raise e if timer.expired?
90
91
 
91
- sleep(0.01)
92
+ sleep interval
92
93
  reload if session_options.automatic_reload
93
94
  else
94
95
  old_base = @base
@@ -103,19 +104,19 @@ module Capybara
103
104
 
104
105
  # @api private
105
106
  def find_css(css, **options)
106
- if base.method(:find_css).arity != 1
107
- base.find_css(css, **options)
108
- else
107
+ if base.method(:find_css).arity == 1
109
108
  base.find_css(css)
109
+ else
110
+ base.find_css(css, **options)
110
111
  end
111
112
  end
112
113
 
113
114
  # @api private
114
115
  def find_xpath(xpath, **options)
115
- if base.method(:find_xpath).arity != 1
116
- base.find_xpath(xpath, **options)
117
- else
116
+ if base.method(:find_xpath).arity == 1
118
117
  base.find_xpath(xpath)
118
+ else
119
+ base.find_xpath(xpath, **options)
119
120
  end
120
121
  end
121
122
 
@@ -40,8 +40,8 @@ module Capybara
40
40
  find(:xpath, '/html').evaluate_script(*args)
41
41
  end
42
42
 
43
- def scroll_to(*args, **options)
44
- find(:xpath, '//body').scroll_to(*args, **options)
43
+ def scroll_to(*args, quirks: false, **options)
44
+ find(:xpath, quirks ? '//body' : '/html').scroll_to(*args, **options)
45
45
  end
46
46
  end
47
47
  end
@@ -115,7 +115,7 @@ module Capybara
115
115
  #
116
116
  # @return [Capybara::Node::Element] The element
117
117
  def set(value, **options)
118
- if ENV['CAPYBARA_THOROUGH'] && readonly?
118
+ if ENV.fetch('CAPYBARA_THOROUGH', nil) && readonly?
119
119
  raise Capybara::ReadOnlyElementError, "Attempt to set readonly element with value: #{value}"
120
120
  end
121
121
 
@@ -435,11 +435,7 @@ module Capybara
435
435
  #
436
436
  # @return [Capybara::Node::Element] The element
437
437
  def drop(*args)
438
- options = args.map do |arg|
439
- return arg.to_path if arg.respond_to?(:to_path)
440
-
441
- arg
442
- end
438
+ options = args.map { |arg| arg.respond_to?(:to_path) ? arg.to_path : arg }
443
439
  synchronize { base.drop(*options) }
444
440
  self
445
441
  end
@@ -476,6 +472,17 @@ module Capybara
476
472
  self
477
473
  end
478
474
 
475
+ ##
476
+ #
477
+ # Return the shadow_root for the current element
478
+ #
479
+ # @return [Capybara::Node::Element] The shadow root
480
+
481
+ def shadow_root
482
+ root = synchronize { base.shadow_root }
483
+ root && Capybara::Node::Element.new(session, root, nil, nil)
484
+ end
485
+
479
486
  ##
480
487
  #
481
488
  # Execute the given JS in the context of the element not returning a result. This is useful for scripts that return
@@ -554,7 +561,7 @@ module Capybara
554
561
  return self unless @allow_reload
555
562
 
556
563
  begin
557
- reloaded = @query.resolve_for(query_scope.reload)[@query_idx.to_i]
564
+ reloaded = @query.resolve_for(query_scope ? query_scope.reload : session)[@query_idx.to_i]
558
565
  @base = reloaded.base if reloaded
559
566
  rescue StandardError => e
560
567
  raise e unless catch_error?(e)
@@ -18,18 +18,19 @@ module Capybara
18
18
  #
19
19
  # @!macro system_filters
20
20
  # @option options [String, Regexp] text Only find elements which contain this text or match this regexp
21
- # @option options [String, Boolean] exact_text
21
+ # @option options [String, Regexp, String] exact_text
22
22
  # When String the elements contained text must match exactly, when Boolean controls whether the `text` option must match exactly.
23
23
  # Defaults to {Capybara.configure exact_text}.
24
24
  # @option options [Boolean] normalize_ws
25
- # Whether the `text`/`exact_text` options are compared against elment text with whitespace normalized or as returned by the driver.
25
+ # Whether the `text`/`exact_text` options are compared against element text with whitespace normalized or as returned by the driver.
26
26
  # Defaults to {Capybara.configure default_normalize_ws}.
27
- # @option options [Boolean, Symbol] visible Only find elements with the specified visibility:
28
- # * true - only finds visible elements.
29
- # * false - finds invisible _and_ visible elements.
30
- # * :all - same as false; finds visible and invisible elements.
31
- # * :hidden - only finds invisible elements.
32
- # * :visible - same as true; only finds visible elements.
27
+ # @option options [Boolean, Symbol] visible
28
+ # Only find elements with the specified visibility. Defaults to behavior indicated by {Capybara.configure ignore_hidden_elements}.
29
+ # * true - only finds visible elements.
30
+ # * false - finds invisible _and_ visible elements.
31
+ # * :all - same as false; finds visible and invisible elements.
32
+ # * :hidden - only finds invisible elements.
33
+ # * :visible - same as true; only finds visible elements.
33
34
  # @option options [Boolean] obscured Only find elements with the specified obscured state:
34
35
  # * true - only find elements whose centerpoint is not in the viewport or is obscured by another non-descendant element.
35
36
  # * false - only find elements whose centerpoint is in the viewport and is not obscured by other non-descendant elements.
@@ -49,6 +50,13 @@ module Capybara
49
50
  #
50
51
  def find(*args, **options, &optional_filter_block)
51
52
  options[:session_options] = session_options
53
+ count_options = options.slice(*Capybara::Queries::BaseQuery::COUNT_KEYS)
54
+ unless count_options.empty?
55
+ Capybara::Helpers.warn(
56
+ "'find' does not support count options (#{count_options}) ignoring. " \
57
+ "Called from: #{Capybara::Helpers.filter_backtrace(caller)}"
58
+ )
59
+ end
52
60
  synced_resolve Capybara::Queries::SelectorQuery.new(*args, **options, &optional_filter_block)
53
61
  end
54
62
 
@@ -141,6 +149,8 @@ module Capybara
141
149
  # @option options [String, Regexp] id Match links with the id provided
142
150
  # @option options [String] title Match links with the title provided
143
151
  # @option options [String] alt Match links with a contained img element whose alt matches
152
+ # @option options [String, Boolean] download Match links with the download provided
153
+ # @option options [String] target Match links with the target provided
144
154
  # @option options [String, Array<String>, Regexp] class Match links that match the class(es) provided
145
155
  # @return [Capybara::Node::Element] The found element
146
156
  #
@@ -60,15 +60,16 @@ module Capybara
60
60
  # @param styles [Hash]
61
61
  # @return [Boolean] If the styles match
62
62
  #
63
- def matches_style?(styles, **options)
63
+ def matches_style?(styles = nil, **options)
64
+ styles, options = options, {} if styles.nil?
64
65
  make_predicate(options) { assert_matches_style(styles, **options) }
65
66
  end
66
67
 
67
68
  ##
68
69
  # @deprecated Use {#matches_style?} instead.
69
70
  #
70
- def has_style?(styles, **options)
71
- warn 'DEPRECATED: has_style? is deprecated, please use matches_style?'
71
+ def has_style?(styles = nil, **options)
72
+ Capybara::Helpers.warn "DEPRECATED: has_style? is deprecated, please use matches_style? : #{Capybara::Helpers.filter_backtrace(caller)}"
72
73
  matches_style?(styles, **options)
73
74
  end
74
75
 
@@ -122,7 +123,8 @@ module Capybara
122
123
  # @param styles [Hash]
123
124
  # @raise [Capybara::ExpectationNotMet] If the element doesn't have the specified styles
124
125
  #
125
- def assert_matches_style(styles, **options)
126
+ def assert_matches_style(styles = nil, **options)
127
+ styles, options = options, {} if styles.nil?
126
128
  query_args, query_opts = _set_query_session_options(styles, options)
127
129
  query = Capybara::Queries::StyleQuery.new(*query_args, **query_opts)
128
130
  synchronize(query.wait) do
@@ -134,7 +136,7 @@ module Capybara
134
136
  ##
135
137
  # @deprecated Use {#assert_matches_style} instead.
136
138
  #
137
- def assert_style(styles, **options)
139
+ def assert_style(styles = nil, **options)
138
140
  warn 'assert_style is deprecated, please use assert_matches_style instead'
139
141
  assert_matches_style(styles, **options)
140
142
  end
@@ -201,12 +203,10 @@ module Capybara
201
203
  selector = extract_selector(args)
202
204
  synchronize(wait) do
203
205
  res = args.map do |locator|
204
- begin
205
- assert_selector(selector, locator, options, &optional_filter_block)
206
- break nil
207
- rescue Capybara::ExpectationNotMet => e
208
- e.message
209
- end
206
+ assert_selector(selector, locator, options, &optional_filter_block)
207
+ break nil
208
+ rescue Capybara::ExpectationNotMet => e
209
+ e.message
210
210
  end
211
211
  raise Capybara::ExpectationNotMet, res.join(' or ') if res
212
212
 
@@ -386,7 +386,7 @@ module Capybara
386
386
  #
387
387
  # page.has_field?('Email', type: 'email')
388
388
  #
389
- # Note: 'textarea' and 'select' are valid type values, matching the associated tag names.
389
+ # NOTE: 'textarea' and 'select' are valid type values, matching the associated tag names.
390
390
  #
391
391
  # @param [String] locator The label, name or id of a field to check for
392
392
  # @option options [String, Regexp] :with The text content of the field or a Regexp to match
@@ -100,7 +100,7 @@ module Capybara
100
100
  # @param [Boolean] check_ancestors Whether to inherit visibility from ancestors
101
101
  # @return [Boolean] Whether the element is visible
102
102
  #
103
- def visible?(check_ancestors = true)
103
+ def visible?(check_ancestors = true) # rubocop:disable Style/OptionalBooleanParameter
104
104
  return false if (tag_name == 'input') && (native[:type] == 'hidden')
105
105
  return false if tag_name == 'template'
106
106
 
@@ -110,7 +110,7 @@ module Capybara
110
110
  # No need for an xpath if only checking the current element
111
111
  !(native.key?('hidden') ||
112
112
  /display:\s?none/.match?(native[:style] || '') ||
113
- %w[script head].include?(tag_name))
113
+ %w[script head style].include?(tag_name))
114
114
  end
115
115
  end
116
116
 
@@ -148,6 +148,10 @@ module Capybara
148
148
  native.has_attribute?('multiple')
149
149
  end
150
150
 
151
+ def readonly?
152
+ native.has_attribute?('readonly')
153
+ end
154
+
151
155
  def synchronize(_seconds = nil)
152
156
  yield # simple nodes don't need to wait
153
157
  end
@@ -187,6 +191,10 @@ module Capybara
187
191
  {}
188
192
  end
189
193
 
194
+ def ==(other)
195
+ eql?(other) || (other.respond_to?(:native) && native == other.native)
196
+ end
197
+
190
198
  private
191
199
 
192
200
  def option_value(option)
@@ -0,0 +1,81 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Capybara
4
+ module Node
5
+ ##
6
+ #
7
+ # {Capybara::Node::WhitespaceNormalizer} provides methods that
8
+ # help to normalize the spacing of text content inside of
9
+ # {Capybara::Node::Element}s by removing various unicode
10
+ # spacing and directional markings.
11
+ #
12
+ module WhitespaceNormalizer
13
+ # Unicode for NBSP, or &nbsp;
14
+ NON_BREAKING_SPACE = "\u00a0"
15
+ LINE_SEPERATOR = "\u2028"
16
+ PARAGRAPH_SEPERATOR = "\u2029"
17
+
18
+ # All spaces except for NBSP
19
+ BREAKING_SPACES = "[[:space:]&&[^#{NON_BREAKING_SPACE}]]"
20
+
21
+ # Whitespace we want to substitute with plain spaces
22
+ SQUEEZED_SPACES = " \n\f\t\v#{LINE_SEPERATOR}#{PARAGRAPH_SEPERATOR}"
23
+
24
+ # Any whitespace at the front of text
25
+ LEADING_SPACES = /\A#{BREAKING_SPACES}+/.freeze
26
+
27
+ # Any whitespace at the end of text
28
+ TRAILING_SPACES = /#{BREAKING_SPACES}+\z/.freeze
29
+
30
+ # "Invisible" space character
31
+ ZERO_WIDTH_SPACE = "\u200b"
32
+
33
+ # Signifies text is read left to right
34
+ LEFT_TO_RIGHT_MARK = "\u200e"
35
+
36
+ # Signifies text is read right to left
37
+ RIGHT_TO_LEFT_MARK = "\u200f"
38
+
39
+ # Characters we want to truncate from text
40
+ REMOVED_CHARACTERS = [ZERO_WIDTH_SPACE, LEFT_TO_RIGHT_MARK, RIGHT_TO_LEFT_MARK].join
41
+
42
+ # Matches multiple empty lines
43
+ EMPTY_LINES = /[\ \n]*\n[\ \n]*/.freeze
44
+
45
+ ##
46
+ #
47
+ # Normalizes the spacing of a node's text to be similar to
48
+ # what matchers might expect.
49
+ #
50
+ # @param text [String]
51
+ # @return [String]
52
+ #
53
+ def normalize_spacing(text)
54
+ text
55
+ .delete(REMOVED_CHARACTERS)
56
+ .tr(SQUEEZED_SPACES, ' ')
57
+ .squeeze(' ')
58
+ .sub(LEADING_SPACES, '')
59
+ .sub(TRAILING_SPACES, '')
60
+ .tr(NON_BREAKING_SPACE, ' ')
61
+ end
62
+
63
+ ##
64
+ #
65
+ # Variant on {Capybara::Node::Normalizer#normalize_spacing} that
66
+ # targets the whitespace of visible elements only.
67
+ #
68
+ # @param text [String]
69
+ # @return [String]
70
+ #
71
+ def normalize_visible_spacing(text)
72
+ text
73
+ .squeeze(' ')
74
+ .gsub(EMPTY_LINES, "\n")
75
+ .sub(LEADING_SPACES, '')
76
+ .sub(TRAILING_SPACES, '')
77
+ .tr(NON_BREAKING_SPACE, ' ')
78
+ end
79
+ end
80
+ end
81
+ end
@@ -0,0 +1,18 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Capybara
4
+ # @api private
5
+ module Queries
6
+ class ActiveElementQuery < BaseQuery
7
+ def initialize(**options)
8
+ @options = options
9
+ super(@options)
10
+ end
11
+
12
+ def resolve_for(session)
13
+ node = session.driver.active_element
14
+ [Capybara::Node::Element.new(session, node, nil, self)]
15
+ end
16
+ end
17
+ end
18
+ end
@@ -8,7 +8,8 @@ module Capybara
8
8
  @child_node = node
9
9
 
10
10
  node.synchronize do
11
- match_results = super(node.session.current_scope, exact)
11
+ scope = node.respond_to?(:session) ? node.session.current_scope : node.find(:xpath, '/*')
12
+ match_results = super(scope, exact)
12
13
  ancestors = node.find_xpath(XPath.ancestor.to_s)
13
14
  .map(&method(:to_element))
14
15
  .select { |el| match_results.include?(el) }
@@ -16,7 +17,7 @@ module Capybara
16
17
  end
17
18
  end
18
19
 
19
- def description(applied = false)
20
+ def description(applied = false) # rubocop:disable Style/OptionalBooleanParameter
20
21
  child_query = @child_node&.instance_variable_get(:@query)
21
22
  desc = super
22
23
  desc += " that is an ancestor of #{child_query.description}" if child_query
@@ -79,8 +79,8 @@ module Capybara
79
79
  if count
80
80
  message << " #{occurrences count}"
81
81
  elsif between
82
- message << " between #{between.begin ? between.first : 1} and" \
83
- " #{between.end ? between.last : 'infinite'} times"
82
+ message << " between #{between.begin ? between.first : 1} and " \
83
+ "#{between.end ? between.last : 'infinite'} times"
84
84
  elsif maximum
85
85
  message << " at most #{occurrences maximum}"
86
86
  elsif minimum
@@ -6,26 +6,30 @@ module Capybara
6
6
  # @api private
7
7
  module Queries
8
8
  class CurrentPathQuery < BaseQuery
9
- def initialize(expected_path, **options)
9
+ def initialize(expected_path, **options, &optional_filter_block)
10
10
  super(options)
11
11
  @expected_path = expected_path
12
12
  @options = {
13
13
  url: !@expected_path.is_a?(Regexp) && !::Addressable::URI.parse(@expected_path || '').hostname.nil?,
14
14
  ignore_query: false
15
15
  }.merge(options)
16
+ @filter_block = optional_filter_block
16
17
  assert_valid_keys
17
18
  end
18
19
 
19
20
  def resolves_for?(session)
20
21
  uri = ::Addressable::URI.parse(session.current_url)
21
- uri&.query = nil if options[:ignore_query]
22
- @actual_path = options[:url] ? uri&.to_s : uri&.request_uri
22
+ @actual_path = (options[:ignore_query] ? uri&.omit(:query) : uri).then do |u|
23
+ options[:url] ? u&.to_s : u&.request_uri
24
+ end
23
25
 
24
- if @expected_path.is_a? Regexp
26
+ res = if @expected_path.is_a? Regexp
25
27
  @actual_path.to_s.match?(@expected_path)
26
28
  else
27
29
  ::Addressable::URI.parse(@expected_path) == ::Addressable::URI.parse(@actual_path)
28
30
  end
31
+
32
+ res && matches_filter_block?(uri)
29
33
  end
30
34
 
31
35
  def failure_message
@@ -38,6 +42,12 @@ module Capybara
38
42
 
39
43
  private
40
44
 
45
+ def matches_filter_block?(url)
46
+ return true unless @filter_block
47
+
48
+ @filter_block.call(url)
49
+ end
50
+
41
51
  def failure_message_helper(negated = '')
42
52
  verb = @expected_path.is_a?(Regexp) ? 'match' : 'equal'
43
53
  "expected #{@actual_path.inspect}#{negated} to #{verb} #{@expected_path.inspect}"