capybara 2.18.0 → 3.38.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (315) hide show
  1. checksums.yaml +4 -4
  2. data/.yardopts +1 -1
  3. data/History.md +889 -12
  4. data/License.txt +1 -1
  5. data/README.md +108 -78
  6. data/lib/capybara/config.rb +29 -57
  7. data/lib/capybara/cucumber.rb +2 -3
  8. data/lib/capybara/driver/base.rb +35 -18
  9. data/lib/capybara/driver/node.rb +40 -10
  10. data/lib/capybara/dsl.rb +10 -7
  11. data/lib/capybara/helpers.rb +70 -31
  12. data/lib/capybara/minitest/spec.rb +173 -83
  13. data/lib/capybara/minitest.rb +219 -112
  14. data/lib/capybara/node/actions.rb +274 -171
  15. data/lib/capybara/node/base.rb +42 -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 +362 -135
  19. data/lib/capybara/node/finders.rb +149 -137
  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 +12 -9
  24. data/lib/capybara/queries/base_query.rb +39 -28
  25. data/lib/capybara/queries/current_path_query.rb +21 -27
  26. data/lib/capybara/queries/match_query.rb +14 -7
  27. data/lib/capybara/queries/selector_query.rb +658 -149
  28. data/lib/capybara/queries/sibling_query.rb +11 -9
  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 +113 -42
  33. data/lib/capybara/rack_test/css_handlers.rb +6 -4
  34. data/lib/capybara/rack_test/driver.rb +22 -17
  35. data/lib/capybara/rack_test/errors.rb +6 -0
  36. data/lib/capybara/rack_test/form.rb +93 -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 +41 -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 +53 -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 +142 -311
  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 +280 -0
  86. data/lib/capybara/selector/filter.rb +2 -17
  87. data/lib/capybara/selector/filter_set.rb +80 -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 +211 -0
  93. data/lib/capybara/selector/selector.rb +93 -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 +332 -261
  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 +226 -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 +44 -0
  112. data/lib/capybara/selenium/node.rb +545 -144
  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 +81 -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 +81 -67
  129. data/lib/capybara/session/matchers.rb +28 -20
  130. data/lib/capybara/session.rb +336 -365
  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 +147 -12
  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 +10 -10
  137. data/lib/capybara/spec/session/active_element_spec.rb +31 -0
  138. data/lib/capybara/spec/session/all_spec.rb +160 -56
  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_spec.rb +258 -0
  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 +144 -69
  147. data/lib/capybara/spec/session/body_spec.rb +12 -13
  148. data/lib/capybara/spec/session/check_spec.rb +117 -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 +12 -11
  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 +8 -7
  162. data/lib/capybara/spec/session/evaluate_script_spec.rb +29 -8
  163. data/lib/capybara/spec/session/execute_script_spec.rb +10 -8
  164. data/lib/capybara/spec/session/fill_in_spec.rb +134 -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 +251 -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 +31 -31
  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 +100 -13
  181. data/lib/capybara/spec/session/has_css_spec.rb +272 -137
  182. data/lib/capybara/spec/session/has_current_path_spec.rb +59 -60
  183. data/lib/capybara/spec/session/has_field_spec.rb +139 -59
  184. data/lib/capybara/spec/session/has_link_spec.rb +43 -6
  185. data/lib/capybara/spec/session/has_none_selectors_spec.rb +42 -40
  186. data/lib/capybara/spec/session/has_select_spec.rb +107 -72
  187. data/lib/capybara/spec/session/has_selector_spec.rb +120 -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 +106 -62
  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 +1002 -153
  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 +82 -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 -81
  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 +85 -53
  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 +83 -44
  224. data/lib/capybara/spec/spec_helper.rb +53 -43
  225. data/lib/capybara/spec/test_app.rb +158 -43
  226. data/lib/capybara/spec/views/animated.erb +49 -0
  227. data/lib/capybara/spec/views/form.erb +154 -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 +2 -1
  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 +2 -1
  247. data/lib/capybara/spec/views/with_hover.erb +3 -2
  248. data/lib/capybara/spec/views/with_hover1.erb +10 -0
  249. data/lib/capybara/spec/views/with_html.erb +67 -12
  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 +30 -5
  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.erb +2 -2
  256. data/lib/capybara/spec/views/with_scope_other.erb +6 -0
  257. data/lib/capybara/spec/views/with_shadow.erb +31 -0
  258. data/lib/capybara/spec/views/with_slow_unload.erb +2 -1
  259. data/lib/capybara/spec/views/with_sortable_js.erb +21 -0
  260. data/lib/capybara/spec/views/with_unload_alert.erb +1 -0
  261. data/lib/capybara/spec/views/with_windows.erb +1 -1
  262. data/lib/capybara/spec/views/within_frames.erb +5 -2
  263. data/lib/capybara/version.rb +2 -1
  264. data/lib/capybara/window.rb +36 -34
  265. data/lib/capybara.rb +129 -103
  266. data/spec/basic_node_spec.rb +60 -34
  267. data/spec/capybara_spec.rb +63 -88
  268. data/spec/counter_spec.rb +35 -0
  269. data/spec/css_builder_spec.rb +101 -0
  270. data/spec/css_splitter_spec.rb +38 -0
  271. data/spec/dsl_spec.rb +85 -64
  272. data/spec/filter_set_spec.rb +27 -9
  273. data/spec/fixtures/certificate.pem +25 -0
  274. data/spec/fixtures/key.pem +27 -0
  275. data/spec/fixtures/selenium_driver_rspec_failure.rb +6 -5
  276. data/spec/fixtures/selenium_driver_rspec_success.rb +6 -5
  277. data/spec/minitest_spec.rb +45 -7
  278. data/spec/minitest_spec_spec.rb +94 -63
  279. data/spec/per_session_config_spec.rb +14 -13
  280. data/spec/rack_test_spec.rb +194 -125
  281. data/spec/regexp_dissassembler_spec.rb +250 -0
  282. data/spec/result_spec.rb +102 -50
  283. data/spec/rspec/features_spec.rb +37 -31
  284. data/spec/rspec/scenarios_spec.rb +9 -7
  285. data/spec/rspec/shared_spec_matchers.rb +449 -422
  286. data/spec/rspec/views_spec.rb +5 -3
  287. data/spec/rspec_matchers_spec.rb +27 -11
  288. data/spec/rspec_spec.rb +109 -89
  289. data/spec/sauce_spec_chrome.rb +43 -0
  290. data/spec/selector_spec.rb +397 -68
  291. data/spec/selenium_spec_chrome.rb +186 -40
  292. data/spec/selenium_spec_chrome_remote.rb +103 -0
  293. data/spec/selenium_spec_edge.rb +49 -0
  294. data/spec/selenium_spec_firefox.rb +194 -41
  295. data/spec/selenium_spec_firefox_remote.rb +82 -0
  296. data/spec/selenium_spec_ie.rb +149 -0
  297. data/spec/selenium_spec_safari.rb +162 -0
  298. data/spec/server_spec.rb +201 -102
  299. data/spec/session_spec.rb +53 -16
  300. data/spec/shared_selenium_node.rb +79 -0
  301. data/spec/shared_selenium_session.rb +474 -122
  302. data/spec/spec_helper.rb +93 -7
  303. data/spec/xpath_builder_spec.rb +93 -0
  304. metadata +360 -73
  305. data/.yard/templates_custom/default/class/html/selectors.erb +0 -38
  306. data/.yard/templates_custom/default/class/html/setup.rb +0 -17
  307. data/.yard/yard_extensions.rb +0 -78
  308. data/lib/capybara/query.rb +0 -7
  309. data/lib/capybara/rspec/compound.rb +0 -95
  310. data/lib/capybara/spec/session/assert_current_path.rb +0 -72
  311. data/lib/capybara/spec/session/assert_selector.rb +0 -148
  312. data/lib/capybara/spec/session/assert_text.rb +0 -234
  313. data/lib/capybara/spec/session/source_spec.rb +0 -0
  314. data/lib/capybara/spec/views/with_title.erb +0 -5
  315. data/spec/selenium_spec_marionette.rb +0 -127
