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,7 +1,7 @@
1
1
  # frozen_string_literal: true
2
+
2
3
  module Capybara
3
4
  module Node
4
-
5
5
  ##
6
6
  #
7
7
  # A {Capybara::Node::Base} represents either an element on a page through the subclass
@@ -67,51 +67,56 @@ module Capybara
67
67
  # time has passed. On rubies/platforms which don't support access to a monotonic process clock
68
68
  # if the return value of `Time.now` is stubbed out, Capybara will raise `Capybara::FrozenInTime`.
69
69
  #
70
- # @param [Integer] seconds Number of seconds to retry this block
71
- # @param options [Hash]
72
- # @option options [Array<Exception>] :errors (driver.invalid_element_errors +
70
+ # @param [Integer] seconds (current sessions default_max_wait_time) Maximum number of seconds to retry this block
71
+ # @param [Array<Exception>] errors (driver.invalid_element_errors +
73
72
  # [Capybara::ElementNotFound]) exception types that cause the block to be rerun
74
73
  # @return [Object] The result of the given block
75
74
  # @raise [Capybara::FrozenInTime] If the return value of `Time.now` appears stuck
76
75
  #
77
- def synchronize(seconds=session_options.default_max_wait_time, options = {})
78
- start_time = Capybara::Helpers.monotonic_time
76
+ def synchronize(seconds = nil, errors: nil)
77
+ return yield if session.synchronized
79
78
 
80
- if session.synchronized
79
+ seconds = session_options.default_max_wait_time if [nil, true].include? seconds
80
+ session.synchronized = true
81
+ timer = Capybara::Helpers.timer(expire_in: seconds)
82
+ begin
81
83
  yield
82
- else
83
- session.synchronized = true
84
- begin
85
- yield
86
- rescue => e
87
- session.raise_server_error!
88
- raise e unless driver.wait?
89
- raise e unless catch_error?(e, options[:errors])
90
- raise e if (Capybara::Helpers.monotonic_time - start_time) >= seconds
91
- sleep(0.05)
92
- raise Capybara::FrozenInTime, "time appears to be frozen, Capybara does not work with libraries which freeze time, consider using time travelling instead" if Capybara::Helpers.monotonic_time == start_time
84
+ rescue StandardError => e
85
+ session.raise_server_error!
86
+ raise e unless catch_error?(e, errors)
87
+
88
+ if driver.wait?
89
+ raise e if timer.expired?
90
+
91
+ sleep(0.01)
92
+ reload if session_options.automatic_reload
93
+ else
94
+ old_base = @base
93
95
  reload if session_options.automatic_reload
94
- retry
95
- ensure
96
- session.synchronized = false
96
+ raise e if old_base == @base
97
97
  end
98
+ retry
99
+ ensure
100
+ session.synchronized = false
98
101
  end
99
102
  end
100
103
 
101
104
  # @api private
102
- def find_css(css)
103
- base.find_css(css)
105
+ def find_css(css, **options)
106
+ if base.method(:find_css).arity == 1
107
+ base.find_css(css)
108
+ else
109
+ base.find_css(css, **options)
110
+ end
104
111
  end
105
112
 
106
113
  # @api private
107
- def find_xpath(xpath)
108
- base.find_xpath(xpath)
109
- end
110
-
111
- # @deprecated Use query_scope instead
112
- def parent
113
- warn "DEPRECATED: #parent is deprecated in favor of #query_scope - Note: #parent was not the elements parent in the document so it's most likely not what you wanted anyway"
114
- query_scope
114
+ def find_xpath(xpath, **options)
115
+ if base.method(:find_xpath).arity == 1
116
+ base.find_xpath(xpath)
117
+ else
118
+ base.find_xpath(xpath, **options)
119
+ end
115
120
  end
116
121
 
117
122
  # @api private
@@ -119,13 +124,15 @@ module Capybara
119
124
  session.config
120
125
  end
121
126
 
127
+ def to_capybara_node
128
+ self
129
+ end
130
+
122
131
  protected
123
132
 
124
133
  def catch_error?(error, errors = nil)
125
134
  errors ||= (driver.invalid_element_errors + [Capybara::ElementNotFound])
