capybara 2.15.1 → 3.35.3

Sign up to get free protection for your applications and to get access to all the features.
Files changed (298) hide show
  1. checksums.yaml +5 -5
  2. data/.yardopts +1 -1
  3. data/History.md +871 -9
  4. data/License.txt +1 -1
  5. data/README.md +99 -75
  6. data/lib/capybara/config.rb +20 -59
  7. data/lib/capybara/cucumber.rb +2 -3
  8. data/lib/capybara/driver/base.rb +35 -18
  9. data/lib/capybara/driver/node.rb +35 -9
  10. data/lib/capybara/dsl.rb +15 -6
  11. data/lib/capybara/helpers.rb +72 -28
  12. data/lib/capybara/minitest/spec.rb +173 -81
  13. data/lib/capybara/minitest.rb +220 -111
  14. data/lib/capybara/node/actions.rb +270 -172
  15. data/lib/capybara/node/base.rb +41 -34
  16. data/lib/capybara/node/document.rb +15 -3
  17. data/lib/capybara/node/document_matchers.rb +19 -21
  18. data/lib/capybara/node/element.rb +353 -137
  19. data/lib/capybara/node/finders.rb +144 -138
  20. data/lib/capybara/node/matchers.rb +369 -209
  21. data/lib/capybara/node/simple.rb +55 -26
  22. data/lib/capybara/queries/ancestor_query.rb +11 -9
  23. data/lib/capybara/queries/base_query.rb +39 -28
  24. data/lib/capybara/queries/current_path_query.rb +22 -25
  25. data/lib/capybara/queries/match_query.rb +14 -7
  26. data/lib/capybara/queries/selector_query.rb +633 -145
  27. data/lib/capybara/queries/sibling_query.rb +10 -9
  28. data/lib/capybara/queries/style_query.rb +45 -0
  29. data/lib/capybara/queries/text_query.rb +56 -38
  30. data/lib/capybara/queries/title_query.rb +8 -11
  31. data/lib/capybara/rack_test/browser.rb +57 -41
  32. data/lib/capybara/rack_test/css_handlers.rb +6 -4
  33. data/lib/capybara/rack_test/driver.rb +18 -13
  34. data/lib/capybara/rack_test/errors.rb +6 -0
  35. data/lib/capybara/rack_test/form.rb +73 -58
  36. data/lib/capybara/rack_test/node.rb +182 -78
  37. data/lib/capybara/rails.rb +3 -7
  38. data/lib/capybara/registration_container.rb +44 -0
  39. data/lib/capybara/registrations/drivers.rb +42 -0
  40. data/lib/capybara/registrations/patches/puma_ssl.rb +29 -0
  41. data/lib/capybara/registrations/servers.rb +45 -0
  42. data/lib/capybara/result.rb +96 -62
  43. data/lib/capybara/rspec/features.rb +17 -50
  44. data/lib/capybara/rspec/matcher_proxies.rb +51 -14
  45. data/lib/capybara/rspec/matchers/base.rb +111 -0
  46. data/lib/capybara/rspec/matchers/become_closed.rb +33 -0
  47. data/lib/capybara/rspec/matchers/compound.rb +88 -0
  48. data/lib/capybara/rspec/matchers/count_sugar.rb +37 -0
  49. data/lib/capybara/rspec/matchers/have_ancestor.rb +28 -0
  50. data/lib/capybara/rspec/matchers/have_current_path.rb +29 -0
  51. data/lib/capybara/rspec/matchers/have_selector.rb +77 -0
  52. data/lib/capybara/rspec/matchers/have_sibling.rb +27 -0
  53. data/lib/capybara/rspec/matchers/have_text.rb +33 -0
  54. data/lib/capybara/rspec/matchers/have_title.rb +29 -0
  55. data/lib/capybara/rspec/matchers/match_selector.rb +27 -0
  56. data/lib/capybara/rspec/matchers/match_style.rb +43 -0
  57. data/lib/capybara/rspec/matchers/spatial_sugar.rb +39 -0
  58. data/lib/capybara/rspec/matchers.rb +144 -264
  59. data/lib/capybara/rspec.rb +7 -11
  60. data/lib/capybara/selector/builders/css_builder.rb +84 -0
  61. data/lib/capybara/selector/builders/xpath_builder.rb +71 -0
  62. data/lib/capybara/selector/css.rb +89 -17
  63. data/lib/capybara/selector/definition/button.rb +63 -0
  64. data/lib/capybara/selector/definition/checkbox.rb +26 -0
  65. data/lib/capybara/selector/definition/css.rb +10 -0
  66. data/lib/capybara/selector/definition/datalist_input.rb +35 -0
  67. data/lib/capybara/selector/definition/datalist_option.rb +25 -0
  68. data/lib/capybara/selector/definition/element.rb +28 -0
  69. data/lib/capybara/selector/definition/field.rb +40 -0
  70. data/lib/capybara/selector/definition/fieldset.rb +14 -0
  71. data/lib/capybara/selector/definition/file_field.rb +13 -0
  72. data/lib/capybara/selector/definition/fillable_field.rb +33 -0
  73. data/lib/capybara/selector/definition/frame.rb +17 -0
  74. data/lib/capybara/selector/definition/id.rb +6 -0
  75. data/lib/capybara/selector/definition/label.rb +62 -0
  76. data/lib/capybara/selector/definition/link.rb +54 -0
  77. data/lib/capybara/selector/definition/link_or_button.rb +16 -0
  78. data/lib/capybara/selector/definition/option.rb +27 -0
  79. data/lib/capybara/selector/definition/radio_button.rb +27 -0
  80. data/lib/capybara/selector/definition/select.rb +81 -0
  81. data/lib/capybara/selector/definition/table.rb +109 -0
  82. data/lib/capybara/selector/definition/table_row.rb +21 -0
  83. data/lib/capybara/selector/definition/xpath.rb +5 -0
  84. data/lib/capybara/selector/definition.rb +278 -0
  85. data/lib/capybara/selector/filter.rb +2 -17
  86. data/lib/capybara/selector/filter_set.rb +83 -33
  87. data/lib/capybara/selector/filters/base.rb +50 -6
  88. data/lib/capybara/selector/filters/expression_filter.rb +8 -26
  89. data/lib/capybara/selector/filters/locator_filter.rb +29 -0
  90. data/lib/capybara/selector/filters/node_filter.rb +16 -12
  91. data/lib/capybara/selector/regexp_disassembler.rb +214 -0
  92. data/lib/capybara/selector/selector.rb +89 -210
  93. data/lib/capybara/selector/xpath_extensions.rb +17 -0
  94. data/lib/capybara/selector.rb +226 -526
  95. data/lib/capybara/selenium/atoms/getAttribute.min.js +1 -0
  96. data/lib/capybara/selenium/atoms/isDisplayed.min.js +1 -0
  97. data/lib/capybara/selenium/atoms/src/getAttribute.js +161 -0
  98. data/lib/capybara/selenium/atoms/src/isDisplayed.js +454 -0
  99. data/lib/capybara/selenium/driver.rb +334 -277
  100. data/lib/capybara/selenium/driver_specializations/chrome_driver.rb +117 -0
  101. data/lib/capybara/selenium/driver_specializations/edge_driver.rb +124 -0
  102. data/lib/capybara/selenium/driver_specializations/firefox_driver.rb +89 -0
  103. data/lib/capybara/selenium/driver_specializations/internet_explorer_driver.rb +26 -0
  104. data/lib/capybara/selenium/driver_specializations/safari_driver.rb +24 -0
  105. data/lib/capybara/selenium/extensions/file_input_click_emulation.rb +34 -0
  106. data/lib/capybara/selenium/extensions/find.rb +110 -0
  107. data/lib/capybara/selenium/extensions/html5_drag.rb +228 -0
  108. data/lib/capybara/selenium/extensions/modifier_keys_stack.rb +28 -0
  109. data/lib/capybara/selenium/extensions/scroll.rb +76 -0
  110. data/lib/capybara/selenium/logger_suppressor.rb +40 -0
  111. data/lib/capybara/selenium/node.rb +506 -124
  112. data/lib/capybara/selenium/nodes/chrome_node.rb +137 -0
  113. data/lib/capybara/selenium/nodes/edge_node.rb +104 -0
  114. data/lib/capybara/selenium/nodes/firefox_node.rb +136 -0
  115. data/lib/capybara/selenium/nodes/ie_node.rb +22 -0
  116. data/lib/capybara/selenium/nodes/safari_node.rb +118 -0
  117. data/lib/capybara/selenium/patches/action_pauser.rb +26 -0
  118. data/lib/capybara/selenium/patches/atoms.rb +18 -0
  119. data/lib/capybara/selenium/patches/is_displayed.rb +16 -0
  120. data/lib/capybara/selenium/patches/logs.rb +45 -0
  121. data/lib/capybara/selenium/patches/pause_duration_fix.rb +9 -0
  122. data/lib/capybara/selenium/patches/persistent_client.rb +20 -0
  123. data/lib/capybara/server/animation_disabler.rb +63 -0
  124. data/lib/capybara/server/checker.rb +44 -0
  125. data/lib/capybara/server/middleware.rb +71 -0
  126. data/lib/capybara/server.rb +59 -67
  127. data/lib/capybara/session/config.rb +79 -59
  128. data/lib/capybara/session/matchers.rb +41 -25
  129. data/lib/capybara/session.rb +360 -356
  130. data/lib/capybara/spec/public/jquery.js +5 -5
  131. data/lib/capybara/spec/public/offset.js +6 -0
  132. data/lib/capybara/spec/public/test.js +159 -13
  133. data/lib/capybara/spec/session/accept_alert_spec.rb +12 -11
  134. data/lib/capybara/spec/session/accept_confirm_spec.rb +6 -5
  135. data/lib/capybara/spec/session/accept_prompt_spec.rb +34 -6
  136. data/lib/capybara/spec/session/all_spec.rb +163 -55
  137. data/lib/capybara/spec/session/ancestor_spec.rb +27 -24
  138. data/lib/capybara/spec/session/assert_all_of_selectors_spec.rb +68 -38
  139. data/lib/capybara/spec/session/assert_current_path_spec.rb +75 -0
  140. data/lib/capybara/spec/session/assert_selector_spec.rb +143 -0
  141. data/lib/capybara/spec/session/assert_style_spec.rb +26 -0
  142. data/lib/capybara/spec/session/{assert_text.rb → assert_text_spec.rb} +91 -59
  143. data/lib/capybara/spec/session/{assert_title.rb → assert_title_spec.rb} +22 -12
  144. data/lib/capybara/spec/session/attach_file_spec.rb +138 -69
  145. data/lib/capybara/spec/session/body_spec.rb +12 -13
  146. data/lib/capybara/spec/session/check_spec.rb +107 -55
  147. data/lib/capybara/spec/session/choose_spec.rb +58 -31
  148. data/lib/capybara/spec/session/click_button_spec.rb +231 -173
  149. data/lib/capybara/spec/session/click_link_or_button_spec.rb +55 -35
  150. data/lib/capybara/spec/session/click_link_spec.rb +82 -58
  151. data/lib/capybara/spec/session/current_scope_spec.rb +11 -10
  152. data/lib/capybara/spec/session/current_url_spec.rb +57 -39
  153. data/lib/capybara/spec/session/dismiss_confirm_spec.rb +4 -4
  154. data/lib/capybara/spec/session/dismiss_prompt_spec.rb +3 -2
  155. data/lib/capybara/spec/session/element/{assert_match_selector.rb → assert_match_selector_spec.rb} +11 -9
  156. data/lib/capybara/spec/session/element/match_css_spec.rb +18 -10
  157. data/lib/capybara/spec/session/element/match_xpath_spec.rb +9 -7
  158. data/lib/capybara/spec/session/element/matches_selector_spec.rb +71 -57
  159. data/lib/capybara/spec/session/evaluate_async_script_spec.rb +23 -0
  160. data/lib/capybara/spec/session/evaluate_script_spec.rb +30 -9
  161. data/lib/capybara/spec/session/execute_script_spec.rb +10 -8
  162. data/lib/capybara/spec/session/fill_in_spec.rb +128 -43
  163. data/lib/capybara/spec/session/find_button_spec.rb +25 -24
  164. data/lib/capybara/spec/session/find_by_id_spec.rb +10 -9
  165. data/lib/capybara/spec/session/find_field_spec.rb +37 -41
  166. data/lib/capybara/spec/session/find_link_spec.rb +36 -17
  167. data/lib/capybara/spec/session/find_spec.rb +245 -144
  168. data/lib/capybara/spec/session/first_spec.rb +79 -51
  169. data/lib/capybara/spec/session/frame/frame_title_spec.rb +23 -0
  170. data/lib/capybara/spec/session/frame/frame_url_spec.rb +23 -0
  171. data/lib/capybara/spec/session/frame/switch_to_frame_spec.rb +33 -20
  172. data/lib/capybara/spec/session/frame/within_frame_spec.rb +50 -32
  173. data/lib/capybara/spec/session/go_back_spec.rb +2 -1
  174. data/lib/capybara/spec/session/go_forward_spec.rb +2 -1
  175. data/lib/capybara/spec/session/has_all_selectors_spec.rb +69 -0
  176. data/lib/capybara/spec/session/has_ancestor_spec.rb +46 -0
  177. data/lib/capybara/spec/session/has_any_selectors_spec.rb +25 -0
  178. data/lib/capybara/spec/session/has_button_spec.rb +70 -13
  179. data/lib/capybara/spec/session/has_css_spec.rb +272 -137
  180. data/lib/capybara/spec/session/has_current_path_spec.rb +87 -45
  181. data/lib/capybara/spec/session/has_field_spec.rb +115 -59
  182. data/lib/capybara/spec/session/has_link_spec.rb +10 -9
  183. data/lib/capybara/spec/session/has_none_selectors_spec.rb +78 -0
  184. data/lib/capybara/spec/session/has_select_spec.rb +103 -74
  185. data/lib/capybara/spec/session/has_selector_spec.rb +105 -71
  186. data/lib/capybara/spec/session/has_sibling_spec.rb +50 -0
  187. data/lib/capybara/spec/session/has_table_spec.rb +172 -5
  188. data/lib/capybara/spec/session/has_text_spec.rb +113 -61
  189. data/lib/capybara/spec/session/has_title_spec.rb +20 -14
  190. data/lib/capybara/spec/session/has_xpath_spec.rb +57 -38
  191. data/lib/capybara/spec/session/{headers.rb → headers_spec.rb} +3 -2
  192. data/lib/capybara/spec/session/html_spec.rb +14 -6
  193. data/lib/capybara/spec/session/matches_style_spec.rb +35 -0
  194. data/lib/capybara/spec/session/node_spec.rb +950 -152
  195. data/lib/capybara/spec/session/node_wrapper_spec.rb +39 -0
  196. data/lib/capybara/spec/session/refresh_spec.rb +12 -6
  197. data/lib/capybara/spec/session/reset_session_spec.rb +69 -35
  198. data/lib/capybara/spec/session/{response_code.rb → response_code_spec.rb} +2 -1
  199. data/lib/capybara/spec/session/save_and_open_page_spec.rb +3 -2
  200. data/lib/capybara/spec/session/save_and_open_screenshot_spec.rb +8 -12
  201. data/lib/capybara/spec/session/save_page_spec.rb +42 -55
  202. data/lib/capybara/spec/session/save_screenshot_spec.rb +16 -14
  203. data/lib/capybara/spec/session/screenshot_spec.rb +2 -2
  204. data/lib/capybara/spec/session/scroll_spec.rb +117 -0
  205. data/lib/capybara/spec/session/select_spec.rb +107 -80
  206. data/lib/capybara/spec/session/selectors_spec.rb +52 -19
  207. data/lib/capybara/spec/session/sibling_spec.rb +10 -10
  208. data/lib/capybara/spec/session/text_spec.rb +37 -21
  209. data/lib/capybara/spec/session/title_spec.rb +17 -5
  210. data/lib/capybara/spec/session/uncheck_spec.rb +42 -22
  211. data/lib/capybara/spec/session/unselect_spec.rb +39 -38
  212. data/lib/capybara/spec/session/visit_spec.rb +99 -32
  213. data/lib/capybara/spec/session/window/become_closed_spec.rb +24 -20
  214. data/lib/capybara/spec/session/window/current_window_spec.rb +5 -3
  215. data/lib/capybara/spec/session/window/open_new_window_spec.rb +5 -3
  216. data/lib/capybara/spec/session/window/switch_to_window_spec.rb +27 -22
  217. data/lib/capybara/spec/session/window/window_opened_by_spec.rb +12 -6
  218. data/lib/capybara/spec/session/window/window_spec.rb +97 -63
  219. data/lib/capybara/spec/session/window/windows_spec.rb +12 -10
  220. data/lib/capybara/spec/session/window/within_window_spec.rb +31 -86
  221. data/lib/capybara/spec/session/within_spec.rb +70 -44
  222. data/lib/capybara/spec/spec_helper.rb +48 -43
  223. data/lib/capybara/spec/test_app.rb +78 -40
  224. data/lib/capybara/spec/views/animated.erb +49 -0
  225. data/lib/capybara/spec/views/form.erb +130 -39
  226. data/lib/capybara/spec/views/frame_child.erb +3 -2
  227. data/lib/capybara/spec/views/frame_one.erb +1 -0
  228. data/lib/capybara/spec/views/initial_alert.erb +10 -0
  229. data/lib/capybara/spec/views/obscured.erb +47 -0
  230. data/lib/capybara/spec/views/offset.erb +32 -0
  231. data/lib/capybara/spec/views/react.erb +45 -0
  232. data/lib/capybara/spec/views/scroll.erb +20 -0
  233. data/lib/capybara/spec/views/spatial.erb +31 -0
  234. data/lib/capybara/spec/views/tables.erb +68 -1
  235. data/lib/capybara/spec/views/with_animation.erb +82 -0
  236. data/lib/capybara/spec/views/with_dragula.erb +24 -0
  237. data/lib/capybara/spec/views/with_fixed_header_footer.erb +17 -0
  238. data/lib/capybara/spec/views/with_hover.erb +6 -0
  239. data/lib/capybara/spec/views/with_hover1.erb +10 -0
  240. data/lib/capybara/spec/views/with_html.erb +69 -10
  241. data/lib/capybara/spec/views/with_html5_svg.erb +20 -0
  242. data/lib/capybara/spec/views/with_jquery_animation.erb +24 -0
  243. data/lib/capybara/spec/views/with_js.erb +37 -0
  244. data/lib/capybara/spec/views/with_jstree.erb +26 -0
  245. data/lib/capybara/spec/views/with_namespace.erb +20 -0
  246. data/lib/capybara/spec/views/with_scope_other.erb +6 -0
  247. data/lib/capybara/spec/views/with_sortable_js.erb +21 -0
  248. data/lib/capybara/spec/views/within_frames.erb +4 -1
  249. data/lib/capybara/version.rb +2 -1
  250. data/lib/capybara/window.rb +35 -33
  251. data/lib/capybara.rb +131 -104
  252. data/spec/basic_node_spec.rb +47 -34
  253. data/spec/capybara_spec.rb +53 -104
  254. data/spec/css_builder_spec.rb +101 -0
  255. data/spec/css_splitter_spec.rb +38 -0
  256. data/spec/dsl_spec.rb +81 -62
  257. data/spec/filter_set_spec.rb +27 -9
  258. data/spec/fixtures/certificate.pem +25 -0
  259. data/spec/fixtures/key.pem +27 -0
  260. data/spec/fixtures/selenium_driver_rspec_failure.rb +5 -4
  261. data/spec/fixtures/selenium_driver_rspec_success.rb +5 -4
  262. data/spec/minitest_spec.rb +49 -7
  263. data/spec/minitest_spec_spec.rb +94 -59
  264. data/spec/per_session_config_spec.rb +14 -13
  265. data/spec/rack_test_spec.rb +176 -125
  266. data/spec/regexp_dissassembler_spec.rb +250 -0
  267. data/spec/result_spec.rb +101 -46
  268. data/spec/rspec/features_spec.rb +37 -31
  269. data/spec/rspec/scenarios_spec.rb +9 -7
  270. data/spec/rspec/shared_spec_matchers.rb +448 -421
  271. data/spec/rspec/views_spec.rb +5 -3
  272. data/spec/rspec_matchers_spec.rb +27 -11
  273. data/spec/rspec_spec.rb +109 -89
  274. data/spec/sauce_spec_chrome.rb +43 -0
  275. data/spec/selector_spec.rb +396 -67
  276. data/spec/selenium_spec_chrome.rb +184 -35
  277. data/spec/selenium_spec_chrome_remote.rb +100 -0
  278. data/spec/selenium_spec_edge.rb +47 -0
  279. data/spec/selenium_spec_firefox.rb +183 -41
  280. data/spec/selenium_spec_firefox_remote.rb +80 -0
  281. data/spec/selenium_spec_ie.rb +150 -0
  282. data/spec/selenium_spec_safari.rb +148 -0
  283. data/spec/server_spec.rb +198 -99
  284. data/spec/session_spec.rb +53 -16
  285. data/spec/shared_selenium_node.rb +83 -0
  286. data/spec/shared_selenium_session.rb +486 -97
  287. data/spec/spec_helper.rb +93 -7
  288. data/spec/xpath_builder_spec.rb +93 -0
  289. metadata +338 -64
  290. data/.yard/templates_custom/default/class/html/selectors.erb +0 -38
  291. data/.yard/templates_custom/default/class/html/setup.rb +0 -17
  292. data/.yard/yard_extensions.rb +0 -78
  293. data/lib/capybara/query.rb +0 -7
  294. data/lib/capybara/rspec/compound.rb +0 -95
  295. data/lib/capybara/spec/session/assert_current_path.rb +0 -72
  296. data/lib/capybara/spec/session/assert_selector.rb +0 -148
  297. data/lib/capybara/spec/session/source_spec.rb +0 -0
  298. data/spec/selenium_spec_marionette.rb +0 -117