@@ -1,14 +1,27 @@
1
1
  # frozen_string_literal: true
2
+
3
+ # Selenium specific implementation of the Capybara::Driver::Node API
4
+
5
+ require 'capybara/selenium/extensions/find'
6
+ require 'capybara/selenium/extensions/scroll'
7
+
2
8
  class Capybara::Selenium::Node < Capybara::Driver::Node
9
+ include Capybara::Selenium::Find
10
+ include Capybara::Selenium::Scroll
3
11
 
4
12
  def visible_text
5
- # Selenium doesn't normalize Unicode whitespace.
6
- Capybara::Helpers.normalize_whitespace(native.text)
13
+ raise NotImplementedError, 'Getting visible text is not currently supported directly on shadow roots' if shadow_root?
14
+
15
+ native.text
7
16
  end
8
17
 
9
18
  def all_text
10
- text = driver.execute_script("return arguments[0].textContent", self)
11
- Capybara::Helpers.normalize_whitespace(text)
19
+ text = driver.evaluate_script('arguments[0].textContent', self) || ''
20
+ text.gsub(/[\u200b\u200e\u200f]/, '')
21
+ .gsub(/[\ \n\f\t\v\u2028\u2029]+/, ' ')
22
+ .gsub(/\A[[:space:]&&[^\u00a0]]+/, '')
23
+ .gsub(/[[:space:]&&[^\u00a0]]+\z/, '')
24
+ .tr("\u00a0", ' ')
12
25
  end