126
- errors.any? do |type|
127
- error.is_a?(type)
128
- end
135
+ errors.any? { |type| error.is_a?(type) }
129
136
  end
130
137
 
131
138
  def driver
@@ -1,7 +1,7 @@
1
1
  # frozen_string_literal: true
2
+
2
3
  module Capybara
3
4
  module Node
4
-
5
5
  ##
6
6
  #
7
7
  # A {Capybara::Document} represents an HTML document. Any operation
@@ -20,8 +20,8 @@ module Capybara
20
20
  #
21
21
  # @return [String] The text of the document
22
22
  #
23
- def text(type=nil)
24
- find(:xpath, '/html').text(type)
23
+ def text(type = nil, normalize_ws: false)
24
+ find(:xpath, '/html').text(type, normalize_ws: normalize_ws)
25
25
  end
26
26
 
27
27
  ##
@@ -31,6 +31,18 @@ module Capybara
31
31
  def title
32
32
  session.driver.title
33
33
  end
34
+
35
+ def execute_script(*args)
36
+ find(:xpath, '/html').execute_script(*args)
37
+ end
38
+
39
+ def evaluate_script(*args)
40
+ find(:xpath, '/html').evaluate_script(*args)
41
+ end
42
+
43
+ def scroll_to(*args, **options)
44
+ find(:xpath, '//body').scroll_to(*args, **options)
45
+ end
34
46
  end
35
47
  end
36
48
  end
@@ -1,4 +1,5 @@
1
1
  # frozen_string_literal: true
2
+
2
3
  module Capybara
3
4
  module Node
4
5
  module DocumentMatchers
@@ -6,17 +7,19 @@ module Capybara
6
7
  # Asserts that the page has the given title.
7
8
  #
8
9
  # @!macro title_query_params
9
- # @overload $0(string, options = {})
10
+ # @overload $0(string, **options)
10
11
  # @param string [String] The string that title should include
11
- # @overload $0(regexp, options = {})
12
+ # @overload $0(regexp, **options)
12
13
  # @param regexp [Regexp] The regexp that title should match to
13
14
  # @option options [Numeric] :wait (Capybara.default_max_wait_time) Maximum time that Capybara will wait for title to eq/match given string/regexp argument
14
15
  # @option options [Boolean] :exact (false) When passed a string should the match be exact or just substring
15
16
  # @raise [Capybara::ExpectationNotMet] if the assertion hasn't succeeded during wait time
16
17
  # @return [true]
17
18
  #
18
- def assert_title(title, options = {})
19
- _verify_title(title,options) { |query| raise Capybara::ExpectationNotMet, query.failure_message unless query.resolves_for?(self) }
19
+ def assert_title(title, **options)
20
+ _verify_title(title, options) do |query|
21
+ raise Capybara::ExpectationNotMet, query.failure_message unless query.resolves_for?(self)
22
+ end
20
23
  end
21
24
 
22
25
  ##
@@ -26,8 +29,10 @@ module Capybara
26
29
  # @raise [Capybara::ExpectationNotMet] if the assertion hasn't succeeded during wait time
27
30
  # @return [true]
28
31
  #
29
- def assert_no_title(title, options = {})
30
- _verify_title(title,options) { |query| raise Capybara::ExpectationNotMet, query.negative_failure_message if query.resolves_for?(self) }
32
+ def assert_no_title(title, **options)
33
+ _verify_title(title, options) do |query|
34
+ raise Capybara::ExpectationNotMet, query.negative_failure_message if query.resolves_for?(self)
35
+ end
31
36
  end
32
37
 
33
38
  ##
@@ -36,10 +41,8 @@ module Capybara
36
41
  # @macro title_query_params
37
42
  # @return [Boolean]
38
43
  #
39
- def has_title?(title, options = {})
40
- assert_title(title, options)
41
- rescue Capybara::ExpectationNotMet
42
- return false
44
+ def has_title?(title, **options)
45
+ make_predicate(options) { assert_title(title, **options) }
43
46
  end
44
47
 
45
48
  ##
@@ -48,22 +51,17 @@ module Capybara
48
51
  # @macro title_query_params
49
52
  # @return [Boolean]
