capybara 3.8.1 → 3.33.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (242) hide show
  1. checksums.yaml +4 -4
  2. data/.yardopts +1 -0
  3. data/History.md +465 -0
  4. data/License.txt +1 -1
  5. data/README.md +58 -57
  6. data/lib/capybara/config.rb +10 -4
  7. data/lib/capybara/cucumber.rb +1 -1
  8. data/lib/capybara/driver/base.rb +2 -2
  9. data/lib/capybara/driver/node.rb +26 -5
  10. data/lib/capybara/dsl.rb +12 -4
  11. data/lib/capybara/helpers.rb +8 -4
  12. data/lib/capybara/minitest/spec.rb +162 -85
  13. data/lib/capybara/minitest.rb +248 -148
  14. data/lib/capybara/node/actions.rb +149 -96
  15. data/lib/capybara/node/base.rb +27 -10
  16. data/lib/capybara/node/document.rb +12 -0
  17. data/lib/capybara/node/document_matchers.rb +9 -5
  18. data/lib/capybara/node/element.rb +254 -109
  19. data/lib/capybara/node/finders.rb +83 -76
  20. data/lib/capybara/node/matchers.rb +279 -141
  21. data/lib/capybara/node/simple.rb +25 -6
  22. data/lib/capybara/queries/ancestor_query.rb +5 -7
  23. data/lib/capybara/queries/base_query.rb +11 -5
  24. data/lib/capybara/queries/current_path_query.rb +3 -3
  25. data/lib/capybara/queries/match_query.rb +1 -0
  26. data/lib/capybara/queries/selector_query.rb +467 -103
  27. data/lib/capybara/queries/sibling_query.rb +5 -4
  28. data/lib/capybara/queries/style_query.rb +6 -2
  29. data/lib/capybara/queries/text_query.rb +17 -3
  30. data/lib/capybara/queries/title_query.rb +2 -2
  31. data/lib/capybara/rack_test/browser.rb +22 -15
  32. data/lib/capybara/rack_test/driver.rb +10 -1
  33. data/lib/capybara/rack_test/errors.rb +6 -0
  34. data/lib/capybara/rack_test/form.rb +33 -28
  35. data/lib/capybara/rack_test/node.rb +74 -6
  36. data/lib/capybara/registration_container.rb +44 -0
  37. data/lib/capybara/registrations/drivers.rb +36 -0
  38. data/lib/capybara/registrations/patches/puma_ssl.rb +27 -0
  39. data/lib/capybara/registrations/servers.rb +44 -0
  40. data/lib/capybara/result.rb +55 -23
  41. data/lib/capybara/rspec/features.rb +4 -4
  42. data/lib/capybara/rspec/matcher_proxies.rb +36 -15
  43. data/lib/capybara/rspec/matchers/base.rb +111 -0
  44. data/lib/capybara/rspec/matchers/become_closed.rb +33 -0
  45. data/lib/capybara/rspec/matchers/compound.rb +88 -0
  46. data/lib/capybara/rspec/matchers/count_sugar.rb +37 -0
  47. data/lib/capybara/rspec/matchers/have_ancestor.rb +28 -0
  48. data/lib/capybara/rspec/matchers/have_current_path.rb +29 -0
  49. data/lib/capybara/rspec/matchers/have_selector.rb +77 -0
  50. data/lib/capybara/rspec/matchers/have_sibling.rb +27 -0
  51. data/lib/capybara/rspec/matchers/have_text.rb +33 -0
  52. data/lib/capybara/rspec/matchers/have_title.rb +29 -0
  53. data/lib/capybara/rspec/matchers/match_selector.rb +27 -0
  54. data/lib/capybara/rspec/matchers/match_style.rb +38 -0
  55. data/lib/capybara/rspec/matchers/spatial_sugar.rb +39 -0
  56. data/lib/capybara/rspec/matchers.rb +117 -311
  57. data/lib/capybara/selector/builders/css_builder.rb +84 -0
  58. data/lib/capybara/selector/builders/xpath_builder.rb +69 -0
  59. data/lib/capybara/selector/css.rb +17 -15
  60. data/lib/capybara/selector/definition/button.rb +52 -0
  61. data/lib/capybara/selector/definition/checkbox.rb +26 -0
  62. data/lib/capybara/selector/definition/css.rb +10 -0
  63. data/lib/capybara/selector/definition/datalist_input.rb +35 -0
  64. data/lib/capybara/selector/definition/datalist_option.rb +25 -0
  65. data/lib/capybara/selector/definition/element.rb +27 -0
  66. data/lib/capybara/selector/definition/field.rb +40 -0
  67. data/lib/capybara/selector/definition/fieldset.rb +14 -0
  68. data/lib/capybara/selector/definition/file_field.rb +13 -0
  69. data/lib/capybara/selector/definition/fillable_field.rb +33 -0
  70. data/lib/capybara/selector/definition/frame.rb +17 -0
  71. data/lib/capybara/selector/definition/id.rb +6 -0
  72. data/lib/capybara/selector/definition/label.rb +62 -0
  73. data/lib/capybara/selector/definition/link.rb +54 -0
  74. data/lib/capybara/selector/definition/link_or_button.rb +16 -0
  75. data/lib/capybara/selector/definition/option.rb +27 -0
  76. data/lib/capybara/selector/definition/radio_button.rb +27 -0
  77. data/lib/capybara/selector/definition/select.rb +81 -0
  78. data/lib/capybara/selector/definition/table.rb +109 -0
  79. data/lib/capybara/selector/definition/table_row.rb +21 -0
  80. data/lib/capybara/selector/definition/xpath.rb +5 -0
  81. data/lib/capybara/selector/definition.rb +277 -0
  82. data/lib/capybara/selector/filter.rb +1 -0
  83. data/lib/capybara/selector/filter_set.rb +26 -19
  84. data/lib/capybara/selector/filters/base.rb +24 -5
  85. data/lib/capybara/selector/filters/expression_filter.rb +3 -3
  86. data/lib/capybara/selector/filters/locator_filter.rb +29 -0
  87. data/lib/capybara/selector/filters/node_filter.rb +16 -2
  88. data/lib/capybara/selector/regexp_disassembler.rb +214 -0
  89. data/lib/capybara/selector/selector.rb +73 -367
  90. data/lib/capybara/selector/xpath_extensions.rb +17 -0
  91. data/lib/capybara/selector.rb +221 -480
  92. data/lib/capybara/selenium/atoms/getAttribute.min.js +1 -0
  93. data/lib/capybara/selenium/atoms/isDisplayed.min.js +1 -0
  94. data/lib/capybara/selenium/atoms/src/getAttribute.js +161 -0
  95. data/lib/capybara/selenium/atoms/src/isDisplayed.js +454 -0
  96. data/lib/capybara/selenium/driver.rb +203 -86
  97. data/lib/capybara/selenium/driver_specializations/chrome_driver.rb +88 -14
  98. data/lib/capybara/selenium/driver_specializations/edge_driver.rb +124 -0
  99. data/lib/capybara/selenium/driver_specializations/firefox_driver.rb +89 -0
  100. data/lib/capybara/selenium/driver_specializations/internet_explorer_driver.rb +26 -0
  101. data/lib/capybara/selenium/driver_specializations/safari_driver.rb +24 -0
  102. data/lib/capybara/selenium/extensions/file_input_click_emulation.rb +34 -0
  103. data/lib/capybara/selenium/extensions/find.rb +110 -0
  104. data/lib/capybara/selenium/extensions/html5_drag.rb +191 -22
  105. data/lib/capybara/selenium/extensions/modifier_keys_stack.rb +28 -0
  106. data/lib/capybara/selenium/extensions/scroll.rb +78 -0
  107. data/lib/capybara/selenium/logger_suppressor.rb +34 -0
  108. data/lib/capybara/selenium/node.rb +298 -93
  109. data/lib/capybara/selenium/nodes/chrome_node.rb +100 -8
  110. data/lib/capybara/selenium/nodes/edge_node.rb +104 -0
  111. data/lib/capybara/selenium/nodes/firefox_node.rb +131 -0
  112. data/lib/capybara/selenium/nodes/ie_node.rb +22 -0
  113. data/lib/capybara/selenium/nodes/safari_node.rb +118 -0
  114. data/lib/capybara/selenium/patches/action_pauser.rb +26 -0
  115. data/lib/capybara/selenium/patches/atoms.rb +18 -0
  116. data/lib/capybara/selenium/patches/is_displayed.rb +16 -0
  117. data/lib/capybara/selenium/patches/logs.rb +45 -0
  118. data/lib/capybara/selenium/patches/pause_duration_fix.rb +1 -3
  119. data/lib/capybara/selenium/patches/persistent_client.rb +20 -0
  120. data/lib/capybara/server/animation_disabler.rb +4 -3
  121. data/lib/capybara/server/checker.rb +6 -2
  122. data/lib/capybara/server/middleware.rb +23 -13
  123. data/lib/capybara/server.rb +30 -7
  124. data/lib/capybara/session/config.rb +14 -10
  125. data/lib/capybara/session/matchers.rb +11 -7
  126. data/lib/capybara/session.rb +152 -111
  127. data/lib/capybara/spec/public/offset.js +6 -0
  128. data/lib/capybara/spec/public/test.js +101 -10
  129. data/lib/capybara/spec/session/all_spec.rb +96 -6
  130. data/lib/capybara/spec/session/ancestor_spec.rb +5 -0
  131. data/lib/capybara/spec/session/assert_all_of_selectors_spec.rb +29 -0
  132. data/lib/capybara/spec/session/assert_current_path_spec.rb +5 -2
  133. data/lib/capybara/spec/session/assert_selector_spec.rb +0 -10
  134. data/lib/capybara/spec/session/assert_style_spec.rb +4 -4
  135. data/lib/capybara/spec/session/assert_text_spec.rb +9 -5
  136. data/lib/capybara/spec/session/attach_file_spec.rb +63 -36
  137. data/lib/capybara/spec/session/check_spec.rb +10 -4
  138. data/lib/capybara/spec/session/choose_spec.rb +8 -2
  139. data/lib/capybara/spec/session/click_button_spec.rb +117 -61
  140. data/lib/capybara/spec/session/click_link_or_button_spec.rb +16 -0
  141. data/lib/capybara/spec/session/click_link_spec.rb +17 -6
  142. data/lib/capybara/spec/session/element/matches_selector_spec.rb +40 -39
  143. data/lib/capybara/spec/session/evaluate_script_spec.rb +13 -0
  144. data/lib/capybara/spec/session/execute_script_spec.rb +1 -0
  145. data/lib/capybara/spec/session/fill_in_spec.rb +47 -6
  146. data/lib/capybara/spec/session/find_field_spec.rb +1 -1
  147. data/lib/capybara/spec/session/find_spec.rb +74 -4
  148. data/lib/capybara/spec/session/first_spec.rb +1 -1
  149. data/lib/capybara/spec/session/frame/switch_to_frame_spec.rb +13 -1
  150. data/lib/capybara/spec/session/frame/within_frame_spec.rb +12 -1
  151. data/lib/capybara/spec/session/has_all_selectors_spec.rb +1 -1
  152. data/lib/capybara/spec/session/has_ancestor_spec.rb +46 -0
  153. data/lib/capybara/spec/session/has_any_selectors_spec.rb +25 -0
  154. data/lib/capybara/spec/session/has_button_spec.rb +16 -0
  155. data/lib/capybara/spec/session/has_css_spec.rb +122 -12
  156. data/lib/capybara/spec/session/has_current_path_spec.rb +6 -4
  157. data/lib/capybara/spec/session/has_field_spec.rb +55 -0
  158. data/lib/capybara/spec/session/has_select_spec.rb +34 -6
  159. data/lib/capybara/spec/session/has_selector_spec.rb +11 -4
  160. data/lib/capybara/spec/session/has_sibling_spec.rb +50 -0
  161. data/lib/capybara/spec/session/has_table_spec.rb +166 -0
  162. data/lib/capybara/spec/session/has_text_spec.rb +48 -1
  163. data/lib/capybara/spec/session/has_xpath_spec.rb +17 -0
  164. data/lib/capybara/spec/session/html_spec.rb +7 -0
  165. data/lib/capybara/spec/session/matches_style_spec.rb +35 -0
  166. data/lib/capybara/spec/session/node_spec.rb +643 -18
  167. data/lib/capybara/spec/session/node_wrapper_spec.rb +1 -1
  168. data/lib/capybara/spec/session/refresh_spec.rb +4 -0
  169. data/lib/capybara/spec/session/reset_session_spec.rb +23 -8
  170. data/lib/capybara/spec/session/save_and_open_screenshot_spec.rb +2 -2
  171. data/lib/capybara/spec/session/save_screenshot_spec.rb +4 -4
  172. data/lib/capybara/spec/session/scroll_spec.rb +117 -0
  173. data/lib/capybara/spec/session/select_spec.rb +10 -10
  174. data/lib/capybara/spec/session/selectors_spec.rb +36 -5
  175. data/lib/capybara/spec/session/uncheck_spec.rb +2 -2
  176. data/lib/capybara/spec/session/unselect_spec.rb +1 -1
  177. data/lib/capybara/spec/session/window/become_closed_spec.rb +20 -17
  178. data/lib/capybara/spec/session/window/switch_to_window_spec.rb +4 -0
  179. data/lib/capybara/spec/session/window/window_opened_by_spec.rb +4 -0
  180. data/lib/capybara/spec/session/window/window_spec.rb +59 -58
  181. data/lib/capybara/spec/session/window/windows_spec.rb +4 -0
  182. data/lib/capybara/spec/session/within_spec.rb +23 -0
  183. data/lib/capybara/spec/spec_helper.rb +16 -6
  184. data/lib/capybara/spec/test_app.rb +28 -23
  185. data/lib/capybara/spec/views/animated.erb +49 -0
  186. data/lib/capybara/spec/views/form.erb +48 -7
  187. data/lib/capybara/spec/views/frame_child.erb +3 -2
  188. data/lib/capybara/spec/views/frame_one.erb +1 -0
  189. data/lib/capybara/spec/views/obscured.erb +47 -0
  190. data/lib/capybara/spec/views/offset.erb +32 -0
  191. data/lib/capybara/spec/views/react.erb +45 -0
  192. data/lib/capybara/spec/views/scroll.erb +20 -0
  193. data/lib/capybara/spec/views/spatial.erb +31 -0
  194. data/lib/capybara/spec/views/tables.erb +67 -0
  195. data/lib/capybara/spec/views/with_animation.erb +29 -1
  196. data/lib/capybara/spec/views/with_dragula.erb +24 -0
  197. data/lib/capybara/spec/views/with_hover.erb +1 -0
  198. data/lib/capybara/spec/views/with_hover1.erb +10 -0
  199. data/lib/capybara/spec/views/with_html.erb +32 -6
  200. data/lib/capybara/spec/views/with_js.erb +3 -1
  201. data/lib/capybara/spec/views/with_jstree.erb +26 -0
  202. data/lib/capybara/spec/views/with_scope_other.erb +6 -0
  203. data/lib/capybara/spec/views/with_sortable_js.erb +21 -0
  204. data/lib/capybara/version.rb +1 -1
  205. data/lib/capybara/window.rb +11 -11
  206. data/lib/capybara.rb +118 -111
  207. data/spec/basic_node_spec.rb +14 -3
  208. data/spec/capybara_spec.rb +29 -29
  209. data/spec/css_builder_spec.rb +101 -0
  210. data/spec/dsl_spec.rb +46 -21
  211. data/spec/filter_set_spec.rb +5 -5
  212. data/spec/fixtures/selenium_driver_rspec_failure.rb +1 -1
  213. data/spec/fixtures/selenium_driver_rspec_success.rb +1 -1
  214. data/spec/minitest_spec.rb +18 -4
  215. data/spec/minitest_spec_spec.rb +59 -44
  216. data/spec/rack_test_spec.rb +117 -89
  217. data/spec/regexp_dissassembler_spec.rb +250 -0
  218. data/spec/result_spec.rb +51 -49
  219. data/spec/rspec/features_spec.rb +3 -0
  220. data/spec/rspec/shared_spec_matchers.rb +112 -97
  221. data/spec/rspec_spec.rb +35 -17
  222. data/spec/sauce_spec_chrome.rb +43 -0
  223. data/spec/selector_spec.rb +244 -28
  224. data/spec/selenium_spec_chrome.rb +125 -54
  225. data/spec/selenium_spec_chrome_remote.rb +26 -12
  226. data/spec/selenium_spec_edge.rb +23 -8
  227. data/spec/selenium_spec_firefox.rb +208 -0
  228. data/spec/selenium_spec_firefox_remote.rb +15 -18
  229. data/spec/selenium_spec_ie.rb +82 -13
  230. data/spec/selenium_spec_safari.rb +148 -0
  231. data/spec/server_spec.rb +118 -77
  232. data/spec/session_spec.rb +19 -3
  233. data/spec/shared_selenium_node.rb +83 -0
  234. data/spec/shared_selenium_session.rb +110 -65
  235. data/spec/spec_helper.rb +57 -9
  236. data/spec/xpath_builder_spec.rb +93 -0
  237. metadata +257 -17
  238. data/lib/capybara/rspec/compound.rb +0 -94
  239. data/lib/capybara/selenium/driver_specializations/marionette_driver.rb +0 -49
  240. data/lib/capybara/selenium/nodes/marionette_node.rb +0 -121
  241. data/lib/capybara/spec/session/has_style_spec.rb +0 -25
  242. data/spec/selenium_spec_marionette.rb +0 -172