13
26
 
14
27
  def [](name)
@@ -18,13 +31,17 @@ class Capybara::Selenium::Node < Capybara::Driver::Node
18
31
  end
19
32
 
20
33
  def value
21
- if tag_name == "select" and multiple?
22
- native.find_elements(:css, "option:checked").map { |n| n[:value] || n.text }
34
+ if tag_name == 'select' && multiple?
35
+ native.find_elements(:css, 'option:checked').map { |el| el[:value] || el.text }
23
36
  else
24
37
  native[:value]
25
38
  end
26
39
  end
27
40
 
41
+ def style(styles)
42
+ styles.to_h { |style| [style, native.css_value(style)] }
43
+ end
44
+
28
45
  ##
29
46
  #
30
47
  # Set the value of the form element to the given value.
@@ -36,91 +53,104 @@ class Capybara::Selenium::Node < Capybara::Driver::Node
36
53
  # :none => append the new value to the existing value <br/>
37
54
  # :backspace => send backspace keystrokes to clear the field <br/>
38
55
  # Array => an array of keys to send before the value being set, e.g. [[:command, 'a'], :backspace]
39
- def set(value, options={})
40
- tag_name = self.tag_name
41
- type = self[:type]
42
-
43
- if (Array === value) && !multiple?
44
- raise ArgumentError.new "Value cannot be an Array when 'multiple' attribute is not present. Not a #{value.class}"
56
+ # @option options [Boolean] :rapid (nil) Whether setting text inputs should use a faster &quot;rapid&quot; mode<br/>
57
+ # nil => Text inputs with length greater than 30 characters will be set using a faster driver script mode<br/>
58
+ # true => Rapid mode will be used regardless of input length<br/>
59
+ # false => Sends keys via conventional mode. This may be required to avoid losing key-presses if you have certain
60
+ # Javascript interactions on form inputs<br/>
61
+ def set(value, **options)
62
+ if value.is_a?(Array) && !multiple?
63
+ raise ArgumentError, "Value cannot be an Array when 'multiple' attribute is not present. Not a #{value.class}"
45
64
  end
46
65
 
66
+ tag_name, type = attrs(:tagName, :type).map { |val| val&.downcase }
67
+ @tag_name ||= tag_name
68
+
47
69
  case tag_name
48
70
  when 'input'
49
71
  case type
50
72
  when 'radio'
51
73
  click
52
74
  when 'checkbox'
53
- click if value ^ native.attribute('checked').to_s.eql?("true")
75
+ click if value ^ checked?
54
76
  when 'file'
55
- path_names = value.to_s.empty? ? [] : value
56
- if driver.chrome?
57
- native.send_keys(Array(path_names).join("\n"))
58
- else
59
- native.send_keys(*path_names)
60
- end
77
+ set_file(value)
78
+ when 'date'
79
+ set_date(value)
80
+ when 'time'
81
+ set_time(value)
82
+ when 'datetime-local'
83
+ set_datetime_local(value)
84
+ when 'color'
85
+ set_color(value)
86
+ when 'range'
87
+ set_range(value)
61
88
  else
62
- set_text(value, options)
89
+ set_text(value, **options)
63
90
  end
64
91
  when 'textarea'
65
- set_text(value, options)
92
+ set_text(value, **options)
66
93
  else
67
- if content_editable?
68
- #ensure we are focused on the element
69
- click
70
-
71
- script = <<-JS
72
- var range = document.createRange();
73
- var sel = window.getSelection();
74
- arguments[0].focus();
75
- range.selectNodeContents(arguments[0]);
76
- sel.removeAllRanges();
77
- sel.addRange(range);
78
- JS
79
- driver.execute_script script, self
80
-
81
- if driver.chrome? || driver.firefox?
82
- # chromedriver raises a can't focus element for child elements if we use native.send_keys
83
- # we've already focused it so just use action api
84
- driver.browser.action.send_keys(value.to_s).perform
85
- else
86
- # action api is really slow here just use native.send_keys
87
- native.send_keys(value.to_s)
88
- end
89
- end
94
+ set_content_editable(value)
90
95
  end
91
96
  end
92
97
 
93
98
  def select_option
94
- native.click unless selected? || disabled?
99
+ click unless selected? || disabled?
95
100
  end
96
101
 
97
102
  def unselect_option
98
- raise Capybara::UnselectNotAllowed, "Cannot unselect option from single select box." if !select_node.multiple?
99
- native.click if selected?
103
+ raise Capybara::UnselectNotAllowed, 'Cannot unselect option from single select box.' unless select_node.multiple?
104
+
105
+ click if selected?
100
106
  end
101
107
 