@@ -1,60 +1,68 @@
1
1
  # frozen_string_literal: true
2
+
2
3
  require 'delegate'
3
4
 
4
5
  module Capybara
5
6
  class SessionConfig
6
- OPTIONS = [:always_include_port, :run_server, :default_selector, :default_max_wait_time, :ignore_hidden_elements,
7
- :automatic_reload, :match, :exact, :exact_text, :raise_server_errors, :visible_text_only, :wait_on_first_by_default,
8
- :automatic_label_click, :enable_aria_label, :save_path, :exact_options, :asset_host, :default_host, :app_host,
9
- :save_and_open_page_path, :server_host, :server_port, :server_errors]
7
+ OPTIONS = %i[always_include_port run_server default_selector default_max_wait_time ignore_hidden_elements
8
+ automatic_reload match exact exact_text raise_server_errors visible_text_only
9
+ automatic_label_click enable_aria_label save_path asset_host default_host app_host
10
+ server_host server_port server_errors default_set_options disable_animation test_id
11
+ predicates_wait default_normalize_ws w3c_click_offset enable_aria_role].freeze
10
12
 
11
13
  attr_accessor(*OPTIONS)
12
14
 
13
15
  ##
14
- #@!method always_include_port
15
- # See {Capybara.configure}
16
- #@!method run_server
17
- # See {Capybara.configure}
18
- #@!method default_selector
19
- # See {Capybara.configure}
20
- #@!method default_max_wait_time
21
- # See {Capybara.configure}
22
- #@!method ignore_hidden_elements
23
- # See {Capybara.configure}
24
- #@!method automatic_reload
25
- # See {Capybara.configure}
26
- #@!method match
27
- # See {Capybara.configure}
28
- #@!method exact
29
- # See {Capybara.configure}
30
- #@!method raise_server_errors
31
- # See {Capybara.configure}
32
- #@!method visible_text_only
33
- # See {Capybara.configure}
34
- #@!method wait_on_first_by_default
35
- # See {Capybara.configure}
36
- #@!method automatic_label_click
37
- # See {Capybara.configure}
38
- #@!method enable_aria_label
39
- # See {Capybara.configure}
40
- #@!method save_path
41
- # See {Capybara.configure}
42
- #@!method exact_options
43
- # See {Capybara.configure}
44
- #@!method asset_host
45
- # See {Capybara.configure}
46
- #@!method default_host
47
- # See {Capybara.configure}
48
- #@!method app_host
49
- # See {Capybara.configure}
50
- #@!method save_and_open_page_path
51
- # See {Capybara.configure}
52
- #@!method server_host
53
- # See {Capybara.configure}
54
- #@!method server_port
55
- # See {Capybara.configure}
56
- #@!method server_errors
57
- # See {Capybara.configure}
16
+ # @!method always_include_port
17
+ # See {Capybara.configure}
18
+ # @!method run_server
19
+ # See {Capybara.configure}
20
+ # @!method default_selector
21
+ # See {Capybara.configure}
22
+ # @!method default_max_wait_time
23
+ # See {Capybara.configure}
24
+ # @!method ignore_hidden_elements
25
+ # See {Capybara.configure}
26
+ # @!method automatic_reload
27
+ # See {Capybara.configure}
28
+ # @!method match
29
+ # See {Capybara.configure}
30
+ # @!method exact
31
+ # See {Capybara.configure}
32
+ # @!method raise_server_errors
33
+ # See {Capybara.configure}
34
+ # @!method visible_text_only
35
+ # See {Capybara.configure}
36
+ # @!method automatic_label_click
37
+ # See {Capybara.configure}
38
+ # @!method enable_aria_label
39
+ # See {Capybara.configure}
40
+ # @!method enable_aria_role
41
+ # See {Capybara.configure}
42
+ # @!method save_path
43
+ # See {Capybara.configure}
44
+ # @!method asset_host
45
+ # See {Capybara.configure}
46
+ # @!method default_host
47
+ # See {Capybara.configure}
48
+ # @!method app_host
49
+ # See {Capybara.configure}
50
+ # @!method server_host
51
+ # See {Capybara.configure}
52
+ # @!method server_port
53
+ # See {Capybara.configure}
54
+ # @!method server_errors
55
+ # See {Capybara.configure}
56
+ # @!method default_set_options
57
+ # See {Capybara.configure}
58
+ # @!method disable_animation
59
+ # See {Capybara.configure}
60
+ # @!method test_id
61
+ # See {Capybara.configure}
62
+ # @!method default_normalize_ws
63
+ # See {Capybara.configure}
64
+ # @!method w3c_click_offset
65
+ # See {Capybara.configure}
58
66
 
