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
@@ -6,14 +6,16 @@ module Capybara
6
6
  module Queries
7
7
  class SelectorQuery < Queries::BaseQuery
8
8
  attr_reader :expression, :selector, :locator, :options
9
+
9
10
  SPATIAL_KEYS = %i[above below left_of right_of near].freeze
10
11
  VALID_KEYS = SPATIAL_KEYS + COUNT_KEYS +
11
- %i[text id class style visible obscured exact exact_text normalize_ws match wait filter_set]
12
+ %i[text id class style visible obscured exact exact_text normalize_ws match wait filter_set focused]
12
13
  VALID_MATCH = %i[first smart prefer_exact one].freeze
13
14
 
14
15
  def initialize(*args,
15
16
  session_options:,
16
17
  enable_aria_label: session_options.enable_aria_label,
18
+ enable_aria_role: session_options.enable_aria_role,
17
19
  test_id: session_options.test_id,
18
20
  selector_format: nil,
19
21
  order: nil,
@@ -25,12 +27,22 @@ module Capybara
25
27
  @order = order
26
28
  @filter_cache = Hash.new { |hsh, key| hsh[key] = {} }
27
29
 
30
+ if @options[:text].is_a?(Regexp) && [true, false].include?(@options[:exact_text])
31
+ Capybara::Helpers.warn(
32
+ "Boolean 'exact_text' option is not supported when 'text' option is a Regexp - ignoring"
33
+ )
34
+ end
35
+
28
36
  super(@options)
29
37
  self.session_options = session_options
30
38
 
31
39
  @selector = Selector.new(
32
40
  find_selector(args[0].is_a?(Symbol) ? args.shift : args[0]),
33
- config: { enable_aria_label: enable_aria_label, test_id: test_id },
41
+ config: {
42
+ enable_aria_label: enable_aria_label,
43
+ enable_aria_role: enable_aria_role,
44
+ test_id: test_id
45
+ },
34
46
  format: selector_format
35
47
  )
36
48
 
@@ -49,7 +61,7 @@ module Capybara
49
61
  def name; selector.name; end
50
62
  def label; selector.label || selector.name; end
51
63
 
52
- def description(only_applied = false)
64
+ def description(only_applied = false) # rubocop:disable Style/OptionalBooleanParameter
53
65
  desc = +''
54
66
  show_for = show_for_stage(only_applied)
55
67
 
@@ -67,6 +79,8 @@ module Capybara
67
79
 
68
80
  desc << " with id #{options[:id]}" if options[:id]
69
81
  desc << " with classes [#{Array(options[:class]).join(',')}]" if options[:class]
82
+ desc << ' that is focused' if options[:focused]
83
+ desc << ' that is not focused' if options[:focused] == false
70
84
 
71
85
  desc << case options[:style]
72
86
  when String
@@ -89,11 +103,9 @@ module Capybara
89
103
  desc << ' that also matches the custom filter block' if @filter_block && show_for[:node]
90
104
 
91
105
  desc << " within #{@resolved_node.inspect}" if describe_within?
92
- if locator.is_a?(String) && locator.start_with?('#', './/', '//')
93
- unless selector.raw_locator?
94
- desc << "\nNote: It appears you may be passing a CSS selector or XPath expression rather than a locator. " \
95
- "Please see the documentation for acceptable locator values.\n\n"
96
- end
106
+ if locator.is_a?(String) && locator.start_with?('#', './/', '//') && !selector.raw_locator?
107
+ desc << "\nNote: It appears you may be passing a CSS selector or XPath expression rather than a locator. " \
108
+ "Please see the documentation for acceptable locator values.\n\n"
97
109
  end
98
110
  desc
99
111
  end
@@ -233,17 +245,18 @@ module Capybara
233
245
  hints[:styles] = options[:style] if use_default_style_filter?
234
246
  hints[:position] = true if use_spatial_filter?
235
247
 
236
- if selector_format == :css
237
- if node.method(:find_css).arity != 1
238
- node.find_css(css, **hints)
239
- else
248
+ case selector_format
249
+ when :css
250
+ if node.method(:find_css).arity == 1
240
251
  node.find_css(css)
241
- end
242
- elsif selector_format == :xpath
243
- if node.method(:find_xpath).arity != 1
244
- node.find_xpath(xpath(exact), **hints)
245
252
  else
253
+ node.find_css(css, **hints)
254
+ end
255
+ when :xpath
256
+ if node.method(:find_xpath).arity == 1
246
257
  node.find_xpath(xpath(exact))
258
+ else
259
+ node.find_xpath(xpath(exact), **hints)
247
260
  end
248
261
  else
249
262
  raise ArgumentError, "Unknown format: #{selector_format}"
@@ -259,7 +272,7 @@ module Capybara
259
272
  end
260
273
 
261
274
  def valid_keys
262
- VALID_KEYS + custom_keys
275
+ (VALID_KEYS + custom_keys).uniq
263
276
  end
264
277
 
265
278
  def matches_node_filters?(node, errors)
@@ -366,6 +379,10 @@ module Capybara
366
379
  options.key?(:style) && !custom_keys.include?(:style)
367
380
  end
368
381
 
382
+ def use_default_focused_filter?
383
+ options.key?(:focused) && !custom_keys.include?(:focused)
384
+ end
385
+
369
386
  def use_spatial_filter?
370
387
  options.values_at(*SPATIAL_KEYS).compact.any?
371
388
  end
@@ -430,6 +447,7 @@ module Capybara
430
447
  matches_id_filter?(node) &&
431
448
  matches_class_filter?(node) &&
432
449
  matches_style_filter?(node) &&
450
+ matches_focused_filter?(node) &&
433
451
  matches_text_filter?(node) &&
434
452
  matches_exact_text_filter?(node)
435
453
  end
@@ -477,9 +495,31 @@ module Capybara
477
495
  end
478
496
 
479
497
  def matches_class_filter?(node)
480
- return true unless use_default_class_filter? && options[:class].is_a?(Regexp)
498
+ return true unless use_default_class_filter? && need_to_process_classes?
499
+
500
+ if options[:class].is_a? Regexp
501
+ options[:class].match? node[:class]
502
+ else
503
+ classes = (node[:class] || '').split
504
+ options[:class].select { |c| c.is_a? Regexp }.all? do |r|
505
+ classes.any? { |cls| r.match? cls }
506
+ end
507
+ end
508
+ end
509
+
510
+ def matches_focused_filter?(node)
511
+ return true unless use_default_focused_filter?
481
512
 
482
- options[:class].match? node[:class]
513
+ (node == node.session.active_element) == options[:focused]
514
+ end
515
+
516
+ def need_to_process_classes?
517
+ case options[:class]
518
+ when Regexp then true
519
+ when Array then options[:class].any?(Regexp)
520
+ else
521
+ false
522
+ end
483
523
  end
484
524
 
485
525
  def matches_style_filter?(node)
@@ -507,16 +547,19 @@ module Capybara
507
547
  def matches_text_filter?(node)
508
548
  value = options[:text]
509
549
  return true unless value
510
- return matches_text_exactly?(node, value) if exact_text == true
550
+ return matches_text_exactly?(node, value) if exact_text == true && !value.is_a?(Regexp)
511
551
 
512
552
  regexp = value.is_a?(Regexp) ? value : Regexp.escape(value.to_s)
513
553
  matches_text_regexp?(node, regexp)
514
554
  end
515
555
 
516
556
  def matches_exact_text_filter?(node)
517
- return true unless exact_text.is_a?(String)
518
-
519
- matches_text_exactly?(node, exact_text)
557
+ case exact_text
558
+ when String, Regexp
559
+ matches_text_exactly?(node, exact_text)
560
+ else
561
+ true
562
+ end
520
563
  end
521
564
 
522
565
  def matches_visibility_filters?(node)
@@ -527,7 +570,9 @@ module Capybara
527
570
  when :visible
528
571
  node.initial_cache[:visible] || (node.initial_cache[:visible].nil? && node.visible?)
529
572
  when :hidden
530
- (node.initial_cache[:visible] == false) || (node.initial_cache[:visbile].nil? && !node.visible?)
573
+ # TODO: check why the 'visbile' cache spelling mistake wasn't caught in a test
574
+ # (node.initial_cache[:visible] == false) || (node.initial_cache[:visbile].nil? && !node.visible?)
575
+ (node.initial_cache[:visible] == false) || (node.initial_cache[:visible].nil? && !node.visible?)
531
576
  else
532
577
  true
533
578
  end
@@ -544,17 +589,21 @@ module Capybara
544
589
 
545
590
  def matches_text_exactly?(node, value)
546
591
  regexp = value.is_a?(Regexp) ? value : /\A#{Regexp.escape(value.to_s)}\z/
547
- matches_text_regexp?(node, regexp)
592
+ matches_text_regexp(node, regexp).then { |m| m&.pre_match == '' && m&.post_match == '' }
548
593
  end
549
594
 
550
595
  def normalize_ws
551
596
  options.fetch(:normalize_ws, session_options.default_normalize_ws)
552
597
  end
553
598
 
554
- def matches_text_regexp?(node, regexp)
599
+ def matches_text_regexp(node, regexp)
555
600
  text_visible = visible
556
601
  text_visible = :all if text_visible == :hidden
557
- node.text(text_visible, normalize_ws: normalize_ws).match?(regexp)
602
+ node.text(text_visible, normalize_ws: normalize_ws).match(regexp)
603
+ end
604
+
605
+ def matches_text_regexp?(node, regexp)
606
+ !matches_text_regexp(node, regexp).nil?
558
607
  end
559
608
 
560
609
  def default_visibility
@@ -575,6 +624,7 @@ module Capybara
575
624
 
576
625
  class Rectangle
577
626
  attr_reader :top, :bottom, :left, :right
627
+
578
628
  def initialize(position)
579
629
  # rubocop:disable Style/RescueModifier
580
630
  @top = position['top'] rescue position['y']
@@ -645,7 +695,7 @@ module Capybara
645
695
 
646
696
  d = u.dot w
647
697
  e = v.dot w
648
- cap_d = (a * c) - (b * b)
698
+ cap_d = (a * c) - (b**2)
649
699
  sD = tD = cap_d
650
700
 
651
701
  # compute the line parameters of the two closest points
@@ -7,7 +7,8 @@ module Capybara
7
7
  def resolve_for(node, exact = nil)
8
8
  @sibling_node = node
9
9
  node.synchronize do
10
- match_results = super(node.session.current_scope, exact)
10
+ scope = node.respond_to?(:session) ? node.session.current_scope : node.find(:xpath, '/*')
11
+ match_results = super(scope, exact)
11
12
  siblings = node.find_xpath((XPath.preceding_sibling + XPath.following_sibling).to_s)
12
13
  .map(&method(:to_element))
13
14
  .select { |el| match_results.include?(el) }
@@ -15,7 +16,7 @@ module Capybara
15
16
  end
16
17
  end
17
18
 
18
- def description(applied = false)
19
+ def description(applied = false) # rubocop:disable Style/OptionalBooleanParameter
19
20
  desc = super
20
21
  sibling_query = @sibling_node&.instance_variable_get(:@query)
21
22
  desc += " that is a sibling of #{sibling_query.description}" if sibling_query
@@ -34,7 +34,7 @@ module Capybara
34
34
  private
35
35
 
36
36
  def stringify_keys(hsh)
37
- hsh.each_with_object({}) { |(k, v), str_keys| str_keys[k.to_s] = v }
37
+ hsh.transform_keys(&:to_s)
38
38
  end
39
39
 
40
40
  def valid_keys
@@ -6,13 +6,15 @@ module Capybara
6
6
  class TextQuery < BaseQuery
7
7
  def initialize(type = nil, expected_text, session_options:, **options) # rubocop:disable Style/OptionalArguments
8
8
  @type = type.nil? ? default_type : type
9
+ raise ArgumentError, "#{@type} is not a valid type for a text query" unless valid_types.include?(@type)
10
+
9
11
  @options = options
10
12
  super(@options)
11
13
  self.session_options = session_options
12
14
 
13
15
  if expected_text.nil? && !exact?
14
- warn 'Checking for expected text of nil is confusing and/or pointless since it will always match. '\
15
- 'Please specify a string or regexp instead.'
16
+ warn 'Checking for expected text of nil is confusing and/or pointless since it will always match. ' \
17
+ "Please specify a string or regexp instead. #{Capybara::Helpers.filter_backtrace(caller)}"
16
18
  end
17
19
 
18
20
  @expected_text = expected_text.is_a?(Regexp) ? expected_text : expected_text.to_s
@@ -89,6 +91,10 @@ module Capybara
89
91
  COUNT_KEYS + %i[wait exact normalize_ws]
90
92
  end
91
93
 
94
+ def valid_types
95
+ %i[all visible]
96
+ end
97
+
92
98
  def check_visible_text?
93
99
  @type == :visible
94
100
  end
@@ -8,6 +8,7 @@ class Capybara::RackTest::Browser
8
8
 
9
9
  def initialize(driver)
10
10
  @driver = driver
11
+ @current_fragment = nil
11
12
  end
12
13
 
13
14
  def app
@@ -19,6 +20,8 @@ class Capybara::RackTest::Browser
19
20
  end
20
21
 
21
22
  def visit(path, **attributes)
23
+ @new_visit_request = true
24
+ reset_cache!
22
25
  reset_host!
23
26
  process_and_follow_redirects(:get, path, attributes)
24
27
  end
@@ -28,22 +31,28 @@ class Capybara::RackTest::Browser
28
31
  request(last_request.fullpath, last_request.env)
29
32
  end
30
33
 
31
- def submit(method, path, attributes)
34
+ def submit(method, path, attributes, content_type: nil)
32
35
  path = request_path if path.nil? || path.empty?
33
36
  uri = build_uri(path)
34
- uri.query = '' if method&.to_s&.downcase == 'get'
35
- process_and_follow_redirects(method, uri.to_s, attributes, 'HTTP_REFERER' => current_url)
37
+ uri.query = '' if method.to_s.casecmp('get').zero?
38
+ process_and_follow_redirects(
39
+ method,
40
+ uri.to_s,
41
+ attributes,
42
+ 'HTTP_REFERER' => referer_url,
43
+ 'CONTENT_TYPE' => content_type
44
+ )
36
45
  end
37
46
 
38
47
  def follow(method, path, **attributes)
39
48
  return if fragment_or_script?(path)
40
49
 
41
- process_and_follow_redirects(method, path, attributes, 'HTTP_REFERER' => current_url)
50
+ process_and_follow_redirects(method, path, attributes, 'HTTP_REFERER' => referer_url)
42
51
  end
43
52
 
44
53
  def process_and_follow_redirects(method, path, attributes = {}, env = {})
54
+ @current_fragment = build_uri(path).fragment
45
55
  process(method, path, attributes, env)
46
-
47
56
  return unless driver.follow_redirects?
48
57
 
49
58
  driver.redirect_limit.times do
@@ -65,25 +74,32 @@ class Capybara::RackTest::Browser
65
74
  method = method.downcase
66
75
  new_uri = build_uri(path)
67
76
  @current_scheme, @current_host, @current_port = new_uri.select(:scheme, :host, :port)
68
-
77
+ @current_fragment = new_uri.fragment || @current_fragment
69
78
  reset_cache!
79
+ @new_visit_request = false
70
80
  send(method, new_uri.to_s, attributes, env.merge(options[:headers] || {}))
71
81
  end
72
82
 
73
83
  def build_uri(path)
74
- URI.parse(path).tap do |uri|
75
- uri.path = request_path if path.empty? || path.start_with?('?')
76
- uri.path = '/' if uri.path.empty?
77
- uri.path = request_path.sub(%r{/[^/]*$}, '/') + uri.path unless uri.path.start_with?('/')
84
+ uri = URI.parse(path)
85
+ base_uri = base_relative_uri_for(uri)
86
+
87
+ uri.path = base_uri.path + uri.path unless uri.absolute? || uri.path.start_with?('/')
78
88
 
89
+ if base_uri.absolute?
90
+ base_uri.merge(uri)
91
+ else
79
92
  uri.scheme ||= @current_scheme
80
93
  uri.host ||= @current_host
81
94
  uri.port ||= @current_port unless uri.default_port == @current_port
95
+ uri
82
96
  end
83
97
  end
84
98
 
85
99
  def current_url
86
- last_request.url
100
+ uri = build_uri(last_request.url)
101
+ uri.fragment = @current_fragment if @current_fragment
102
+ uri.to_s
87
103
  rescue Rack::Test::Error
88
104
  ''
89
105
  end
@@ -119,8 +135,39 @@ class Capybara::RackTest::Browser
119
135
  dom.title
120
136
  end
121
137
 
138
+ def last_request
139
+ raise Rack::Test::Error if @new_visit_request
140
+
141
+ super
142
+ end
143
+
144
+ def last_response
145
+ raise Rack::Test::Error if @new_visit_request
146
+
147
+ super
148
+ end
149
+
122
150
  protected
123
151
 
152
+ def base_href
153
+ find(:css, 'head > base').first&.[](:href).to_s
154
+ end
155
+
156
+ def base_relative_uri_for(uri)
157
+ base_uri = URI.parse(base_href)
158
+ current_uri = URI.parse(safe_last_request&.url.to_s).tap do |c|
159
+ c.path.sub!(%r{/[^/]*$}, '/') unless uri.path.empty?
160
+ c.path = '/' if c.path.empty?
161
+ end
162
+
163
+ if [current_uri, base_uri].any?(&:absolute?)
164
+ current_uri.merge(base_uri)
165
+ else
166
+ base_uri.path = current_uri.path if base_uri.path.empty?
167
+ base_uri
168
+ end
169
+ end
170
+
124
171
  def build_rack_mock_session
125
172
  reset_host! unless current_host
126
173
  Rack::MockSession.new(app, current_host)
@@ -132,9 +179,21 @@ protected
132
179
  '/'
133
180
  end
134
181
 
182
+ def safe_last_request
183
+ last_request
184
+ rescue Rack::Test::Error
185
+ nil
186
+ end
187
+
135
188
  private
136
189
 
137
190
  def fragment_or_script?(path)
138
191
  path.gsub(/^#{Regexp.escape(request_path)}/, '').start_with?('#') || path.downcase.start_with?('javascript:')
139
192
  end
193
+
194
+ def referer_url
195
+ build_uri(last_request.url).to_s
196
+ rescue Rack::Test::Error
197
+ ''
198
+ end
140
199
  end
@@ -17,6 +17,7 @@ class Capybara::RackTest::Driver < Capybara::Driver::Base
17
17
  def initialize(app, **options)
18
18
  raise ArgumentError, 'rack-test requires a rack application, but none was given' unless app
19
19
 
20
+ super()
20
21
  @app = app
21
22
  @options = DEFAULT_OPTIONS.merge(options)
22
23
  end
@@ -97,10 +98,10 @@ class Capybara::RackTest::Driver < Capybara::Driver::Base
97
98
  @browser = nil
98
99
  end
99
100
 
100
- def get(*args, &block); browser.get(*args, &block); end
101
- def post(*args, &block); browser.post(*args, &block); end
102
- def put(*args, &block); browser.put(*args, &block); end
103
- def delete(*args, &block); browser.delete(*args, &block); end
101
+ def get(...); browser.get(...); end
102
+ def post(...); browser.post(...); end
103
+ def put(...); browser.put(...); end
104
+ def delete(...); browser.delete(...); end
104
105
  def header(key, value); browser.header(key, value); end
105
106
 
106
107
  def invalid_element_errors
@@ -6,7 +6,7 @@ class Capybara::RackTest::Form < Capybara::RackTest::Node
6
6
  # That check should be based solely on the form element's 'enctype' attribute value,
7
7
  # which should probably be provided to Rack::Test in its non-GET request methods.
8
8
  class NilUploadedFile < Rack::Test::UploadedFile
9
- def initialize
9
+ def initialize # rubocop:disable Lint/MissingSuper
10
10
  @empty_file = Tempfile.new('nil_uploaded_file')
11
11
  @empty_file.close
12
12
  end
@@ -16,6 +16,8 @@ class Capybara::RackTest::Form < Capybara::RackTest::Node
16
16
  def path; @empty_file.path; end
17
17
  def size; 0; end
18
18
  def read; ''; end
19
+ def append_to(_); end
20
+ def set_encoding(_); end # rubocop:disable Naming/AccessorMethodName
19
21
  end
20
22
 
21
23
  def params(button)
@@ -28,19 +30,31 @@ class Capybara::RackTest::Form < Capybara::RackTest::Node
28
30
 
29
31
  form_elements = native.xpath(form_elements_xpath).reject { |el| submitter?(el) && (el != button.native) }
30
32
 
31
- form_elements.each_with_object(make_params) do |field, params|
33
+ form_params = form_elements.each_with_object({}.compare_by_identity) do |field, params|
32
34
  case field.name
33
35
  when 'input', 'button' then add_input_param(field, params)
34
36
  when 'select' then add_select_param(field, params)
35
37
  when 'textarea' then add_textarea_param(field, params)
36
38
  end
39
+ end
40
+
41
+ form_params.each_with_object(make_params) do |(name, value), params|
42
+ merge_param!(params, name, value)
37
43
  end.to_params_hash
44
+
45
+ # form_elements.each_with_object(make_params) do |field, params|
46
+ # case field.name
47
+ # when 'input', 'button' then add_input_param(field, params)
48
+ # when 'select' then add_select_param(field, params)
49
+ # when 'textarea' then add_textarea_param(field, params)
50
+ # end
51
+ # end.to_params_hash
38
52
  end
39
53
 
40
54
  def submit(button)
41
55
  action = button&.[]('formaction') || native['action']
42
56
  method = button&.[]('formmethod') || request_method
43
- driver.submit(method, action.to_s, params(button))
57
+ driver.submit(method, action.to_s, params(button), content_type: native['enctype'])
44
58
  end
45
59
 
46
60
  def multipart?
@@ -86,6 +100,8 @@ private
86
100
 
87
101
  Capybara::RackTest::Node.new(driver, field).value.to_s
88
102
  when 'file'
103
+ return if value.empty? && params.keys.include?(name) && Rack::Test::VERSION.to_f >= 2.0 # rubocop:disable Performance/InefficientHashSearch
104
+
89
105
  if multipart?
90
106
  file_to_upload(value)
91
107
  else
@@ -94,7 +110,8 @@ private
94
110
  else
95
111
  value
96
112
  end
97
- merge_param!(params, name, value)
113
+ # merge_param!(params, name, value)
114
+ params[name] = value
98
115
  end
99
116
 
100
117
  def file_to_upload(filename)
@@ -107,18 +124,23 @@ private
107
124
  end
108
125
 
109
126
  def add_select_param(field, params)
127
+ name = field['name']
110
128
  if field.has_attribute?('multiple')
111
- field.xpath('.//option[@selected]').each do |option|
112
- merge_param!(params, field['name'], (option['value'] || option.text).to_s)
129
+ value = field.xpath('.//option[@selected]').map do |option|
130
+ # merge_param!(params, field['name'], (option['value'] || option.text).to_s)
131
+ (option['value'] || option.text).to_s
113
132
  end
133
+ params[name] = value unless value.empty?
114
134
  else
115
135
  option = field.xpath('.//option[@selected]').first || field.xpath('.//option').first
116
- merge_param!(params, field['name'], (option['value'] || option.text).to_s) if option
136
+ # merge_param!(params, field['name'], (option['value'] || option.text).to_s) if option
137
+ params[name] = (option['value'] || option.text).to_s if option
117
138
  end
118
139
  end
119
140
 
120
141
  def add_textarea_param(field, params)
121
- merge_param!(params, field['name'], field['_capybara_raw_value'].to_s.gsub(/\n/, "\r\n"))
142
+ # merge_param!(params, field['name'], field['_capybara_raw_value'].to_s.gsub(/\r?\n/, "\r\n"))
143
+ params[field['name']] = field['_capybara_raw_value'].to_s.gsub(/\r?\n/, "\r\n")
122
144
  end
123
145
 
124
146
  def submitter?(el)
@@ -1,25 +1,19 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  require 'capybara/rack_test/errors'
4
+ require 'capybara/node/whitespace_normalizer'
4
5
 
5
6
  class Capybara::RackTest::Node < Capybara::Driver::Node
7
+ include Capybara::Node::WhitespaceNormalizer
8
+
6
9
  BLOCK_ELEMENTS = %w[p h1 h2 h3 h4 h5 h6 ol ul pre address blockquote dl div fieldset form hr noscript table].freeze
7
10
 
8
11
  def all_text
9
- native.text
10
- .gsub(/[\u200b\u200e\u200f]/, '')
11
- .gsub(/[\ \n\f\t\v\u2028\u2029]+/, ' ')
12
- .gsub(/\A[[:space:]&&[^\u00a0]]+/, '')
13
- .gsub(/[[:space:]&&[^\u00a0]]+\z/, '')
14
- .tr("\u00a0", ' ')
12
+ normalize_spacing(native.text)
15
13
  end
16
14
 
17
15
  def visible_text
18
- displayed_text.gsub(/\ +/, ' ')
19
- .gsub(/[\ \n]*\n[\ \n]*/, "\n")
20
- .gsub(/\A[[:space:]&&[^\u00a0]]+/, '')
21
- .gsub(/[[:space:]&&[^\u00a0]]+\z/, '')
22
- .tr("\u00a0", ' ')
16
+ normalize_visible_spacing(displayed_text)
23
17
  end
24
18
 
25
19
  def [](name)
@@ -108,6 +102,13 @@ class Capybara::RackTest::Node < Capybara::Driver::Node
108
102
  end
109
103
  end
110
104
 
105
+ def readonly?
106
+ # readonly attribute not valid on these input types
107
+ return false if input_field? && %w[hidden range color checkbox radio file submit image reset button].include?(type)
108
+
109
+ super
110
+ end
111
+
111
112
  def path
112
113
  native.path
113
114
  end
@@ -139,10 +140,6 @@ class Capybara::RackTest::Node < Capybara::Driver::Node
139
140
  end
140
141
  end
141
142
 
142
- def ==(other)
143
- native == other.native
144
- end
145
-
146
143
  protected
147
144
 
148
145
  # @api private
@@ -150,16 +147,18 @@ protected
150
147
  if !string_node.visible?(check_ancestor)
151
148
  ''
152
149
  elsif native.text?
153
- native.text
154
- .gsub(/[\u200b\u200e\u200f]/, '')
155
- .gsub(/[\ \n\f\t\v\u2028\u2029]+/, ' ')
150
+ native
151
+ .text
152
+ .delete(REMOVED_CHARACTERS)
153
+ .tr(SQUEEZED_SPACES, ' ')
154
+ .squeeze(' ')
156
155
  elsif native.element?
157
156
  text = native.children.map do |child|
158
157
  Capybara::RackTest::Node.new(driver, child).displayed_text(check_ancestor: false)
159
158
  end.join || ''
160
159
  text = "\n#{text}\n" if BLOCK_ELEMENTS.include?(tag_name)
161
160
  text
162
- else
161
+ else # rubocop:disable Lint/DuplicateBranch
163
162
  ''
164
163
  end
165
164
  end
@@ -213,7 +212,7 @@ private
213
212
  min, max, step = (native['min'] || 0).to_f, (native['max'] || 100).to_f, (native['step'] || 1).to_f
214
213
  value = value.to_f
215
214
  value = value.clamp(min, max)
216
- value = ((value - min) / step).round * step + min
215
+ value = (((value - min) / step).round * step) + min
217
216
  native['value'] = value.clamp(min, max)
218
217
  end
219
218
 
@@ -232,7 +231,14 @@ private
232
231
  end
233
232
  native.remove
234
233
  else
235
- native['value'] = value.to_s
234
+ value.to_s.tap do |set_value|
235
+ if set_value.end_with?("\n") && form&.css('input, textarea')&.count
236
+ native['value'] = set_value.to_s.chop
237
+ Capybara::RackTest::Form.new(driver, form).submit(self)
238
+ else
239
+ native['value'] = set_value
240
+ end
241
+ end
236
242
  end
237
243
  end
238
244
 
@@ -241,7 +247,7 @@ private
241
247
  end
242
248
 
243
249
  def follow_link
244
- method = self['data-method'] if driver.options[:respect_data_method]
250
+ method = self['data-method'] || self['data-turbo-method'] if driver.options[:respect_data_method]
245
251
  method ||= :get
246
252
  driver.follow(method, self[:href].to_s)
247
253
  end