102
- def click
103
- native.click
104
- rescue => e
105
- if e.is_a?(::Selenium::WebDriver::Error::ElementClickInterceptedError) ||
106
- e.message =~ /Other element would receive the click/
107
- begin
108
- driver.execute_script("arguments[0].scrollIntoView({behavior: 'instant', block: 'center', inline: 'center'})", self)
109
- rescue
108
+ def click(keys = [], **options)
109
+ click_options = ClickOptions.new(keys, options)
110
+ return native.click if click_options.empty?
111
+
112
+ perform_with_options(click_options) do |action|
113
+ target = click_options.coords? ? nil : native
114
+ if click_options.delay.zero?
115
+ action.click(target)
116
+ else
117
+ action.click_and_hold(target)
118
+ action_pause(action, click_options.delay)
119
+ action.release
110
120
  end
111
121
  end
122
+ rescue StandardError => e
123
+ if e.is_a?(::Selenium::WebDriver::Error::ElementClickInterceptedError) ||
124
+ e.message.include?('Other element would receive the click')
125
+ scroll_to_center
126
+ end
127
+
112
128
  raise e
113
129
  end
114
130
 
115
- def right_click
116
- scroll_if_needed do
117
- driver.browser.action.context_click(native).perform
131
+ def right_click(keys = [], **options)
132
+ click_options = ClickOptions.new(keys, options)
133
+ perform_with_options(click_options) do |action|
134
+ target = click_options.coords? ? nil : native
135
+ if click_options.delay.zero?
136
+ action.context_click(target)
137
+ elsif w3c?
138
+ action.move_to(target) if target
139
+ action.pointer_down(:right).then do |act|
140
+ action_pause(act, click_options.delay)
141
+ end.pointer_up(:right)
142
+ else
143
+ raise ArgumentError, 'Delay is not supported when right clicking with legacy (non-w3c) selenium driver'
144
+ end
118
145
  end
119
146
  end
120
147
 
121
- def double_click
122
- scroll_if_needed do
123
- driver.browser.action.double_click(native).perform
148
+ def double_click(keys = [], **options)
149
+ click_options = ClickOptions.new(keys, options)
150
+ raise ArgumentError, "double_click doesn't support a delay option" unless click_options.delay.zero?
151
+
152
+ perform_with_options(click_options) do |action|
153
+ click_options.coords? ? action.double_click : action.double_click(native)
124
154
  end
125
155
  end
126
156
 
@@ -129,136 +159,507 @@ class Capybara::Selenium::Node < Capybara::Driver::Node
129
159
  end
130
160
 
131
161
  def hover
132
- scroll_if_needed do
133
- driver.browser.action.move_to(native).perform
134
- end
162
+ scroll_if_needed { browser_action.move_to(native).perform }
135
163
  end
136
164
 
137
- def drag_to(element)
138
- scroll_if_needed do
139
- driver.browser.action.drag_and_drop(native, element.native).perform
165
+ def drag_to(element, drop_modifiers: [], **)
166
+ drop_modifiers = Array(drop_modifiers)
167
+ # Due to W3C spec compliance - The Actions API no longer scrolls to elements when necessary
168
+ # which means Seleniums `drag_and_drop` is now broken - do it manually
169
+ scroll_if_needed { browser_action.click_and_hold(native).perform }
170
+ # element.scroll_if_needed { browser_action.move_to(element.native).release.perform }
171
+ element.scroll_if_needed do
172
+ keys_down = modifiers_down(browser_action, drop_modifiers)
173
+ keys_up = modifiers_up(keys_down.move_to(element.native).release, drop_modifiers)
174
+ keys_up.perform
140
175
  end
141
176
  end
142
177
 
143
- def tag_name
144
- native.tag_name.downcase
178
+ def drop(*_)
179
+ raise NotImplementedError, 'Out of browser drop emulation is not implemented for the current browser'
145
180
  end
146
181
 
147
- def visible?
148
- displayed = native.displayed?
149
- displayed and displayed != "false"
182
+ def tag_name
183
+ @tag_name ||=
184
+ if native.respond_to? :tag_name
185
+ native.tag_name.downcase
186
+ else
187
+ shadow_root? ? 'ShadowRoot' : 'Unknown'
188
+ end
150
189
  end
151
190
 
152
- def selected?
153
- selected = native.selected?
154
- selected and selected != "false"
155
- end
191
+ def visible?; boolean_attr(native.displayed?); end
192
+ def readonly?; boolean_attr(self[:readonly]); end
193
+ def multiple?; boolean_attr(self[:multiple]); end
194
+ def selected?; boolean_attr(native.selected?); end
156
195
  alias :checked? :selected?
157
196
 
158
197
  def disabled?