59
67
  remove_method :server_host
60
68
 
@@ -73,21 +81,33 @@ module Capybara
73
81
 
74
82
  remove_method :app_host=
75
83
  def app_host=(url)
76
- raise ArgumentError.new("Capybara.app_host should be set to a url (http://www.example.com)") unless url.nil? || (url =~ URI::Parser.new.make_regexp)
84
+ unless url.nil? || url.match?(URI::DEFAULT_PARSER.make_regexp)
85
+ raise ArgumentError, "Capybara.app_host should be set to a url (http://www.example.com). Attempted to set #{url.inspect}."
86
+ end
87
+
77
88
  @app_host = url
78
89
  end
79
90
 
80
91
  remove_method :default_host=
81
92
  def default_host=(url)
82
- raise ArgumentError.new("Capybara.default_host should be set to a url (http://www.example.com)") unless url.nil? || (url =~ URI::Parser.new.make_regexp)
93
+ unless url.nil? || url.match?(URI::DEFAULT_PARSER.make_regexp)
94
+ raise ArgumentError, "Capybara.default_host should be set to a url (http://www.example.com). Attempted to set #{url.inspect}."
95
+ end
96
+
83
97
  @default_host = url
84
98
  end
85
99
 
86
- remove_method :save_and_open_page_path=
87
- def save_and_open_page_path=(path)
88
- warn "DEPRECATED: #save_and_open_page_path is deprecated, please use #save_path instead. \n"\
89
- "Note: Behavior is slightly different with relative paths - see documentation" unless path.nil?
90
- @save_and_open_page_path = path
100
+ remove_method :test_id=
101
+ ##
102
+ #
103
+ # Set an attribue to be optionally matched against the locator for builtin selector types.
104
+ # This attribute will be checked by builtin selector types whenever id would normally be checked.
105
+ # If `nil` then it will be ignored.
106
+ #
107
+ # @param [String, Symbol, nil] id Name of the attribute to use as the test id
108
+ #
109
+ def test_id=(id)
110
+ @test_id = id&.to_sym
91
111
  end
92
112
 
93
113
  def initialize_copy(other)
@@ -97,10 +117,10 @@ module Capybara
97
117
  end
98
118
 
99
119
  class ReadOnlySessionConfig < SimpleDelegator
100
- SessionConfig::OPTIONS.each do |m|
101
- define_method "#{m}=" do |val|
102
- raise "Per session settings are only supported when Capybara.threadsafe == true"
120
+ SessionConfig::OPTIONS.each do |option|
121
+ define_method "#{option}=" do |_|
122
+ raise 'Per session settings are only supported when Capybara.threadsafe == true'
103
123
  end
104
124
  end
105
125
  end
106
- end
126
+ end
@@ -1,71 +1,87 @@
1
1
  # frozen_string_literal: true
2
+
2
3
  module Capybara
3
4
  module SessionMatchers
4
5
  ##
5
6
  # Asserts that the page has the given path.
6
- # By default this will compare against the path+query portion of the full url
7
+ # By default, if passed a full url this will compare against the full url,
8
+ # if passed a path only the path+query portion will be compared, if passed a regexp
9
+ # the comparison will depend on the :url option (path+query by default)
7
10
  #
8
11
  # @!macro current_path_query_params
9
- # @overload $0(string, options = {})
12
+ # @overload $0(string, **options)
10
13
  # @param string [String] The string that the current 'path' should equal
11
- # @overload $0(regexp, options = {})
14
+ # @overload $0(regexp, **options)
12
15
  # @param regexp [Regexp] The regexp that the current 'path' should match to
13
- # @option options [Numeric] :wait (Capybara.default_max_wait_time) Maximum time that Capybara will wait for the current path to eq/match given string/regexp argument
14
- # @option options [Boolean] :url (false) Whether the compare should be done against the full url
15
- # @option options [Boolean] :only_path (false) Whether the compare should be done against just the path protion of the url
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
+ # @option options [Boolean] :ignore_query (false) Whether the query portion of the current url/path should be ignored
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
16
19
  # @raise [Capybara::ExpectationNotMet] if the assertion hasn't succeeded during wait time
17
20
  # @return [true]
18
21
  #
19
- def assert_current_path(path, options={})
20
- _verify_current_path(path,options) { |query| raise Capybara::ExpectationNotMet, query.failure_message unless query.resolves_for?(self) }
22
+ def assert_current_path(path, **options, &optional_filter_block)
23
+ _verify_current_path(path, optional_filter_block, **options) do |query|
24
+ raise Capybara::ExpectationNotMet, query.failure_message unless query.resolves_for?(self)
25
+ end
21
26
  end
22
27
 
23
28
  ##
24
29
  # Asserts that the page doesn't have the given path.
25
- # By default this will compare against the path+query portion of the full url
30
+ # By default, if passed a full url this will compare against the full url,
31
+ # if passed a path only the path+query portion will be compared, if passed a regexp
32
+ # the comparison will depend on the :url option
26
33
  #
27
34
  # @macro current_path_query_params
28
35
  # @raise [Capybara::ExpectationNotMet] if the assertion hasn't succeeded during wait time
29
36
  # @return [true]
30
37
  #
31
- def assert_no_current_path(path, options={})
32
- _verify_current_path(path,options) { |query| raise Capybara::ExpectationNotMet, query.negative_failure_message if query.resolves_for?(self) }
38
+ def assert_no_current_path(path, **options, &optional_filter_block)
39
+ _verify_current_path(path, optional_filter_block, **options) do |query|
40
+ raise Capybara::ExpectationNotMet, query.negative_failure_message if query.resolves_for?(self)
41
+ end
33
42
  end
34
43
 
35
44
  ##
36
45
  # Checks if the page has the given path.
37
- # By default this will compare against the path+query portion of the full url
46
+ # By default, if passed a full url this will compare against the full url,
47
+ # if passed a path only the path+query portion will be compared, if passed a regexp
48
+ # the comparison will depend on the :url option
38
49
  #
39
50
  # @macro current_path_query_params
40
51
  # @return [Boolean]
41
52
  #
42
- def has_current_path?(path, options={})
43
- assert_current_path(path, options)
44
- rescue Capybara::ExpectationNotMet
45
- return false
53
+ def has_current_path?(path, **options, &optional_filter_block)
54
+ make_predicate(options) { assert_current_path(path, **options, &optional_filter_block) }
46
55
  end