50
53
  #
51
- def has_no_title?(title, options = {})
52
- assert_no_title(title, options)
53
- rescue Capybara::ExpectationNotMet
54
- return false
54
+ def has_no_title?(title, **options)
55
+ make_predicate(options) { assert_no_title(title, **options) }
55
56
  end
56
57
 
57
- private
58
+ private
58
59
 
59
60
  def _verify_title(title, options)
60
- query = Capybara::Queries::TitleQuery.new(title, options)
61
- synchronize(query.wait) do
62
- yield(query)
63
- end
64
- return true
61
+ query = Capybara::Queries::TitleQuery.new(title, **options)
62
+ synchronize(query.wait) { yield(query) }
63
+ true
65
64
  end
66
-
67
65
  end
68
66
  end
69
67
  end
@@ -1,7 +1,7 @@
1
1
  # frozen_string_literal: true
2
+
2
3
  module Capybara
3
4
  module Node
4
-
5
5
  ##
6
6
  #
7
7
  # A {Capybara::Node::Element} represents a single element on the page. It is possible
@@ -22,15 +22,16 @@ module Capybara
22
22
  # @see Capybara::Node
23
23
  #
24
24
  class Element < Base
25
-
26
25
  def initialize(session, base, query_scope, query)
27
26
  super(session, base)
28
27
  @query_scope = query_scope
29
28
  @query = query
30
29
  @allow_reload = false
30
+ @query_idx = nil
31
31
  end
32
32
 
33
- def allow_reload!
33
+ def allow_reload!(idx = nil)
34
+ @query_idx = idx
34
35
  @allow_reload = true
35
36
  end
36
37
 
@@ -44,30 +45,25 @@ module Capybara
44
45
 
45
46
  ##
46
47
  #
47
- # Retrieve the text of the element. If `Capybara.ignore_hidden_elements`
48
+ # Retrieve the text of the element. If {Capybara.configure ignore_hidden_elements}
48
49
  # is `true`, which it is by default, then this will return only text
49
50
  # which is visible. The exact semantics of this may differ between
50
51
  # drivers, but generally any text within elements with `display:none` is
51
52
  # ignored. This behaviour can be overridden by passing `:all` to this
52
53
  # method.
53
54
  #
54
- # @param [:all, :visible] type Whether to return only visible or all text
55
+ # @param type [:all, :visible] Whether to return only visible or all text
55
56
  # @return [String] The text of the element
56
57
  #
57
- def text(type=nil)
58
- type ||= :all unless session_options.ignore_hidden_elements or session_options.visible_text_only
59
- synchronize do
60
- if type == :all
61
- base.all_text
62
- else
63
- base.visible_text
64
- end
65
- end
58
+ def text(type = nil, normalize_ws: false)
59
+ type ||= :all unless session_options.ignore_hidden_elements || session_options.visible_text_only
60
+ txt = synchronize { type == :all ? base.all_text : base.visible_text }
61
+ normalize_ws ? txt.gsub(/[[:space:]]+/, ' ').strip : txt
66
62
  end
67
63
 
68
64
  ##
69
65
  #
70
- # Retrieve the given attribute
66
+ # Retrieve the given attribute.
71
67
  #
72
68
  # element[:title] # => HTML title attribute
73
69
  #
@@ -78,6 +74,30 @@ module Capybara
78
74
  synchronize { base[attribute] }
79
75
  end
80
76
 
77
+ ##
78
+ #
79
+ # Retrieve the given CSS styles.
80
+ #
81
+ # element.style('color', 'font-size') # => Computed values of CSS 'color' and 'font-size' styles
82
+ #
83
+ # @param [Array<String>] styles Names of the desired CSS properties
84
+ # @return [Hash] Hash of the CSS property names to computed values
85
+ #
86
+ def style(*styles)
87
+ styles = styles.flatten.map(&:to_s)
88
+ raise ArgumentError, 'You must specify at least one CSS style' if styles.empty?
89
+
90
+ begin
91
+ synchronize { base.style(styles) }
92
+ rescue NotImplementedError => e
93
+ begin
94
+ evaluate_script(STYLE_SCRIPT, *styles)
95
+ rescue Capybara::NotSupportedByDriverError
96
+ raise e
97
+ end
98
+ end
99
+ end
100
+
81
101
  ##