159
- # workaround for selenium-webdriver/geckodriver reporting elements as enabled when they are nested in disabling elements
160
- if driver.marionette?
161
- if %w(option optgroup).include? tag_name
162
- !native.enabled? || find_xpath("parent::*[self::optgroup or self::select]")[0].disabled?
198
+ return true unless native.enabled?
199
+
200
+ # WebDriver only defines `disabled?` for form controls but fieldset makes sense too
201
+ find_xpath('self::fieldset/ancestor-or-self::fieldset[@disabled]').any?
202
+ end
203
+
204
+ def content_editable?
205
+ native.attribute('isContentEditable') == 'true'
206
+ end
207
+
208
+ def path
209
+ driver.evaluate_script GET_XPATH_SCRIPT, self
210
+ end
211
+
212
+ def obscured?(x: nil, y: nil)
213
+ res = driver.evaluate_script(OBSCURED_OR_OFFSET_SCRIPT, self, x, y)
214
+ return true if res == true
215
+
216
+ driver.frame_obscured_at?(x: res['x'], y: res['y'])
217
+ end
218
+
219
+ def rect
220
+ native.rect
221
+ end
222
+
223
+ def shadow_root
224
+ raise_error 'You must be using Selenium 4.1+ for shadow_root support' unless native.respond_to? :shadow_root
225
+
226
+ root = native.shadow_root
227
+ root && build_node(native.shadow_root)
228
+ end
229
+
230
+ protected
231
+
232
+ def scroll_if_needed
233
+ yield
234
+ rescue ::Selenium::WebDriver::Error::MoveTargetOutOfBoundsError
235
+ scroll_to_center
236
+ yield
237
+ end
238
+
239
+ def scroll_to_center
240
+ script = <<-'JS'
241
+ try {
242
+ arguments[0].scrollIntoView({behavior: 'instant', block: 'center', inline: 'center'});
243
+ } catch(e) {
244
+ arguments[0].scrollIntoView(true);
245
+ }
246
+ JS
247
+ begin
248
+ driver.execute_script(script, self)
249
+ rescue StandardError
250
+ # Swallow error if scrollIntoView with options isn't supported
251
+ end
252
+ end
253
+
254
+ private
255
+
256
+ def sibling_index(parent, node, selector)
257
+ siblings = parent.find_xpath(selector)
258
+ case siblings.size
259
+ when 0
260
+ '[ERROR]' # IE doesn't support full XPath (namespace-uri, etc)
261
+ when 1
262
+ '' # index not necessary when only one matching element
263
+ else
264
+ idx = siblings.index(node)
265
+ # Element may not be found in the siblings if it has gone away
266
+ idx.nil? ? '[ERROR]' : "[#{idx + 1}]"
267
+ end
268
+ end
269
+
270
+ def boolean_attr(val)
271
+ val && (val != 'false')
272
+ end
273
+
274
+ # a reference to the select node if this is an option node
275
+ def select_node
276
+ find_xpath(XPath.ancestor(:select)[1]).first
277
+ end
278
+
279
+ def set_text(value, clear: nil, rapid: nil, **_unused)
280
+ value = value.to_s
281
+ if value.empty? && clear.nil?
282
+ native.clear
283
+ elsif clear == :backspace
284
+ # Clear field by sending the correct number of backspace keys.
285
+ backspaces = [:backspace] * self.value.to_s.length
286
+ send_keys(:end, *backspaces, value)
287
+ elsif clear.is_a? Array
288
+ send_keys(*clear, value)
289
+ else
290
+ driver.execute_script 'arguments[0].select()', self unless clear == :none
291
+ if rapid == true || ((value.length > auto_rapid_set_length) && rapid != false)
292
+ send_keys(value[0..3])
293
+ driver.execute_script RAPID_APPEND_TEXT, self, value[4...-3]
294
+ send_keys(value[-3..])
163
295
  else
164
- !native.enabled? || !find_xpath("parent::fieldset[@disabled] | ancestor::*[not(self::legend) or preceding-sibling::legend][parent::fieldset[@disabled]]").empty?
296
+ send_keys(value)
165
297
  end
166
- else
167
- !native.enabled?
168
298
  end
169
299
  end
170
300
 
171
- def readonly?
172
- readonly = self[:readonly]
173
- readonly and readonly != "false"
301
+ def auto_rapid_set_length
302
+ 30
174
303
  end
175
304
 
176
- def multiple?
177
- multiple = self[:multiple]
178
- multiple and multiple != "false"
305
+ def perform_with_options(click_options, &block)
306
+ raise ArgumentError, 'A block must be provided' unless block
307
+
308
+ scroll_if_needed do
309
+ action_with_modifiers(click_options) do |action|
310
+ if block
311
+ yield action
312
+ else
313
+ click_options.coords? ? action.click : action.click(native)
314
+ end
315
+ end
316
+ end
179
317
  end
180
318
 