@@ -3,66 +3,66 @@
3
3
  module Capybara
4
4
  module Node
5
5
  module Actions
6
- ##
7
- #
8
- # Finds a button or link and clicks it. See {Capybara::Node::Actions#click_button} and
9
- # {Capybara::Node::Actions#click_link} for what locator will match against for each type of element
10
6
  # @!macro waiting_behavior
11
- # If the driver is capable of executing JavaScript, +$0+ will wait for a set amount of time
7
+ # If the driver is capable of executing JavaScript, this method will wait for a set amount of time
12
8
  # and continuously retry finding the element until either the element is found or the time
13
- # expires. The length of time +find+ will wait is controlled through {Capybara.default_max_wait_time}
9
+ # expires. The length of time this method will wait is controlled through {Capybara.configure default_max_wait_time}.
14
10
  #
15
- # @option options [false, Numeric] wait (Capybara.default_max_wait_time) Maximum time to wait for matching element to appear.
11
+ # @option options [false, true, Numeric] wait
12
+ # Maximum time to wait for matching element to appear. Defaults to {Capybara.configure default_max_wait_time}.
13
+
14
+ ##
16
15
  #
17
- # @overload click_link_or_button([locator], options)
16
+ # Finds a button or link and clicks it. See {#click_button} and
17
+ # {#click_link} for what locator will match against for each type of element.
18
18
  #