82
102
  #
83
103
  # @return [String] The value of the form element
@@ -91,163 +111,179 @@ module Capybara
91
111
  # Set the value of the form element to the given value.
92
112
  #
93
113
  # @param [String] value The new value
94
- # @param [Hash{}] options Driver specific options for how to set the value
114
+ # @param [Hash] options Driver specific options for how to set the value. Take default values from {Capybara.configure default_set_options}.
95
115
  #
96
116
  # @return [Capybara::Node::Element] The element
97
- def set(value, options={})
98
- options ||= {}
99
-
100
- driver_supports_options = (base.method(:set).arity != 1)
101
-
102
- unless options.empty? || driver_supports_options
103
- warn "Options passed to Capybara::Node#set but the driver doesn't support them"
117
+ def set(value, **options)
118
+ if ENV['CAPYBARA_THOROUGH'] && readonly?
119
+ raise Capybara::ReadOnlyElementError, "Attempt to set readonly element with value: #{value}"
104
120
  end
105
121
 
106
- synchronize do
107
- if driver_supports_options
108
- base.set(value, options)
109
- else
110
- base.set(value)
111
- end
112
- end
113
- return self
122
+ options = session_options.default_set_options.to_h.merge(options)
123
+ synchronize { base.set(value, **options) }
124
+ self
114
125
  end
115
126
 
116
127
  ##
117
128
  #
118
- # Select this node if is an option element inside a select tag
129
+ # Select this node if it is an option element inside a select tag.
130
+ #
131
+ # @!macro action_waiting_behavior
132
+ # If the driver dynamic pages (JS) and the element is currently non-interactable, this method will
133
+ # continuously retry the action until either the element becomes interactable or the maximum
134
+ # wait time expires.
119
135
  #
136
+ # @param [false, Numeric] wait
137
+ # Maximum time to wait for the action to succeed. Defaults to {Capybara.configure default_max_wait_time}.
120
138
  # @return [Capybara::Node::Element] The element
121
- def select_option
122
- warn "Attempt to select disabled option: #{value || text}" if disabled?
123
- synchronize { base.select_option }
124
- return self
139
+ def select_option(wait: nil)
140
+ synchronize(wait) { base.select_option }
141
+ self
125
142
  end
126
143
 
127
144
  ##
128
145
  #
129
- # Unselect this node if is an option element inside a multiple select tag
146
+ # Unselect this node if it is an option element inside a multiple select tag.
130
147
  #
148
+ # @macro action_waiting_behavior
131
149
  # @return [Capybara::Node::Element] The element
132
- def unselect_option
133
- synchronize { base.unselect_option }
134
- return self
150
+ def unselect_option(wait: nil)
151
+ synchronize(wait) { base.unselect_option }
152
+ self
135
153
  end
136
154
 
137
155
  ##
138
156
  #
139
- # Click the Element
140
- #
157
+ # Click the Element.
158
+ #
159
+ # @macro action_waiting_behavior
160
+ # @!macro click_modifiers
161
+ # Both x: and y: must be specified if an offset is wanted, if not specified the click will occur at the middle of the element.
162
+ # @overload $0(*modifier_keys, wait: nil, **offset)
163
+ # @param *modifier_keys [:alt, :control, :meta, :shift] ([]) Keys to be held down when clicking
164
+ # @option options [Integer] x X coordinate to offset the click location. If {Capybara.configure w3c_click_offset} is `true` the
165
+ # offset will be from the element center, otherwise it will be from the top left corner of the element
166
+ # @option options [Integer] y Y coordinate to offset the click location. If {Capybara.configure w3c_click_offset} is `true` the
167
+ # offset will be from the element center, otherwise it will be from the top left corner of the element
168
+ # @option options [Float] delay Delay between the mouse down and mouse up events in seconds (0)
141
169
  # @return [Capybara::Node::Element] The element
142
- def click
143
- synchronize { base.click }
144
- return self
170
+ def click(*keys, **options)
171
+ perform_click_action(keys, **options) do |k, opts|
172
+ base.click(k, **opts)
173
+ end
145
174
  end
