capybara 2.15.1 → 3.36.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (311) hide show
  1. checksums.yaml +5 -5
  2. data/.yardopts +1 -1
  3. data/History.md +902 -10
  4. data/License.txt +1 -1
  5. data/README.md +103 -75
  6. data/lib/capybara/config.rb +29 -56
  7. data/lib/capybara/cucumber.rb +2 -3
  8. data/lib/capybara/driver/base.rb +39 -18
  9. data/lib/capybara/driver/node.rb +36 -10
  10. data/lib/capybara/dsl.rb +15 -6
  11. data/lib/capybara/helpers.rb +65 -30
  12. data/lib/capybara/minitest/spec.rb +173 -81
  13. data/lib/capybara/minitest.rb +220 -111
  14. data/lib/capybara/node/actions.rb +274 -171
  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 +59 -26
  22. data/lib/capybara/queries/active_element_query.rb +18 -0
  23. data/lib/capybara/queries/ancestor_query.rb +13 -10
  24. data/lib/capybara/queries/base_query.rb +39 -28
  25. data/lib/capybara/queries/current_path_query.rb +22 -25
  26. data/lib/capybara/queries/match_query.rb +14 -7
  27. data/lib/capybara/queries/selector_query.rb +646 -145
  28. data/lib/capybara/queries/sibling_query.rb +12 -10
  29. data/lib/capybara/queries/style_query.rb +45 -0
  30. data/lib/capybara/queries/text_query.rb +56 -38
  31. data/lib/capybara/queries/title_query.rb +8 -11
  32. data/lib/capybara/rack_test/browser.rb +57 -41
  33. data/lib/capybara/rack_test/css_handlers.rb +6 -4
  34. data/lib/capybara/rack_test/driver.rb +18 -13
  35. data/lib/capybara/rack_test/errors.rb +6 -0
  36. data/lib/capybara/rack_test/form.rb +73 -58
  37. data/lib/capybara/rack_test/node.rb +188 -81
  38. data/lib/capybara/rails.rb +3 -7
  39. data/lib/capybara/registration_container.rb +44 -0
  40. data/lib/capybara/registrations/drivers.rb +42 -0
  41. data/lib/capybara/registrations/patches/puma_ssl.rb +29 -0
  42. data/lib/capybara/registrations/servers.rb +45 -0
  43. data/lib/capybara/result.rb +96 -62
  44. data/lib/capybara/rspec/features.rb +17 -50
  45. data/lib/capybara/rspec/matcher_proxies.rb +52 -15
  46. data/lib/capybara/rspec/matchers/base.rb +111 -0
  47. data/lib/capybara/rspec/matchers/become_closed.rb +33 -0
  48. data/lib/capybara/rspec/matchers/compound.rb +88 -0
  49. data/lib/capybara/rspec/matchers/count_sugar.rb +37 -0
  50. data/lib/capybara/rspec/matchers/have_ancestor.rb +28 -0
  51. data/lib/capybara/rspec/matchers/have_current_path.rb +29 -0
  52. data/lib/capybara/rspec/matchers/have_selector.rb +77 -0
  53. data/lib/capybara/rspec/matchers/have_sibling.rb +27 -0
  54. data/lib/capybara/rspec/matchers/have_text.rb +33 -0
  55. data/lib/capybara/rspec/matchers/have_title.rb +29 -0
  56. data/lib/capybara/rspec/matchers/match_selector.rb +27 -0
  57. data/lib/capybara/rspec/matchers/match_style.rb +43 -0
  58. data/lib/capybara/rspec/matchers/spatial_sugar.rb +39 -0
  59. data/lib/capybara/rspec/matchers.rb +144 -264
  60. data/lib/capybara/rspec.rb +7 -11
  61. data/lib/capybara/selector/builders/css_builder.rb +84 -0
  62. data/lib/capybara/selector/builders/xpath_builder.rb +71 -0
  63. data/lib/capybara/selector/css.rb +89 -17
  64. data/lib/capybara/selector/definition/button.rb +68 -0
  65. data/lib/capybara/selector/definition/checkbox.rb +26 -0
  66. data/lib/capybara/selector/definition/css.rb +10 -0
  67. data/lib/capybara/selector/definition/datalist_input.rb +35 -0
  68. data/lib/capybara/selector/definition/datalist_option.rb +25 -0
  69. data/lib/capybara/selector/definition/element.rb +28 -0
  70. data/lib/capybara/selector/definition/field.rb +40 -0
  71. data/lib/capybara/selector/definition/fieldset.rb +14 -0
  72. data/lib/capybara/selector/definition/file_field.rb +13 -0
  73. data/lib/capybara/selector/definition/fillable_field.rb +33 -0
  74. data/lib/capybara/selector/definition/frame.rb +17 -0
  75. data/lib/capybara/selector/definition/id.rb +6 -0
  76. data/lib/capybara/selector/definition/label.rb +62 -0
  77. data/lib/capybara/selector/definition/link.rb +54 -0
  78. data/lib/capybara/selector/definition/link_or_button.rb +16 -0
  79. data/lib/capybara/selector/definition/option.rb +27 -0
  80. data/lib/capybara/selector/definition/radio_button.rb +27 -0
  81. data/lib/capybara/selector/definition/select.rb +81 -0
  82. data/lib/capybara/selector/definition/table.rb +109 -0
  83. data/lib/capybara/selector/definition/table_row.rb +21 -0
  84. data/lib/capybara/selector/definition/xpath.rb +5 -0
  85. data/lib/capybara/selector/definition.rb +279 -0
  86. data/lib/capybara/selector/filter.rb +2 -17
  87. data/lib/capybara/selector/filter_set.rb +81 -33
  88. data/lib/capybara/selector/filters/base.rb +50 -6
  89. data/lib/capybara/selector/filters/expression_filter.rb +8 -26
  90. data/lib/capybara/selector/filters/locator_filter.rb +29 -0
  91. data/lib/capybara/selector/filters/node_filter.rb +16 -12
  92. data/lib/capybara/selector/regexp_disassembler.rb +214 -0
  93. data/lib/capybara/selector/selector.rb +89 -210
  94. data/lib/capybara/selector/xpath_extensions.rb +17 -0
  95. data/lib/capybara/selector.rb +227 -526
  96. data/lib/capybara/selenium/atoms/getAttribute.min.js +1 -0
  97. data/lib/capybara/selenium/atoms/isDisplayed.min.js +1 -0
  98. data/lib/capybara/selenium/atoms/src/getAttribute.js +161 -0
  99. data/lib/capybara/selenium/atoms/src/isDisplayed.js +454 -0
  100. data/lib/capybara/selenium/driver.rb +343 -276
  101. data/lib/capybara/selenium/driver_specializations/chrome_driver.rb +117 -0
  102. data/lib/capybara/selenium/driver_specializations/edge_driver.rb +124 -0
  103. data/lib/capybara/selenium/driver_specializations/firefox_driver.rb +89 -0
  104. data/lib/capybara/selenium/driver_specializations/internet_explorer_driver.rb +26 -0
  105. data/lib/capybara/selenium/driver_specializations/safari_driver.rb +24 -0
  106. data/lib/capybara/selenium/extensions/file_input_click_emulation.rb +34 -0
  107. data/lib/capybara/selenium/extensions/find.rb +110 -0
  108. data/lib/capybara/selenium/extensions/html5_drag.rb +228 -0
  109. data/lib/capybara/selenium/extensions/modifier_keys_stack.rb +28 -0
  110. data/lib/capybara/selenium/extensions/scroll.rb +76 -0
  111. data/lib/capybara/selenium/logger_suppressor.rb +40 -0
  112. data/lib/capybara/selenium/node.rb +508 -124
  113. data/lib/capybara/selenium/nodes/chrome_node.rb +137 -0
  114. data/lib/capybara/selenium/nodes/edge_node.rb +104 -0
  115. data/lib/capybara/selenium/nodes/firefox_node.rb +136 -0
  116. data/lib/capybara/selenium/nodes/ie_node.rb +22 -0
  117. data/lib/capybara/selenium/nodes/safari_node.rb +118 -0
  118. data/lib/capybara/selenium/patches/action_pauser.rb +26 -0
  119. data/lib/capybara/selenium/patches/atoms.rb +18 -0
  120. data/lib/capybara/selenium/patches/is_displayed.rb +16 -0
  121. data/lib/capybara/selenium/patches/logs.rb +45 -0
  122. data/lib/capybara/selenium/patches/pause_duration_fix.rb +9 -0
  123. data/lib/capybara/selenium/patches/persistent_client.rb +20 -0
  124. data/lib/capybara/server/animation_disabler.rb +69 -0
  125. data/lib/capybara/server/checker.rb +44 -0
  126. data/lib/capybara/server/middleware.rb +71 -0
  127. data/lib/capybara/server.rb +59 -67
  128. data/lib/capybara/session/config.rb +79 -59
  129. data/lib/capybara/session/matchers.rb +41 -25
  130. data/lib/capybara/session.rb +371 -357
  131. data/lib/capybara/spec/public/jquery.js +5 -5
  132. data/lib/capybara/spec/public/offset.js +6 -0
  133. data/lib/capybara/spec/public/test.js +159 -13
  134. data/lib/capybara/spec/session/accept_alert_spec.rb +12 -11
  135. data/lib/capybara/spec/session/accept_confirm_spec.rb +6 -5
  136. data/lib/capybara/spec/session/accept_prompt_spec.rb +34 -6
  137. data/lib/capybara/spec/session/active_element_spec.rb +31 -0
  138. data/lib/capybara/spec/session/all_spec.rb +161 -55
  139. data/lib/capybara/spec/session/ancestor_spec.rb +27 -24
  140. data/lib/capybara/spec/session/assert_all_of_selectors_spec.rb +68 -38
  141. data/lib/capybara/spec/session/assert_current_path_spec.rb +75 -0
  142. data/lib/capybara/spec/session/assert_selector_spec.rb +143 -0
  143. data/lib/capybara/spec/session/assert_style_spec.rb +26 -0
  144. data/lib/capybara/spec/session/{assert_text.rb → assert_text_spec.rb} +91 -59
  145. data/lib/capybara/spec/session/{assert_title.rb → assert_title_spec.rb} +22 -12
  146. data/lib/capybara/spec/session/attach_file_spec.rb +138 -69
  147. data/lib/capybara/spec/session/body_spec.rb +12 -13
  148. data/lib/capybara/spec/session/check_spec.rb +116 -55
  149. data/lib/capybara/spec/session/choose_spec.rb +64 -31
  150. data/lib/capybara/spec/session/click_button_spec.rb +231 -173
  151. data/lib/capybara/spec/session/click_link_or_button_spec.rb +55 -35
  152. data/lib/capybara/spec/session/click_link_spec.rb +82 -58
  153. data/lib/capybara/spec/session/current_scope_spec.rb +11 -10
  154. data/lib/capybara/spec/session/current_url_spec.rb +57 -39
  155. data/lib/capybara/spec/session/dismiss_confirm_spec.rb +4 -4
  156. data/lib/capybara/spec/session/dismiss_prompt_spec.rb +3 -2
  157. data/lib/capybara/spec/session/element/{assert_match_selector.rb → assert_match_selector_spec.rb} +11 -9
  158. data/lib/capybara/spec/session/element/match_css_spec.rb +18 -10
  159. data/lib/capybara/spec/session/element/match_xpath_spec.rb +9 -7
  160. data/lib/capybara/spec/session/element/matches_selector_spec.rb +71 -57
  161. data/lib/capybara/spec/session/evaluate_async_script_spec.rb +23 -0
  162. data/lib/capybara/spec/session/evaluate_script_spec.rb +30 -9
  163. data/lib/capybara/spec/session/execute_script_spec.rb +10 -8
  164. data/lib/capybara/spec/session/fill_in_spec.rb +128 -43
  165. data/lib/capybara/spec/session/find_button_spec.rb +25 -24
  166. data/lib/capybara/spec/session/find_by_id_spec.rb +10 -9
  167. data/lib/capybara/spec/session/find_field_spec.rb +37 -41
  168. data/lib/capybara/spec/session/find_link_spec.rb +36 -17
  169. data/lib/capybara/spec/session/find_spec.rb +245 -144
  170. data/lib/capybara/spec/session/first_spec.rb +79 -51
  171. data/lib/capybara/spec/session/frame/frame_title_spec.rb +23 -0
  172. data/lib/capybara/spec/session/frame/frame_url_spec.rb +23 -0
  173. data/lib/capybara/spec/session/frame/switch_to_frame_spec.rb +33 -20
  174. data/lib/capybara/spec/session/frame/within_frame_spec.rb +50 -32
  175. data/lib/capybara/spec/session/go_back_spec.rb +2 -1
  176. data/lib/capybara/spec/session/go_forward_spec.rb +2 -1
  177. data/lib/capybara/spec/session/has_all_selectors_spec.rb +69 -0
  178. data/lib/capybara/spec/session/has_ancestor_spec.rb +46 -0
  179. data/lib/capybara/spec/session/has_any_selectors_spec.rb +29 -0
  180. data/lib/capybara/spec/session/has_button_spec.rb +94 -13
  181. data/lib/capybara/spec/session/has_css_spec.rb +272 -137
  182. data/lib/capybara/spec/session/has_current_path_spec.rb +87 -45
  183. data/lib/capybara/spec/session/has_field_spec.rb +139 -59
  184. data/lib/capybara/spec/session/has_link_spec.rb +34 -9
  185. data/lib/capybara/spec/session/has_none_selectors_spec.rb +78 -0
  186. data/lib/capybara/spec/session/has_select_spec.rb +103 -74
  187. data/lib/capybara/spec/session/has_selector_spec.rb +105 -71
  188. data/lib/capybara/spec/session/has_sibling_spec.rb +50 -0
  189. data/lib/capybara/spec/session/has_table_spec.rb +172 -5
  190. data/lib/capybara/spec/session/has_text_spec.rb +113 -61
  191. data/lib/capybara/spec/session/has_title_spec.rb +20 -14
  192. data/lib/capybara/spec/session/has_xpath_spec.rb +57 -38
  193. data/lib/capybara/spec/session/{headers.rb → headers_spec.rb} +3 -2
  194. data/lib/capybara/spec/session/html_spec.rb +14 -6
  195. data/lib/capybara/spec/session/matches_style_spec.rb +35 -0
  196. data/lib/capybara/spec/session/node_spec.rb +950 -152
  197. data/lib/capybara/spec/session/node_wrapper_spec.rb +39 -0
  198. data/lib/capybara/spec/session/refresh_spec.rb +12 -6
  199. data/lib/capybara/spec/session/reset_session_spec.rb +69 -35
  200. data/lib/capybara/spec/session/{response_code.rb → response_code_spec.rb} +2 -1
  201. data/lib/capybara/spec/session/save_and_open_page_spec.rb +3 -2
  202. data/lib/capybara/spec/session/save_and_open_screenshot_spec.rb +8 -12
  203. data/lib/capybara/spec/session/save_page_spec.rb +42 -55
  204. data/lib/capybara/spec/session/save_screenshot_spec.rb +16 -14
  205. data/lib/capybara/spec/session/screenshot_spec.rb +2 -2
  206. data/lib/capybara/spec/session/scroll_spec.rb +117 -0
  207. data/lib/capybara/spec/session/select_spec.rb +107 -80
  208. data/lib/capybara/spec/session/selectors_spec.rb +52 -19
  209. data/lib/capybara/spec/session/sibling_spec.rb +10 -10
  210. data/lib/capybara/spec/session/text_spec.rb +37 -21
  211. data/lib/capybara/spec/session/title_spec.rb +17 -5
  212. data/lib/capybara/spec/session/uncheck_spec.rb +42 -22
  213. data/lib/capybara/spec/session/unselect_spec.rb +39 -38
  214. data/lib/capybara/spec/session/visit_spec.rb +99 -32
  215. data/lib/capybara/spec/session/window/become_closed_spec.rb +24 -20
  216. data/lib/capybara/spec/session/window/current_window_spec.rb +5 -3
  217. data/lib/capybara/spec/session/window/open_new_window_spec.rb +5 -3
  218. data/lib/capybara/spec/session/window/switch_to_window_spec.rb +27 -22
  219. data/lib/capybara/spec/session/window/window_opened_by_spec.rb +12 -6
  220. data/lib/capybara/spec/session/window/window_spec.rb +97 -63
  221. data/lib/capybara/spec/session/window/windows_spec.rb +12 -10
  222. data/lib/capybara/spec/session/window/within_window_spec.rb +31 -86
  223. data/lib/capybara/spec/session/within_spec.rb +70 -44
  224. data/lib/capybara/spec/spec_helper.rb +49 -43
  225. data/lib/capybara/spec/test_app.rb +89 -42
  226. data/lib/capybara/spec/views/animated.erb +49 -0
  227. data/lib/capybara/spec/views/form.erb +141 -42
  228. data/lib/capybara/spec/views/frame_child.erb +4 -3
  229. data/lib/capybara/spec/views/frame_one.erb +2 -1
  230. data/lib/capybara/spec/views/frame_parent.erb +1 -1
  231. data/lib/capybara/spec/views/frame_two.erb +1 -1
  232. data/lib/capybara/spec/views/initial_alert.erb +11 -0
  233. data/lib/capybara/spec/views/layout.erb +10 -0
  234. data/lib/capybara/spec/views/obscured.erb +47 -0
  235. data/lib/capybara/spec/views/offset.erb +33 -0
  236. data/lib/capybara/spec/views/path.erb +2 -2
  237. data/lib/capybara/spec/views/popup_one.erb +1 -1
  238. data/lib/capybara/spec/views/popup_two.erb +1 -1
  239. data/lib/capybara/spec/views/react.erb +45 -0
  240. data/lib/capybara/spec/views/scroll.erb +21 -0
  241. data/lib/capybara/spec/views/spatial.erb +31 -0
  242. data/lib/capybara/spec/views/tables.erb +68 -1
  243. data/lib/capybara/spec/views/with_animation.erb +81 -0
  244. data/lib/capybara/spec/views/with_base_tag.erb +2 -2
  245. data/lib/capybara/spec/views/with_dragula.erb +24 -0
  246. data/lib/capybara/spec/views/with_fixed_header_footer.erb +18 -0
  247. data/lib/capybara/spec/views/with_hover.erb +8 -2
  248. data/lib/capybara/spec/views/with_hover1.erb +10 -0
  249. data/lib/capybara/spec/views/with_html.erb +70 -11
  250. data/lib/capybara/spec/views/with_html5_svg.erb +20 -0
  251. data/lib/capybara/spec/views/with_jquery_animation.erb +24 -0
  252. data/lib/capybara/spec/views/with_js.erb +39 -3
  253. data/lib/capybara/spec/views/with_jstree.erb +26 -0
  254. data/lib/capybara/spec/views/with_namespace.erb +21 -0
  255. data/lib/capybara/spec/views/with_scope_other.erb +6 -0
  256. data/lib/capybara/spec/views/with_slow_unload.erb +2 -1
  257. data/lib/capybara/spec/views/with_sortable_js.erb +21 -0
  258. data/lib/capybara/spec/views/with_unload_alert.erb +1 -0
  259. data/lib/capybara/spec/views/with_windows.erb +1 -1
  260. data/lib/capybara/spec/views/within_frames.erb +5 -2
  261. data/lib/capybara/version.rb +2 -1
  262. data/lib/capybara/window.rb +35 -33
  263. data/lib/capybara.rb +126 -103
  264. data/spec/basic_node_spec.rb +60 -34
  265. data/spec/capybara_spec.rb +53 -104
  266. data/spec/css_builder_spec.rb +101 -0
  267. data/spec/css_splitter_spec.rb +38 -0
  268. data/spec/dsl_spec.rb +81 -62
  269. data/spec/filter_set_spec.rb +27 -9
  270. data/spec/fixtures/certificate.pem +25 -0
  271. data/spec/fixtures/key.pem +27 -0
  272. data/spec/fixtures/selenium_driver_rspec_failure.rb +5 -4
  273. data/spec/fixtures/selenium_driver_rspec_success.rb +5 -4
  274. data/spec/minitest_spec.rb +49 -7
  275. data/spec/minitest_spec_spec.rb +94 -59
  276. data/spec/per_session_config_spec.rb +14 -13
  277. data/spec/rack_test_spec.rb +180 -125
  278. data/spec/regexp_dissassembler_spec.rb +250 -0
  279. data/spec/result_spec.rb +99 -45
  280. data/spec/rspec/features_spec.rb +37 -31
  281. data/spec/rspec/scenarios_spec.rb +9 -7
  282. data/spec/rspec/shared_spec_matchers.rb +448 -421
  283. data/spec/rspec/views_spec.rb +5 -3
  284. data/spec/rspec_matchers_spec.rb +27 -11
  285. data/spec/rspec_spec.rb +109 -89
  286. data/spec/sauce_spec_chrome.rb +43 -0
  287. data/spec/selector_spec.rb +396 -67
  288. data/spec/selenium_spec_chrome.rb +183 -35
  289. data/spec/selenium_spec_chrome_remote.rb +101 -0
  290. data/spec/selenium_spec_edge.rb +47 -0
  291. data/spec/selenium_spec_firefox.rb +184 -41
  292. data/spec/selenium_spec_firefox_remote.rb +80 -0
  293. data/spec/selenium_spec_ie.rb +147 -0
  294. data/spec/selenium_spec_safari.rb +156 -0
  295. data/spec/server_spec.rb +198 -99
  296. data/spec/session_spec.rb +53 -16
  297. data/spec/shared_selenium_node.rb +79 -0
  298. data/spec/shared_selenium_session.rb +489 -97
  299. data/spec/spec_helper.rb +93 -7
  300. data/spec/xpath_builder_spec.rb +93 -0
  301. metadata +365 -70
  302. data/.yard/templates_custom/default/class/html/selectors.erb +0 -38
  303. data/.yard/templates_custom/default/class/html/setup.rb +0 -17
  304. data/.yard/yard_extensions.rb +0 -78
  305. data/lib/capybara/query.rb +0 -7
  306. data/lib/capybara/rspec/compound.rb +0 -95
  307. data/lib/capybara/spec/session/assert_current_path.rb +0 -72
  308. data/lib/capybara/spec/session/assert_selector.rb +0 -148
  309. data/lib/capybara/spec/session/source_spec.rb +0 -0
  310. data/lib/capybara/spec/views/with_title.erb +0 -5
  311. data/spec/selenium_spec_marionette.rb +0 -117