181
- def content_editable?
182
- native.attribute('isContentEditable')
319
+ def set_date(value) # rubocop:disable Naming/AccessorMethodName
320
+ value = SettableValue.new(value)
321
+ return set_text(value) unless value.dateable?
322
+
323
+ # TODO: this would be better if locale can be detected and correct keystrokes sent
324
+ update_value_js(value.to_date_str)
325
+ end
326
+
327
+ def set_time(value) # rubocop:disable Naming/AccessorMethodName
328
+ value = SettableValue.new(value)
329
+ return set_text(value) unless value.timeable?
330
+
331
+ # TODO: this would be better if locale can be detected and correct keystrokes sent
332
+ update_value_js(value.to_time_str)
183
333
  end
184
334
 
185
- def find_xpath(locator)
186
- native.find_elements(:xpath, locator).map { |n| self.class.new(driver, n) }
335
+ def set_datetime_local(value) # rubocop:disable Naming/AccessorMethodName
336
+ value = SettableValue.new(value)
337
+ return set_text(value) unless value.timeable?
338
+
339
+ # TODO: this would be better if locale can be detected and correct keystrokes sent
340
+ update_value_js(value.to_datetime_str)
187
341
  end
188
342
 
189
- def find_css(locator)
190
- native.find_elements(:css, locator).map { |n| self.class.new(driver, n) }
343
+ def set_color(value) # rubocop:disable Naming/AccessorMethodName
344
+ update_value_js(value)
191
345
  end
192
346
 
193
- def ==(other)
194
- native == other.native
347
+ def set_range(value) # rubocop:disable Naming/AccessorMethodName
348
+ update_value_js(value)
195
349
  end
196
350
 
197
- def path
198
- path = find_xpath('ancestor::*').reverse
199
- path.unshift self
351
+ def update_value_js(value)
352
+ driver.execute_script(<<-JS, self, value)
353
+ if (arguments[0].readOnly) { return };
354
+ if (document.activeElement !== arguments[0]){
355
+ arguments[0].focus();
356
+ }
357
+ if (arguments[0].value != arguments[1]) {
358
+ arguments[0].value = arguments[1]
359
+ arguments[0].dispatchEvent(new InputEvent('input'));
360
+ arguments[0].dispatchEvent(new Event('change', { bubbles: true }));
361
+ }
362
+ JS
363
+ end
200
364
 
201
- result = []
202
- while node = path.shift
203
- parent = path.first
365
+ def set_file(value) # rubocop:disable Naming/AccessorMethodName
366
+ with_file_detector do
367
+ path_names = value.to_s.empty? ? [] : value
368
+ file_names = Array(path_names).map do |pn|
369
+ Pathname.new(pn).absolute? ? pn : File.expand_path(pn)
370
+ end.join("\n")
371
+ native.send_keys(file_names)
372
+ end
373
+ end
204
374
 
205
- if parent
206
- siblings = parent.find_xpath(node.tag_name)
207
- if siblings.size == 1
208
- result.unshift node.tag_name
375
+ def with_file_detector
376
+ if driver.options[:browser] == :remote &&
377
+ bridge.respond_to?(:file_detector) &&
378
+ bridge.file_detector.nil?
379
+ begin
380
+ bridge.file_detector = lambda do |(fn, *)|
381
+ str = fn.to_s
382
+ str if File.exist?(str)
383
+ end
384
+ yield
385
+ ensure
386
+ bridge.file_detector = nil
387
+ end
388
+ else
389
+ yield
390
+ end
391
+ end
392
+
393
+ def set_content_editable(value) # rubocop:disable Naming/AccessorMethodName
394
+ # Ensure we are focused on the element
395
+ click
396
+
397
+ editable = driver.execute_script <<-JS, self
398
+ if (arguments[0].isContentEditable) {
399
+ var range = document.createRange();
400
+ var sel = window.getSelection();
401
+ arguments[0].focus();
402
+ range.selectNodeContents(arguments[0]);
403
+ sel.removeAllRanges();
404
+ sel.addRange(range);
405
+ return true;
406
+ }
407
+ return false;
408
+ JS
409
+
410
+ # The action api has a speed problem but both chrome and firefox 58 raise errors
411
+ # if we use the faster direct send_keys. For now just send_keys to the element
412
+ # we've already focused.
413
+ # native.send_keys(value.to_s)
414
+ browser_action.send_keys(value.to_s).perform if editable
415
+ end
416
+
417
+ def action_with_modifiers(click_options)
418
+ actions = browser_action.tap do |acts|
419
+ if click_options.coords?
420
+ if click_options.center_offset?
421
+ if Selenium::WebDriver::VERSION.to_f >= 4.3
422
+ acts.move_to(native, *click_options.coords)
423
+ else
424
+ ::Selenium::WebDriver.logger.suppress_deprecations do
425
+ acts.move_to(native).move_by(*click_options.coords)
426
+ end
427
+ end
428
+ elsif Selenium::WebDriver::VERSION.to_f >= 4.3
429
+ right_by, down_by = *click_options.coords
430
+ size = native.size
431
+ left_offset = (size[:width] / 2).to_i
432
+ top_offset = (size[:height] / 2).to_i
433
+ left = -left_offset + right_by
434
+ top = -top_offset + down_by
435
+ acts.move_to(native, left, top)
209
436
  else
