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
@@ -16,43 +16,65 @@ module Capybara
16
16
 
17
17
  def initialize(app)
18
18
  @app = app
19
- @disable_markup = format(DISABLE_MARKUP_TEMPLATE, selector: self.class.selector_for(Capybara.disable_animation))
19
+ @disable_css_markup = format(DISABLE_CSS_MARKUP_TEMPLATE,
20
+ selector: self.class.selector_for(Capybara.disable_animation))
21
+ @disable_js_markup = +DISABLE_JS_MARKUP_TEMPLATE
20
22
  end
21
23
 
22
24
  def call(env)
23
- @status, @headers, @body = @app.call(env)
24
- return [@status, @headers, @body] unless html_content?
25
+ status, headers, body = @app.call(env)
26
+ return [status, headers, body] unless html_content?(headers)
25
27
 
26
- response = Rack::Response.new([], @status, @headers)
28
+ nonces = directive_nonces(headers).transform_values { |nonce| "nonce=\"#{nonce}\"" if nonce && !nonce.empty? }
29
+ response = Rack::Response.new([], status, headers)
27
30
 
28
- @body.each { |html| response.write insert_disable(html) }
29
- @body.close if @body.respond_to?(:close)
31
+ body.each { |html| response.write insert_disable(html, nonces) }
32
+ body.close if body.respond_to?(:close)
30
33
 
31
34
  response.finish
32
35
  end
33
36
 
34
37
  private
35
38
 
36
- attr_reader :disable_markup
39
+ attr_reader :disable_css_markup, :disable_js_markup
37
40
 
38
- def html_content?
39
- /html/.match?(@headers['Content-Type'])
41
+ def html_content?(headers)
42
+ /html/.match?(headers['Content-Type']) # rubocop:todo Performance/StringInclude
40
43
  end
41
44
 
42
- def insert_disable(html)
43
- html.sub(%r{(</head>)}, disable_markup + '\\1')
45
+ def insert_disable(html, nonces)
46
+ html.sub(%r{(</head>)}, "<style #{nonces['style-src']}>#{disable_css_markup}</style>\\1")
47
+ .sub(%r{(</body>)}, "<script #{nonces['script-src']}>#{disable_js_markup}</script>\\1")
44
48
  end
45
49
 
46
- DISABLE_MARKUP_TEMPLATE = <<~HTML
47
- <script defer>(typeof jQuery !== 'undefined') && (jQuery.fx.off = true);</script>
48
- <style>
49
- %<selector>s, %<selector>s::before, %<selector>s::after {
50
- transition: none !important;
51
- animation-duration: 0s !important;
52
- animation-delay: 0s !important;
53
- }
54
- </style>
55
- HTML
50
+ def directive_nonces(headers)
51
+ headers.fetch('Content-Security-Policy', '')
52
+ .split(';')
53
+ .map(&:split)
54
+ .to_h do |s|
55
+ [
56
+ s[0], s[1..].filter_map do |value|
57
+ /^'nonce-(?<nonce>.+)'/ =~ value
58
+ nonce
59
+ end[0]
60
+ ]
61
+ end
62
+ end
63
+
64
+ DISABLE_CSS_MARKUP_TEMPLATE = <<~CSS
65
+ %<selector>s, %<selector>s::before, %<selector>s::after {
66
+ transition: none !important;
67
+ animation-duration: 0s !important;
68
+ animation-delay: 0s !important;
69
+ scroll-behavior: auto !important;
70
+ }
71
+ CSS
72
+
73
+ DISABLE_JS_MARKUP_TEMPLATE = <<~SCRIPT
74
+ //<![CDATA[
75
+ (typeof jQuery !== 'undefined') && (jQuery.fx.off = true);
76
+ //]]>
77
+ SCRIPT
56
78
  end
57
79
  end
58
80
  end
@@ -14,7 +14,7 @@ module Capybara
14
14
  end
15
15
 
16
16
  def decrement(uri)
17
- @mutex.synchronize { @value.delete_at(@value.index(uri) || @value.length) }
17
+ @mutex.synchronize { @value.delete_at(@value.index(uri) || - 1) }
18
18
  end
19
19
 
20
20
  def positive?
@@ -53,14 +53,16 @@ module Capybara
53
53
  if env['PATH_INFO'] == '/__identify__'
54
54
  [200, {}, [@app.object_id.to_s]]
55
55
  else
56
- @counter.increment(env['REQUEST_URI'])
56
+ request_uri = env['REQUEST_URI']
57
+ @counter.increment(request_uri)
58
+
57
59
  begin