@@ -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!
@@ -294,10 +303,28 @@ module Capybara
294
303
  driver.go_forward
295
304
  end
296
305
 
306
+ ##
307
+ # @!method send_keys
308
+ # @see Capybara::Node::Element#send_keys
309
+ #
310
+ def send_keys(*args, **kw_args)
311
+ driver.send_keys(*args, **kw_args)
312
+ end
313
+
297
314
  ##
298
315
  #
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
316
+ # Returns the element with focus.
317
+ #
318
+ # Not supported by Rack Test
319
+ #
320
+ def active_element
321
+ Capybara::Queries::ActiveElementQuery.new.resolve_for(self)[0].tap(&:allow_reload!)
322
+ end
323
+
324
+ ##
325
+ #
326
+ # Executes the given block within the context of a node. {#within} takes the
327
+ # same options as {Capybara::Node::Finders#find #find}, as well as a block. For the duration of the
301
328
  # block, any command to Capybara will be handled as though it were scoped
302
329
  # to the given element.
303
330
  #
@@ -305,18 +332,18 @@ module Capybara
305
332
  # fill_in('Street', with: '12 Main Street')
306
333
  # end
307
334
  #
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
335
+ # Just as with `#find`, if multiple elements match the selector given to
336
+ # {#within}, an error will be raised, and just as with `#find`, this
310
337
  # behaviour can be controlled through the `:match` and `:exact` options.