19
- # @param [String] locator See {Capybara::Node::Actions#click_button} and {Capybara::Node::Actions#click_link}
19
+ # @overload click_link_or_button([locator], **options)
20
+ # @macro waiting_behavior
21
+ # @param [String] locator See {#click_button} and {#click_link}
20
22
  #
21
23
  # @return [Capybara::Node::Element] The element clicked
22
24
  #
23
25
  def click_link_or_button(locator = nil, **options)
24
- find(:link_or_button, locator, options).click
26
+ find(:link_or_button, locator, **options).click
25
27
  end
26
28
  alias_method :click_on, :click_link_or_button
27
29
 
28
30
  ##
29
31
  #
30
- # Finds a link by id, Capybara.test_id attribute, text or title and clicks it. Also looks at image
32
+ # Finds a link by id, {Capybara.configure test_id} attribute, text or title and clicks it. Also looks at image
31
33
  # alt text inside the link.
32
34
  #
33
- # @macro waiting_behavior
34
- #
35
- # @overload click_link([locator], options)
36
- # @param [String] locator text, id, Capybara.test_id attribute, title or nested image's alt attribute
37
- # @param options See {Capybara::Node::Finders#find_link}
35
+ # @overload click_link([locator], **options)
36
+ # @macro waiting_behavior
37
+ # @param [String] locator text, id, {Capybara.configure test_id} attribute, title or nested image's alt attribute
38
+ # @param [Hash] options See {Capybara::Node::Finders#find_link}
38
39
  #