58
60
  @extended_app.call(env)
59
61
  rescue *@server_errors => e
60
62
  @error ||= e
61
63
  raise e
62
64
  ensure
63
- @counter.decrement(env['REQUEST_URI'])
65
+ @counter.decrement(request_uri)
64
66
  end
65
67
  end
66
68
  end
@@ -8,7 +8,7 @@ module Capybara
8
8
  automatic_reload match exact exact_text raise_server_errors visible_text_only
9
9
  automatic_label_click enable_aria_label save_path asset_host default_host app_host
10
10
  server_host server_port server_errors default_set_options disable_animation test_id
11
- predicates_wait default_normalize_ws w3c_click_offset].freeze
11
+ predicates_wait default_normalize_ws w3c_click_offset enable_aria_role default_retry_interval].freeze
12
12
 
13
13
  attr_accessor(*OPTIONS)
14
14
 
@@ -21,6 +21,8 @@ module Capybara
21
21
  # See {Capybara.configure}
22
22
  # @!method default_max_wait_time
23
23
  # See {Capybara.configure}
24
+ # @!method default_retry_interval
25
+ # See {Capybara.configure}
24
26
  # @!method ignore_hidden_elements
25
27
  # See {Capybara.configure}
26
28
  # @!method automatic_reload
@@ -37,6 +39,8 @@ module Capybara
37
39
  # See {Capybara.configure}
38
40
  # @!method enable_aria_label
39
41
  # See {Capybara.configure}
42
+ # @!method enable_aria_role
43
+ # See {Capybara.configure}
40
44
  # @!method save_path
41
45
  # See {Capybara.configure}
42
46
  # @!method asset_host
@@ -98,7 +102,7 @@ module Capybara
98
102
  remove_method :test_id=
99
103
  ##
100
104
  #
101
- # Set an attribue to be optionally matched against the locator for builtin selector types.
105
+ # Set an attribute to be optionally matched against the locator for builtin selector types.
102
106
  # This attribute will be checked by builtin selector types whenever id would normally be checked.
103
107
  # If `nil` then it will be ignored.
104
108
  #
@@ -13,14 +13,14 @@ module Capybara
13
13
  # @param string [String] The string that the current 'path' should equal
14
14
  # @overload $0(regexp, **options)
15
15
  # @param regexp [Regexp] The regexp that the current 'path' should match to
16
- # @option options [Boolean] :url (true if `string` ia a full url, otherwise false) Whether the compare should be done against the full current url or just the path
16
+ # @option options [Boolean] :url (true if `string` is a full url, otherwise false) Whether the comparison should be done against the full current url or just the path
17
17
  # @option options [Boolean] :ignore_query (false) Whether the query portion of the current url/path should be ignored
18
18
  # @option options [Numeric] :wait (Capybara.default_max_wait_time) Maximum time that Capybara will wait for the current url/path to eq/match given string/regexp argument
19
19
  # @raise [Capybara::ExpectationNotMet] if the assertion hasn't succeeded during wait time
20
20
  # @return [true]
21
21
  #
22
- def assert_current_path(path, **options)
23
- _verify_current_path(path, **options) do |query|
22
+ def assert_current_path(path, **options, &optional_filter_block)
23
+ _verify_current_path(path, optional_filter_block, **options) do |query|
24
24
  raise Capybara::ExpectationNotMet, query.failure_message unless query.resolves_for?(self)
25
25
  end
26
26
  end
@@ -35,8 +35,8 @@ module Capybara
35
35
  # @raise [Capybara::ExpectationNotMet] if the assertion hasn't succeeded during wait time
36
36
  # @return [true]
37
37
  #
38
- def assert_no_current_path(path, **options)
39
- _verify_current_path(path, **options) do |query|
38
+ def assert_no_current_path(path, **options, &optional_filter_block)
39
+ _verify_current_path(path, optional_filter_block, **options) do |query|
40
40
  raise Capybara::ExpectationNotMet, query.negative_failure_message if query.resolves_for?(self)
41
41
  end
42
42
  end
@@ -50,8 +50,8 @@ module Capybara
50
50
  # @macro current_path_query_params
51
51
  # @return [Boolean]
52
52
  #
53
- def has_current_path?(path, **options)
54
- make_predicate(options) { assert_current_path(path, **options) }
53
+ def has_current_path?(path, **options, &optional_filter_block)
54
+ make_predicate(options) { assert_current_path(path, **options, &optional_filter_block) }
55
55
  end