311
338
  #
312
339
  # 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.
340
+ # assumed to be of the type set in {Capybara.configure default_selector}.
314
341
  #
315
342
  # within('div#delivery-address') do
316
343
  # fill_in('Street', with: '12 Main Street')
317
344
  # end
318
345
  #
319
- # Note that a lot of uses of `within` can be replaced more succinctly with
346
+ # Note that a lot of uses of {#within} can be replaced more succinctly with
320
347
  # chaining:
321
348
  #
322
349
  # find('div#delivery-address').fill_in('Street', with: '12 Main Street')
@@ -329,11 +356,11 @@ module Capybara
329
356
  #
330
357
  # @raise [Capybara::ElementNotFound] If the scope can't be found before time expires
331
358
  #
332
- def within(*args)
333
- new_scope = if args.first.is_a?(Capybara::Node::Base) then args.first else find(*args) end
359
+ def within(*args, **kw_args)
360
+ new_scope = args.first.respond_to?(:to_capybara_node) ? args.first.to_capybara_node : find(*args, **kw_args)
334
361
  begin
335
362
  scopes.push(new_scope)
336
- yield
363
+ yield if block_given?
337
364
  ensure
338
365
  scopes.pop
339
366
  end
@@ -346,10 +373,8 @@ module Capybara
346
373
  #