47
56
 
48
57
  ##
49
58
  # Checks if the page doesn't have the given path.
50
- # By default this will compare against the path+query portion of the full url
59
+ # By default, if passed a full url this will compare against the full url,
60
+ # if passed a path only the path+query portion will be compared, if passed a regexp
61
+ # the comparison will depend on the :url option
51
62
  #
52
63
  # @macro current_path_query_params
53
64
  # @return [Boolean]
54
65
  #
55
- def has_no_current_path?(path, options={})
56
- assert_no_current_path(path, options)
57
- rescue Capybara::ExpectationNotMet
58
- return false
66
+ def has_no_current_path?(path, **options, &optional_filter_block)
67
+ make_predicate(options) { assert_no_current_path(path, **options, &optional_filter_block) }
59
68
  end
60
69
 
61
- private
70
+ private
62
71
 
63
- def _verify_current_path(path, options)
64
- 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)
65
74
  document.synchronize(query.wait) do
66
75
  yield(query)
67
76
  end
68
- return true
77
+ true
78
+ end
79
+
80
+ def make_predicate(options)
81
+ options[:wait] = 0 unless options.key?(:wait) || config.predicates_wait
82
+ yield
83
+ rescue Capybara::ExpectationNotMet
84
+ false
69
85
  end
70
86
  end
71
87
  end
@@ -1,12 +1,12 @@
1
1
  # frozen_string_literal: true
2
+
2
3
  require 'capybara/session/matchers'
3
4
  require 'addressable/uri'
4
5
 
5
6
  module Capybara
6
-
7
7
  ##
8
8
  #
9
- # The Session class represents a single user's interaction with the system. The Session can use
9
+ # The {Session} class represents a single user's interaction with the system. The {Session} can use
10
10
  # any of the underlying drivers. A session can be initialized manually like this:
11
11
  #
12
12
  # session = Capybara::Session.new(:culerity, MyRackApp)
@@ -17,85 +17,88 @@ module Capybara
17
17
  # session = Capybara::Session.new(:culerity)
18
18
  # session.visit('http://www.google.com')
19
19
  #
20
- # When Capybara.threadsafe == true the sessions options will be initially set to the
20
+ # When {Capybara.configure threadsafe} is `true` the sessions options will be initially set to the
21
21
  # current values of the global options and a configuration block can be passed to the session initializer.
22
- # For available options see {Capybara::SessionConfig::OPTIONS}
22
+ # For available options see {Capybara::SessionConfig::OPTIONS}:
23
23
  #
24
24
  # session = Capybara::Session.new(:driver, MyRackApp) do |config|
25
25
  # config.app_host = "http://my_host.dev"
26
26
  # end
27
27
  #
28
- # Session provides a number of methods for controlling the navigation of the page, such as +visit+,
29
- # +current_path, and so on. It also delegate a number of methods to a Capybara::Document, representing
28
+ # The {Session} provides a number of methods for controlling the navigation of the page, such as {#visit},
29
+ # {#current_path}, and so on. It also delegates a number of methods to a {Capybara::Document}, representing
30
30
  # the current HTML document. This allows interaction:
31
31
  #
32
32
  # session.fill_in('q', with: 'Capybara')
33
33
  # session.click_button('Search')
34
34
  # expect(session).to have_content('Capybara')
35
35
  #
36
- # When using capybara/dsl, the Session is initialized automatically for you.
36
+ # When using `capybara/dsl`, the {Session} is initialized automatically for you.
37
37
  #
38
38
  class Session
39
39
  include Capybara::SessionMatchers
40
40
 
41
- NODE_METHODS = [
42
- :all, :first, :attach_file, :text, :check, :choose,
43
- :click_link_or_button, :click_button, :click_link, :field_labeled,
44
- :fill_in, :find, :find_all, :find_button, :find_by_id, :find_field, :find_link,
45
- :has_content?, :has_text?, :has_css?, :has_no_content?, :has_no_text?,
46
- :has_no_css?, :has_no_xpath?, :resolve, :has_xpath?, :select, :uncheck,
47
- :has_link?, :has_no_link?, :has_button?, :has_no_button?, :has_field?,
48
- :has_no_field?, :has_checked_field?, :has_unchecked_field?,
49
- :has_no_table?, :has_table?, :unselect, :has_select?, :has_no_select?,
50
- :has_selector?, :has_no_selector?, :click_on, :has_no_checked_field?,
51
- :has_no_unchecked_field?, :query, :assert_selector, :assert_no_selector,
52
- :assert_all_of_selectors, :assert_none_of_selectors,
53
- :refute_selector, :assert_text, :assert_no_text
54
- ]
41
+ NODE_METHODS = %i[
42
+ all first attach_file text check choose scroll_to scroll_by
43
+ click_link_or_button click_button click_link
44
+ fill_in find find_all find_button find_by_id find_field find_link
45
+ has_content? has_text? has_css? has_no_content? has_no_text?
46
+ has_no_css? has_no_xpath? has_xpath? select uncheck
47
+ has_link? has_no_link? has_button? has_no_button? has_field?
48
+ has_no_field? has_checked_field? has_unchecked_field?
49
+ has_no_table? has_table? unselect has_select? has_no_select?
50
+ has_selector? has_no_selector? click_on has_no_checked_field?
51
+ has_no_unchecked_field? query assert_selector assert_no_selector
52
+ assert_all_of_selectors assert_none_of_selectors assert_any_of_selectors
53
+ refute_selector assert_text assert_no_text
54
+ ].freeze
55
55
  # @api private
56
- DOCUMENT_METHODS = [
57
- :title, :assert_title, :assert_no_title, :has_title?, :has_no_title?
58
- ]
59
- SESSION_METHODS = [
60
- :body, :html, :source, :current_url, :current_host, :current_path,
61
- :execute_script, :evaluate_script, :visit, :refresh, :go_back, :go_forward,
62
- :within, :within_element, :within_fieldset, :within_table, :within_frame, :switch_to_frame,
63
- :current_window, :windows, :open_new_window, :switch_to_window, :within_window, :window_opened_by,
64
- :save_page, :save_and_open_page, :save_screenshot,
65
- :save_and_open_screenshot, :reset_session!, :response_headers,
66
- :status_code, :current_scope,
67
- :assert_current_path, :assert_no_current_path, :has_current_path?, :has_no_current_path?
68
- ] + DOCUMENT_METHODS
69
- MODAL_METHODS = [
70
- :accept_alert, :accept_confirm, :dismiss_confirm, :accept_prompt,
71
- :dismiss_prompt
72
- ]
56
+ DOCUMENT_METHODS = %i[
57
+ title assert_title assert_no_title has_title? has_no_title?
58
+ ].freeze
59
+ SESSION_METHODS = %i[
60
+ body html source current_url current_host current_path
61
+ execute_script evaluate_script visit refresh go_back go_forward send_keys
62
+ within within_element within_fieldset within_table within_frame switch_to_frame
63
+ current_window windows open_new_window switch_to_window within_window window_opened_by
64
+ save_page save_and_open_page save_screenshot
65
+ save_and_open_screenshot reset_session! response_headers
66
+ status_code current_scope
67
+ assert_current_path assert_no_current_path has_current_path? has_no_current_path?
68
+ ].freeze + DOCUMENT_METHODS
69
+ MODAL_METHODS = %i[
70
+ accept_alert accept_confirm dismiss_confirm accept_prompt dismiss_prompt
71
+ ].freeze
73
72
  DSL_METHODS = NODE_METHODS + SESSION_METHODS + MODAL_METHODS
74
73
 
75
74
  attr_reader :mode, :app, :server
76
75
  attr_accessor :synchronized
77
76
 
78
- def initialize(mode, app=nil)
79
- raise TypeError, "The second parameter to Session::new should be a rack app if passed." if app && !app.respond_to?(:call)
80
- @@instance_created = true
77
+ def initialize(mode, app = nil)
78
+ if app && !app.respond_to?(:call)
79
+ raise TypeError, 'The second parameter to Session::new should be a rack app if passed.'
80
+ end
81
+
82
+ @@instance_created = true # rubocop:disable Style/ClassVars
81
83
  @mode = mode
82
84
  @app = app
83
85
  if block_given?
84
- raise "A configuration block is only accepted when Capybara.threadsafe == true" unless Capybara.threadsafe
85
- yield config if block_given?
86
+ raise 'A configuration block is only accepted when Capybara.threadsafe == true' unless Capybara.threadsafe
87
+
88
+ yield config
86
89
  end
87
- if config.run_server and @app and driver.needs_server?
88
- @server = Capybara::Server.new(@app, config.server_port, config.server_host, config.server_errors).boot
89
- else
90
- @server = nil
90
+ @server = if config.run_server && @app && driver.needs_server?
91
+ server_options = { port: config.server_port, host: config.server_host, reportable_errors: config.server_errors }
92
+ server_options[:extra_middleware] = [Capybara::Server::AnimationDisabler] if config.disable_animation
93
+ Capybara::Server.new(@app, **server_options).boot
91
94
  end
92
95
  @touched = false
93
96
  end
94
97
 
95
98
  def driver
96
99
  @driver ||= begin
97
- unless Capybara.drivers.has_key?(mode)
98
- other_drivers = Capybara.drivers.keys.map { |key| key.inspect }
100
+ unless Capybara.drivers[mode]
101
+ other_drivers = Capybara.drivers.names.map(&:inspect)
99
102
  raise Capybara::DriverNotFoundError, "no driver called #{mode.inspect} was found, available drivers: #{other_drivers.join(', ')}"
100
103
  end
101
104
  driver = Capybara.drivers[mode].call(app)
@@ -106,28 +109,28 @@ module Capybara
106
109
 
107
110
  ##
108
111
  #
109
- # Reset the session (i.e. remove cookies and navigate to blank page)
112
+ # Reset the session (i.e. remove cookies and navigate to blank page).
110
113
  #
111
114
  # This method does not:
112
115
  #
113
- # * accept modal dialogs if they are present (Selenium driver now does, others may not)
114
- # * clear browser cache/HTML 5 local storage/IndexedDB/Web SQL database/etc.
115
- # * modify state of the driver/underlying browser in any other way
116
+ # * accept modal dialogs if they are present (Selenium driver now does, others may not)
117
+ # * clear browser cache/HTML 5 local storage/IndexedDB/Web SQL database/etc.
118
+ # * modify state of the driver/underlying browser in any other way
116
119
  #
117
120
  # as doing so will result in performance downsides and it's not needed to do everything from the list above for most apps.
118
121
  #
119
122
  # If you want to do anything from the list above on a general basis you can:
120
123
  #
121
- # * write RSpec/Cucumber/etc. after hook
122
- # * monkeypatch this method
123
- # * use Ruby's `prepend` method
124
+ # * write RSpec/Cucumber/etc. after hook
125
+ # * monkeypatch this method
126
+ # * use Ruby's `prepend` method
124
127
  #