56
56
 
57
57
  ##
@@ -63,14 +63,14 @@ module Capybara
63
63
  # @macro current_path_query_params
64
64
  # @return [Boolean]
65
65
  #
66
- def has_no_current_path?(path, **options)
67
- make_predicate(options) { assert_no_current_path(path, **options) }
66
+ def has_no_current_path?(path, **options, &optional_filter_block)
67
+ make_predicate(options) { assert_no_current_path(path, **options, &optional_filter_block) }
68
68
  end
69
69
 
70
70
  private
71
71
 
72
- def _verify_current_path(path, **options)
73
- query = Capybara::Queries::CurrentPathQuery.new(path, **options)
72
+ def _verify_current_path(path, filter_block, **options)
73
+ query = Capybara::Queries::CurrentPathQuery.new(path, **options, &filter_block)
74
74
  document.synchronize(query.wait) do
75
75
  yield(query)
76
76
  end
@@ -40,6 +40,7 @@ module Capybara
40
40
 
41
41
  NODE_METHODS = %i[
42
42
  all first attach_file text check choose scroll_to scroll_by
43
+ click double_click right_click
43
44
  click_link_or_button click_button click_link
44
45
  fill_in find find_all find_button find_by_id find_field find_link
45
46
  has_content? has_text? has_css? has_no_content? has_no_text?
@@ -58,7 +59,7 @@ module Capybara
58
59
  ].freeze