347
374
  # @param [String] locator Id or legend of the fieldset
348
375
  #
349
- def within_fieldset(locator)
350
- within :fieldset, locator do
351
- yield
352
- end
376
+ def within_fieldset(locator, &block)
377
+ within(:fieldset, locator, &block)
353
378
  end
354
379
 
355
380
  ##
@@ -358,26 +383,24 @@ module Capybara
358
383
  #
359
384
  # @param [String] locator Id or caption of the table
360
385
  #
361
- def within_table(locator)
362
- within :table, locator do
363
- yield
364
- end
386
+ def within_table(locator, &block)
387
+ within(:table, locator, &block)
365
388
  end
366
389
 
367
390
  ##
368
391
  #
369
- # Switch to the given frame
392
+ # Switch to the given frame.
370
393
  #
371
394
  # 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.
395
+ # {#within_frame} is preferred over this method and should be used when possible.
373
396
  # May not be supported by all drivers.
374
397
  #
375
398
  # @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
399
+ # @param [Capybara::Node::Element] element iframe/frame element to switch to
400
+ # @overload switch_to_frame(location)
401
+ # @param [Symbol] location relative location of the frame to switch to
402
+ # * :parent - the parent frame
403
+ # * :top - the top level document
381
404
  #
382
405
  def switch_to_frame(frame)