146
175
 
147
176
  ##
148
177
  #
149
- # Right Click the Element
178
+ # Right Click the Element.
150
179
  #
180
+ # @macro action_waiting_behavior
181
+ # @macro click_modifiers
182
+ # @option options [Float] delay Delay between the mouse down and mouse up events in seconds (0)
151
183
  # @return [Capybara::Node::Element] The element
152
- def right_click
153
- synchronize { base.right_click }
154
- return self
184
+ def right_click(*keys, **options)
185
+ perform_click_action(keys, **options) do |k, opts|
186
+ base.right_click(k, **opts)
187
+ end
155
188
  end
156
189
 
157
190
  ##
158
191
  #
159
- # Double Click the Element
192
+ # Double Click the Element.
160
193
  #
194
+ # @macro action_waiting_behavior
195
+ # @macro click_modifiers
161
196
  # @return [Capybara::Node::Element] The element
162
- def double_click
163
- synchronize { base.double_click }
164
- return self
197
+ def double_click(*keys, **options)
198
+ perform_click_action(keys, **options) do |k, opts|
199
+ base.double_click(k, **opts)
200
+ end
165
201
  end
166
202
 
167
203
  ##
168
204
  #
169
- # Send Keystrokes to the Element
205
+ # Send Keystrokes to the Element.
170
206
  #
171
207
  # @overload send_keys(keys, ...)
172
- # @param [String, Symbol, Array<String,Symbol>] keys
208
+ # @param keys [String, Symbol, Array<String,Symbol>]
173
209
  #
174
210
  # Examples:
175
211
  #
176
212
  # element.send_keys "foo" #=> value: 'foo'
177
- # element.send_keys "tet", :left, "s" #=> value: 'test'
213
+ # element.send_keys "tet", :left, "s" #=> value: 'test'
178
214
  # element.send_keys [:control, 'a'], :space #=> value: ' ' - assuming ctrl-a selects all contents
179
215
  #
180
- # Symbols supported for keys
181
- # :cancel
182
- # :help
183
- # :backspace
184
- # :tab
185
- # :clear
186
- # :return
187
- # :enter
188
- # :shift
189
- # :control
190
- # :alt
191
- # :pause
192
- # :escape
193
- # :space
194
- # :page_up
195
- # :page_down
196
- # :end
197
- # :home
198
- # :left
199
- # :up
200
- # :right
201
- # :down
202
- # :insert
203
- # :delete
204
- # :semicolon
205
- # :equals
206
- # :numpad0
207
- # :numpad1
208
- # :numpad2
209
- # :numpad3
210
- # :numpad4
211
- # :numpad5
212
- # :numpad6
213
- # :numpad7
214
- # :numpad8
215
- # :numpad9
216
- # :multiply - numeric keypad *
217
- # :add - numeric keypad +
218
- # :separator - numeric keypad 'separator' key ??
219
- # :subtract - numeric keypad -
220
- # :decimal - numeric keypad .
221
- # :divide - numeric keypad /
222
- # :f1
223
- # :f2
224
- # :f3
225
- # :f4
226
- # :f5
227
- # :f6
228
- # :f7
229
- # :f8
230
- # :f9
231
- # :f10
232
- # :f11
233
- # :f12
234
- # :meta
235
- # :command - alias of :meta
216
+ # Symbols supported for keys:
217
+ # * :cancel
218
+ # * :help
219
+ # * :backspace
220
+ # * :tab
221
+ # * :clear
222
+ # * :return
223
+ # * :enter
224
+ # * :shift
225
+ # * :control
226
+ # * :alt
227
+ # * :pause
228
+ # * :escape
229
+ # * :space
230
+ # * :page_up
231
+ # * :page_down
232
+ # * :end
233
+ # * :home
234
+ # * :left
235
+ # * :up
236
+ # * :right
237
+ # * :down
238
+ # * :insert
239
+ # * :delete
240
+ # * :semicolon
241
+ # * :equals
242
+ # * :numpad0
243
+ # * :numpad1
244
+ # * :numpad2
245
+ # * :numpad3
246
+ # * :numpad4
247
+ # * :numpad5
248
+ # * :numpad6
249
+ # * :numpad7
250
+ # * :numpad8
251
+ # * :numpad9
252
+ # * :multiply - numeric keypad *
253
+ # * :add - numeric keypad +
254
+ # * :separator - numeric keypad 'separator' key ??
255
+ # * :subtract - numeric keypad -
256
+ # * :decimal - numeric keypad .
257
+ # * :divide - numeric keypad /
258
+ # * :f1
259
+ # * :f2
260
+ # * :f3
261
+ # * :f4
262
+ # * :f5
263
+ # * :f6
264
+ # * :f7
265
+ # * :f8
266
+ # * :f9
267
+ # * :f10
268
+ # * :f11
269
+ # * :f12
270
+ # * :meta
271
+ # * :command - alias of :meta
236
272
  #