59
60
  SESSION_METHODS = %i[
60
61
  body html source current_url current_host current_path
61
- execute_script evaluate_script visit refresh go_back go_forward
62
+ execute_script evaluate_script evaluate_async_script visit refresh go_back go_forward send_keys
62
63
  within within_element within_fieldset within_table within_frame switch_to_frame
63
64
  current_window windows open_new_window switch_to_window within_window window_opened_by
64
65
  save_page save_and_open_page save_screenshot
@@ -97,8 +98,8 @@ module Capybara
97
98
 
98
99
  def driver
99
100
  @driver ||= begin
100
- unless Capybara.drivers.key?(mode)
101
- other_drivers = Capybara.drivers.keys.map(&:inspect)
101
+ unless Capybara.drivers[mode]
102
+ other_drivers = Capybara.drivers.names.map(&:inspect)
102
103
  raise Capybara::DriverNotFoundError, "no driver called #{mode.inspect} was found, available drivers: #{other_drivers.join(', ')}"
103
104
  end
104
105
  driver = Capybara.drivers[mode].call(app)
@@ -129,6 +130,8 @@ module Capybara
129
130
  if @touched
130
131
  driver.reset!
131
132
  @touched = false
133
+ switch_to_frame(:top) rescue nil # rubocop:disable Style/RescueModifier
134
+ @scopes = [nil]
132
135
  end
133
136
  @server&.wait_for_pending_requests
134
137
  raise_server_error!
@@ -159,9 +162,8 @@ module Capybara
159
162
  if config.raise_server_errors
160
163
  raise CapybaraError, 'Your application server raised an error - It has been raised in your test code because Capybara.raise_server_errors == true'
161
164
  end
162
- rescue CapybaraError
163
- # needed to get the cause set correctly in JRuby -- otherwise we could just do raise @server.error
164
- raise @server.error, @server.error.message, @server.error.backtrace
165
+ rescue CapybaraError => capy_error # rubocop:disable Naming/RescuedExceptionsVariableName
166
+ raise @server.error, cause: capy_error
165
167
  ensure
166
168
  @server.reset_error!
167
169
  end
@@ -192,7 +194,7 @@ module Capybara
192
194
  # @return [String] A snapshot of the DOM of the current document, as it looks right now (potentially modified by JavaScript).
193
195
  #
194
196
  def html
195
- driver.html
197
+ driver.html || ''
196
198
  end
197
199
  alias_method :body, :html
198
200
  alias_method :source, :html
@@ -303,6 +305,24 @@ module Capybara
303
305
  driver.go_forward
304
306
  end
305
307
 
308
+ ##
309
+ # @!method send_keys
310
+ # @see Capybara::Node::Element#send_keys
311
+ #
312
+ def send_keys(*args, **kw_args)
313
+ driver.send_keys(*args, **kw_args)
314
+ end
315
+
316
+ ##
317
+ #
318
+ # Returns the element with focus.
319
+ #
320
+ # Not supported by Rack Test
321
+ #
322
+ def active_element
323
+ Capybara::Queries::ActiveElementQuery.new.resolve_for(self)[0].tap(&:allow_reload!)
324
+ end
325
+
306
326
  ##
307
327
  #
308
328
  # Executes the given block within the context of a node. {#within} takes the
@@ -342,7 +362,7 @@ module Capybara
342
362
  new_scope = args.first.respond_to?(:to_capybara_node) ? args.first.to_capybara_node : find(*args, **kw_args)
343
363
  begin
344
364
  scopes.push(new_scope)
345
- yield if block_given?
365
+ yield new_scope if block_given?
346
366
  ensure
347
367
  scopes.pop
348
368
  end
@@ -355,8 +375,8 @@ module Capybara
355
375
  #
356
376
  # @param [String] locator Id or legend of the fieldset
357
377
  #
358
- def within_fieldset(locator)
359
- within(:fieldset, locator) { yield }
378
+ def within_fieldset(locator, &block)
379
+ within(:fieldset, locator, &block)
360
380
  end
361
381
 
362
382
  ##
@@ -365,8 +385,8 @@ module Capybara
365
385
  #
366
386
  # @param [String] locator Id or caption of the table
367
387
  #
368
- def within_table(locator)
369
- within(:table, locator) { yield }
388
+ def within_table(locator, &block)
389
+ within(:table, locator, &block)
370
390
  end
371
391
 
372
392
  ##
@@ -391,19 +411,20 @@ module Capybara
391
411
  scopes.push(:frame)
392
412
  when :parent
393
413
  if scopes.last != :frame
394
- raise Capybara::ScopeError, "`switch_to_frame(:parent)` cannot be called from inside a descendant frame's "\
414
+ raise Capybara::ScopeError, "`switch_to_frame(:parent)` cannot be called from inside a descendant frame's " \
395
415
  '`within` block.'
396
416
  end
397
417
  scopes.pop
398
418
  driver.switch_to_frame(:parent)
399
419
  when :top
400
420
  idx = scopes.index(:frame)
421
+ top_level_scopes = [:frame, nil]
401
422
  if idx
402
- if scopes.slice(idx..-1).any? { |scope| ![:frame, nil].include?(scope) }
403
- raise Capybara::ScopeError, "`switch_to_frame(:top)` cannot be called from inside a descendant frame's "\
423
+ if scopes.slice(idx..).any? { |scope| !top_level_scopes.include?(scope) }
424
+ raise Capybara::ScopeError, "`switch_to_frame(:top)` cannot be called from inside a descendant frame's " \
404
425
  '`within` block.'
405
426
  end
406
- scopes.slice!(idx..-1)
427
+ scopes.slice!(idx..)
407
428
  driver.switch_to_frame(:top)
408
429
  end
409
430
  else
@@ -488,11 +509,11 @@ module Capybara
488
509
  # @raise [ArgumentError] if both or neither arguments were provided
489
510
  #
490
511
  def switch_to_window(window = nil, **options, &window_locator)
491
- raise ArgumentError, '`switch_to_window` can take either a block or a window, not both' if window && block_given?
492
- raise ArgumentError, '`switch_to_window`: either window or block should be provided' if !window && !block_given?
512
+ raise ArgumentError, '`switch_to_window` can take either a block or a window, not both' if window && window_locator
513
+ raise ArgumentError, '`switch_to_window`: either window or block should be provided' if !window && !window_locator
493
514
 
494
515
  unless scopes.last.nil?
495
- raise Capybara::ScopeError, '`switch_to_window` is not supposed to be invoked from '\
516
+ raise Capybara::ScopeError, '`switch_to_window` is not supposed to be invoked from ' \
496
517
  '`within` or `within_frame` blocks.'
497
518
  end
498
519
 
@@ -563,7 +584,7 @@ module Capybara
563
584
  synchronize_windows(options) do
564
585
  opened_handles = (driver.window_handles - old_handles)
565
586
  if opened_handles.size != 1
566
- raise Capybara::WindowError, 'block passed to #window_opened_by '\
587
+ raise Capybara::WindowError, 'block passed to #window_opened_by ' \
567
588
  "opened #{opened_handles.size} windows instead of 1"
568
589
  end
569
590
  Window.new(self, opened_handles.first)
@@ -738,7 +759,7 @@ module Capybara
738
759
  # @param [Hash] options a customizable set of options
739
760
  #
740
761
  def save_and_open_screenshot(path = nil, **options)
741
- save_screenshot(path, **options).tap { |s_path| open_file(s_path) } # rubocop:disable Lint/Debugger
762
+ save_screenshot(path, **options).tap { |s_path| open_file(s_path) }
742
763
  end
743
764
 
744
765
  def document
@@ -746,33 +767,20 @@ module Capybara
746
767
  end
747
768
 
748
769
  NODE_METHODS.each do |method|
749
- if RUBY_VERSION >= '2.7'
750
- class_eval <<~METHOD, __FILE__, __LINE__ + 1
751
- def #{method}(...)
752
- @touched = true
753
- current_scope.#{method}(...)
754
- end
755
- METHOD
756
- else
757
- define_method method do |*args, &block|
770
+ class_eval <<~METHOD, __FILE__, __LINE__ + 1
771
+ def #{method}(...)
758
772
  @touched = true
759
- current_scope.send(method, *args, &block)
773
+ current_scope.#{method}(...)
760
774
  end
761
- end
775
+ METHOD
762
776
  end
763
777
 
764
778
  DOCUMENT_METHODS.each do |method|
765
- if RUBY_VERSION >= '2.7'
766
- class_eval <<~METHOD, __FILE__, __LINE__ + 1
767
- def #{method}(...)
768
- document.#{method}(...)
769
- end
770
- METHOD
771
- else
772
- define_method method do |*args, &block|
773
- document.send(method, *args, &block)
779
+ class_eval <<~METHOD, __FILE__, __LINE__ + 1
780
+ def #{method}(...)
781
+ document.#{method}(...)
774
782
  end
775
- end
783
+ METHOD
776
784
  end
777
785
 
778
786
  def inspect
@@ -788,7 +796,7 @@ module Capybara
788
796
  #
789
797
  # Yield a block using a specific maximum wait time.
790
798
  #
791
- def using_wait_time(seconds)
799
+ def using_wait_time(seconds, &block)
792
800
  if Capybara.threadsafe
793
801
  begin
794
802
  previous_wait_time = config.default_max_wait_time
@@ -798,7 +806,7 @@ module Capybara
798
806
  config.default_max_wait_time = previous_wait_time
799
807
  end
800
808
  else
801
- Capybara.using_wait_time(seconds) { yield }
809
+ Capybara.using_wait_time(seconds, &block)
802
810
  end
803
811
  end
804
812
 
@@ -44,6 +44,10 @@ $(function() {
44
44
  $(this).after('<div class="log">DragOver with client position: ' + ev.clientX + ',' + ev.clientY)
45
45
  if ($(this).hasClass('drop')) { ev.preventDefault(); }
46
46
  });
47
+ $('#drop_html5, #drop_html5_scroll').on('dragenter', function(ev){
48
+ $(this).after('<div class="log">DragEnter')
49
+ if ($(this).hasClass('drop')) { ev.preventDefault(); }
50
+ });
47
51
  $('#drop_html5, #drop_html5_scroll').on('dragleave', function(ev){
48
52
  $(this).after('<div class="log">DragLeave with client position: ' + ev.clientX + ',' + ev.clientY)
49
53
  if ($(this).hasClass('drop')) { ev.preventDefault(); }
@@ -108,6 +112,13 @@ $(function() {
108
112
  }, 4000);
109
113
  return false;
110
114
  });
115
+ $('#aria-button').click(function() {
116
+ var span = $(this);
117
+ setTimeout(function() {
118
+ $(span).after('<span role="button">ARIA button has been clicked</span>')
119
+ }, 1000);
120
+ return false;
121
+ });
111
122
  $('#waiter').change(function() {
112
123
  activeRequests = 1;
113
124
  setTimeout(function() {
@@ -264,5 +275,10 @@ $(function() {
264
275
  });
265
276
  $('#multiple-file, #hidden_file').change(function(e){
266
277
  $('body').append($('<p class="file_change">File input changed</p>'));
267
- })
278
+ });
279
+
280
+ var shadow = document.querySelector('#shadow').attachShadow({mode: 'open'});
281
+ var span = document.createElement('span');
282
+ span.textContent = 'The things we do in the shadows';
283
+ shadow.appendChild(span);
268
284
  });
@@ -53,7 +53,7 @@ Capybara::SpecHelper.spec '#accept_alert', requires: [:modals] do
53
53
  @session.click_link('Alert page change')
54
54
  sleep 1 # ensure page change occurs before the accept_alert block exits
55
55
  end
56
- expect(@session).to have_current_path('/with_html')
56
+ expect(@session).to have_current_path('/with_html', wait: 5)
57
57
  end
58
58
 
59
59
  context 'with an asynchronous alert' do
@@ -0,0 +1,31 @@
1
+ # frozen_string_literal: true
2
+
3
+ Capybara::SpecHelper.spec '#active_element', requires: [:active_element] do
4
+ it 'should return the active element' do
5
+ @session.visit('/form')
6
+ @session.send_keys(:tab)
7
+
8
+ expect(@session.active_element).to match_selector(:css, '[tabindex="1"]')
9
+
10
+ @session.send_keys(:tab)
11
+
12
+ expect(@session.active_element).to match_selector(:css, '[tabindex="2"]')
13
+ end
14
+
15
+ it 'should support reloading' do
16
+ @session.visit('/form')
17
+ expect(@session.active_element).to match_selector(:css, 'body')
18
+ @session.execute_script <<-JS
19
+ window.setTimeout(() => {
20
+ document.querySelector('#form_title').focus();
21
+ }, 1000)
22
+ JS
23
+ expect(@session.active_element).to match_selector(:css, 'body', wait: false)
24
+ expect(@session.active_element).to match_selector(:css, '#form_title', wait: 2)
25
+ end
26
+
27
+ it 'should return a Capybara::Element' do
28
+ @session.visit('/form')
29
+ expect(@session.active_element).to be_a Capybara::Node::Element
30
+ end
31
+ end
@@ -31,7 +31,7 @@ Capybara::SpecHelper.spec '#all' do
31
31
  it 'should accept an XPath instance', :exact_false do
32
32
  @session.visit('/form')
33
33
  @xpath = Capybara::Selector.new(:fillable_field, config: {}, format: :xpath).call('Name')
34
- expect(@xpath).to be_a(::XPath::Union)
34
+ expect(@xpath).to be_a(XPath::Union)
35
35
  @result = @session.all(@xpath).map(&:value)
36
36
  expect(@result).to include('Smith', 'John', 'John Smith')
37
37
  end
@@ -132,7 +132,7 @@ Capybara::SpecHelper.spec '#all' do
132
132
  it 'should use the sessions ignore_hidden_elements', psc: true do
133
133
  Capybara.ignore_hidden_elements = true
134
134
  @session.config.ignore_hidden_elements = false
135
- expect(Capybara.ignore_hidden_elements).to eq(true)
135
+ expect(Capybara.ignore_hidden_elements).to be(true)
136
136
  expect(@session.all(:css, 'a.simple').size).to eq(2)
137
137
  @session.config.ignore_hidden_elements = true
138
138
  expect(@session.all(:css, 'a.simple').size).to eq(1)
@@ -203,19 +203,15 @@ Capybara::SpecHelper.spec '#all' do
203
203
  expect { @session.all(:css, 'h1, p', between: 0..3) }.to raise_error(Capybara::ExpectationNotMet)
204
204
  end
205
205
 
206
- eval <<~TEST, binding, __FILE__, __LINE__ + 1 if RUBY_VERSION.to_f > 2.5
207
- it'treats an endless range as minimum' do
208
- expect { @session.all(:css, 'h1, p', between: 2..) }.not_to raise_error
209
- expect { @session.all(:css, 'h1, p', between: 5..) }.to raise_error(Capybara::ExpectationNotMet)
210
- end
211
- TEST
206
+ it 'treats an endless range as minimum' do
207
+ expect { @session.all(:css, 'h1, p', between: 2..) }.not_to raise_error
208
+ expect { @session.all(:css, 'h1, p', between: 5..) }.to raise_error(Capybara::ExpectationNotMet)
209
+ end
212
210
 
213
- eval <<~TEST, binding, __FILE__, __LINE__ + 1 if RUBY_VERSION.to_f > 2.6
214
- it'treats a beginless range as maximum' do
215
- expect { @session.all(:css, 'h1, p', between: ..7) }.not_to raise_error
216
- expect { @session.all(:css, 'h1, p', between: ..3) }.to raise_error(Capybara::ExpectationNotMet)
217
- end
218
- TEST
211
+ it 'treats a beginless range as maximum' do
212
+ expect { @session.all(:css, 'h1, p', between: ..7) }.not_to raise_error
213
+ expect { @session.all(:css, 'h1, p', between: ..3) }.to raise_error(Capybara::ExpectationNotMet)
214
+ end
219
215
  end
220
216
 
221
217
  context 'with multiple count filters' do