383
406
  case frame
@@ -385,18 +408,25 @@ module Capybara
385
408
  driver.switch_to_frame(frame)
386
409
  scopes.push(:frame)
387
410
  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
411
+ if scopes.last != :frame
412
+ raise Capybara::ScopeError, "`switch_to_frame(:parent)` cannot be called from inside a descendant frame's "\
413
+ '`within` block.'
414
+ end
390
415
  scopes.pop
391
416
  driver.switch_to_frame(:parent)
392
417
  when :top
393
418
  idx = scopes.index(:frame)
419
+ top_level_scopes = [:frame, nil]
394
420
  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)}
397
- scopes.slice!(idx..-1)
421
+ if scopes.slice(idx..).any? { |scope| !top_level_scopes.include?(scope) }
422
+ raise Capybara::ScopeError, "`switch_to_frame(:top)` cannot be called from inside a descendant frame's "\
423
+ '`within` block.'
424
+ end
425
+ scopes.slice!(idx..)
398
426
  driver.switch_to_frame(:top)
399
427
  end
428
+ else
429
+ raise ArgumentError, 'You must provide a frame element, :parent, or :top when calling switch_to_frame'
400
430
  end
401
431
  end
402
432
 
@@ -407,35 +437,17 @@ module Capybara
407
437
  #
408
438
  # @overload within_frame(element)
409
439
  # @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
440
+ # @overload within_frame([kind = :frame], locator, **options)
441
+ # @param [Symbol] kind Optional selector type (:frame, :css, :xpath, etc.) - Defaults to :frame
412
442
  # @param [String] locator The locator for the given selector kind. For :frame this is the name/id of a frame/iframe element
413
443
  # @overload within_frame(index)
414
444
  # @param [Integer] index index of a frame (0 based)
415
- def within_frame(*args)
416
- frame = _find_frame(*args)
417
-
445
+ def within_frame(*args, **kw_args)
446
+ switch_to_frame(_find_frame(*args, **kw_args))
418
447
  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
448
+ yield if block_given?
449
+ ensure
450
+ switch_to_frame(:parent)
439
451
  end
440
452
  end
441
453
 
@@ -460,48 +472,50 @@ module Capybara
460
472
  end
461
473
 
462
474
  ##
463
- # Open new window.
464
- # Current window doesn't change as the result of this call.
475
+ # Open a new window.
476
+ # The current window doesn't change as the result of this call.
465
477
  # It should be switched to explicitly.
466
478
  #
467
479
  # @return [Capybara::Window] window that has been opened
468
480
  #
469
- def open_new_window
481
+ def open_new_window(kind = :tab)
470
482
  window_opened_by do
471
- driver.open_new_window
483
+ if driver.method(:open_new_window).arity.zero?
484
+ driver.open_new_window
485
+ else
486
+ driver.open_new_window(kind)
487
+ end
472
488
  end
473
489
  end
474
490
 
475
491
  ##
492
+ # Switch to the given window.
493
+ #
476
494
  # @overload switch_to_window(&block)
477
495
  # 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.
496
+ # If window that matches block can't be found, the window will be switched back and {Capybara::WindowError} will be raised.
479
497
  # @example
480
498
  # window = switch_to_window { title == 'Page title' }
481
499
  # @raise [Capybara::WindowError] if no window matches given block
482
500
  # @overload switch_to_window(window)
483
501
  # @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
502
+ # @raise [Capybara::Driver::Base#no_such_window_error] if nonexistent (e.g. closed) window was passed
485
503
  #
486
504
  # @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
505
+ # @raise [Capybara::ScopeError] if this method is invoked inside {#within} or
506
+ # {#within_frame} methods
489
507
  # @raise [ArgumentError] if both or neither arguments were provided
490
508
  #
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."
509
+ def switch_to_window(window = nil, **options, &window_locator)
510
+ raise ArgumentError, '`switch_to_window` can take either a block or a window, not both' if window && window_locator
511
+ raise ArgumentError, '`switch_to_window`: either window or block should be provided' if !window && !window_locator
512
+
513
+ unless scopes.last.nil?
514
+ raise Capybara::ScopeError, '`switch_to_window` is not supposed to be invoked from '\
515
+ '`within` or `within_frame` blocks.'
502
516
  end
503
517
 
504
- _switch_to_window(window, options, &window_locator)
518
+ _switch_to_window(window, **options, &window_locator)
505
519
  end
506
520
 
507
521
  ##
@@ -509,63 +523,42 @@ module Capybara
509
523
  #
510
524
  # 1. Switches to the given window (it can be located by window instance/lambda/string).
511
525
  # 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)
526
+ # 3. Switches back (this step will be invoked even if an exception occurs at the second step).
513
527
  #
514
528
  # @overload within_window(window) { do_something }
515
- # @param window [Capybara::Window] instance of `Capybara::Window` class
529
+ # @param window [Capybara::Window] instance of {Capybara::Window} class
516
530
  # that will be switched to