210
- index = siblings.index(node)
211
- result.unshift "#{node.tag_name}[#{index+1}]"
437
+ ::Selenium::WebDriver.logger.suppress_deprecations do
438
+ acts.move_to(native, *click_options.coords)
439
+ end
212
440
  end
213
441
  else
214
- result.unshift node.tag_name
442
+ acts.move_to(native)
215
443
  end
216
444
  end
445
+ modifiers_down(actions, click_options.keys)
446
+ yield actions
447
+ modifiers_up(actions, click_options.keys)
448
+ actions.perform
449
+ ensure
450
+ act = browser_action
451
+ act.release_actions if act.respond_to?(:release_actions)
452
+ end
217
453
 
218
- '/' + result.join('/')
454
+ def modifiers_down(actions, keys)
455
+ each_key(keys) { |key| actions.key_down(key) }
456
+ actions
219
457
  end
220
458
 
221
- private
222
- # a reference to the select node if this is an option node
223
- def select_node
224
- find_xpath('./ancestor::select[1]').first
459
+ def modifiers_up(actions, keys)
460
+ each_key(keys) { |key| actions.key_up(key) }
461
+ actions
225
462
  end
226
463
 
227
- def set_text(value, options)
228
- if readonly?
229
- warn "Attempt to set readonly element with value: #{value} \n *This will raise an exception in a future version of Capybara"
230
- elsif value.to_s.empty? && options[:clear].nil?
231
- native.clear
464
+ def browser
465
+ driver.browser
466
+ end
467
+
468
+ def bridge
469
+ browser.send(:bridge)
470
+ end
471
+
472
+ def browser_action
473
+ browser.action
474
+ end
475
+
476
+ def capabilities
477
+ browser.capabilities
478
+ end
479
+
480
+ def w3c?
481
+ (defined?(Selenium::WebDriver::VERSION) && (Selenium::WebDriver::VERSION.to_f >= 4)) ||
482
+ capabilities.is_a?(::Selenium::WebDriver::Remote::W3C::Capabilities)
483
+ end
484
+
485
+ def action_pause(action, duration)
486
+ if w3c?
487
+ if Selenium::WebDriver::VERSION.to_f >= 4.2
488
+ action.pause(device: action.pointer_inputs.first, duration: duration)
489
+ else
490
+ action.pause(action.pointer_inputs.first, duration)
491
+ end
232
492
  else
233
- if options[:clear] == :backspace
234
- # Clear field by sending the correct number of backspace keys.
235
- backspaces = [:backspace] * self.value.to_s.length
236
- native.send_keys(*(backspaces + [value.to_s]))
237
- elsif options[:clear] == :none
238
- native.send_keys(value.to_s)
239
- elsif options[:clear].is_a? Array
240
- native.send_keys(*options[:clear], value.to_s)
493
+ action.pause(duration)
494
+ end
495
+ end
496
+
497
+ def normalize_keys(keys)
498
+ keys.map do |key|
499
+ case key
500
+ when :ctrl then :control
501
+ when :command, :cmd then :meta
241
502
  else
242
- # Clear field by JavaScript assignment of the value property.
243
- # Script can change a readonly element which user input cannot, so
244
- # don't execute if readonly.
245
- driver.execute_script "arguments[0].value = ''", self
246
- native.send_keys(value.to_s)
503
+ key
247
504
  end
248
505
  end
249
506
  end
250
507
 
251
- def scroll_if_needed(&block)
252
- block.call
253
- rescue ::Selenium::WebDriver::Error::MoveTargetOutOfBoundsError
254
- script = <<-JS
255
- try {
256
- arguments[0].scrollIntoView({behavior: 'instant', block: 'center', inline: 'center'});
257
- } catch(e) {
258
- arguments[0].scrollIntoView(true);
259
- }
508
+ def each_key(keys, &block)
509
+ normalize_keys(keys).each(&block)
510
+ end
511
+
512
+ def find_context
513
+ native
514
+ end
515
+
516
+ def build_node(native_node, initial_cache = {})
517
+ self.class.new(driver, native_node, initial_cache)
518
+ end
519
+
520
+ def attrs(*attr_names)
521
+ return attr_names.map { |name| self[name.to_s] } if ENV['CAPYBARA_THOROUGH']
522
+
523
+ driver.evaluate_script <<~'JS', self, attr_names.map(&:to_s)
524
+ (function(el, names){
525
+ return names.map(function(name){
526
+ return el[name]
527
+ });
528
+ })(arguments[0], arguments[1]);
260
529
  JS