125
128
  def reset!
126
129
  if @touched
127
130
  driver.reset!
128
131
  @touched = false
129
132
  end
130
- @server.wait_for_pending_requests if @server
133
+ @server&.wait_for_pending_requests
131
134
  raise_server_error!
132
135
  end
133
136
  alias_method :cleanup!, :reset!
@@ -135,29 +138,40 @@ module Capybara
135
138
 
136
139
  ##
137
140
  #
138
- # Raise errors encountered in the server
141
+ # Disconnect from the current driver. A new driver will be instantiated on the next interaction.
142
+ #
143
+ def quit
144
+ @driver.quit if @driver.respond_to? :quit
145
+ @document = @driver = nil
146
+ @touched = false
147
+ @server&.reset_error!
148
+ end
149
+
150
+ ##
151
+ #
152
+ # Raise errors encountered in the server.
139
153
  #
140
154
  def raise_server_error!
141
- if @server and @server.error
142
- # Force an explanation for the error being raised as the exception cause
143
- begin
144
- if config.raise_server_errors
145
- raise CapybaraError, "Your application server raised an error - It has been raised in your test code because Capybara.raise_server_errors == true"
146
- end
147
- rescue CapybaraError
148
- #needed to get the cause set correctly in JRuby -- otherwise we could just do raise @server.error
149
- raise @server.error, @server.error.message, @server.error.backtrace
150
- ensure
151
- @server.reset_error!
155
+ return unless @server&.error
156
+
157
+ # Force an explanation for the error being raised as the exception cause
158
+ begin
159
+ if config.raise_server_errors
160
+ raise CapybaraError, 'Your application server raised an error - It has been raised in your test code because Capybara.raise_server_errors == true'
152
161
  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
+ ensure
166
+ @server.reset_error!
153
167
  end
154
168
  end
155
169
 
156
170
  ##
157
171
  #
158
- # Returns a hash of response headers. Not supported by all drivers (e.g. Selenium)
172
+ # Returns a hash of response headers. Not supported by all drivers (e.g. Selenium).
159
173
  #
160
- # @return [Hash{String => String}] A hash of response headers.
174
+ # @return [Hash<String, String>] A hash of response headers.
161
175
  #
162
176
  def response_headers
163
177
  driver.response_headers
@@ -165,7 +179,7 @@ module Capybara
165
179
 
166
180
  ##
167
181
  #
168
- # Returns the current HTTP status code as an Integer. Not supported by all drivers (e.g. Selenium)
182
+ # Returns the current HTTP status code as an integer. Not supported by all drivers (e.g. Selenium).
169
183
  #
170
184
  # @return [Integer] Current HTTP status code
171
185
  #
@@ -178,7 +192,7 @@ module Capybara
178
192
  # @return [String] A snapshot of the DOM of the current document, as it looks right now (potentially modified by JavaScript).
179
193
  #
180
194
  def html
181
- driver.html
195
+ driver.html || ''
182
196
  end
183
197
  alias_method :body, :html
184
198
  alias_method :source, :html
@@ -191,13 +205,11 @@ module Capybara
191
205
  # Addressable parsing is more lenient than URI
192
206
  uri = ::Addressable::URI.parse(current_url)
193
207
 
194
- # If current_url ends up being nil, won't be able to call .path on a NilClass.
195
- return nil if uri.nil?
196
-
197
208
  # Addressable doesn't support opaque URIs - we want nil here
198
- return nil if uri.scheme == "about"
199
- path = uri.path
200
- path if path and not path.empty?
209
+ return nil if uri&.scheme == 'about'
210
+
211
+ path = uri&.path
212
+ path unless path&.empty?
201
213
  end
202
214
 
203
215
  ##
@@ -227,13 +239,13 @@ module Capybara
227
239
  #
228
240
  # For drivers which can run against an external application, such as the selenium driver
229
241
  # giving an absolute URL will navigate to that page. This allows testing applications
230
- # running on remote servers. For these drivers, setting {Capybara.app_host} will make the
242
+ # running on remote servers. For these drivers, setting {Capybara.configure app_host} will make the
231
243
  # remote server the default. For example:
232
244
  #
233
245
  # Capybara.app_host = 'http://google.com'
234
246
  # session.visit('/') # visits the google homepage
235
247
  #
236
- # If {Capybara.always_include_port} is set to true and this session is running against
248
+ # If {Capybara.configure always_include_port} is set to `true` and this session is running against
237
249
  # a rack application, then the port that the rack application is running on will automatically
238
250
  # be inserted into the URL. Supposing the app is running on port `4567`, doing something like:
239
251
  #
@@ -247,31 +259,28 @@ module Capybara
247
259
  raise_server_error!
248
260
  @touched = true
249
261
 
250
- visit_uri = URI.parse(visit_uri.to_s)
262
+ visit_uri = ::Addressable::URI.parse(visit_uri.to_s)
263
+ base_uri = ::Addressable::URI.parse(config.app_host || server_url)
251
264
 
252
- uri_base = if @server
253
- visit_uri.port = @server.port if config.always_include_port && (visit_uri.port == visit_uri.default_port)
254
- URI.parse(config.app_host || "http://#{@server.host}:#{@server.port}")
255
- else
256
- config.app_host && URI.parse(config.app_host)
257
- end
265
+ if base_uri && [nil, 'http', 'https'].include?(visit_uri.scheme)
266
+ if visit_uri.relative?
267
+ visit_uri_parts = visit_uri.to_hash.compact
258
268
 
259
- # TODO - this is only for compatability with previous 2.x behavior that concatenated
260
- # Capybara.app_host and a "relative" path - Consider removing in 3.0
261
- # @abotalov brought up a good point about this behavior potentially being useful to people
262
- # deploying to a subdirectory and/or single page apps where only the url fragment changes
263
- if visit_uri.scheme.nil? && uri_base
264
- visit_uri.path = uri_base.path + visit_uri.path
265
- end
269
+ # Useful to people deploying to a subdirectory
270
+ # and/or single page apps where only the url fragment changes
271
+ visit_uri_parts[:path] = base_uri.path + visit_uri.path
266
272
 
267
- visit_uri = uri_base.merge(visit_uri) unless uri_base.nil?
273
+ visit_uri = base_uri.merge(visit_uri_parts)
274
+ end
275
+ adjust_server_port(visit_uri)
276
+ end
268
277
 
269
278
  driver.visit(visit_uri.to_s)
270
279
  end
271
280
 
272
281
  ##
273
282
  #
274
- # Refresh the page
283
+ # Refresh the page.
275
284
  #
276
285
  def refresh
277
286
  raise_server_error!
@@ -295,9 +304,17 @@ module Capybara
295
304
  end
296
305
 
297
306
  ##
307
+ # @!method send_keys
308
+ # @see Capybara::Node::Element#send_keys
298
309
  #
299
- # Executes the given block within the context of a node. `within` takes the
300
- # same options as `find`, as well as a block. For the duration of the
310
+ def send_keys(*args, **kw_args)
311
+ driver.send_keys(*args, **kw_args)
312
+ end
313
+
314
+ ##
315
+ #
316
+ # Executes the given block within the context of a node. {#within} takes the
317
+ # same options as {Capybara::Node::Finders#find #find}, as well as a block. For the duration of the
301
318
  # block, any command to Capybara will be handled as though it were scoped
302
319
  # to the given element.
303
320
  #
@@ -305,18 +322,18 @@ module Capybara
305
322
  # fill_in('Street', with: '12 Main Street')
306
323
  # end
307
324
  #
308
- # Just as with `find`, if multiple elements match the selector given to
309
- # `within`, an error will be raised, and just as with `find`, this
325
+ # Just as with `#find`, if multiple elements match the selector given to
326
+ # {#within}, an error will be raised, and just as with `#find`, this
310
327
  # behaviour can be controlled through the `:match` and `:exact` options.
311
328
  #
312
329
  # It is possible to omit the first parameter, in that case, the selector is
313
- # assumed to be of the type set in Capybara.default_selector.
330
+ # assumed to be of the type set in {Capybara.configure default_selector}.
314
331
  #
315
332
  # within('div#delivery-address') do
316
333
  # fill_in('Street', with: '12 Main Street')
317
334
  # end
318
335
  #
319
- # Note that a lot of uses of `within` can be replaced more succinctly with
336
+ # Note that a lot of uses of {#within} can be replaced more succinctly with
320
337
  # chaining:
321
338
  #
322
339
  # find('div#delivery-address').fill_in('Street', with: '12 Main Street')
@@ -329,11 +346,11 @@ module Capybara
329
346
  #
330
347
  # @raise [Capybara::ElementNotFound] If the scope can't be found before time expires
331
348
  #
332
- def within(*args)
333
- new_scope = if args.first.is_a?(Capybara::Node::Base) then args.first else find(*args) end
349
+ def within(*args, **kw_args)
350
+ new_scope = args.first.respond_to?(:to_capybara_node) ? args.first.to_capybara_node : find(*args, **kw_args)
334
351
  begin
335
352
  scopes.push(new_scope)
336
- yield
353
+ yield if block_given?
337
354
  ensure
338
355
  scopes.pop
339
356
  end
@@ -346,10 +363,8 @@ module Capybara
346
363
  #
347
364
  # @param [String] locator Id or legend of the fieldset
348
365
  #
349
- def within_fieldset(locator)
350
- within :fieldset, locator do
351
- yield
352
- end
366
+ def within_fieldset(locator, &block)
367
+ within(:fieldset, locator, &block)
353
368
  end
354
369
 
355
370
  ##
@@ -358,26 +373,24 @@ module Capybara
358
373
  #
359
374
  # @param [String] locator Id or caption of the table
360
375
  #
361
- def within_table(locator)
362
- within :table, locator do
363
- yield
364
- end
376
+ def within_table(locator, &block)
377
+ within(:table, locator, &block)
365
378
  end
366
379
 
367
380
  ##
368
381
  #
369
- # Switch to the given frame
382
+ # Switch to the given frame.
370
383
  #
371
384
  # If you use this method you are responsible for making sure you switch back to the parent frame when done in the frame changed to.
372
- # Capybara::Session#within_frame is preferred over this method and should be used when possible.
385
+ # {#within_frame} is preferred over this method and should be used when possible.
373
386
  # May not be supported by all drivers.
374
387
  #
375
388
  # @overload switch_to_frame(element)
376
- # @param [Capybara::Node::Element] iframe/frame element to switch to
377
- # @overload switch_to_frame(:parent)
378
- # Switch to the parent element
379
- # @overload switch_to_frame(:top)
380
- # Switch to the top level document
389
+ # @param [Capybara::Node::Element] element iframe/frame element to switch to
390
+ # @overload switch_to_frame(location)
391
+ # @param [Symbol] location relative location of the frame to switch to
392
+ # * :parent - the parent frame
393
+ # * :top - the top level document
381
394
  #
382
395
  def switch_to_frame(frame)
383
396
  case frame