517
- # @raise [driver#no_such_window_error] if unexistent (e.g. closed) window was passed
531
+ # @raise [driver#no_such_window_error] if nonexistent (e.g. closed) window was passed
518
532
  # @overload within_window(proc_or_lambda) { do_something }
519
- # @param lambda [Proc] lambda. First window for which lambda
533
+ # @param lambda [Proc] First window for which lambda
520
534
  # returns a value other than false or nil will be switched to.
521
535
  # @example
522
536
  # within_window(->{ page.title == 'Page title' }) { click_button 'Submit' }
523
537
  # @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
538
  #
528
- # @raise [Capybara::ScopeError] if this method is invoked inside `within_frame` method
539
+ # @raise [Capybara::ScopeError] if this method is invoked inside {#within_frame} method
529
540
  # @return value returned by the block
530
541
  #
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
542
+ def within_window(window_or_proc)
543
+ original = current_window
544
+ scopes << nil
545
+ begin
546
+ case window_or_proc
547
+ when Capybara::Window
548
+ _switch_to_window(window_or_proc) unless original == window_or_proc
549
+ when Proc
550
+ _switch_to_window { window_or_proc.call }
551
+ else
552
+ raise ArgumentError, '`#within_window` requires a `Capybara::Window` instance or a lambda'
557
553
  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})"
554
+
563
555
  begin
564
- scopes << nil
565
- driver.within_window(window_or_handle) { yield }
556
+ yield if block_given?
566
557
  ensure
567
- scopes.pop
558
+ _switch_to_window(original) unless original == window_or_proc
568
559
  end
560
+ ensure
561
+ scopes.pop
569
562
  end
570
563
  end
571
564
 
@@ -573,23 +566,23 @@ module Capybara
573
566
  # Get the window that has been opened by the passed block.
574
567
  # It will wait for it to be opened (in the same way as other Capybara methods wait).
575
568
  # 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}
569
+ # {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
570
  #
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
571
+ # @overload window_opened_by(**options, &block)
572
+ # @param options [Hash]
573
+ # @option options [Numeric] :wait maximum wait time. Defaults to {Capybara.configure default_max_wait_time}
574
+ # @return [Capybara::Window] the window that has been opened within a block
575
+ # @raise [Capybara::WindowError] if block passed to window hasn't opened window
576
+ # or opened more than one window
583
577
  #
584
- def window_opened_by(options = {}, &block)
578
+ def window_opened_by(**options)
585
579
  old_handles = driver.window_handles
586
- block.call
580
+ yield
587
581
 
588
- wait_time = Capybara::Queries::BaseQuery.wait(options, config.default_max_wait_time)
589
- document.synchronize(wait_time, errors: [Capybara::WindowError]) do
582
+ synchronize_windows(options) do
590
583
  opened_handles = (driver.window_handles - old_handles)
591
584
  if opened_handles.size != 1
592
- raise Capybara::WindowError, "block passed to #window_opened_by "\
585
+ raise Capybara::WindowError, 'block passed to #window_opened_by '\
593
586
  "opened #{opened_handles.size} windows instead of 1"
594
587
  end
595
588
  Window.new(self, opened_handles.first)
@@ -599,39 +592,44 @@ module Capybara
599
592
  ##
600
593
  #
601
594
  # 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.
595
+ # complex objects, such as jQuery statements. {#execute_script} should be used over
596
+ # {#evaluate_script} whenever possible.
604
597
  #
605
598
  # @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
599
+ # @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
600
  #
608
601
  def execute_script(script, *args)
609
602
  @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
603
+ driver.execute_script(script, *driver_args(args))
616
604
  end
617
605
 
618
606
  ##
619
607
  #
620
608
  # 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
609
+ # scripts that return complex objects, such as jQuery statements. {#execute_script} might
622
610
  # be a better alternative.
623
611
  #
624
612
  # @param [String] script A string of JavaScript to evaluate
613
+ # @param args Optional arguments that will be passed to the script
625
614
  # @return [Object] The result of the evaluated JavaScript (may be driver specific)
626
615
  #
627
616
  def evaluate_script(script, *args)
628
617
  @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
618
+ result = driver.evaluate_script(script.strip, *driver_args(args))
619
+ element_script_result(result)
620
+ end
621
+
622
+ ##
623
+ #
624
+ # Evaluate the given JavaScript and obtain the result from a callback function which will be passed as the last argument to the script.
625
+ #
626
+ # @param [String] script A string of JavaScript to evaluate
627
+ # @param args Optional arguments that will be passed to the script
628
+ # @return [Object] The result of the evaluated JavaScript (may be driver specific)
629
+ #
630
+ def evaluate_async_script(script, *args)
631
+ @touched = true
632
+ result = driver.evaluate_async_script(script, *driver_args(args))
635
633
  element_script_result(result)
636
634
  end
637
635
 
@@ -640,15 +638,23 @@ module Capybara
640
638
  # Execute the block, accepting a alert.
641
639
  #
642
640
  # @!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.
641
+ # Expects a block whose actions will trigger the display modal to appear.
642
+ # @example
643
+ # $0 do
644
+ # click_link('link that triggers appearance of system modal')
645
+ # end
646
+ # @overload $0(text, **options, &blk)
647
+ # @param text [String, Regexp] Text or regex to match against the text in the modal. If not provided any modal is matched.
648
+ # @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}.
649
+ # @yield Block whose actions will trigger the system modal
650
+ # @overload $0(**options, &blk)
651
+ # @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}.
652
+ # @yield Block whose actions will trigger the system modal
647
653
  # @return [String] the message shown in the modal
648
654
  # @raise [Capybara::ModalNotFound] if modal dialog hasn't been found
649
655
  #
650
- def accept_alert(text_or_options=nil, options={}, &blk)
651
- accept_modal(:alert, text_or_options, options, &blk)
656
+ def accept_alert(text = nil, **options, &blk)
657
+ accept_modal(:alert, text, options, &blk)
652
658
  end
653
659
 
654
660
  ##
@@ -657,8 +663,8 @@ module Capybara
657
663
  #
658
664
  # @macro modal_params
659
665
  #
660
- def accept_confirm(text_or_options=nil, options={}, &blk)
661
- accept_modal(:confirm, text_or_options, options, &blk)
666
+ def accept_confirm(text = nil, **options, &blk)
667
+ accept_modal(:confirm, text, options, &blk)
662
668
  end