237
273
  # @return [Capybara::Node::Element] The element
238
274
  def send_keys(*args)
239
275
  synchronize { base.send_keys(*args) }
240
- return self
276
+ self
241
277
  end
242
278
 
243
279
  ##
244
280
  #
245
- # Hover on the Element
281
+ # Hover on the Element.
246
282
  #
247
283
  # @return [Capybara::Node::Element] The element
248
284
  def hover
249
285
  synchronize { base.hover }
250
- return self
286
+ self
251
287
  end
252
288
 
253
289
  ##
@@ -255,7 +291,8 @@ module Capybara
255
291
  # @return [String] The tag name of the element
256
292
  #
257
293
  def tag_name
258
- synchronize { base.tag_name }
294
+ # Element type is immutable so cache it
295
+ @tag_name ||= initial_cache[:tag_name] || synchronize { base.tag_name }
259
296
  end
260
297
 
261
298
  ##
@@ -269,6 +306,17 @@ module Capybara
269
306
  synchronize { base.visible? }
270
307
  end
271
308
 
309
+ ##
310
+ #
311
+ # Whether or not the element is currently in the viewport and it (or descendants)
312
+ # would be considered clickable at the elements center point.
313
+ #
314
+ # @return [Boolean] Whether the elements center is obscured.
315
+ #
316
+ def obscured?
317
+ synchronize { base.obscured? }
318
+ end
319
+
272
320
  ##
273
321
  #
274
322
  # Whether or not the element is checked.
@@ -321,7 +369,7 @@ module Capybara
321
369
 
322
370
  ##
323
371
  #
324
- # An XPath expression describing where on the page the element can be found
372
+ # An XPath expression describing where on the page the element can be found.
325
373
  #
326
374
  # @return [String] An XPath expression
327
375
  #
@@ -329,17 +377,23 @@ module Capybara
329
377
  synchronize { base.path }
330
378
  end
331
379
 
380
+ def rect
381
+ synchronize { base.rect }
382
+ end
383
+
332
384
  ##
333
385
  #
334
386
  # Trigger any event on the current element, for example mouseover or focus
335
- # events. Does not work in Selenium.
387
+ # events. Not supported with the Selenium driver, and SHOULDN'T BE USED IN TESTING unless you
388
+ # fully understand why you're using it, that it can allow actions a user could never
389
+ # perform, and that it may completely invalidate your test.
336
390
  #
337
391
  # @param [String] event The name of the event to trigger
338
392
  #
339
393
  # @return [Capybara::Node::Element] The element
340
394
  def trigger(event)
341
395
  synchronize { base.trigger(event) }
342
- return self
396
+ self
343
397
  end
344
398
 
345
399
  ##
@@ -351,35 +405,197 @@ module Capybara
351
405
  # source.drag_to(target)
352
406
  #
353
407
  # @param [Capybara::Node::Element] node The element to drag to