39
40
  # @return [Capybara::Node::Element] The element clicked
40
41
  def click_link(locator = nil, **options)
41
- find(:link, locator, options).click
42
+ find(:link, locator, **options).click
42
43
  end
43
44
 
44
45
  ##
45
46
  #
46
47
  # Finds a button on the page and clicks it.
47
- # This can be any \<input> element of type submit, reset, image, button or it can be a
48
- # \<button> element. All buttons can be found by their id, Capybara.test_id attribute, value, or title. \<button> elements can also be found
49
- # by their text content, and image \<input> elements by their alt attribute
50
- #
51
- # @macro waiting_behavior
48
+ # This can be any `<input>` element of type submit, reset, image, button or it can be a
49
+ # `<button>` element. All buttons can be found by their id, name, {Capybara.configure test_id} attribute, value, or title. `<button>` elements can also be found
50
+ # by their text content, and image `<input>` elements by their alt attribute.
52
51
  #
53
52
  # @overload click_button([locator], **options)
53
+ # @macro waiting_behavior
54
54
  # @param [String] locator Which button to find
55
- # @param options See {Capybara::Node::Finders#find_button}
55
+ # @param [Hash] options See {Capybara::Node::Finders#find_button}
56
56
  # @return [Capybara::Node::Element] The element clicked
57
57
  def click_button(locator = nil, **options)
58
- find(:button, locator, options).click
58
+ find(:button, locator, **options).click
59
59
  end
60
60
 
61
61
  ##
62
62
  #
63
- # Locate a text field or text area and fill it in with the given text
64
- # The field can be found via its name, id, Capybara.test_id attribute, or label text.
65
- # If no locator is provided will operate on self or a descendant
63
+ # Locate a text field or text area and fill it in with the given text.
64
+ # The field can be found via its name, id, {Capybara.configure test_id} attribute, placeholder, or label text.
65
+ # If no locator is provided this will operate on self or a descendant.
66
66
  #
67
67
  # # will fill in a descendant fillable field with name, id, or label text matching 'Name'
68
68
  # page.fill_in 'Name', with: 'Bob'
@@ -74,31 +74,32 @@ module Capybara
74
74
  # @overload fill_in([locator], with:, **options)
75
75
  # @param [String] locator Which field to fill in
76
76
  # @param [Hash] options
77
- # @param with: [String] The value to fill_in
77
+ # @param with: [String] The value to fill in
78
78
  # @macro waiting_behavior
79
79
  # @option options [String] currently_with The current value property of the field to fill in
80
80
  # @option options [Boolean] multiple Match fields that can have multiple values?
81
- # @option options [String] id Match fields that match the id attribute
81
+ # @option options [String, Regexp] id Match fields that match the id attribute
82
82
  # @option options [String] name Match fields that match the name attribute