261
- driver.execute_script(script, self)
262
- block.call
263
530
  end
531
+
532
+ def native_id
533
+ # Selenium 3 -> 4 changed the return of ref
534
+ type_or_id, id = native.ref
535
+ id || type_or_id
536
+ end
537
+
538
+ def shadow_root?
539
+ defined?(::Selenium::WebDriver::ShadowRoot) && native.is_a?(::Selenium::WebDriver::ShadowRoot)
540
+ end
541
+
542
+ GET_XPATH_SCRIPT = <<~'JS'
543
+ (function(el, xml){
544
+ var xpath = '';
545
+ var pos, tempitem2;
546
+
547
+ if (el.getRootNode && el.getRootNode() instanceof ShadowRoot) {
548
+ return "(: Shadow DOM element - no XPath :)";
549
+ };
550
+ while(el !== xml.documentElement) {
551
+ pos = 0;
552
+ tempitem2 = el;
553
+ while(tempitem2) {
554
+ if (tempitem2.nodeType === 1 && tempitem2.nodeName === el.nodeName) { // If it is ELEMENT_NODE of the same name
555
+ pos += 1;
556
+ }
557
+ tempitem2 = tempitem2.previousSibling;
558
+ }
559
+
560
+ if (el.namespaceURI != xml.documentElement.namespaceURI) {
561
+ xpath = "*[local-name()='"+el.nodeName+"' and namespace-uri()='"+(el.namespaceURI===null?'':el.namespaceURI)+"']["+pos+']'+'/'+xpath;
562
+ } else {
563
+ xpath = el.nodeName.toUpperCase()+"["+pos+"]/"+xpath;
564
+ }
565
+
566
+ el = el.parentNode;
567
+ }
568
+ xpath = '/'+xml.documentElement.nodeName.toUpperCase()+'/'+xpath;
569
+ xpath = xpath.replace(/\/$/, '');
570
+ return xpath;
571
+ })(arguments[0], document)
572
+ JS
573
+
574
+ OBSCURED_OR_OFFSET_SCRIPT = <<~'JS'
575
+ (function(el, x, y) {
576
+ var box = el.getBoundingClientRect();
577
+ if (x == null) x = box.width/2;
578
+ if (y == null) y = box.height/2 ;
579
+
580
+ var px = box.left + x,
581
+ py = box.top + y,
582
+ e = document.elementFromPoint(px, py);
583
+
584
+ if (!el.contains(e))
585
+ return true;
586
+
587
+ return { x: px, y: py };
588
+ })(arguments[0], arguments[1], arguments[2])
589
+ JS
590
+
591
+ RAPID_APPEND_TEXT = <<~'JS'
592
+ (function(el, value) {
593
+ value = el.value + value;
594
+ if (el.maxLength && el.maxLength != -1){
595
+ value = value.slice(0, el.maxLength);
596
+ }
597
+ el.value = value;
598
+ })(arguments[0], arguments[1])
599
+ JS
600
+
601
+ # SettableValue encapsulates time/date field formatting
602
+ class SettableValue
603
+ attr_reader :value
604
+
605
+ def initialize(value)
606
+ @value = value
607
+ end
608
+
609
+ def to_s
610
+ value.to_s
611
+ end
612
+
613
+ def dateable?
614
+ !value.is_a?(String) && value.respond_to?(:to_date)
615
+ end
616
+
617
+ def to_date_str
618
+ value.to_date.iso8601
619
+ end
620
+
621
+ def timeable?
622
+ !value.is_a?(String) && value.respond_to?(:to_time)
623
+ end
624
+
625
+ def to_time_str
626
+ value.to_time.strftime('%H:%M')
627
+ end
628
+
629
+ def to_datetime_str
630
+ value.to_time.strftime('%Y-%m-%dT%H:%M')
631
+ end
632
+ end
633
+ private_constant :SettableValue
634
+
635
+ # ClickOptions encapsulates click option logic
636
+ class ClickOptions
637
+ attr_reader :keys, :options
638
+
639
+ def initialize(keys, options)
640
+ @keys = keys
641
+ @options = options
642
+ end
643
+
644
+ def coords?
645
+ options[:x] && options[:y]
646
+ end
647
+
648
+ def coords
649
+ [options[:x], options[:y]]
650
+ end
651
+
652
+ def center_offset?
653
+ options[:offset] == :center
654
+ end
655
+
656
+ def empty?
657
+ keys.empty? && !coords? && delay.zero?
658
+ end
659
+
660
+ def delay
661
+ options[:delay] || 0
662
+ end
663
+ end
664
+ private_constant :ClickOptions
264
665
  end