408
+ # @param [Hash] options Driver specific options for dragging. May not be supported by all drivers.
409
+ # @option options [Numeric] :delay (0.05) When using Chrome/Firefox with Selenium and HTML5 dragging this is the number
410
+ # of seconds between each stage of the drag.
411
+ # @option options [Boolean] :html5 When using Chrome/Firefox with Selenium enables to force the use of HTML5
412
+ # (true) or legacy (false) dragging. If not specified the driver will attempt to
413
+ # detect the correct method to use.
414
+ # @option options [Array<Symbol>,Symbol] :drop_modifiers Modifier keys which should be held while the dragged element is dropped.
415
+ #
416
+ #
417
+ # @return [Capybara::Node::Element] The dragged element
418
+ def drag_to(node, **options)
419
+ synchronize { base.drag_to(node.base, **options) }
420
+ self
421
+ end
422
+
423
+ ##
424
+ #
425
+ # Drop items on the current element.
426
+ #
427
+ # target = page.find('#foo')
428
+ # target.drop('/some/path/file.csv')
429
+ #
430
+ # @overload drop(path, ...)
431
+ # @param [String, #to_path] path Location of the file to drop on the element
432
+ #
433
+ # @overload drop(strings, ...)
434
+ # @param [Hash] strings A hash of type to data to be dropped - `{ "text/url" => "https://www.google.com" }`
435
+ #
436
+ # @return [Capybara::Node::Element] The element
437
+ def drop(*args)
438
+ options = args.map { |arg| arg.respond_to?(:to_path) ? arg.to_path : arg }
439
+ synchronize { base.drop(*options) }
440
+ self
441
+ end
442
+
443
+ ##
444
+ #
445
+ # Scroll the page or element.
446
+ #
447
+ # @overload scroll_to(position, offset: [0,0])
448
+ # Scroll the page or element to its top, bottom or middle.
449
+ # @param [:top, :bottom, :center, :current] position
450
+ # @param [[Integer, Integer]] offset
451
+ #
452
+ # @overload scroll_to(element, align: :top)
453
+ # Scroll the page or current element until the given element is aligned at the top, bottom, or center of it.
454
+ # @param [Capybara::Node::Element] element The element to be scrolled into view
455
+ # @param [:top, :bottom, :center] align Where to align the element being scrolled into view with relation to the current page/element if possible
456
+ #
457
+ # @overload scroll_to(x,y)
458
+ # @param [Integer] x Horizontal scroll offset
459
+ # @param [Integer] y Vertical scroll offset
354
460
  #
355
461
  # @return [Capybara::Node::Element] The element
356
- def drag_to(node)
357
- synchronize { base.drag_to(node.base) }
358
- return self
462
+ def scroll_to(pos_or_el_or_x, y = nil, align: :top, offset: nil)
463
+ case pos_or_el_or_x
464
+ when Symbol
465
+ synchronize { base.scroll_to(nil, pos_or_el_or_x) } unless pos_or_el_or_x == :current
466
+ when Capybara::Node::Element
467
+ synchronize { base.scroll_to(pos_or_el_or_x.base, align) }
468
+ else
469
+ synchronize { base.scroll_to(nil, nil, [pos_or_el_or_x, y]) }
470
+ end
471
+ synchronize { base.scroll_by(*offset) } unless offset.nil?
472
+ self
473
+ end
474
+
475
+ ##
476
+ #
477
+ # Execute the given JS in the context of the element not returning a result. This is useful for scripts that return
478
+ # complex objects, such as jQuery statements. {#execute_script} should be used over
479
+ # {#evaluate_script} whenever a result is not expected or needed. `this` in the script will refer to the element this is called on.
480
+ #
481
+ # @param [String] script A string of JavaScript to execute
482
+ # @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
483
+ #
484
+ def execute_script(script, *args)
485
+ session.execute_script(<<~JS, self, *args)
486
+ (function (){
487
+ #{script}
488
+ }).apply(arguments[0], Array.prototype.slice.call(arguments,1));
489
+ JS
359
490
  end
360
491
 