83
83
  # @option options [String] placeholder Match fields that match the placeholder attribute
84
- # @option options [String, Array<String>] class Match fields that match the class(es) provided
85
- # @option options [Hash] fill_options Driver specific options regarding how to fill fields (Defaults come from Capybara.default_set_options)
84
+ # @option options [String, Array<String>, Regexp] class Match fields that match the class(es) provided
85
+ # @option options [Hash] fill_options Driver specific options regarding how to fill fields (Defaults come from {Capybara.configure default_set_options})
86
86
  #
87
- # @return [Capybara::Node::Element] The element filled_in
87
+ # @return [Capybara::Node::Element] The element filled in
88
88
  def fill_in(locator = nil, with:, currently_with: nil, fill_options: {}, **find_options)
89
89
  find_options[:with] = currently_with if currently_with
90
90
  find_options[:allow_self] = true if locator.nil?
91
- find(:fillable_field, locator, find_options).set(with, fill_options)
91
+ find(:fillable_field, locator, **find_options).set(with, **fill_options)
92
92
  end
93
93
 
94
94
  # @!macro label_click
95
- # @option options [Boolean] allow_label_click (Capybara.automatic_label_click) Attempt to click the label to toggle state if element is non-visible.
95
+ # @option options [Boolean] allow_label_click
96
+ # Attempt to click the label to toggle state if element is non-visible. Defaults to {Capybara.configure automatic_label_click}.
96
97
 
97
98
  ##
98
99
  #
99
100
  # Find a descendant radio button and mark it as checked. The radio button can be found
100
- # via name, id or label text. If no locator is provided this will match against self or
101
- # a descendant.
101
+ # via name, id, {Capybara.configure test_id} attribute or label text. If no locator is
102
+ # provided this will match against self or a descendant.
102
103
  #
103
104
  # # will choose a descendant radio button with a name, id, or label text matching 'Male'
104
105
  # page.choose('Male')
@@ -110,22 +111,22 @@ module Capybara
110
111
  # @param [String] locator Which radio button to choose
111
112
  #
112
113
  # @option options [String] option Value of the radio_button to choose
113
- # @option options [String] id Match fields that match the id attribute
114
+ # @option options [String, Regexp] id Match fields that match the id attribute
114
115
  # @option options [String] name Match fields that match the name attribute
115
- # @option options [String, Array<String>] class Match fields that match the class(es) provided
116
+ # @option options [String, Array<String>, Regexp] class Match fields that match the class(es) provided
116
117
  # @macro waiting_behavior
117
118
  # @macro label_click
118
119
  #
119
120
  # @return [Capybara::Node::Element] The element chosen or the label clicked
120
121
  def choose(locator = nil, **options)
121
- _check_with_label(:radio_button, true, locator, options)
122
+ _check_with_label(:radio_button, true, locator, **options)
122
123
  end
123
124
 
124
125
  ##
125
126
  #
126
127
  # Find a descendant check box and mark it as checked. The check box can be found
127
- # via name, id or label text. If no locator is provided this will match against
128
- # self or a descendant.
128
+ # via name, id, {Capybara.configure test_id} attribute, or label text. If no locator
129
+ # is provided this will match against self or a descendant.
129
130
  #
130
131
  # # will check a descendant checkbox with a name, id, or label text matching 'German'
131
132
  # page.check('German')
@@ -138,22 +139,22 @@ module Capybara
138
139
  # @param [String] locator Which check box to check
139
140
  #
140
141
  # @option options [String] option Value of the checkbox to select
141
- # @option options [String] id Match fields that match the id attribute
142
+ # @option options [String, Regexp] id Match fields that match the id attribute
142
143
  # @option options [String] name Match fields that match the name attribute
143
- # @option options [String, Array<String>] class Match fields that match the class(es) provided
144
+ # @option options [String, Array<String>, Regexp] class Match fields that match the class(es) provided
144
145
  # @macro label_click
145
146
  # @macro waiting_behavior
146
147
  #
147
148
  # @return [Capybara::Node::Element] The element checked or the label clicked
148
149
  def check(locator = nil, **options)
149
- _check_with_label(:checkbox, true, locator, options)
150
+ _check_with_label(:checkbox, true, locator, **options)
150
151
  end
151
152
 
152
153
  ##
153
154
  #
154
155
  # Find a descendant check box and uncheck it. The check box can be found
155
- # via name, id or label text. If no locator is provided this will match against
156
- # self or a descendant.
156
+ # via name, id, {Capybara.configure test_id} attribute, or label text. If
157
+ # no locator is provided this will match against self or a descendant.
157
158
  #
158
159
  # # will uncheck a descendant checkbox with a name, id, or label text matching 'German'
159
160
  # page.uncheck('German')
@@ -166,70 +167,82 @@ module Capybara
166
167
  # @param [String] locator Which check box to uncheck
167
168
  #
168
169
  # @option options [String] option Value of the checkbox to deselect
169
- # @option options [String] id Match fields that match the id attribute
170
+ # @option options [String, Regexp] id Match fields that match the id attribute
170
171
  # @option options [String] name Match fields that match the name attribute
171
- # @option options [String, Array<String>] class Match fields that match the class(es) provided
172
+ # @option options [String, Array<String>, Regexp] class Match fields that match the class(es) provided
172
173
  # @macro label_click
173
174
  # @macro waiting_behavior
174
175
  #
175
176
  # @return [Capybara::Node::Element] The element unchecked or the label clicked
176
177
  def uncheck(locator = nil, **options)
177
- _check_with_label(:checkbox, false, locator, options)
178
+ _check_with_label(:checkbox, false, locator, **options)
178
179
  end
179
180
 
180
181
  ##
181
182
  #