663
669
 
664
670
  ##
@@ -667,8 +673,8 @@ module Capybara
667
673
  #
668
674
  # @macro modal_params
669
675
  #
670
- def dismiss_confirm(text_or_options=nil, options={}, &blk)
671
- dismiss_modal(:confirm, text_or_options, options, &blk)
676
+ def dismiss_confirm(text = nil, **options, &blk)
677
+ dismiss_modal(:confirm, text, options, &blk)
672
678
  end
673
679
 
674
680
  ##
@@ -678,8 +684,8 @@ module Capybara
678
684
  # @macro modal_params
679
685
  # @option options [String] :with Response to provide to the prompt
680
686
  #
681
- def accept_prompt(text_or_options=nil, options={}, &blk)
682
- accept_modal(:prompt, text_or_options, options, &blk)
687
+ def accept_prompt(text = nil, **options, &blk)
688
+ accept_modal(:prompt, text, options, &blk)
683
689
  end
684
690
 
685
691
  ##
@@ -688,82 +694,70 @@ module Capybara
688
694
  #
689
695
  # @macro modal_params
690
696
  #
691
- def dismiss_prompt(text_or_options=nil, options={}, &blk)
692
- dismiss_modal(:prompt, text_or_options, options, &blk)
697
+ def dismiss_prompt(text = nil, **options, &blk)
698
+ dismiss_modal(:prompt, text, options, &blk)
693
699
  end
694
700
 
695
701
  ##
696
702
  #
697
- # Save a snapshot of the page. If `Capybara.asset_host` is set it will inject `base` tag
698
- # pointing to `asset_host`.
703
+ # Save a snapshot of the page. If {Capybara.configure asset_host} is set it will inject `base` tag
704
+ # pointing to {Capybara.configure asset_host}.
699
705
  #
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
706
+ # If invoked without arguments it will save file to {Capybara.configure save_path}
707
+ # and file will be given randomly generated filename. If invoked with a relative path
708
+ # the path will be relative to {Capybara.configure save_path}.
705
709
  #
706
710
  # @param [String] path the path to where it should be saved
707
711
  # @return [String] the path to which the file was saved
708
712
  #
709
713
  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
714
+ prepare_path(path, 'html').tap do |p_path|
715
+ File.write(p_path, Capybara::Helpers.inject_asset_host(body, host: config.asset_host), mode: 'wb')
716
+ end
713
717
  end
714
718
 
715
719
  ##
716
720
  #
717
721
  # Save a snapshot of the page and open it in a browser for inspection.
718
722
  #
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
723
+ # If invoked without arguments it will save file to {Capybara.configure save_path}
724
+ # and file will be given randomly generated filename. If invoked with a relative path
725
+ # the path will be relative to {Capybara.configure save_path}.
724
726
  #
725
727
  # @param [String] path the path to where it should be saved
726
728
  #
727
729
  def save_and_open_page(path = nil)
728
- path = save_page(path)
729
- open_file(path)
730
+ save_page(path).tap { |s_path| open_file(s_path) }
730
731
  end
731
732
 
732
733
  ##
733
734
  #
734
735
  # Save a screenshot of page.
735
736
  #
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
737
+ # If invoked without arguments it will save file to {Capybara.configure save_path}
738
+ # and file will be given randomly generated filename. If invoked with a relative path
739
+ # the path will be relative to {Capybara.configure save_path}.
741
740
  #
742
741
  # @param [String] path the path to where it should be saved
743
742
  # @param [Hash] options a customizable set of options
744
743
  # @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
744
+ def save_screenshot(path = nil, **options)
745
+ prepare_path(path, 'png').tap { |p_path| driver.save_screenshot(p_path, **options) }
749
746
  end
750
747
 
751
748
  ##
752
749
  #
753
750
  # Save a screenshot of the page and open it for inspection.
754
751
  #
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
752
+ # If invoked without arguments it will save file to {Capybara.configure save_path}
753
+ # and file will be given randomly generated filename. If invoked with a relative path
754
+ # the path will be relative to {Capybara.configure save_path}.
760
755
  #
761
756
  # @param [String] path the path to where it should be saved
762
757
  # @param [Hash] options a customizable set of options
763
758
  #
764
- def save_and_open_screenshot(path = nil, options = {})
765
- path = save_screenshot(path, options)
766
- open_file(path)
759
+ def save_and_open_screenshot(path = nil, **options)
760
+ save_screenshot(path, **options).tap { |s_path| open_file(s_path) }
767
761
  end
768
762
 
769
763
  def document
@@ -771,15 +765,32 @@ module Capybara
771
765
  end
772
766
 
773
767
  NODE_METHODS.each do |method|
774
- define_method method do |*args, &block|
775
- @touched = true
776
- current_scope.send(method, *args, &block)
768
+ if RUBY_VERSION >= '2.7'
769
+ class_eval <<~METHOD, __FILE__, __LINE__ + 1
770
+ def #{method}(...)
771
+ @touched = true
772
+ current_scope.#{method}(...)
773
+ end
774
+ METHOD
775
+ else
776
+ define_method method do |*args, &block|
777
+ @touched = true
778
+ current_scope.send(method, *args, &block)
779
+ end
777
780
  end
778
781
  end
779
782
 
780
783
  DOCUMENT_METHODS.each do |method|
781
- define_method method do |*args, &block|
782
- document.send(method, *args, &block)
784
+ if RUBY_VERSION >= '2.7'
785
+ class_eval <<~METHOD, __FILE__, __LINE__ + 1
786
+ def #{method}(...)
787
+ document.#{method}(...)
788
+ end
789
+ METHOD
790
+ else
791
+ define_method method do |*args, &block|
792
+ document.send(method, *args, &block)
793
+ end
783
794
  end
784
795
  end
785
796
 
@@ -789,15 +800,14 @@ module Capybara
789
800
 
790
801
  def current_scope
791
802
  scope = scopes.last
792
- scope = document if [nil, :frame].include? scope
793
- scope
803
+ [nil, :frame].include?(scope) ? document : scope
794
804
  end
795
805
 
796
806
  ##
797
807
  #
798
- # Yield a block using a specific wait time
808
+ # Yield a block using a specific maximum wait time.
799
809
  #
800
- def using_wait_time(seconds)
810
+ def using_wait_time(seconds, &block)
801
811
  if Capybara.threadsafe