492
+ ##
493
+ #
494
+ # Evaluate the given JS in the context of the element and return the result. Be careful when using this with
495
+ # scripts that return complex objects, such as jQuery statements. {#execute_script} might
496
+ # be a better alternative. `this` in the script will refer to the element this is called on.
497
+ #
498
+ # @param [String] script A string of JavaScript to evaluate
499
+ # @return [Object] The result of the evaluated JavaScript (may be driver specific)
500
+ #
501
+ def evaluate_script(script, *args)
502
+ session.evaluate_script(<<~JS, self, *args)
503
+ (function(){
504
+ return #{script.strip}
505
+ }).apply(arguments[0], Array.prototype.slice.call(arguments,1));
506
+ JS
507
+ end
508
+
509
+ ##
510
+ #
511
+ # Evaluate the given JavaScript in the context of the element and obtain the result from a
512
+ # callback function which will be passed as the last argument to the script. `this` in the
513
+ # script will refer to the element this is called on.
514
+ #
515
+ # @param [String] script A string of JavaScript to evaluate
516
+ # @return [Object] The result of the evaluated JavaScript (may be driver specific)
517
+ #
518
+ def evaluate_async_script(script, *args)
519
+ session.evaluate_async_script(<<~JS, self, *args)
520
+ (function (){
521
+ #{script}
522
+ }).apply(arguments[0], Array.prototype.slice.call(arguments,1));
523
+ JS
524
+ end
525
+
526
+ ##
527
+ #
528
+ # Toggle the elements background color between white and black for a period of time.
529
+ #
530
+ # @return [Capybara::Node::Element] The element
531
+ def flash
532
+ execute_script(<<~JS, 100)
533
+ async function flash(el, delay){
534
+ var old_bg = el.style.backgroundColor;
535
+ var colors = ["black", "white"];
536
+ for(var i=0; i<20; i++){
537
+ el.style.backgroundColor = colors[i % colors.length];
538
+ await new Promise(resolve => setTimeout(resolve, delay));
539
+ }
540
+ el.style.backgroundColor = old_bg;
541
+ }
542
+ flash(this, arguments[0]);
543
+ JS
544
+
545
+ self
546
+ end
547
+
548
+ # @api private
361
549
  def reload
362
- if @allow_reload
363
- begin
364
- reloaded = query_scope.reload.first(@query.name, @query.locator, @query.options)
365
- @base = reloaded.base if reloaded
366
- rescue => e
367
- raise e unless catch_error?(e)
368
- end
550
+ return self unless @allow_reload
551
+
552
+ begin
553
+ reloaded = @query.resolve_for(query_scope.reload)[@query_idx.to_i]
554
+ @base = reloaded.base if reloaded
555
+ rescue StandardError => e
556
+ raise e unless catch_error?(e)
369
557
  end
370
558
  self
371
559
  end
372
560
 
561
+ ##
562
+ #
563
+ # A human-readable representation of the element.
564
+ #
565
+ # @return [String] A string representation
373
566
  def inspect
374
- %(#<Capybara::Node::Element tag="#{tag_name}" path="#{path}">)
567
+ %(#<Capybara::Node::Element tag="#{base.tag_name}" path="#{base.path}">)
375
568
  rescue NotSupportedByDriverError
376
- %(#<Capybara::Node::Element tag="#{tag_name}">)
377
- rescue => e
378
- if session.driver.invalid_element_errors.any? { |et| e.is_a?(et)}
379
- %(Obsolete #<Capybara::Node::Element>)
380
- else
381
- raise
382
- end
569
+ %(#<Capybara::Node::Element tag="#{base.tag_name}">)
570
+ rescue *session.driver.invalid_element_errors
571
+ %(Obsolete #<Capybara::Node::Element>)
572
+ end
573
+
574
+ # @api private
575
+ def initial_cache
576
+ base.respond_to?(:initial_cache) ? base.initial_cache : {}
577
+ end
578
+
579
+ STYLE_SCRIPT = <<~JS
580
+ (function(){
581
+ var s = window.getComputedStyle(this);
582
+ var result = {};
583
+ for (var i = arguments.length; i--; ) {
584
+ var property_name = arguments[i];
585
+ result[property_name] = s.getPropertyValue(property_name);
586
+ }
587
+ return result;
588
+ }).apply(this, arguments)
589
+ JS
590
+
591
+ private
592
+
593
+ def perform_click_action(keys, wait: nil, **options)
594
+ raise ArgumentError, 'You must specify both x: and y: for a click offset' if nil ^ options[:x] ^ options[:y]
595
+
596
+ options[:offset] ||= :center if session_options.w3c_click_offset
597
+ synchronize(wait) { yield keys, options }
598
+ self
383
599
  end
384
600
  end
385
601
  end