@@ -385,18 +398,25 @@ module Capybara
385
398
  driver.switch_to_frame(frame)
386
399
  scopes.push(:frame)
387
400
  when :parent
388
- raise Capybara::ScopeError, "`switch_to_frame(:parent)` cannot be called from inside a descendant frame's "\
389
- "`within` block." if scopes.last() != :frame
401
+ if scopes.last != :frame
402
+ raise Capybara::ScopeError, "`switch_to_frame(:parent)` cannot be called from inside a descendant frame's "\
403
+ '`within` block.'
404
+ end
390
405
  scopes.pop
391
406
  driver.switch_to_frame(:parent)
392
407
  when :top
393
408
  idx = scopes.index(:frame)
409
+ top_level_scopes = [:frame, nil]
394
410
  if idx
395
- raise Capybara::ScopeError, "`switch_to_frame(:top)` cannot be called from inside a descendant frame's "\
396
- "`within` block." if scopes.slice(idx..-1).any? {|scope| ![:frame, nil].include?(scope)}
411
+ if scopes.slice(idx..-1).any? { |scope| !top_level_scopes.include?(scope) }
412
+ raise Capybara::ScopeError, "`switch_to_frame(:top)` cannot be called from inside a descendant frame's "\
413
+ '`within` block.'
414
+ end
397
415
  scopes.slice!(idx..-1)
398
416
  driver.switch_to_frame(:top)
399
417
  end
418
+ else
419
+ raise ArgumentError, 'You must provide a frame element, :parent, or :top when calling switch_to_frame'
400
420
  end
401
421
  end
402
422
 
@@ -407,35 +427,17 @@ module Capybara
407
427
  #
408
428
  # @overload within_frame(element)
409
429
  # @param [Capybara::Node::Element] frame element
410
- # @overload within_frame([kind = :frame], locator, options = {})
411
- # @param [Symobl] kind Optional selector type (:css, :xpath, :field, etc.) - Defaults to :frame
430
+ # @overload within_frame([kind = :frame], locator, **options)
431
+ # @param [Symbol] kind Optional selector type (:frame, :css, :xpath, etc.) - Defaults to :frame
412
432
  # @param [String] locator The locator for the given selector kind. For :frame this is the name/id of a frame/iframe element
413
433
  # @overload within_frame(index)
414
434
  # @param [Integer] index index of a frame (0 based)
415
- def within_frame(*args)
416
- frame = _find_frame(*args)
417
-
435
+ def within_frame(*args, **kw_args)
436
+ switch_to_frame(_find_frame(*args, **kw_args))
418
437
  begin
419
- switch_to_frame(frame)
420
- begin
421
- yield
422
- ensure
423
- switch_to_frame(:parent)
424
- end
425
- rescue Capybara::NotSupportedByDriverError
426
- # Support older driver frame API for now
427
- if driver.respond_to?(:within_frame)
428
- begin
429
- scopes.push(:frame)
430
- driver.within_frame(frame) do
431
- yield
432
- end
433
- ensure
434
- scopes.pop
435
- end
436
- else
437
- raise
438
- end
438
+ yield if block_given?
439
+ ensure
440
+ switch_to_frame(:parent)
439
441
  end
440
442
  end
441
443
 
@@ -460,48 +462,50 @@ module Capybara
460
462
  end
461
463
 
462
464
  ##
463
- # Open new window.
464
- # Current window doesn't change as the result of this call.
465
+ # Open a new window.
466
+ # The current window doesn't change as the result of this call.
465
467
  # It should be switched to explicitly.
466
468
  #
467
469
  # @return [Capybara::Window] window that has been opened
468
470
  #
469
- def open_new_window
471
+ def open_new_window(kind = :tab)
470
472
  window_opened_by do
471
- driver.open_new_window
473
+ if driver.method(:open_new_window).arity.zero?
474
+ driver.open_new_window
475
+ else
476
+ driver.open_new_window(kind)
477
+ end
472
478
  end
473
479
  end
474
480
 
475
481
  ##
482
+ # Switch to the given window.
483
+ #
476
484
  # @overload switch_to_window(&block)
477
485
  # Switches to the first window for which given block returns a value other than false or nil.
478
- # If window that matches block can't be found, the window will be switched back and `WindowError` will be raised.
486
+ # If window that matches block can't be found, the window will be switched back and {Capybara::WindowError} will be raised.
479
487
  # @example
480
488
  # window = switch_to_window { title == 'Page title' }
481
489
  # @raise [Capybara::WindowError] if no window matches given block
482
490
  # @overload switch_to_window(window)
483
491
  # @param window [Capybara::Window] window that should be switched to
484
- # @raise [Capybara::Driver::Base#no_such_window_error] if non-existent (e.g. closed) window was passed
492
+ # @raise [Capybara::Driver::Base#no_such_window_error] if nonexistent (e.g. closed) window was passed
485
493
  #
486
494
  # @return [Capybara::Window] window that has been switched to
487
- # @raise [Capybara::ScopeError] if this method is invoked inside `within` or
488
- # `within_frame` methods
495
+ # @raise [Capybara::ScopeError] if this method is invoked inside {#within} or
496
+ # {#within_frame} methods
489
497
  # @raise [ArgumentError] if both or neither arguments were provided
490
498
  #
491
- def switch_to_window(window = nil, options= {}, &window_locator)
492
- options, window = window, nil if window.is_a? Hash
493
-
494
- block_given = block_given?
495
- if window && block_given
496
- raise ArgumentError, "`switch_to_window` can take either a block or a window, not both"
497
- elsif !window && !block_given
498
- raise ArgumentError, "`switch_to_window`: either window or block should be provided"
499
- elsif !scopes.last.nil?
500
- raise Capybara::ScopeError, "`switch_to_window` is not supposed to be invoked from "\
501
- "`within` or `within_frame` blocks."
499
+ def switch_to_window(window = nil, **options, &window_locator)
500
+ raise ArgumentError, '`switch_to_window` can take either a block or a window, not both' if window && window_locator
501
+ raise ArgumentError, '`switch_to_window`: either window or block should be provided' if !window && !window_locator
502
+
503
+ unless scopes.last.nil?
504
+ raise Capybara::ScopeError, '`switch_to_window` is not supposed to be invoked from '\
505
+ '`within` or `within_frame` blocks.'
502
506
  end
503
507
 
504
- _switch_to_window(window, options, &window_locator)
508
+ _switch_to_window(window, **options, &window_locator)
505
509
  end
506
510
 
507
511
  ##
@@ -509,63 +513,42 @@ module Capybara
509
513
  #
510
514
  # 1. Switches to the given window (it can be located by window instance/lambda/string).
511
515
  # 2. Executes the given block (within window located at previous step).
512
- # 3. Switches back (this step will be invoked even if exception will happen at second step)
516
+ # 3. Switches back (this step will be invoked even if an exception occurs at the second step).
513
517
  #
514
518
  # @overload within_window(window) { do_something }
515
- # @param window [Capybara::Window] instance of `Capybara::Window` class
519
+ # @param window [Capybara::Window] instance of {Capybara::Window} class
516
520
  # that will be switched to
517
- # @raise [driver#no_such_window_error] if unexistent (e.g. closed) window was passed
521
+ # @raise [driver#no_such_window_error] if nonexistent (e.g. closed) window was passed
518
522
  # @overload within_window(proc_or_lambda) { do_something }
519
- # @param lambda [Proc] lambda. First window for which lambda
523
+ # @param lambda [Proc] First window for which lambda
520
524
  # returns a value other than false or nil will be switched to.
521
525
  # @example
522
526
  # within_window(->{ page.title == 'Page title' }) { click_button 'Submit' }
523
527
  # @raise [Capybara::WindowError] if no window matching lambda was found
524
- # @overload within_window(string) { do_something }
525
- # @deprecated Pass window or lambda instead
526
- # @param [String] handle, name, url or title of the window
527
528
  #
528
- # @raise [Capybara::ScopeError] if this method is invoked inside `within_frame` method
529
+ # @raise [Capybara::ScopeError] if this method is invoked inside {#within_frame} method
529
530
  # @return value returned by the block
530
531
  #
531
- def within_window(window_or_handle)
532
- if window_or_handle.instance_of?(Capybara::Window)
533
- original = current_window
534
- scopes << nil
535
- begin
536
- _switch_to_window(window_or_handle) unless original == window_or_handle
537
- begin
538
- yield
539
- ensure
540
- _switch_to_window(original) unless original == window_or_handle
541
- end
542
- ensure
543
- scopes.pop
544
- end
545
- elsif window_or_handle.is_a?(Proc)
546
- original = current_window
547
- scopes << nil
548
- begin
549
- _switch_to_window { window_or_handle.call }
550
- begin
551
- yield
552
- ensure
553
- _switch_to_window(original)
554
- end
555
- ensure
556
- scopes.pop
532
+ def within_window(window_or_proc)
533
+ original = current_window
534
+ scopes << nil
535
+ begin
536
+ case window_or_proc
537
+ when Capybara::Window
538
+ _switch_to_window(window_or_proc) unless original == window_or_proc
539
+ when Proc
540
+ _switch_to_window { window_or_proc.call }
541
+ else
542
+ raise ArgumentError, '`#within_window` requires a `Capybara::Window` instance or a lambda'
557
543
  end
558
- else
559
- offending_line = caller.first
560
- file_line = offending_line.match(/^(.+?):(\d+)/)[0]
561
- warn "DEPRECATION WARNING: Passing string argument to #within_window is deprecated. "\
562
- "Pass window object or lambda. (called from #{file_line})"
544
+
563
545
  begin
564
- scopes << nil
565
- driver.within_window(window_or_handle) { yield }
546
+ yield if block_given?
566
547
  ensure
567
- scopes.pop
548
+ _switch_to_window(original) unless original == window_or_proc
568
549
  end
550
+ ensure
551
+ scopes.pop
569
552
  end
570
553
  end
571
554
 
@@ -573,23 +556,23 @@ module Capybara
573
556
  # Get the window that has been opened by the passed block.
574
557
  # It will wait for it to be opened (in the same way as other Capybara methods wait).
575
558
  # It's better to use this method than `windows.last`
576
- # {https://dvcs.w3.org/hg/webdriver/raw-file/default/webdriver-spec.html#h_note_10 as order of windows isn't defined in some drivers}
559
+ # {https://dvcs.w3.org/hg/webdriver/raw-file/default/webdriver-spec.html#h_note_10 as order of windows isn't defined in some drivers}.
577
560
  #
578
- # @param options [Hash]
579
- # @option options [Numeric] :wait (Capybara.default_max_wait_time) maximum wait time
580
- # @return [Capybara::Window] the window that has been opened within a block
581
- # @raise [Capybara::WindowError] if block passed to window hasn't opened window
582
- # or opened more than one window
561
+ # @overload window_opened_by(**options, &block)
562
+ # @param options [Hash]
563
+ # @option options [Numeric] :wait maximum wait time. Defaults to {Capybara.configure default_max_wait_time}
564
+ # @return [Capybara::Window] the window that has been opened within a block
565
+ # @raise [Capybara::WindowError] if block passed to window hasn't opened window
566
+ # or opened more than one window
583
567
  #