802
812
  begin
803
813
  previous_wait_time = config.default_max_wait_time
@@ -807,17 +817,18 @@ module Capybara
807
817
  config.default_max_wait_time = previous_wait_time
808
818
  end
809
819
  else
810
- Capybara.using_wait_time(seconds) { yield }
820
+ Capybara.using_wait_time(seconds, &block)
811
821
  end
812
822
  end
813
823
 
814
824
  ##
815
825
  #
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
826
+ # Accepts a block to set the configuration options if {Capybara.configure threadsafe} is `true`. Note that some options only have an effect
827
+ # if set at initialization time, so look at the configuration block that can be passed to the initializer too.
818
828
  #
819
829
  def configure
820
- raise "Session configuration is only supported when Capybara.threadsafe == true" unless Capybara.threadsafe
830
+ raise 'Session configuration is only supported when Capybara.threadsafe == true' unless Capybara.threadsafe
831
+
821
832
  yield config
822
833
  end
823
834
 
@@ -832,48 +843,48 @@ module Capybara
832
843
  Capybara::ReadOnlySessionConfig.new(Capybara.session_options)
833
844
  end
834
845
  end
846
+
847
+ def server_url
848
+ @server&.base_url
849
+ end
850
+
835
851
  private
836
852
 
837
- @@instance_created = false
853
+ @@instance_created = false # rubocop:disable Style/ClassVars
854
+
855
+ def driver_args(args)
856
+ args.map { |arg| arg.is_a?(Capybara::Node::Element) ? arg.base : arg }
857
+ end
838
858
 
839
859
  def accept_modal(type, text_or_options, options, &blk)
840
- driver.accept_modal(type, modal_options(text_or_options, options), &blk)
860
+ driver.accept_modal(type, **modal_options(text_or_options, **options), &blk)
841
861
  end
842
862
 
843
863
  def dismiss_modal(type, text_or_options, options, &blk)
844
- driver.dismiss_modal(type, modal_options(text_or_options, options), &blk)
864
+ driver.dismiss_modal(type, **modal_options(text_or_options, **options), &blk)
845
865
  end
846
866
 
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?
867
+ def modal_options(text = nil, **options)
868
+ options[:text] ||= text unless text.nil?
850
869
  options[:wait] ||= config.default_max_wait_time
851
870
  options
852
871
  end
853
872
 
854
-
855
873
  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
874
+ require 'launchy'
875
+ Launchy.open(path)
876
+ rescue LoadError
877
+ warn "File saved to #{path}.\nPlease install the launchy gem to open the file automatically."
863
878
  end
864
879
 
865
880
  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?
881
+ File.expand_path(path || default_fn(extension), config.save_path).tap do |p_path|
882
+ FileUtils.mkdir_p(File.dirname(p_path))
870
883
  end
871
- FileUtils.mkdir_p(File.dirname(path))
872
- path
873
884
  end
874
885
 
875
886
  def default_fn(extension)
876
- timestamp = Time.new.strftime("%Y%m%d%H%M%S")
887
+ timestamp = Time.new.strftime('%Y%m%d%H%M%S')
877
888
  "capybara-#{timestamp}#{rand(10**10)}.#{extension}"
878
889
  end
879
890
 
@@ -884,9 +895,9 @@ module Capybara
884
895
  def element_script_result(arg)
885
896
  case arg
886
897
  when Array
887
- arg.map { |e| element_script_result(e) }
898
+ arg.map { |subarg| element_script_result(subarg) }
888
899
  when Hash
889
- arg.each { |k, v| arg[k] = element_script_result(v) }
900
+ arg.transform_values! { |value| element_script_result(value) }
890
901
  when Capybara::Driver::Node
891
902
  Capybara::Node::Element.new(self, arg, nil, nil)
892
903
  else
@@ -894,54 +905,57 @@ module Capybara
894
905
  end
895
906
  end
896
907
 
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
908
+ def adjust_server_port(uri)
909
+ uri.port ||= @server.port if @server && config.always_include_port
913
910
  end
914
911
 
915
- def _switch_to_window(window = nil, options= {})
916
- options, window = window, nil if window.is_a? Hash
912
+ def _find_frame(*args, **kw_args)
913
+ case args[0]
914
+ when Capybara::Node::Element
915
+ args[0]
916
+ when String, nil
917
+ find(:frame, *args, **kw_args)
918
+ when Symbol
919
+ find(*args, **kw_args)
920
+ when Integer
921
+ idx = args[0]
922
+ all(:frame, minimum: idx + 1)[idx]
923
+ else
924
+ raise TypeError
925
+ end
926
+ end
917
927
 
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?
928
+ def _switch_to_window(window = nil, **options, &window_locator)
929
+ raise Capybara::ScopeError, 'Window cannot be switched inside a `within_frame` block' if scopes.include?(:frame)
930
+ raise Capybara::ScopeError, 'Window cannot be switched inside a `within` block' unless scopes.last.nil?
920
931
 
921
932
  if window
922
933
  driver.switch_to_window(window.handle)
923
934
  window
924
935
  else
925
- wait_time = Capybara::Queries::BaseQuery.wait(options, config.default_max_wait_time)
926
- document.synchronize(wait_time, errors: [Capybara::WindowError]) do
936
+ synchronize_windows(options) do
927
937
  original_window_handle = driver.current_window_handle
928
938
  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
936
- driver.switch_to_window(original_window_handle)
937
- raise e
938
- else
939
+ _switch_to_window_by_locator(&window_locator)
940
+ rescue StandardError
939
941
  driver.switch_to_window(original_window_handle)
940
- raise Capybara::WindowError, "Could not find a window matching block/lambda"
942
+ raise
941
943
  end
942
944
  end
943
945
  end
944
946
  end
945
947
 
948
+ def _switch_to_window_by_locator
949
+ driver.window_handles.each do |handle|
950
+ driver.switch_to_window handle
951
+ return Window.new(self, handle) if yield
952
+ end
953
+ raise Capybara::WindowError, 'Could not find a window matching block/lambda'
954
+ end
955
+
956
+ def synchronize_windows(options, &block)
957
+ wait_time = Capybara::Queries::BaseQuery.wait(options, config.default_max_wait_time)
958
+ document.synchronize(wait_time, errors: [Capybara::WindowError], &block)
959
+ end
946
960
  end
947
961
  end