182
- # If `:from` option is present, `select` finds a select box, or text input with associated datalist,
183
+ # If `from` option is present, {#select} finds a select box, or text input with associated datalist,
183
184
  # on the page and selects a particular option from it.
184
185
  # Otherwise it finds an option inside current scope and selects it.
185
- # If the select box is a multiple select, +select+ can be called multiple times to select more than
186
+ # If the select box is a multiple select, {#select} can be called multiple times to select more than
186
187
  # one option.
187
- # The select box can be found via its name, id or label text. The option can be found by its text.
188
+ # The select box can be found via its name, id, {Capybara.configure test_id} attribute, or label text.
189
+ # The option can be found by its text.
188
190
  #
189
191
  # page.select 'March', from: 'Month'
190
192
  #
191
- # @macro waiting_behavior
193
+ # @overload select(value = nil, from: nil, **options)
194
+ # @macro waiting_behavior
192
195
  #
193
- # @param value [String] Which option to select
194
- # @param from [String] The id, Capybara.test_id atrtribute, name or label of the select box
196
+ # @param value [String] Which option to select
197
+ # @param from [String] The id, {Capybara.configure test_id} attribute, name or label of the select box
195
198
  #
196
199
  # @return [Capybara::Node::Element] The option element selected
197
200
  def select(value = nil, from: nil, **options)
201
+ raise ArgumentError, 'The :from option does not take an element' if from.is_a? Capybara::Node::Element
202
+
198
203
  el = from ? find_select_or_datalist_input(from, options) : self
199
204
 
200
205
  if el.respond_to?(:tag_name) && (el.tag_name == 'input')
201
206
  select_datalist_option(el, value)
202
207
  else
203
- el.find(:option, value, options).select_option
208
+ el.find(:option, value, **options).select_option
204
209
  end
205
210
  end
206
211
 
207
212
  ##
208
213
  #
209
214
  # Find a select box on the page and unselect a particular option from it. If the select
210
- # box is a multiple select, +unselect+ can be called multiple times to unselect more than
211
- # one option. The select box can be found via its name, id or label text.
215
+ # box is a multiple select, {#unselect} can be called multiple times to unselect more than
216
+ # one option. The select box can be found via its name, id, {Capybara.configure test_id} attribute,
217
+ # or label text.
212
218
  #
213
219
  # page.unselect 'March', from: 'Month'
214
220
  #
215
- # @macro waiting_behavior
221
+ # @overload unselect(value = nil, from: nil, **options)
222
+ # @macro waiting_behavior
223
+ #
224
+ # @param value [String] Which option to unselect
225
+ # @param from [String] The id, {Capybara.configure test_id} attribute, name or label of the select box
216
226
  #
217
- # @param value [String] Which option to unselect
218
- # @param from [String] The id, Capybara.test_id attribute, name or label of the select box
219
227
  #
220
228
  # @return [Capybara::Node::Element] The option element unselected
221
229
  def unselect(value = nil, from: nil, **options)
222
- scope = from ? find(:select, from, options) : self
223
- scope.find(:option, value, options).unselect_option
230
+ raise ArgumentError, 'The :from option does not take an element' if from.is_a? Capybara::Node::Element
231
+
232
+ scope = from ? find(:select, from, **options) : self
233
+ scope.find(:option, value, **options).unselect_option
224
234
  end
225
235
 
226
236
  ##
227
237
  #
228
- # Find a descendant file field on the page and attach a file given its path. The file field can
229
- # be found via its name, id or label text. In the case of the file field being hidden for
238
+ # Find a descendant file field on the page and attach a file given its path. There are two ways to use
239
+ # {#attach_file}, in the first method the file field can be found via its name, id,
240
+ # {Capybara.configure test_id} attribute, or label text. In the case of the file field being hidden for
230
241
  # styling reasons the `make_visible` option can be used to temporarily change the CSS of
231
242
  # the file field, attach the file, and then revert the CSS back to original. If no locator is
232
243
  # passed this will match self or a descendant.
244
+ # The second method, which is currently in beta and may be changed/removed, involves passing a block
245
+ # which performs whatever actions would trigger the file chooser to appear.
233
246
  #
234
247
  # # will attach file to a descendant file input element that has a name, id, or label_text matching 'My File'
235
248
  # page.attach_file('My File', '/path/to/file.png')
@@ -237,32 +250,57 @@ module Capybara
237
250
  # # will attach file to el if it's a file input element
238
251
  # el.attach_file('/path/to/file.png')
239
252
  #
253
+ # # will attach file to whatever file input is triggered by the block
254
+ # page.attach_file('/path/to/file.png') do
255
+ # page.find('#upload_button').click
256
+ # end
257
+ #
240
258
  # @overload attach_file([locator], paths, **options)
241
259
  # @macro waiting_behavior
242
260
  #
243
261
  # @param [String] locator Which field to attach the file to
244
262
  # @param [String, Array<String>] paths The path(s) of the file(s) that will be attached
245
263
  #
246
- # @option options [Symbol] match (Capybara.match) The matching strategy to use (:one, :first, :prefer_exact, :smart).
247
- # @option options [Boolean] exact (Capybara.exact) Match the exact label name/contents or accept a partial match.
264
+ # @option options [Symbol] match
265
+ # The matching strategy to use (:one, :first, :prefer_exact, :smart). Defaults to {Capybara.configure match}.
266
+ # @option options [Boolean] exact
267
+ # Match the exact label name/contents or accept a partial match. Defaults to {Capybara.configure exact}.
248
268
  # @option options [Boolean] multiple Match field which allows multiple file selection
249
- # @option options [String] id Match fields that match the id attribute
269
+ # @option options [String, Regexp] id Match fields that match the id attribute
250
270
  # @option options [String] name Match fields that match the name attribute