584
- def window_opened_by(options = {}, &block)
568
+ def window_opened_by(**options)
585
569
  old_handles = driver.window_handles
586
- block.call
570
+ yield
587
571
 
588
- wait_time = Capybara::Queries::BaseQuery.wait(options, config.default_max_wait_time)
589
- document.synchronize(wait_time, errors: [Capybara::WindowError]) do
572
+ synchronize_windows(options) do
590
573
  opened_handles = (driver.window_handles - old_handles)
591
574
  if opened_handles.size != 1
592
- raise Capybara::WindowError, "block passed to #window_opened_by "\
575
+ raise Capybara::WindowError, 'block passed to #window_opened_by '\
593
576
  "opened #{opened_handles.size} windows instead of 1"
594
577
  end
595
578
  Window.new(self, opened_handles.first)
@@ -599,39 +582,44 @@ module Capybara
599
582
  ##
600
583
  #
601
584
  # Execute the given script, not returning a result. This is useful for scripts that return
602
- # complex objects, such as jQuery statements. +execute_script+ should be used over
603
- # +evaluate_script+ whenever possible.
585
+ # complex objects, such as jQuery statements. {#execute_script} should be used over
586
+ # {#evaluate_script} whenever possible.
604
587
  #
605
588
  # @param [String] script A string of JavaScript to execute
606
- # @param args Optional arguments that will be passed to the script. Driver support for this is optional and types of objects supported may differ between drivers
589
+ # @param args Optional arguments that will be passed to the script. Driver support for this is optional and types of objects supported may differ between drivers
607
590
  #
608
591
  def execute_script(script, *args)
609
592
  @touched = true
610
- if args.empty?
611
- driver.execute_script(script)
612
- else
613
- raise Capybara::NotSupportedByDriverError, "The current driver does not support execute_script arguments" if driver.method(:execute_script).arity == 1
614
- driver.execute_script(script, *args.map { |arg| arg.is_a?(Capybara::Node::Element) ? arg.base : arg} )
615
- end
593
+ driver.execute_script(script, *driver_args(args))
616
594
  end
617
595
 
618
596
  ##
619
597
  #
620
598
  # Evaluate the given JavaScript and return the result. Be careful when using this with
621
- # scripts that return complex objects, such as jQuery statements. +execute_script+ might
599
+ # scripts that return complex objects, such as jQuery statements. {#execute_script} might
622
600
  # be a better alternative.
623
601
  #
624
602
  # @param [String] script A string of JavaScript to evaluate
603
+ # @param args Optional arguments that will be passed to the script
625
604
  # @return [Object] The result of the evaluated JavaScript (may be driver specific)
626
605
  #
627
606
  def evaluate_script(script, *args)
628
607
  @touched = true
629
- result = if args.empty?
630
- driver.evaluate_script(script)
631
- else
632
- raise Capybara::NotSupportedByDriverError, "The current driver does not support evaluate_script arguments" if driver.method(:evaluate_script).arity == 1
633
- driver.evaluate_script(script, *args.map { |arg| arg.is_a?(Capybara::Node::Element) ? arg.base : arg} )
634
- end
608
+ result = driver.evaluate_script(script.strip, *driver_args(args))
609
+ element_script_result(result)
610
+ end
611
+
612
+ ##
613
+ #
614
+ # Evaluate the given JavaScript and obtain the result from a callback function which will be passed as the last argument to the script.
615
+ #
616
+ # @param [String] script A string of JavaScript to evaluate
617
+ # @param args Optional arguments that will be passed to the script
618
+ # @return [Object] The result of the evaluated JavaScript (may be driver specific)
619
+ #
620
+ def evaluate_async_script(script, *args)
621
+ @touched = true
622
+ result = driver.evaluate_async_script(script, *driver_args(args))
635
623
  element_script_result(result)
636
624
  end
637
625
 
@@ -640,15 +628,23 @@ module Capybara
640
628
  # Execute the block, accepting a alert.
641
629
  #
642
630
  # @!macro modal_params
643
- # @overload $0(text, options = {}, &blk)
644
- # @param text [String, Regexp] Text or regex to match against the text in the modal. If not provided any modal is matched
645
- # @overload $0(options = {}, &blk)
646
- # @option options [Numeric] :wait (Capybara.default_max_wait_time) Maximum time to wait for the modal to appear after executing the block.
631
+ # Expects a block whose actions will trigger the display modal to appear.
632
+ # @example
633
+ # $0 do
634
+ # click_link('link that triggers appearance of system modal')
635
+ # end
636
+ # @overload $0(text, **options, &blk)
637
+ # @param text [String, Regexp] Text or regex to match against the text in the modal. If not provided any modal is matched.
638
+ # @option options [Numeric] :wait Maximum time to wait for the modal to appear after executing the block. Defaults to {Capybara.configure default_max_wait_time}.
639
+ # @yield Block whose actions will trigger the system modal
640
+ # @overload $0(**options, &blk)
641
+ # @option options [Numeric] :wait Maximum time to wait for the modal to appear after executing the block. Defaults to {Capybara.configure default_max_wait_time}.
642
+ # @yield Block whose actions will trigger the system modal
647
643
  # @return [String] the message shown in the modal
648
644
  # @raise [Capybara::ModalNotFound] if modal dialog hasn't been found
649
645
  #
650
- def accept_alert(text_or_options=nil, options={}, &blk)
651
- accept_modal(:alert, text_or_options, options, &blk)
646
+ def accept_alert(text = nil, **options, &blk)
647
+ accept_modal(:alert, text, options, &blk)
652
648
  end
653
649
 
654
650
  ##
@@ -657,8 +653,8 @@ module Capybara
657
653
  #
658
654
  # @macro modal_params
659
655
  #
660
- def accept_confirm(text_or_options=nil, options={}, &blk)
661
- accept_modal(:confirm, text_or_options, options, &blk)
656
+ def accept_confirm(text = nil, **options, &blk)
657
+ accept_modal(:confirm, text, options, &blk)
662
658
  end
663
659
 
664
660
  ##
@@ -667,8 +663,8 @@ module Capybara
667
663
  #
668
664
  # @macro modal_params
669
665
  #
670
- def dismiss_confirm(text_or_options=nil, options={}, &blk)
671
- dismiss_modal(:confirm, text_or_options, options, &blk)
666
+ def dismiss_confirm(text = nil, **options, &blk)
667
+ dismiss_modal(:confirm, text, options, &blk)
672
668
  end
673
669
 
674
670
  ##
@@ -678,8 +674,8 @@ module Capybara
678
674
  # @macro modal_params
679
675
  # @option options [String] :with Response to provide to the prompt
680
676
  #
681
- def accept_prompt(text_or_options=nil, options={}, &blk)
682
- accept_modal(:prompt, text_or_options, options, &blk)
677
+ def accept_prompt(text = nil, **options, &blk)
678
+ accept_modal(:prompt, text, options, &blk)
683
679
  end
684
680
 
685
681
  ##
@@ -688,82 +684,70 @@ module Capybara
688
684
  #
689
685
  # @macro modal_params
690
686
  #
691
- def dismiss_prompt(text_or_options=nil, options={}, &blk)
692
- dismiss_modal(:prompt, text_or_options, options, &blk)
687
+ def dismiss_prompt(text = nil, **options, &blk)
688
+ dismiss_modal(:prompt, text, options, &blk)
693
689
  end
694
690
 
695
691
  ##
696
692
  #
697
- # Save a snapshot of the page. If `Capybara.asset_host` is set it will inject `base` tag
698
- # pointing to `asset_host`.
693
+ # Save a snapshot of the page. If {Capybara.configure asset_host} is set it will inject `base` tag
694
+ # pointing to {Capybara.configure asset_host}.
699
695
  #
700
- # If invoked without arguments it will save file to `Capybara.save_path`
701
- # and file will be given randomly generated filename. If invoked with a relative path
702
- # the path will be relative to `Capybara.save_path`, which is different from
703
- # the previous behavior with `Capybara.save_and_open_page_path` where the relative path was
704
- # relative to Dir.pwd
696
+ # If invoked without arguments it will save file to {Capybara.configure save_path}
697
+ # and file will be given randomly generated filename. If invoked with a relative path
698
+ # the path will be relative to {Capybara.configure save_path}.
705
699
  #
706
700
  # @param [String] path the path to where it should be saved
707
701
  # @return [String] the path to which the file was saved
708
702
  #
709
703
  def save_page(path = nil)
710
- path = prepare_path(path, 'html')
711
- File.write(path, Capybara::Helpers.inject_asset_host(body, config.asset_host), mode: 'wb')
712
- path
704
+ prepare_path(path, 'html').tap do |p_path|
705
+ File.write(p_path, Capybara::Helpers.inject_asset_host(body, host: config.asset_host), mode: 'wb')
706
+ end
713
707
  end
714
708
 
715
709
  ##
716
710
  #
717
711
  # Save a snapshot of the page and open it in a browser for inspection.
718
712
  #
719
- # If invoked without arguments it will save file to `Capybara.save_path`
720
- # and file will be given randomly generated filename. If invoked with a relative path
721
- # the path will be relative to `Capybara.save_path`, which is different from
722
- # the previous behavior with `Capybara.save_and_open_page_path` where the relative path was
723
- # relative to Dir.pwd
713
+ # If invoked without arguments it will save file to {Capybara.configure save_path}
714
+ # and file will be given randomly generated filename. If invoked with a relative path
715
+ # the path will be relative to {Capybara.configure save_path}.
724
716
  #
725
717
  # @param [String] path the path to where it should be saved
726
718
  #
727
719
  def save_and_open_page(path = nil)
728
- path = save_page(path)
729
- open_file(path)
720
+ save_page(path).tap { |s_path| open_file(s_path) }
730
721
  end
731
722
 
732
723
  ##
733
724
  #
734
725
  # Save a screenshot of page.
735
726
  #
736
- # If invoked without arguments it will save file to `Capybara.save_path`
737
- # and file will be given randomly generated filename. If invoked with a relative path
738
- # the path will be relative to `Capybara.save_path`, which is different from
739
- # the previous behavior with `Capybara.save_and_open_page_path` where the relative path was
740
- # relative to Dir.pwd
727
+ # If invoked without arguments it will save file to {Capybara.configure save_path}
728
+ # and file will be given randomly generated filename. If invoked with a relative path
729
+ # the path will be relative to {Capybara.configure save_path}.
741
730
  #
742
731
  # @param [String] path the path to where it should be saved
743
732
  # @param [Hash] options a customizable set of options
744
733
  # @return [String] the path to which the file was saved
745
- def save_screenshot(path = nil, options = {})
746
- path = prepare_path(path, 'png')
747
- driver.save_screenshot(path, options)
748
- path
734
+ def save_screenshot(path = nil, **options)
735
+ prepare_path(path, 'png').tap { |p_path| driver.save_screenshot(p_path, **options) }
749
736
  end