251
- # @option options [String, Array<String>] class Match fields that match the class(es) provided
252
- # @option options [true, Hash] make_visible A Hash of CSS styles to change before attempting to attach the file, if `true` { opacity: 1, display: 'block', visibility: 'visible' } is used (may not be supported by all drivers)
253
- #
254
- # @return [Capybara::Node::Element] The file field element
271
+ # @option options [String, Array<String>, Regexp] class Match fields that match the class(es) provided
272
+ # @option options [true, Hash] make_visible
273
+ # A Hash of CSS styles to change before attempting to attach the file, if `true`, `{ opacity: 1, display: 'block', visibility: 'visible' }` is used (may not be supported by all drivers).
274
+ # @overload attach_file(paths, &blk)
275
+ # @param [String, Array<String>] paths The path(s) of the file(s) that will be attached
276
+ # @yield Block whose actions will trigger the system file chooser to be shown
277
+ # @return [Capybara::Node::Element] The file field element
255
278
  def attach_file(locator = nil, paths, make_visible: nil, **options) # rubocop:disable Style/OptionalArguments
279
+ if locator && block_given?
280
+ raise ArgumentError, '``#attach_file` does not support passing both a locator and a block'
281
+ end
282
+
256
283
  Array(paths).each do |path|
257
284
  raise Capybara::FileNotFound, "cannot attach file, #{path} does not exist" unless File.exist?(path.to_s)
258
285
  end
259
286
  options[:allow_self] = true if locator.nil?
287
+
288
+ if block_given?
289
+ begin
290
+ execute_script CAPTURE_FILE_ELEMENT_SCRIPT
291
+ yield
292
+ file_field = evaluate_script 'window._capybara_clicked_file_input'
293
+ raise ArgumentError, "Capybara was unable to determine the file input you're attaching to" unless file_field
294
+ rescue ::Capybara::NotSupportedByDriverError
295
+ warn 'Block mode of `#attach_file` is not supported by the current driver - ignoring.'
296
+ end
297
+ end
260
298
  # Allow user to update the CSS style of the file input since they are so often hidden on a page
261
299
  if make_visible
262
- ff = find(:file_field, locator, options.merge(visible: :all))
300
+ ff = file_field || find(:file_field, locator, **options.merge(visible: :all))
263
301
  while_visible(ff, make_visible) { |el| el.set(paths) }
264
302
  else
265
- find(:file_field, locator, options).set(paths)
303
+ (file_field || find(:file_field, locator, **options)).set(paths)
266
304
  end
267
305
  end
268
306
 
@@ -270,15 +308,14 @@ module Capybara
270
308
 
271
309
  def find_select_or_datalist_input(from, options)
272
310
  synchronize(Capybara::Queries::BaseQuery.wait(options, session_options.default_max_wait_time)) do
311
+ find(:select, from, **options)
312
+ rescue Capybara::ElementNotFound => select_error # rubocop:disable Naming/RescuedExceptionsVariableName
313
+ raise if %i[selected with_selected multiple].any? { |option| options.key?(option) }
314
+
273
315
  begin
274
- find(:select, from, options)
275
- rescue Capybara::ElementNotFound => select_error
276
- raise if %i[selected with_selected multiple].any? { |option| options.key?(option) }
277
- begin
278
- find(:datalist_input, from, options)
279
- rescue Capybara::ElementNotFound => dlinput_error
280
- raise Capybara::ElementNotFound, "#{select_error.message} and #{dlinput_error.message}"
281
- end
316
+ find(:datalist_input, from, **options)
317
+ rescue Capybara::ElementNotFound => dlinput_error # rubocop:disable Naming/RescuedExceptionsVariableName
318
+ raise Capybara::ElementNotFound, "#{select_error.message} and #{dlinput_error.message}"
282
319
  end
283
320
  end
284
321
  end
@@ -287,6 +324,7 @@ module Capybara
287
324
  datalist_options = input.evaluate_script(DATALIST_OPTIONS_SCRIPT)
288
325
  option = datalist_options.find { |opt| opt.values_at('value', 'label').include?(value) }
289
326
  raise ::Capybara::ElementNotFound, %(Unable to find datalist option "#{value}") unless option
327
+
290
328
  input.set(option['value'])
291
329
  rescue ::Capybara::NotSupportedByDriverError
292
330
  # Implement for drivers that don't support JS
@@ -296,9 +334,14 @@ module Capybara
296
334
  end
297
335
 
298
336
  def while_visible(element, visible_css)
299
- visible_css = { opacity: 1, display: 'block', visibility: 'visible' } if visible_css == true
337
+ if visible_css == true
338
+ visible_css = { opacity: 1, display: 'block', visibility: 'visible', width: 'auto', height: 'auto' }
339
+ end
300
340
  _update_style(element, visible_css)
301
- raise ExpectationNotMet, 'The style changes in :make_visible did not make the file input visible' unless element.visible?
341
+ unless element.visible?
342
+ raise ExpectationNotMet, 'The style changes in :make_visible did not make the file input visible'
343
+ end
344
+
302
345
  begin
303
346
  yield element
304
347
  ensure
@@ -314,24 +357,24 @@ module Capybara
314
357
 
315
358
  def _reset_style(element)
316
359
  element.execute_script(RESET_STYLE_SCRIPT)
317
- rescue StandardError # rubocop:disable Lint/HandleExceptions swallow extra errors
360
+ rescue StandardError # rubocop:disable Lint/SuppressedException swallow extra errors
318
361
  end
319
362
 
320
- def _check_with_label(selector, checked, locator, allow_label_click: session_options.automatic_label_click, **options)
363
+ def _check_with_label(selector, checked, locator,
364
+ allow_label_click: session_options.automatic_label_click, **options)
321
365
  options[:allow_self] = true if locator.nil?
322
366
 
323
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
+
324
373
  begin
325
- el = find(selector, locator, options)
326
- el.set(checked)
327
- rescue StandardError => err
328
- raise unless allow_label_click && catch_error?(err)
329
- begin
330
- el ||= find(selector, locator, options.merge(visible: :all))
331
- el.session.find(:label, for: el, visible: true).click unless el.checked? == checked
332
- rescue StandardError # swallow extra errors - raise original
333
- raise err
334
- end
374
+ el ||= find(selector, locator, **options.merge(visible: :all))
375
+ el.session.find(:label, for: el, visible: true).click unless el.checked? == checked
376
+ rescue StandardError # swallow extra errors - raise original
377
+ raise e
335
378
  end
336
379
  end
337
380
  end
@@ -358,6 +401,16 @@ module Capybara
358
401
  filter(function(el){ return !el.disabled }).
359
402
  map(function(el){ return { "value": el.value, "label": el.label} })
360
403
  JS
404
+
405
+ CAPTURE_FILE_ELEMENT_SCRIPT = <<~'JS'
406
+ document.addEventListener('click', function file_catcher(e){
407
+ if (e.target.matches("input[type='file']")) {
408
+ window._capybara_clicked_file_input = e.target;
409
+ this.removeEventListener('click', file_catcher);
410
+ e.preventDefault();
411
+ }
412
+ })
413
+ JS
361
414
  end
362
415
  end
363
416
  end
@@ -73,19 +73,28 @@ module Capybara
73
73
  # @return [Object] The result of the given block
74
74
  # @raise [Capybara::FrozenInTime] If the return value of `Time.now` appears stuck
75
75
  #
76
- def synchronize(seconds = session_options.default_max_wait_time, errors: nil)
76
+ def synchronize(seconds = nil, errors: nil)
77
77
  return yield if session.synchronized
78
78
 
79
+ seconds = session_options.default_max_wait_time if [nil, true].include? seconds
79
80
  session.synchronized = true
80
81
  timer = Capybara::Helpers.timer(expire_in: seconds)
81
82
  begin
82
83
  yield
83
- rescue StandardError => err
84
+ rescue StandardError => e
84
85
  session.raise_server_error!
85
- raise err unless driver.wait? && catch_error?(err, errors)
86
- raise err if timer.expired?
87
- sleep(0.05)
88
- reload if session_options.automatic_reload
86
+ raise e unless catch_error?(e, errors)
87
+
88
+ if driver.wait?
89
+ raise e if timer.expired?
90
+
91
+ sleep(0.01)
92
+ reload if session_options.automatic_reload
93
+ else
94
+ old_base = @base
95
+ reload if session_options.automatic_reload
96
+ raise e if old_base == @base
97
+ end
89
98
  retry
90
99
  ensure
91
100
  session.synchronized = false
@@ -93,13 +102,21 @@ module Capybara
93
102
  end
94
103
 
95
104
  # @api private
96
- def find_css(css)
97
- base.find_css(css)
105
+ def find_css(css, **options)
106
+ if base.method(:find_css).arity != 1
107
+ base.find_css(css, **options)
108
+ else
109
+ base.find_css(css)
110
+ end
98
111
  end
99
112
 
100
113
  # @api private
101
- def find_xpath(xpath)
102
- base.find_xpath(xpath)
114
+ def find_xpath(xpath, **options)
115
+ if base.method(:find_xpath).arity != 1
116
+ base.find_xpath(xpath, **options)
117
+ else
118
+ base.find_xpath(xpath)
119
+ end
103
120
  end
104
121
 
105
122
  # @api private
@@ -31,6 +31,18 @@ module Capybara
31
31
  def title
32
32
  session.driver.title
33
33
  end
34
+
35
+ def execute_script(*args)
36
+ find(:xpath, '/html').execute_script(*args)
37
+ end
38
+
39
+ def evaluate_script(*args)
40
+ find(:xpath, '/html').evaluate_script(*args)
41
+ end
42
+
43
+ def scroll_to(*args, **options)
44
+ find(:xpath, '//body').scroll_to(*args, **options)
45
+ end
34
46
  end
35
47
  end
36
48
  end
@@ -17,7 +17,9 @@ module Capybara
17
17
  # @return [true]
18
18
  #
19
19
  def assert_title(title, **options)
20
- _verify_title(title, options) { |query| raise Capybara::ExpectationNotMet, query.failure_message unless query.resolves_for?(self) }
20
+ _verify_title(title, options) do |query|
21
+ raise Capybara::ExpectationNotMet, query.failure_message unless query.resolves_for?(self)
22
+ end
21
23
  end
22
24
 
23
25
  ##
@@ -28,7 +30,9 @@ module Capybara
28
30
  # @return [true]
29
31
  #
30
32
  def assert_no_title(title, **options)
31
- _verify_title(title, options) { |query| raise Capybara::ExpectationNotMet, query.negative_failure_message if query.resolves_for?(self) }
33
+ _verify_title(title, options) do |query|
34
+ raise Capybara::ExpectationNotMet, query.negative_failure_message if query.resolves_for?(self)
35
+ end
32
36
  end
33
37
 
34
38
  ##
@@ -38,7 +42,7 @@ module Capybara
38
42
  # @return [Boolean]
39
43
  #
40
44
  def has_title?(title, **options)
41
- make_predicate(options) { assert_title(title, options) }
45
+ make_predicate(options) { assert_title(title, **options) }
42
46
  end
43
47
 
44
48
  ##
@@ -48,13 +52,13 @@ module Capybara
48
52
  # @return [Boolean]
49
53
  #
50
54
  def has_no_title?(title, **options)
51
- make_predicate(options) { assert_no_title(title, options) }
55
+ make_predicate(options) { assert_no_title(title, **options) }
52
56
  end
53
57
 
54
58
  private
55
59
 
56
60
  def _verify_title(title, options)
57
- query = Capybara::Queries::TitleQuery.new(title, options)
61
+ query = Capybara::Queries::TitleQuery.new(title, **options)
58
62
  synchronize(query.wait) { yield(query) }
59
63
  true
60
64
  end