750
737
 
751
738
  ##
752
739
  #
753
740
  # Save a screenshot of the page and open it for inspection.
754
741
  #
755
- # If invoked without arguments it will save file to `Capybara.save_path`
756
- # and file will be given randomly generated filename. If invoked with a relative path
757
- # the path will be relative to `Capybara.save_path`, which is different from
758
- # the previous behavior with `Capybara.save_and_open_page_path` where the relative path was
759
- # relative to Dir.pwd
742
+ # If invoked without arguments it will save file to {Capybara.configure save_path}
743
+ # and file will be given randomly generated filename. If invoked with a relative path
744
+ # the path will be relative to {Capybara.configure save_path}.
760
745
  #
761
746
  # @param [String] path the path to where it should be saved
762
747
  # @param [Hash] options a customizable set of options
763
748
  #
764
- def save_and_open_screenshot(path = nil, options = {})
765
- path = save_screenshot(path, options)
766
- open_file(path)
749
+ def save_and_open_screenshot(path = nil, **options)
750
+ save_screenshot(path, **options).tap { |s_path| open_file(s_path) }
767
751
  end
768
752
 
769
753
  def document
@@ -771,15 +755,32 @@ module Capybara
771
755
  end
772
756
 
773
757
  NODE_METHODS.each do |method|
774
- define_method method do |*args, &block|
775
- @touched = true
776
- current_scope.send(method, *args, &block)
758
+ if RUBY_VERSION >= '2.7'
759
+ class_eval <<~METHOD, __FILE__, __LINE__ + 1
760
+ def #{method}(...)
761
+ @touched = true
762
+ current_scope.#{method}(...)
763
+ end
764
+ METHOD
765
+ else
766
+ define_method method do |*args, &block|
767
+ @touched = true
768
+ current_scope.send(method, *args, &block)
769
+ end
777
770
  end
778
771
  end
779
772
 
780
773
  DOCUMENT_METHODS.each do |method|
781
- define_method method do |*args, &block|
782
- document.send(method, *args, &block)
774
+ if RUBY_VERSION >= '2.7'
775
+ class_eval <<~METHOD, __FILE__, __LINE__ + 1
776
+ def #{method}(...)
777
+ document.#{method}(...)
778
+ end
779
+ METHOD
780
+ else
781
+ define_method method do |*args, &block|
782
+ document.send(method, *args, &block)
783
+ end
783
784
  end
784
785
  end
785
786
 
@@ -789,15 +790,14 @@ module Capybara
789
790
 
790
791
  def current_scope
791
792
  scope = scopes.last
792
- scope = document if [nil, :frame].include? scope
793
- scope
793
+ [nil, :frame].include?(scope) ? document : scope
794
794
  end
795
795
 
796
796
  ##
797
797
  #
798
- # Yield a block using a specific wait time
798
+ # Yield a block using a specific maximum wait time.
799
799
  #
800
- def using_wait_time(seconds)
800
+ def using_wait_time(seconds, &block)
801
801
  if Capybara.threadsafe
802
802
  begin
803
803
  previous_wait_time = config.default_max_wait_time
@@ -807,17 +807,18 @@ module Capybara
807
807
  config.default_max_wait_time = previous_wait_time
808
808
  end
809
809
  else
810
- Capybara.using_wait_time(seconds) { yield }
810
+ Capybara.using_wait_time(seconds, &block)
811
811
  end
812
812
  end
813
813
 
814
814
  ##
815
815
  #
816
- # Accepts a block to set the configuration options if Capybara.threadsafe == true. Note that some options only have an effect
817
- # if set at initialization time, so look at the configuration block that can be passed to the initializer too
816
+ # Accepts a block to set the configuration options if {Capybara.configure threadsafe} is `true`. Note that some options only have an effect
817
+ # if set at initialization time, so look at the configuration block that can be passed to the initializer too.
818
818
  #
819
819
  def configure
820
- raise "Session configuration is only supported when Capybara.threadsafe == true" unless Capybara.threadsafe
820
+ raise 'Session configuration is only supported when Capybara.threadsafe == true' unless Capybara.threadsafe
821
+
821
822
  yield config
822
823
  end
823
824
 
@@ -832,48 +833,48 @@ module Capybara
832
833
  Capybara::ReadOnlySessionConfig.new(Capybara.session_options)
833
834
  end
834
835
  end
836
+
837
+ def server_url
838
+ @server&.base_url
839
+ end
840
+
835
841
  private
836
842
 
837
- @@instance_created = false
843
+ @@instance_created = false # rubocop:disable Style/ClassVars
844
+
845
+ def driver_args(args)
846
+ args.map { |arg| arg.is_a?(Capybara::Node::Element) ? arg.base : arg }
847
+ end
838
848
 
839
849
  def accept_modal(type, text_or_options, options, &blk)
840
- driver.accept_modal(type, modal_options(text_or_options, options), &blk)
850
+ driver.accept_modal(type, **modal_options(text_or_options, **options), &blk)
841
851
  end
842
852
 
843
853
  def dismiss_modal(type, text_or_options, options, &blk)
844
- driver.dismiss_modal(type, modal_options(text_or_options, options), &blk)
854
+ driver.dismiss_modal(type, **modal_options(text_or_options, **options), &blk)
845
855
  end
846
856
 
847
- def modal_options(text_or_options, options)
848
- text_or_options, options = nil, text_or_options if text_or_options.is_a?(Hash)
849
- options[:text] ||= text_or_options unless text_or_options.nil?
857
+ def modal_options(text = nil, **options)
858
+ options[:text] ||= text unless text.nil?
850
859
  options[:wait] ||= config.default_max_wait_time
851
860
  options
852
861
  end
853
862
 
854
-
855
863
  def open_file(path)
856
- begin
857
- require "launchy"
858
- Launchy.open(path)
859
- rescue LoadError
860
- warn "File saved to #{path}."
861
- warn "Please install the launchy gem to open the file automatically."
862
- end
864
+ require 'launchy'
865
+ Launchy.open(path)
866
+ rescue LoadError
867
+ warn "File saved to #{path}.\nPlease install the launchy gem to open the file automatically."
863
868
  end
864
869
 
865
870
  def prepare_path(path, extension)
866
- if config.save_path || config.save_and_open_page_path.nil?
867
- path = File.expand_path(path || default_fn(extension), config.save_path)
868
- else
869
- path = File.expand_path(default_fn(extension), config.save_and_open_page_path) if path.nil?
871
+ File.expand_path(path || default_fn(extension), config.save_path).tap do |p_path|
872
+ FileUtils.mkdir_p(File.dirname(p_path))
870
873
  end
871
- FileUtils.mkdir_p(File.dirname(path))
872
- path
873
874
  end
874
875
 
875
876
  def default_fn(extension)
876
- timestamp = Time.new.strftime("%Y%m%d%H%M%S")
877
+ timestamp = Time.new.strftime('%Y%m%d%H%M%S')
877
878
  "capybara-#{timestamp}#{rand(10**10)}.#{extension}"
878
879
  end
879
880
 
@@ -884,9 +885,9 @@ module Capybara
884
885
  def element_script_result(arg)
885
886
  case arg
886
887
  when Array
887
- arg.map { |e| element_script_result(e) }
888
+ arg.map { |subarg| element_script_result(subarg) }
888
889
  when Hash
889
- arg.each { |k, v| arg[k] = element_script_result(v) }
890
+ arg.transform_values! { |value| element_script_result(value) }
890
891
  when Capybara::Driver::Node
891
892
  Capybara::Node::Element.new(self, arg, nil, nil)
892
893
  else
@@ -894,54 +895,57 @@ module Capybara
894
895
  end
895
896
  end
896
897
 
897
- def _find_frame(*args)
898
- within(document) do # Previous 2.x versions ignored current scope when finding frames - consider changing in 3.0
899
- case args[0]
900
- when Capybara::Node::Element
901
- args[0]
902
- when String, Hash
903
- find(:frame, *args)
904
- when Symbol
905
- find(*args)
906
- when Integer
907
- idx = args[0]
908
- all(:frame, minimum: idx+1)[idx]
909
- else
910
- raise TypeError
911
- end
912
- end
898
+ def adjust_server_port(uri)
899
+ uri.port ||= @server.port if @server && config.always_include_port
913
900
  end
914
901
 
915
- def _switch_to_window(window = nil, options= {})
916
- options, window = window, nil if window.is_a? Hash
902
+ def _find_frame(*args, **kw_args)
903
+ case args[0]
904
+ when Capybara::Node::Element
905
+ args[0]
906
+ when String, nil
907
+ find(:frame, *args, **kw_args)
908
+ when Symbol
909
+ find(*args, **kw_args)
910
+ when Integer
911
+ idx = args[0]
912
+ all(:frame, minimum: idx + 1)[idx]
913
+ else
914
+ raise TypeError
915
+ end
916
+ end
917
917
 
918
- raise Capybara::ScopeError, "Window cannot be switched inside a `within_frame` block" if scopes.include?(:frame)
919
- raise Capybara::ScopeError, "Window cannot be switch inside a `within` block" unless scopes.last.nil?
918
+ def _switch_to_window(window = nil, **options, &window_locator)
919
+ raise Capybara::ScopeError, 'Window cannot be switched inside a `within_frame` block' if scopes.include?(:frame)
920
+ raise Capybara::ScopeError, 'Window cannot be switched inside a `within` block' unless scopes.last.nil?
920
921
 
921
922
  if window
922
923
  driver.switch_to_window(window.handle)
923
924
  window
924
925
  else
925
- wait_time = Capybara::Queries::BaseQuery.wait(options, config.default_max_wait_time)
926
- document.synchronize(wait_time, errors: [Capybara::WindowError]) do
926
+ synchronize_windows(options) do
927
927
  original_window_handle = driver.current_window_handle
928
928
  begin
929
- driver.window_handles.each do |handle|
930
- driver.switch_to_window handle
931
- if yield
932
- return Window.new(self, handle)
933
- end
934
- end
935
- rescue => e
929
+ _switch_to_window_by_locator(&window_locator)
930
+ rescue StandardError
936
931
  driver.switch_to_window(original_window_handle)
937
- raise e
938
- else
939
- driver.switch_to_window(original_window_handle)
940
- raise Capybara::WindowError, "Could not find a window matching block/lambda"
932
+ raise
941
933
  end
942
934
  end
943
935
  end
944
936
  end
945
937
 
938
+ def _switch_to_window_by_locator
939
+ driver.window_handles.each do |handle|
940
+ driver.switch_to_window handle
941
+ return Window.new(self, handle) if yield
942
+ end
943
+ raise Capybara::WindowError, 'Could not find a window matching block/lambda'
944
+ end
945
+
946
+ def synchronize_windows(options, &block)
947
+ wait_time = Capybara::Queries::BaseQuery.wait(options, config.default_max_wait_time)
948
+ document.synchronize(wait_time, errors: [Capybara::WindowError], &block)
949
+ end
946
950
  end
947
951
  end