capybara 3.13.2 → 3.40.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (260) hide show
  1. checksums.yaml +4 -4
  2. data/.yardopts +1 -0
  3. data/History.md +587 -16
  4. data/README.md +240 -90
  5. data/lib/capybara/config.rb +24 -11
  6. data/lib/capybara/cucumber.rb +1 -1
  7. data/lib/capybara/driver/base.rb +8 -0
  8. data/lib/capybara/driver/node.rb +20 -4
  9. data/lib/capybara/dsl.rb +5 -3
  10. data/lib/capybara/helpers.rb +25 -4
  11. data/lib/capybara/minitest/spec.rb +174 -90
  12. data/lib/capybara/minitest.rb +256 -142
  13. data/lib/capybara/node/actions.rb +123 -77
  14. data/lib/capybara/node/base.rb +20 -12
  15. data/lib/capybara/node/document.rb +2 -2
  16. data/lib/capybara/node/document_matchers.rb +3 -3
  17. data/lib/capybara/node/element.rb +223 -117
  18. data/lib/capybara/node/finders.rb +81 -71
  19. data/lib/capybara/node/matchers.rb +271 -134
  20. data/lib/capybara/node/simple.rb +18 -5
  21. data/lib/capybara/node/whitespace_normalizer.rb +81 -0
  22. data/lib/capybara/queries/active_element_query.rb +18 -0
  23. data/lib/capybara/queries/ancestor_query.rb +8 -9
  24. data/lib/capybara/queries/base_query.rb +3 -2
  25. data/lib/capybara/queries/current_path_query.rb +15 -5
  26. data/lib/capybara/queries/selector_query.rb +364 -54
  27. data/lib/capybara/queries/sibling_query.rb +8 -6
  28. data/lib/capybara/queries/style_query.rb +2 -2
  29. data/lib/capybara/queries/text_query.rb +13 -1
  30. data/lib/capybara/queries/title_query.rb +1 -1
  31. data/lib/capybara/rack_test/browser.rb +76 -11
  32. data/lib/capybara/rack_test/driver.rb +10 -5
  33. data/lib/capybara/rack_test/errors.rb +6 -0
  34. data/lib/capybara/rack_test/form.rb +31 -9
  35. data/lib/capybara/rack_test/node.rb +74 -23
  36. data/lib/capybara/registration_container.rb +41 -0
  37. data/lib/capybara/registrations/drivers.rb +42 -0
  38. data/lib/capybara/registrations/patches/puma_ssl.rb +29 -0
  39. data/lib/capybara/registrations/servers.rb +66 -0
  40. data/lib/capybara/result.rb +44 -20
  41. data/lib/capybara/rspec/matcher_proxies.rb +13 -11
  42. data/lib/capybara/rspec/matchers/base.rb +31 -16
  43. data/lib/capybara/rspec/matchers/compound.rb +1 -1
  44. data/lib/capybara/rspec/matchers/count_sugar.rb +37 -0
  45. data/lib/capybara/rspec/matchers/have_ancestor.rb +28 -0
  46. data/lib/capybara/rspec/matchers/have_current_path.rb +2 -2
  47. data/lib/capybara/rspec/matchers/have_selector.rb +21 -21
  48. data/lib/capybara/rspec/matchers/have_sibling.rb +27 -0
  49. data/lib/capybara/rspec/matchers/have_text.rb +4 -4
  50. data/lib/capybara/rspec/matchers/have_title.rb +2 -2
  51. data/lib/capybara/rspec/matchers/match_selector.rb +3 -3
  52. data/lib/capybara/rspec/matchers/match_style.rb +7 -2
  53. data/lib/capybara/rspec/matchers/spatial_sugar.rb +39 -0
  54. data/lib/capybara/rspec/matchers.rb +111 -68
  55. data/lib/capybara/rspec.rb +2 -0
  56. data/lib/capybara/selector/builders/css_builder.rb +11 -7
  57. data/lib/capybara/selector/builders/xpath_builder.rb +5 -3
  58. data/lib/capybara/selector/css.rb +11 -9
  59. data/lib/capybara/selector/definition/button.rb +68 -0
  60. data/lib/capybara/selector/definition/checkbox.rb +26 -0
  61. data/lib/capybara/selector/definition/css.rb +10 -0
  62. data/lib/capybara/selector/definition/datalist_input.rb +35 -0
  63. data/lib/capybara/selector/definition/datalist_option.rb +25 -0
  64. data/lib/capybara/selector/definition/element.rb +28 -0
  65. data/lib/capybara/selector/definition/field.rb +40 -0
  66. data/lib/capybara/selector/definition/fieldset.rb +14 -0
  67. data/lib/capybara/selector/definition/file_field.rb +13 -0
  68. data/lib/capybara/selector/definition/fillable_field.rb +33 -0
  69. data/lib/capybara/selector/definition/frame.rb +17 -0
  70. data/lib/capybara/selector/definition/id.rb +6 -0
  71. data/lib/capybara/selector/definition/label.rb +62 -0
  72. data/lib/capybara/selector/definition/link.rb +55 -0
  73. data/lib/capybara/selector/definition/link_or_button.rb +16 -0
  74. data/lib/capybara/selector/definition/option.rb +27 -0
  75. data/lib/capybara/selector/definition/radio_button.rb +27 -0
  76. data/lib/capybara/selector/definition/select.rb +81 -0
  77. data/lib/capybara/selector/definition/table.rb +109 -0
  78. data/lib/capybara/selector/definition/table_row.rb +21 -0
  79. data/lib/capybara/selector/definition/xpath.rb +5 -0
  80. data/lib/capybara/selector/definition.rb +280 -0
  81. data/lib/capybara/selector/filter_set.rb +19 -18
  82. data/lib/capybara/selector/filters/base.rb +11 -2
  83. data/lib/capybara/selector/filters/locator_filter.rb +13 -3
  84. data/lib/capybara/selector/regexp_disassembler.rb +11 -7
  85. data/lib/capybara/selector/selector.rb +50 -440
  86. data/lib/capybara/selector/xpath_extensions.rb +17 -0
  87. data/lib/capybara/selector.rb +473 -482
  88. data/lib/capybara/selenium/atoms/getAttribute.min.js +1 -0
  89. data/lib/capybara/selenium/atoms/isDisplayed.min.js +1 -0
  90. data/lib/capybara/selenium/atoms/src/getAttribute.js +161 -0
  91. data/lib/capybara/selenium/atoms/src/isDisplayed.js +454 -0
  92. data/lib/capybara/selenium/driver.rb +174 -62
  93. data/lib/capybara/selenium/driver_specializations/chrome_driver.rb +74 -18
  94. data/lib/capybara/selenium/driver_specializations/edge_driver.rb +128 -0
  95. data/lib/capybara/selenium/driver_specializations/firefox_driver.rb +37 -3
  96. data/lib/capybara/selenium/driver_specializations/internet_explorer_driver.rb +14 -1
  97. data/lib/capybara/selenium/driver_specializations/safari_driver.rb +24 -0
  98. data/lib/capybara/selenium/extensions/file_input_click_emulation.rb +34 -0
  99. data/lib/capybara/selenium/extensions/find.rb +68 -45
  100. data/lib/capybara/selenium/extensions/html5_drag.rb +192 -22
  101. data/lib/capybara/selenium/extensions/modifier_keys_stack.rb +28 -0
  102. data/lib/capybara/selenium/extensions/scroll.rb +8 -10
  103. data/lib/capybara/selenium/node.rb +268 -72
  104. data/lib/capybara/selenium/nodes/chrome_node.rb +105 -9
  105. data/lib/capybara/selenium/nodes/edge_node.rb +110 -0
  106. data/lib/capybara/selenium/nodes/firefox_node.rb +51 -61
  107. data/lib/capybara/selenium/nodes/ie_node.rb +22 -0
  108. data/lib/capybara/selenium/nodes/safari_node.rb +118 -0
  109. data/lib/capybara/selenium/patches/atoms.rb +18 -0
  110. data/lib/capybara/selenium/patches/is_displayed.rb +16 -0
  111. data/lib/capybara/selenium/patches/logs.rb +45 -0
  112. data/lib/capybara/selenium/patches/pause_duration_fix.rb +1 -1
  113. data/lib/capybara/selenium/patches/persistent_client.rb +20 -0
  114. data/lib/capybara/server/animation_disabler.rb +43 -21
  115. data/lib/capybara/server/checker.rb +6 -2
  116. data/lib/capybara/server/middleware.rb +25 -13
  117. data/lib/capybara/server.rb +20 -4
  118. data/lib/capybara/session/config.rb +15 -11
  119. data/lib/capybara/session/matchers.rb +11 -11
  120. data/lib/capybara/session.rb +162 -131
  121. data/lib/capybara/spec/public/offset.js +6 -0
  122. data/lib/capybara/spec/public/test.js +105 -6
  123. data/lib/capybara/spec/session/accept_alert_spec.rb +1 -1
  124. data/lib/capybara/spec/session/active_element_spec.rb +31 -0
  125. data/lib/capybara/spec/session/all_spec.rb +89 -15
  126. data/lib/capybara/spec/session/ancestor_spec.rb +5 -0
  127. data/lib/capybara/spec/session/assert_current_path_spec.rb +5 -2
  128. data/lib/capybara/spec/session/assert_text_spec.rb +26 -22
  129. data/lib/capybara/spec/session/attach_file_spec.rb +64 -31
  130. data/lib/capybara/spec/session/check_spec.rb +26 -4
  131. data/lib/capybara/spec/session/choose_spec.rb +14 -2
  132. data/lib/capybara/spec/session/click_button_spec.rb +109 -61
  133. data/lib/capybara/spec/session/click_link_or_button_spec.rb +9 -0
  134. data/lib/capybara/spec/session/click_link_spec.rb +23 -1
  135. data/lib/capybara/spec/session/current_scope_spec.rb +1 -1
  136. data/lib/capybara/spec/session/current_url_spec.rb +11 -1
  137. data/lib/capybara/spec/session/element/matches_selector_spec.rb +40 -39
  138. data/lib/capybara/spec/session/evaluate_script_spec.rb +12 -0
  139. data/lib/capybara/spec/session/fill_in_spec.rb +46 -5
  140. data/lib/capybara/spec/session/find_link_spec.rb +10 -0
  141. data/lib/capybara/spec/session/find_spec.rb +80 -7
  142. data/lib/capybara/spec/session/first_spec.rb +2 -2
  143. data/lib/capybara/spec/session/frame/switch_to_frame_spec.rb +14 -1
  144. data/lib/capybara/spec/session/frame/within_frame_spec.rb +14 -1
  145. data/lib/capybara/spec/session/has_all_selectors_spec.rb +5 -5
  146. data/lib/capybara/spec/session/has_ancestor_spec.rb +46 -0
  147. data/lib/capybara/spec/session/has_any_selectors_spec.rb +6 -2
  148. data/lib/capybara/spec/session/has_button_spec.rb +81 -0
  149. data/lib/capybara/spec/session/has_css_spec.rb +45 -8
  150. data/lib/capybara/spec/session/has_current_path_spec.rb +22 -7
  151. data/lib/capybara/spec/session/has_element_spec.rb +47 -0
  152. data/lib/capybara/spec/session/has_field_spec.rb +59 -1
  153. data/lib/capybara/spec/session/has_link_spec.rb +40 -0
  154. data/lib/capybara/spec/session/has_none_selectors_spec.rb +7 -7
  155. data/lib/capybara/spec/session/has_select_spec.rb +42 -8
  156. data/lib/capybara/spec/session/has_selector_spec.rb +19 -4
  157. data/lib/capybara/spec/session/has_sibling_spec.rb +50 -0
  158. data/lib/capybara/spec/session/has_table_spec.rb +177 -0
  159. data/lib/capybara/spec/session/has_text_spec.rb +31 -3
  160. data/lib/capybara/spec/session/html_spec.rb +1 -1
  161. data/lib/capybara/spec/session/matches_style_spec.rb +6 -4
  162. data/lib/capybara/spec/session/node_spec.rb +697 -23
  163. data/lib/capybara/spec/session/node_wrapper_spec.rb +1 -1
  164. data/lib/capybara/spec/session/refresh_spec.rb +2 -1
  165. data/lib/capybara/spec/session/reset_session_spec.rb +21 -7
  166. data/lib/capybara/spec/session/save_and_open_screenshot_spec.rb +2 -2
  167. data/lib/capybara/spec/session/save_page_spec.rb +4 -4
  168. data/lib/capybara/spec/session/save_screenshot_spec.rb +4 -4
  169. data/lib/capybara/spec/session/scroll_spec.rb +9 -7
  170. data/lib/capybara/spec/session/select_spec.rb +5 -10
  171. data/lib/capybara/spec/session/selectors_spec.rb +24 -3
  172. data/lib/capybara/spec/session/uncheck_spec.rb +3 -3
  173. data/lib/capybara/spec/session/unselect_spec.rb +1 -1
  174. data/lib/capybara/spec/session/visit_spec.rb +20 -0
  175. data/lib/capybara/spec/session/window/become_closed_spec.rb +20 -17
  176. data/lib/capybara/spec/session/window/switch_to_window_spec.rb +1 -1
  177. data/lib/capybara/spec/session/window/window_opened_by_spec.rb +1 -1
  178. data/lib/capybara/spec/session/window/window_spec.rb +54 -57
  179. data/lib/capybara/spec/session/window/windows_spec.rb +2 -2
  180. data/lib/capybara/spec/session/within_spec.rb +36 -0
  181. data/lib/capybara/spec/spec_helper.rb +30 -19
  182. data/lib/capybara/spec/test_app.rb +122 -34
  183. data/lib/capybara/spec/views/animated.erb +49 -0
  184. data/lib/capybara/spec/views/form.erb +86 -8
  185. data/lib/capybara/spec/views/frame_child.erb +3 -2
  186. data/lib/capybara/spec/views/frame_one.erb +2 -1
  187. data/lib/capybara/spec/views/frame_parent.erb +1 -1
  188. data/lib/capybara/spec/views/frame_two.erb +1 -1
  189. data/lib/capybara/spec/views/initial_alert.erb +2 -1
  190. data/lib/capybara/spec/views/layout.erb +10 -0
  191. data/lib/capybara/spec/views/obscured.erb +10 -10
  192. data/lib/capybara/spec/views/offset.erb +33 -0
  193. data/lib/capybara/spec/views/path.erb +2 -2
  194. data/lib/capybara/spec/views/popup_one.erb +1 -1
  195. data/lib/capybara/spec/views/popup_two.erb +1 -1
  196. data/lib/capybara/spec/views/react.erb +45 -0
  197. data/lib/capybara/spec/views/scroll.erb +2 -1
  198. data/lib/capybara/spec/views/spatial.erb +31 -0
  199. data/lib/capybara/spec/views/tables.erb +67 -0
  200. data/lib/capybara/spec/views/with_animation.erb +39 -4
  201. data/lib/capybara/spec/views/with_base_tag.erb +2 -2
  202. data/lib/capybara/spec/views/with_dragula.erb +24 -0
  203. data/lib/capybara/spec/views/with_fixed_header_footer.erb +2 -1
  204. data/lib/capybara/spec/views/with_hover.erb +3 -2
  205. data/lib/capybara/spec/views/with_hover1.erb +10 -0
  206. data/lib/capybara/spec/views/with_html.erb +34 -6
  207. data/lib/capybara/spec/views/with_jquery_animation.erb +24 -0
  208. data/lib/capybara/spec/views/with_js.erb +7 -4
  209. data/lib/capybara/spec/views/with_jstree.erb +26 -0
  210. data/lib/capybara/spec/views/with_namespace.erb +1 -0
  211. data/lib/capybara/spec/views/with_scope.erb +2 -2
  212. data/lib/capybara/spec/views/with_scope_other.erb +6 -0
  213. data/lib/capybara/spec/views/with_shadow.erb +31 -0
  214. data/lib/capybara/spec/views/with_slow_unload.erb +2 -1
  215. data/lib/capybara/spec/views/with_sortable_js.erb +21 -0
  216. data/lib/capybara/spec/views/with_unload_alert.erb +1 -0
  217. data/lib/capybara/spec/views/with_windows.erb +1 -1
  218. data/lib/capybara/spec/views/within_frames.erb +1 -1
  219. data/lib/capybara/version.rb +1 -1
  220. data/lib/capybara/window.rb +14 -18
  221. data/lib/capybara.rb +91 -126
  222. data/spec/basic_node_spec.rb +30 -16
  223. data/spec/capybara_spec.rb +40 -28
  224. data/spec/counter_spec.rb +35 -0
  225. data/spec/css_builder_spec.rb +3 -1
  226. data/spec/css_splitter_spec.rb +1 -1
  227. data/spec/dsl_spec.rb +33 -22
  228. data/spec/filter_set_spec.rb +5 -5
  229. data/spec/fixtures/selenium_driver_rspec_failure.rb +3 -3
  230. data/spec/fixtures/selenium_driver_rspec_success.rb +3 -3
  231. data/spec/minitest_spec.rb +24 -2
  232. data/spec/minitest_spec_spec.rb +60 -45
  233. data/spec/per_session_config_spec.rb +1 -1
  234. data/spec/rack_test_spec.rb +131 -98
  235. data/spec/regexp_dissassembler_spec.rb +53 -39
  236. data/spec/result_spec.rb +68 -66
  237. data/spec/rspec/features_spec.rb +9 -4
  238. data/spec/rspec/scenarios_spec.rb +6 -2
  239. data/spec/rspec/shared_spec_matchers.rb +137 -98
  240. data/spec/rspec_matchers_spec.rb +25 -0
  241. data/spec/rspec_spec.rb +23 -21
  242. data/spec/sauce_spec_chrome.rb +43 -0
  243. data/spec/selector_spec.rb +77 -21
  244. data/spec/selenium_spec_chrome.rb +141 -39
  245. data/spec/selenium_spec_chrome_remote.rb +32 -17
  246. data/spec/selenium_spec_edge.rb +36 -8
  247. data/spec/selenium_spec_firefox.rb +110 -68
  248. data/spec/selenium_spec_firefox_remote.rb +22 -15
  249. data/spec/selenium_spec_ie.rb +29 -22
  250. data/spec/selenium_spec_safari.rb +162 -0
  251. data/spec/server_spec.rb +153 -81
  252. data/spec/session_spec.rb +11 -4
  253. data/spec/shared_selenium_node.rb +79 -0
  254. data/spec/shared_selenium_session.rb +179 -74
  255. data/spec/spec_helper.rb +80 -5
  256. data/spec/whitespace_normalizer_spec.rb +54 -0
  257. data/spec/xpath_builder_spec.rb +3 -1
  258. metadata +218 -30
  259. data/lib/capybara/spec/session/source_spec.rb +0 -0
  260. data/lib/capybara/spec/views/with_title.erb +0 -5
@@ -1,503 +1,494 @@
1
1
  # frozen_string_literal: true
2
2
 
3
+ require 'capybara/selector/xpath_extensions'
3
4
  require 'capybara/selector/selector'
5
+ require 'capybara/selector/definition'
6
+
7
+ #
8
+ # All Selectors below support the listed selector specific filters in addition to the following system-wide filters
9
+ # * :id (String, Regexp, XPath::Expression) - Matches the id attribute
10
+ # * :class (String, Array<String | Regexp>, Regexp, XPath::Expression) - Matches the class(es) provided
11
+ # * :style (String, Regexp, Hash<String, String>) - Match on elements style
12
+ # * :above (Element) - Match elements above the passed element on the page
13
+ # * :below (Element) - Match elements below the passed element on the page
14
+ # * :left_of (Element) - Match elements left of the passed element on the page
15
+ # * :right_of (Element) - Match elements right of the passed element on the page
16
+ # * :near (Element) - Match elements near (within 50px) the passed element on the page
17
+ # * :focused (Boolean) - Match elements with focus (requires driver support)
18
+ #
19
+ # ### Built-in Selectors
20
+ #
21
+ # * **:xpath** - Select elements by XPath expression
22
+ # * Locator: An XPath expression
23
+ #
24
+ # ```ruby
25
+ # page.html # => '<input>'
26
+ #
27
+ # page.find :xpath, './/input'
28
+ # ```
29
+ #
30
+ # * **:css** - Select elements by CSS selector
31
+ # * Locator: A CSS selector
32
+ #
33
+ # ```ruby
34
+ # page.html # => '<input>'
35
+ #
36
+ # page.find :css, 'input'
37
+ # ```
38
+ #
39
+ # * **:id** - Select element by id
40
+ # * Locator: (String, Regexp, XPath::Expression) The id of the element to match
41
+ #
42
+ # ```ruby
43
+ # page.html # => '<input id="field">'
44
+ #
45
+ # page.find :id, 'content'
46
+ # ```
47
+ #
48
+ # * **:field** - Select field elements (input [not of type submit, image, or hidden], textarea, select)
49
+ # * Locator: Matches against the id, {Capybara.configure test_id} attribute, name, placeholder, or
50
+ # associated label text
51
+ # * Filters:
52
+ # * :name (String, Regexp) - Matches the name attribute
53
+ # * :placeholder (String, Regexp) - Matches the placeholder attribute
54
+ # * :type (String) - Matches the type attribute of the field or element type for 'textarea' and 'select'
55
+ # * :readonly (Boolean) - Match on the element being readonly
56
+ # * :with (String, Regexp) - Matches the current value of the field
57
+ # * :checked (Boolean) - Match checked fields?
58
+ # * :unchecked (Boolean) - Match unchecked fields?
59
+ # * :disabled (Boolean, :all) - Match disabled field? (Default: false)
60
+ # * :multiple (Boolean) - Match fields that accept multiple values
61
+ # * :valid (Boolean) - Match fields that are valid/invalid according to HTML5 form validation
62
+ # * :validation_message (String, Regexp) - Matches the elements current validationMessage
63
+ #
64
+ # ```ruby
65
+ # page.html # => '<label for="article_title">Title</label>
66
+ # <input id="article_title" name="article[title]" value="Hello world">'
67
+ #
68
+ # page.find :field, 'article_title'
69
+ # page.find :field, 'article[title]'
70
+ # page.find :field, 'Title'
71
+ # page.find :field, 'Title', type: 'text', with: 'Hello world'
72
+ # ```
73
+ #
74
+ # * **:fieldset** - Select fieldset elements
75
+ # * Locator: Matches id, {Capybara.configure test_id}, or contents of wrapped legend
76
+ # * Filters:
77
+ # * :legend (String) - Matches contents of wrapped legend
78
+ # * :disabled (Boolean) - Match disabled fieldset?
79
+ #
80
+ # ```ruby
81
+ # page.html # => '<fieldset disabled>
82
+ # <legend>Fields (disabled)</legend>
83
+ # </fieldset>'
84
+ #
85
+ # page.find :fieldset, 'Fields (disabled)', disabled: true
86
+ # ```
87
+ #
88
+ # * **:link** - Find links (`<a>` elements with an href attribute)
89
+ # * Locator: Matches the id, {Capybara.configure test_id}, or title attributes, or the string content of the link,
90
+ # or the alt attribute of a contained img element. By default this selector requires a link to have an href attribute.
91
+ # * Filters:
92
+ # * :title (String) - Matches the title attribute
93
+ # * :alt (String) - Matches the alt attribute of a contained img element
94
+ # * :href (String, Regexp, nil, false) - Matches the normalized href of the link, if nil will find `<a>` elements with no href attribute, if false ignores href presence
95
+ #
96
+ # ```ruby
97
+ # page.html # => '<a href="/">Home</a>'
98
+ #
99
+ # page.find :link, 'Home', href: '/'
100
+ #
101
+ # page.html # => '<a href="/"><img src="/logo.png" alt="The logo"></a>'
102
+ #
103
+ # page.find :link, 'The logo', href: '/'
104
+ # page.find :link, alt: 'The logo', href: '/'
105
+ # ```
106
+ #
107
+ # * **:button** - Find buttons ( input [of type submit, reset, image, button] or button elements )
108
+ # * Locator: Matches the id, {Capybara.configure test_id} attribute, name, value, or title attributes, string content of a button, or the alt attribute of an image type button or of a descendant image of a button
109
+ # * Filters:
110
+ # * :name (String, Regexp) - Matches the name attribute
111
+ # * :title (String) - Matches the title attribute
112
+ # * :value (String) - Matches the value of an input button
113
+ # * :type (String) - Matches the type attribute
114
+ # * :disabled (Boolean, :all) - Match disabled buttons (Default: false)
115
+ #
116
+ # ```ruby
117
+ # page.html # => '<button>Submit</button>'
118
+ #
119
+ # page.find :button, 'Submit'
120
+ #
121
+ # page.html # => '<button name="article[state]" value="draft">Save as draft</button>'
122
+ #
123
+ # page.find :button, 'Save as draft', name: 'article[state]', value: 'draft'
124
+ # ```
125
+ #
126
+ # * **:link_or_button** - Find links or buttons
127
+ # * Locator: See :link and :button selectors
128
+ # * Filters:
129
+ # * :disabled (Boolean, :all) - Match disabled buttons? (Default: false)
130
+ #
131
+ # ```ruby
132
+ # page.html # => '<a href="/">Home</a>'
133
+ #
134
+ # page.find :link_or_button, 'Home'
135
+ #
136
+ # page.html # => '<button>Submit</button>'
137
+ #
138
+ # page.find :link_or_button, 'Submit'
139
+ # ```
140
+ #
141
+ # * **:fillable_field** - Find text fillable fields ( textarea, input [not of type submit, image, radio, checkbox, hidden, file] )
142
+ # * Locator: Matches against the id, {Capybara.configure test_id} attribute, name, placeholder, or associated label text
143
+ # * Filters:
144
+ # * :name (String, Regexp) - Matches the name attribute
145
+ # * :placeholder (String, Regexp) - Matches the placeholder attribute
146
+ # * :with (String, Regexp) - Matches the current value of the field
147
+ # * :type (String) - Matches the type attribute of the field or element type for 'textarea'
148
+ # * :disabled (Boolean, :all) - Match disabled field? (Default: false)
149
+ # * :multiple (Boolean) - Match fields that accept multiple values
150
+ # * :valid (Boolean) - Match fields that are valid/invalid according to HTML5 form validation
151
+ # * :validation_message (String, Regexp) - Matches the elements current validationMessage
152
+ #
153
+ # ```ruby
154
+ # page.html # => '<label for="article_body">Body</label>
155
+ # <textarea id="article_body" name="article[body]"></textarea>'
156
+ #
157
+ # page.find :fillable_field, 'article_body'
158
+ # page.find :fillable_field, 'article[body]'
159
+ # page.find :fillable_field, 'Body'
160
+ # page.find :field, 'Body', type: 'textarea'
161
+ # ```
162
+ #
163
+ # * **:radio_button** - Find radio buttons
164
+ # * Locator: Match id, {Capybara.configure test_id} attribute, name, or associated label text
165
+ # * Filters:
166
+ # * :name (String, Regexp) - Matches the name attribute
167
+ # * :checked (Boolean) - Match checked fields?
168
+ # * :unchecked (Boolean) - Match unchecked fields?
169
+ # * :disabled (Boolean, :all) - Match disabled field? (Default: false)
170
+ # * :option (String, Regexp) - Match the current value
171
+ # * :with - Alias of :option
172
+ #
173
+ # ```ruby
174
+ # page.html # => '<input type="radio" id="article_state_published" name="article[state]" value="published" checked>
175
+ # <label for="article_state_published">Published</label>
176
+ # <input type="radio" id="article_state_draft" name="article[state]" value="draft">
177
+ # <label for="article_state_draft">Draft</label>'
178
+ #
179
+ # page.find :radio_button, 'article_state_published'
180
+ # page.find :radio_button, 'article[state]', option: 'published'
181
+ # page.find :radio_button, 'Published', checked: true
182
+ # page.find :radio_button, 'Draft', unchecked: true
183
+ # ```
184
+ #
185
+ # * **:checkbox** - Find checkboxes
186
+ # * Locator: Match id, {Capybara.configure test_id} attribute, name, or associated label text
187
+ # * Filters:
188
+ # * :name (String, Regexp) - Matches the name attribute
189
+ # * :checked (Boolean) - Match checked fields?
190
+ # * :unchecked (Boolean) - Match unchecked fields?
191
+ # * :disabled (Boolean, :all) - Match disabled field? (Default: false)
192
+ # * :with (String, Regexp) - Match the current value
193
+ # * :option - Alias of :with
194
+ #
195
+ # ```ruby
196
+ # page.html # => '<input type="checkbox" id="registration_terms" name="registration[terms]" value="true">
197
+ # <label for="registration_terms">I agree to terms and conditions</label>'
198
+ #
199
+ # page.find :checkbox, 'registration_terms'
200
+ # page.find :checkbox, 'registration[terms]'
201
+ # page.find :checkbox, 'I agree to terms and conditions', unchecked: true
202
+ # ```
203
+ #
204
+ # * **:select** - Find select elements
205
+ # * Locator: Match id, {Capybara.configure test_id} attribute, name, placeholder, or associated label text
206
+ # * Filters:
207
+ # * :name (String, Regexp) - Matches the name attribute
208
+ # * :placeholder (String, Placeholder) - Matches the placeholder attribute
209
+ # * :disabled (Boolean, :all) - Match disabled field? (Default: false)
210
+ # * :multiple (Boolean) - Match fields that accept multiple values
211
+ # * :options (Array<String>) - Exact match options
212
+ # * :enabled_options (Array<String>) - Exact match enabled options
213
+ # * :disabled_options (Array<String>) - Exact match disabled options
214
+ # * :with_options (Array<String>) - Partial match options
215
+ # * :selected (String, Array<String>) - Match the selection(s)
216
+ # * :with_selected (String, Array<String>) - Partial match the selection(s)
217
+ #
218
+ # ```ruby
219
+ # page.html # => '<label for="article_category">Category</label>
220
+ # <select id="article_category" name="article[category]">
221
+ # <option value="General" checked></option>
222
+ # <option value="Other"></option>
223
+ # </select>'
224
+ #
225
+ # page.find :select, 'article_category'
226
+ # page.find :select, 'article[category]'
227
+ # page.find :select, 'Category'
228
+ # page.find :select, 'Category', selected: 'General'
229
+ # page.find :select, with_options: ['General']
230
+ # page.find :select, with_options: ['Other']
231
+ # page.find :select, options: ['General', 'Other']
232
+ # page.find :select, options: ['General'] # => raises Capybara::ElementNotFound
233
+ # ```
234
+ #
235
+ # * **:option** - Find option elements
236
+ # * Locator: Match text of option
237
+ # * Filters:
238
+ # * :disabled (Boolean) - Match disabled option
239
+ # * :selected (Boolean) - Match selected option
240
+ #
241
+ # ```ruby
242
+ # page.html # => '<option value="General" checked></option>
243
+ # <option value="Disabled" disabled></option>
244
+ # <option value="Other"></option>'
245
+ #
246
+ # page.find :option, 'General'
247
+ # page.find :option, 'General', selected: true
248
+ # page.find :option, 'Disabled', disabled: true
249
+ # page.find :option, 'Other', selected: false
250
+ # ```
251
+ #
252
+ # * **:datalist_input** - Find input field with datalist completion
253
+ # * Locator: Matches against the id, {Capybara.configure test_id} attribute, name,
254
+ # placeholder, or associated label text
255
+ # * Filters:
256
+ # * :name (String, Regexp) - Matches the name attribute
257
+ # * :placeholder (String, Regexp) - Matches the placeholder attribute
258
+ # * :disabled (Boolean, :all) - Match disabled field? (Default: false)
259
+ # * :options (Array<String>) - Exact match options
260
+ # * :with_options (Array<String>) - Partial match options
261
+ #
262
+ # ```ruby
263
+ # page.html # => '<label for="ice_cream_flavor">Flavor</label>
264
+ # <input list="ice_cream_flavors" id="ice_cream_flavor" name="ice_cream[flavor]">
265
+ # <datalist id="ice_cream_flavors">
266
+ # <option value="Chocolate"></option>
267
+ # <option value="Strawberry"></option>
268
+ # <option value="Vanilla"></option>
269
+ # </datalist>'
270
+ #
271
+ # page.find :datalist_input, 'ice_cream_flavor'
272
+ # page.find :datalist_input, 'ice_cream[flavor]'
273
+ # page.find :datalist_input, 'Flavor'
274
+ # page.find :datalist_input, with_options: ['Chocolate', 'Strawberry']
275
+ # page.find :datalist_input, options: ['Chocolate', 'Strawberry', 'Vanilla']
276
+ # page.find :datalist_input, options: ['Chocolate'] # => raises Capybara::ElementNotFound
277
+ # ```
278
+ #
279
+ # * **:datalist_option** - Find datalist option
280
+ # * Locator: Match text or value of option
281
+ # * Filters:
282
+ # * :disabled (Boolean) - Match disabled option
283
+ #
284
+ # ```ruby
285
+ # page.html # => '<datalist>
286
+ # <option value="Chocolate"></option>
287
+ # <option value="Strawberry"></option>
288
+ # <option value="Vanilla"></option>
289
+ # <option value="Forbidden" disabled></option>
290
+ # </datalist>'
291
+ #
292
+ # page.find :datalist_option, 'Chocolate'
293
+ # page.find :datalist_option, 'Strawberry'
294
+ # page.find :datalist_option, 'Vanilla'
295
+ # page.find :datalist_option, 'Forbidden', disabled: true
296
+ # ```
297
+ #
298
+ # * **:file_field** - Find file input elements
299
+ # * Locator: Match id, {Capybara.configure test_id} attribute, name, or associated label text
300
+ # * Filters:
301
+ # * :name (String, Regexp) - Matches the name attribute
302
+ # * :disabled (Boolean, :all) - Match disabled field? (Default: false)
303
+ # * :multiple (Boolean) - Match field that accepts multiple values
304
+ #
305
+ # ```ruby
306
+ # page.html # => '<label for="article_banner_image">Banner Image</label>
307
+ # <input type="file" id="article_banner_image" name="article[banner_image]">'
308
+ #
309
+ # page.find :file_field, 'article_banner_image'
310
+ # page.find :file_field, 'article[banner_image]'
311
+ # page.find :file_field, 'Banner Image'
312
+ # page.find :file_field, 'Banner Image', name: 'article[banner_image]'
313
+ # page.find :field, 'Banner Image', type: 'file'
314
+ # ```
315
+ #
316
+ # * **:label** - Find label elements
317
+ # * Locator: Match id, {Capybara.configure test_id}, or text contents
318
+ # * Filters:
319
+ # * :for (Element, String, Regexp) - The element or id of the element associated with the label
320
+ #
321
+ # ```ruby
322
+ # page.html # => '<label for="article_title">Title</label>
323
+ # <input id="article_title" name="article[title]">'
324
+ #
325
+ # page.find :label, 'Title'
326
+ # page.find :label, 'Title', for: 'article_title'
327
+ # page.find :label, 'Title', for: page.find('article[title]')
328
+ # ```
329
+ #
330
+ # * **:table** - Find table elements
331
+ # * Locator: id, {Capybara.configure test_id}, or caption text of table
332
+ # * Filters:
333
+ # * :caption (String) - Match text of associated caption
334
+ # * :with_rows (Array<Array<String>>, Array<Hash<String, String>>) - Partial match `<td>` data - visibility of `<td>` elements is not considered
335
+ # * :rows (Array<Array<String>>) - Match all `<td>`s - visibility of `<td>` elements is not considered
336
+ # * :with_cols (Array<Array<String>>, Array<Hash<String, String>>) - Partial match `<td>` data - visibility of `<td>` elements is not considered
337
+ # * :cols (Array<Array<String>>) - Match all `<td>`s - visibility of `<td>` elements is not considered
338
+ #
339
+ # ```ruby
340
+ # page.html # => '<table>
341
+ # <caption>A table</caption>
342
+ # <tr>
343
+ # <th>A</th>
344
+ # <th>B</th>
345
+ # </tr>
346
+ # <tr>
347
+ # <td>1</td>
348
+ # <td>2</td>
349
+ # </tr>
350
+ # <tr>
351
+ # <td>3</td>
352
+ # <td>4</td>
353
+ # </tr>
354
+ # </table>'
355
+ #
356
+ # page.find :table, 'A table'
357
+ # page.find :table, with_rows: [
358
+ # { 'A' => '1', 'B' => '2' },
359
+ # { 'A' => '3', 'B' => '4' },
360
+ # ]
361
+ # page.find :table, with_rows: [
362
+ # ['1', '2'],
363
+ # ['3', '4'],
364
+ # ]
365
+ # page.find :table, rows: [
366
+ # { 'A' => '1', 'B' => '2' },
367
+ # { 'A' => '3', 'B' => '4' },
368
+ # ]
369
+ # page.find :table, rows: [
370
+ # ['1', '2'],
371
+ # ['3', '4'],
372
+ # ]
373
+ # page.find :table, rows: [ ['1', '2'] ] # => raises Capybara::ElementNotFound
374
+ # ```
375
+ #
376
+ # * **:table_row** - Find table row
377
+ # * Locator: Array<String>, Hash<String, String> table row `<td>` contents - visibility of `<td>` elements is not considered
378
+ #
379
+ # ```ruby
380
+ # page.html # => '<table>
381
+ # <tr>
382
+ # <th>A</th>
383
+ # <th>B</th>
384
+ # </tr>
385
+ # <tr>
386
+ # <td>1</td>
387
+ # <td>2</td>
388
+ # </tr>
389
+ # <tr>
390
+ # <td>3</td>
391
+ # <td>4</td>
392
+ # </tr>
393
+ # </table>'
394
+ #
395
+ # page.find :table_row, 'A' => '1', 'B' => '2'
396
+ # page.find :table_row, 'A' => '3', 'B' => '4'
397
+ # ```
398
+ #
399
+ # * **:frame** - Find frame/iframe elements
400
+ # * Locator: Match id, {Capybara.configure test_id} attribute, or name
401
+ # * Filters:
402
+ # * :name (String) - Match name attribute
403
+ #
404
+ # ```ruby
405
+ # page.html # => '<iframe id="embed_frame" name="embed" src="https://example.com/embed"></iframe>'
406
+ #
407
+ # page.find :frame, 'embed_frame'
408
+ # page.find :frame, 'embed'
409
+ # page.find :frame, name: 'embed'
410
+ # ```
411
+ #
412
+ # * **:element**
413
+ # * Locator: Type of element ('div', 'a', etc) - if not specified defaults to '*'
414
+ # * Filters:
415
+ # * :\<any> (String, Regexp) - Match on any specified element attribute
416
+ #
417
+ # ```ruby
418
+ # page.html # => '<button type="button" role="menuitemcheckbox" aria-checked="true">Check me</button>
419
+ #
420
+ # page.find :element, 'button'
421
+ # page.find :element, type: 'button', text: 'Check me'
422
+ # page.find :element, role: 'menuitemcheckbox'
423
+ # page.find :element, role: /checkbox/, 'aria-checked': 'true'
424
+ # ```
425
+ #
426
+ class Capybara::Selector; end # rubocop:disable Lint/EmptyClass
427
+
4
428
  Capybara::Selector::FilterSet.add(:_field) do
5
429
  node_filter(:checked, :boolean) { |node, value| !(value ^ node.checked?) }
6
430
  node_filter(:unchecked, :boolean) { |node, value| (value ^ node.checked?) }
7
431
  node_filter(:disabled, :boolean, default: false, skip_if: :all) { |node, value| !(value ^ node.disabled?) }
8
- node_filter(:multiple, :boolean) { |node, value| !(value ^ node.multiple?) }
9
-
10
- expression_filter(:name) { |xpath, val| xpath[XPath.attr(:name) == val] }
11
- expression_filter(:placeholder) { |xpath, val| xpath[XPath.attr(:placeholder) == val] }
12
-
13
- describe(:node_filters) do |checked: nil, unchecked: nil, disabled: nil, multiple: nil, **|
14
- desc, states = +'', []
15
- states << 'checked' if checked || (unchecked == false)
16
- states << 'not checked' if unchecked || (checked == false)
17
- states << 'disabled' if disabled == true
18
- states << 'not disabled' if disabled == false
19
- desc << " that is #{states.join(' and ')}" unless states.empty?
20
- desc << ' with the multiple attribute' if multiple == true
21
- desc << ' without the multiple attribute' if multiple == false
22
- desc
23
- end
24
- end
25
-
26
- # rubocop:disable Metrics/BlockLength
27
-
28
- Capybara.add_selector(:xpath, locator_type: [:to_xpath, String], raw_locator: true) do
29
- xpath { |xpath| xpath }
30
- end
31
-
32
- Capybara.add_selector(:css, locator_type: [String, Symbol], raw_locator: true) do
33
- css { |css| css }
34
- end
35
-
36
- Capybara.add_selector(:id, locator_type: [String, Symbol, Regexp]) do
37
- xpath { |id| builder(XPath.descendant).add_attribute_conditions(id: id) }
38
- locator_filter { |node, id| id.is_a?(Regexp) ? node[:id] =~ id : true }
39
- end
40
-
41
- Capybara.add_selector(:field, locator_type: [String, Symbol]) do
42
- visible { |options| :hidden if options[:type].to_s == 'hidden' }
43
- xpath do |locator, **options|
44
- invalid_types = %w[submit image]
45
- invalid_types << 'hidden' unless options[:type].to_s == 'hidden'
46
- xpath = XPath.descendant(:input, :textarea, :select)[!XPath.attr(:type).one_of(*invalid_types)]
47
- locate_field(xpath, locator, options)
48
- end
49
-
50
- expression_filter(:type) do |expr, type|
51
- type = type.to_s
52
- if %w[textarea select].include?(type)
53
- expr.self(type.to_sym)
54
- else
55
- expr[XPath.attr(:type) == type]
56
- end
57
- end
58
-
59
- filter_set(:_field) # checked/unchecked/disabled/multiple/name/placeholder
60
-
61
- node_filter(:readonly, :boolean) { |node, value| !(value ^ node.readonly?) }
62
- node_filter(:with) do |node, with|
63
- val = node.value
64
- (with.is_a?(Regexp) ? val =~ with : val == with.to_s).tap do |res|
65
- add_error("Expected value to be #{with.inspect} but was #{val.inspect}") unless res
432
+ node_filter(:valid, :boolean) { |node, value| node.evaluate_script('this.validity.valid') == value }
433
+ node_filter(:name) { |node, value| !value.is_a?(Regexp) || value.match?(node[:name]) }
434
+ node_filter(:placeholder) { |node, value| !value.is_a?(Regexp) || value.match?(node[:placeholder]) }
435
+ node_filter(:validation_message) do |node, msg|
436
+ vm = node[:validationMessage]
437
+ (msg.is_a?(Regexp) ? msg.match?(vm) : vm == msg.to_s).tap do |res|
438
+ add_error("Expected validation message to be #{msg.inspect} but was #{vm}") unless res
66
439
  end
67
440
  end
68
441
 
69
- describe_expression_filters do |type: nil, **options|
70
- desc = +''
71
- (expression_filters.keys & options.keys).each { |ef| desc << " with #{ef} #{options[ef]}" }
72
- desc << " of type #{type.inspect}" if type
73
- desc
442
+ expression_filter(:name) do |xpath, val|
443
+ builder(xpath).add_attribute_conditions(name: val)
74
444
  end
75
-
76
- describe_node_filters do |**options|
77
- " with value #{options[:with].to_s.inspect}" if options.key?(:with)
445
+ expression_filter(:placeholder) do |xpath, val|
446
+ builder(xpath).add_attribute_conditions(placeholder: val)
78
447
  end
79
- end
80
-
81
- Capybara.add_selector(:fieldset, locator_type: [String, Symbol]) do
82
- xpath do |locator, legend: nil, **|
83
- locator_matchers = (XPath.attr(:id) == locator.to_s) | XPath.child(:legend)[XPath.string.n.is(locator.to_s)]
84
- locator_matchers |= XPath.attr(test_id) == locator.to_s if test_id
85
- xpath = XPath.descendant(:fieldset)[locator && locator_matchers]
86
- xpath = xpath[XPath.child(:legend)[XPath.string.n.is(legend)]] if legend
87
- xpath
88
- end
89
-
90
- node_filter(:disabled, :boolean) { |node, value| !(value ^ node.disabled?) }
91
- end
92
-
93
- Capybara.add_selector(:link, locator_type: [String, Symbol]) do
94
- xpath do |locator, href: true, alt: nil, title: nil, **|
95
- xpath = builder(XPath.descendant(:a)).add_attribute_conditions(href: href)
96
-
97
- unless locator.nil?
98
- locator = locator.to_s
99
- matchers = [XPath.attr(:id) == locator,
100
- XPath.string.n.is(locator),
101
- XPath.attr(:title).is(locator),
102
- XPath.descendant(:img)[XPath.attr(:alt).is(locator)]]
103
- matchers << XPath.attr(:'aria-label').is(locator) if enable_aria_label
104
- matchers << XPath.attr(test_id) == locator if test_id
105
- xpath = xpath[matchers.reduce(:|)]
106
- end
448
+ expression_filter(:disabled) { |xpath, val| val ? xpath : xpath[~XPath.attr(:disabled)] }
449
+ expression_filter(:multiple) { |xpath, val| xpath[val ? XPath.attr(:multiple) : ~XPath.attr(:multiple)] }
107
450
 
108
- xpath = xpath[find_by_attr(:title, title)]
109
- xpath = xpath[XPath.descendant(:img)[XPath.attr(:alt) == alt]] if alt
110
- xpath
111
- end
112
-
113
- node_filter(:href) do |node, href|
114
- # If not a Regexp it's been handled in the main XPath
115
- (href.is_a?(Regexp) ? node[:href].match(href) : true).tap do |res|
116
- add_error "Expected href to match #{href.inspect} but it was #{node[:href].inspect}" unless res
117
- end
118
- end
119
-
120
- expression_filter(:download, valid_values: [true, false, String]) do |expr, download|
121
- builder(expr).add_attribute_conditions(download: download)
122
- end
123
-
124
- describe_expression_filters do |**options|
451
+ describe(:expression_filters) do |name: nil, placeholder: nil, disabled: nil, multiple: nil, **|
125
452
  desc = +''
126
- if (href = options[:href])
127
- desc << " with href #{'matching ' if href.is_a? Regexp}#{href.inspect}"
128
- elsif options.key?(:href) # is nil/false specified?
129
- desc << ' with no href attribute'
130
- end
131
- desc
132
- end
133
- end
134
-
135
- Capybara.add_selector(:button, locator_type: [String, Symbol]) do
136
- xpath(:value, :title, :type) do |locator, **options|
137
- input_btn_xpath = XPath.descendant(:input)[XPath.attr(:type).one_of('submit', 'reset', 'image', 'button')]
138
- btn_xpath = XPath.descendant(:button)
139
- image_btn_xpath = XPath.descendant(:input)[XPath.attr(:type) == 'image']
140
-
141
- unless locator.nil?
142
- locator = locator.to_s
143
- locator_matchers = XPath.attr(:id).equals(locator) | XPath.attr(:value).is(locator) | XPath.attr(:title).is(locator)
144
- locator_matchers |= XPath.attr(:'aria-label').is(locator) if enable_aria_label
145
- locator_matchers |= XPath.attr(test_id) == locator if test_id
146
-
147
- input_btn_xpath = input_btn_xpath[locator_matchers]
148
-
149
- btn_xpath = btn_xpath[locator_matchers | XPath.string.n.is(locator) | XPath.descendant(:img)[XPath.attr(:alt).is(locator)]]
150
-
151
- alt_matches = XPath.attr(:alt).is(locator)
152
- alt_matches |= XPath.attr(:'aria-label').is(locator) if enable_aria_label
153
- image_btn_xpath = image_btn_xpath[alt_matches]
154
- end
155
-
156
- %i[value title type].inject(input_btn_xpath.union(btn_xpath).union(image_btn_xpath)) do |memo, ef|
157
- memo[find_by_attr(ef, options[ef])]
158
- end
159
- end
160
-
161
- node_filter(:disabled, :boolean, default: false, skip_if: :all) { |node, value| !(value ^ node.disabled?) }
162
-
163
- describe_expression_filters
164
- describe_node_filters do |disabled: nil, **|
165
- ' that is disabled' if disabled == true
166
- end
167
- end
168
-
169
- Capybara.add_selector(:link_or_button, locator_type: [String, Symbol]) do
170
- label 'link or button'
171
- xpath do |locator, **options|
172
- self.class.all.values_at(:link, :button).map do |selector|
173
- instance_exec(locator, options, &selector.xpath)
174
- end.reduce(:union)
175
- end
176
-
177
- node_filter(:disabled, :boolean, default: false, skip_if: :all) { |node, value| node.tag_name == 'a' || !(value ^ node.disabled?) }
178
-
179
- describe_node_filters do |disabled: nil, **|
180
- ' that is disabled' if disabled == true
181
- end
182
- end
183
-
184
- Capybara.add_selector(:fillable_field, locator_type: [String, Symbol]) do
185
- label 'field'
186
- xpath do |locator, allow_self: nil, **options|
187
- xpath = XPath.axis(allow_self ? :"descendant-or-self" : :descendant, :input, :textarea)[
188
- !XPath.attr(:type).one_of('submit', 'image', 'radio', 'checkbox', 'hidden', 'file')
189
- ]
190
- locate_field(xpath, locator, options)
191
- end
192
-
193
- expression_filter(:type) do |expr, type|
194
- type = type.to_s
195
- if type == 'textarea'
196
- expr.self(type.to_sym)
197
- else
198
- expr[XPath.attr(:type) == type]
199
- end
200
- end
201
-
202
- filter_set(:_field, %i[disabled multiple name placeholder])
203
-
204
- node_filter(:with) do |node, with|
205
- val = node.value
206
- (with.is_a?(Regexp) ? val =~ with : val == with.to_s).tap do |res|
207
- add_error("Expected value to be #{with.inspect} but was #{val.inspect}") unless res
208
- end
209
- end
210
-
211
- describe_expression_filters
212
- describe_node_filters do |**options|
213
- " with value #{options[:with].to_s.inspect}" if options.key?(:with)
214
- end
215
- end
216
-
217
- Capybara.add_selector(:radio_button, locator_type: [String, Symbol]) do
218
- label 'radio button'
219
- xpath do |locator, allow_self: nil, **options|
220
- xpath = XPath.axis(allow_self ? :"descendant-or-self" : :descendant, :input)[
221
- XPath.attr(:type) == 'radio'
222
- ]
223
- locate_field(xpath, locator, options)
224
- end
225
-
226
- filter_set(:_field, %i[checked unchecked disabled name])
227
-
228
- node_filter(:option) do |node, value|
229
- val = node.value
230
- (val == value.to_s).tap do |res|
231
- add_error("Expected option value to be #{value.inspect} but it was #{val.inspect}") unless res
232
- end
233
- end
234
-
235
- describe_expression_filters
236
- describe_node_filters do |option: nil, **|
237
- " with value #{option.inspect}" if option
238
- end
239
- end
240
-
241
- Capybara.add_selector(:checkbox, locator_type: [String, Symbol]) do
242
- xpath do |locator, allow_self: nil, **options|
243
- xpath = XPath.axis(allow_self ? :"descendant-or-self" : :descendant, :input)[
244
- XPath.attr(:type) == 'checkbox'
245
- ]
246
- locate_field(xpath, locator, options)
247
- end
248
-
249
- filter_set(:_field, %i[checked unchecked disabled name])
250
-
251
- node_filter(:option) do |node, value|
252
- val = node.value
253
- (val == value.to_s).tap do |res|
254
- add_error("Expected option value to be #{value.inspect} but it was #{val.inspect}") unless res
255
- end
256
- end
257
-
258
- describe_expression_filters
259
- describe_node_filters do |option: nil, **|
260
- " with value #{option.inspect}" if option
261
- end
262
- end
263
-
264
- Capybara.add_selector(:select, locator_type: [String, Symbol]) do
265
- label 'select box'
266
-
267
- xpath do |locator, **options|
268
- xpath = XPath.descendant(:select)
269
- locate_field(xpath, locator, options)
270
- end
271
-
272
- filter_set(:_field, %i[disabled multiple name placeholder])
273
-
274
- node_filter(:options) do |node, options|
275
- actual = if node.visible?
276
- node.all(:xpath, './/option', wait: false).map(&:text)
277
- else
278
- node.all(:xpath, './/option', visible: false, wait: false).map { |option| option.text(:all) }
279
- end
280
- (options.sort == actual.sort).tap do |res|
281
- add_error("Expected options #{options.inspect} found #{actual.inspect}") unless res
282
- end
283
- end
284
-
285
- expression_filter(:with_options) do |expr, options|
286
- options.inject(expr) do |xpath, option|
287
- xpath[self.class.all[:option].call(option)]
288
- end
289
- end
290
-
291
- node_filter(:selected) do |node, selected|
292
- actual = node.all(:xpath, './/option', visible: false, wait: false).select(&:selected?).map { |option| option.text(:all) }
293
- (Array(selected).sort == actual.sort).tap do |res|
294
- add_error("Expected #{selected.inspect} to be selected found #{actual.inspect}") unless res
295
- end
296
- end
297
-
298
- node_filter(:with_selected) do |node, selected|
299
- actual = node.all(:xpath, './/option', visible: false, wait: false).select(&:selected?).map { |option| option.text(:all) }
300
- (Array(selected) - actual).empty?.tap do |res|
301
- add_error("Expected at least #{selected.inspect} to be selected found #{actual.inspect}") unless res
302
- end
303
- end
304
-
305
- describe_expression_filters do |with_options: nil, **opts|
306
- desc = +''
307
- desc << " with at least options #{with_options.inspect}" if with_options
308
- desc << describe_all_expression_filters(opts)
309
- desc
310
- end
311
-
312
- describe_node_filters do |options: nil, selected: nil, with_selected: nil, **|
313
- desc = +''
314
- desc << " with options #{options.inspect}" if options
315
- desc << " with #{selected.inspect} selected" if selected
316
- desc << " with at least #{with_selected.inspect} selected" if with_selected
317
- desc
318
- end
319
- end
320
-
321
- Capybara.add_selector(:datalist_input, locator_type: [String, Symbol]) do
322
- label 'input box with datalist completion'
323
-
324
- xpath do |locator, **options|
325
- xpath = XPath.descendant(:input)[XPath.attr(:list)]
326
- locate_field(xpath, locator, options)
327
- end
328
-
329
- filter_set(:_field, %i[disabled name placeholder])
330
-
331
- node_filter(:options) do |node, options|
332
- actual = node.find("//datalist[@id=#{node[:list]}]", visible: :all).all(:datalist_option, wait: false).map(&:value)
333
- (options.sort == actual.sort).tap do |res|
334
- add_error("Expected #{options.inspect} options found #{actual.inspect}") unless res
335
- end
336
- end
337
-
338
- expression_filter(:with_options) do |expr, options|
339
- options.inject(expr) do |xpath, option|
340
- xpath[XPath.attr(:list) == XPath.anywhere(:datalist)[self.class.all[:datalist_option].call(option)].attr(:id)]
341
- end
342
- end
343
-
344
- describe_expression_filters do |with_options: nil, **opts|
345
- desc = +''
346
- desc << " with at least options #{with_options.inspect}" if with_options
347
- desc << describe_all_expression_filters(opts)
453
+ desc << ' that is not disabled' if disabled == false
454
+ desc << " with name #{name}" if name
455
+ desc << " with placeholder #{placeholder}" if placeholder
456
+ desc << ' with the multiple attribute' if multiple == true
457
+ desc << ' without the multiple attribute' if multiple == false
348
458
  desc
349
459
  end
350
460
 
351
- describe_node_filters do |options: nil, **|
352
- " with options #{options.inspect}" if options
353
- end
354
- end
355
-
356
- Capybara.add_selector(:option, locator_type: [String, Symbol]) do
357
- xpath do |locator|
358
- xpath = XPath.descendant(:option)
359
- xpath = xpath[XPath.string.n.is(locator.to_s)] unless locator.nil?
360
- xpath
361
- end
362
-
363
- node_filter(:disabled, :boolean) { |node, value| !(value ^ node.disabled?) }
364
- node_filter(:selected, :boolean) { |node, value| !(value ^ node.selected?) }
365
-
366
- describe_node_filters do |**options|
367
- desc = +''
368
- desc << " that is#{' not' unless options[:disabled]} disabled" if options.key?(:disabled)
369
- desc << " that is#{' not' unless options[:selected]} selected" if options.key?(:selected)
461
+ describe(:node_filters) do |checked: nil, unchecked: nil, disabled: nil, valid: nil, validation_message: nil, **|
462
+ desc, states = +'', []
463
+ states << 'checked' if checked || (unchecked == false)
464
+ states << 'not checked' if unchecked || (checked == false)
465
+ states << 'disabled' if disabled == true
466
+ desc << " that is #{states.join(' and ')}" unless states.empty?
467
+ desc << ' that is valid' if valid == true
468
+ desc << ' that is invalid' if valid == false
469
+ desc << " with validation message #{validation_message.to_s.inspect}" if validation_message
370
470
  desc
371
471
  end
372
472
  end
373
473
 
374
- Capybara.add_selector(:datalist_option, locator_type: [String, Symbol]) do
375
- label 'datalist option'
376
- visible(:all)
377
-
378
- xpath do |locator|
379
- xpath = XPath.descendant(:option)
380
- xpath = xpath[XPath.string.n.is(locator.to_s) | (XPath.attr(:value) == locator.to_s)] unless locator.nil?
381
- xpath
382
- end
383
-
384
- node_filter(:disabled, :boolean) { |node, value| !(value ^ node.disabled?) }
385
-
386
- describe_node_filters do |**options|
387
- " that is#{' not' unless options[:disabled]} disabled" if options.key?(:disabled)
388
- end
389
- end
390
-
391
- Capybara.add_selector(:file_field, locator_type: [String, Symbol]) do
392
- label 'file field'
393
- xpath do |locator, allow_self: nil, **options|
394
- xpath = XPath.axis(allow_self ? :"descendant-or-self" : :descendant, :input)[
395
- XPath.attr(:type) == 'file'
396
- ]
397
- locate_field(xpath, locator, options)
398
- end
399
-
400
- filter_set(:_field, %i[disabled multiple name])
401
-
402
- describe_expression_filters
403
- end
404
-
405
- Capybara.add_selector(:label, locator_type: [String, Symbol]) do
406
- label 'label'
407
- xpath(:for) do |locator, options|
408
- xpath = XPath.descendant(:label)
409
- unless locator.nil?
410
- locator_matchers = XPath.string.n.is(locator.to_s) | (XPath.attr(:id) == locator.to_s)
411
- locator_matchers |= XPath.attr(test_id) == locator if test_id
412
- xpath = xpath[locator_matchers]
413
- end
414
- if options.key?(:for)
415
- if (for_option = options[:for].is_a?(Capybara::Node::Element) ? options[:for][:id] : options[:for])
416
- with_attr = XPath.attr(:for) == for_option.to_s
417
- labelable_elements = %i[button input keygen meter output progress select textarea]
418
- wrapped = !XPath.attr(:for) &
419
- XPath.descendant(*labelable_elements)[XPath.attr(:id) == for_option.to_s]
420
- xpath = xpath[with_attr | wrapped]
421
- end
422
- end
423
- xpath
424
- end
425
-
426
- node_filter(:for) do |node, field_or_value|
427
- # Non element values were handled through the expression filter
428
- next true unless field_or_value.is_a? Capybara::Node::Element
429
-
430
- if (for_val = node[:for])
431
- field_or_value[:id] == for_val
432
- else
433
- field_or_value.find_xpath('./ancestor::label[1]').include? node.base
434
- end
435
- end
436
-
437
- describe_expression_filters do |**options|
438
- " for element with id of \"#{options[:for]}\"" if options.key?(:for) && !options[:for].is_a?(Capybara::Node::Element)
439
- end
440
- describe_node_filters do |**options|
441
- " for element #{options[:for]}" if options[:for]&.is_a?(Capybara::Node::Element)
442
- end
443
- end
444
-
445
- Capybara.add_selector(:table, locator_type: [String, Symbol]) do
446
- xpath do |locator, caption: nil, **|
447
- xpath = XPath.descendant(:table)
448
- unless locator.nil?
449
- locator_matchers = (XPath.attr(:id) == locator.to_s) | XPath.descendant(:caption).is(locator.to_s)
450
- locator_matchers |= XPath.attr(test_id) == locator if test_id
451
- xpath = xpath[locator_matchers]
452
- end
453
- xpath = xpath[XPath.descendant(:caption) == caption] if caption
454
- xpath
455
- end
456
-
457
- describe_expression_filters do |caption: nil, **|
458
- " with caption \"#{caption}\"" if caption
459
- end
460
- end
461
-
462
- Capybara.add_selector(:frame, locator_type: [String, Symbol]) do
463
- xpath do |locator, name: nil, **|
464
- xpath = XPath.descendant(:iframe).union(XPath.descendant(:frame))
465
- unless locator.nil?
466
- locator_matchers = (XPath.attr(:id) == locator.to_s) | (XPath.attr(:name) == locator.to_s)
467
- locator_matchers |= XPath.attr(test_id) == locator if test_id
468
- xpath = xpath[locator_matchers]
469
- end
470
- xpath[find_by_attr(:name, name)]
471
- end
472
-
473
- describe_expression_filters do |name: nil, **|
474
- " with name #{name}" if name
475
- end
476
- end
477
-
478
- Capybara.add_selector(:element, locator_type: [String, Symbol]) do
479
- xpath do |locator, **|
480
- XPath.descendant.where(locator ? XPath.local_name == locator.to_s : nil)
481
- end
482
-
483
- expression_filter(:attributes, matcher: /.+/) do |xpath, name, val|
484
- builder(xpath).add_attribute_conditions(name => val)
485
- end
486
-
487
- node_filter(:attributes, matcher: /.+/) do |node, name, val|
488
- next true unless val.is_a?(Regexp)
489
-
490
- (node[name] =~ val).tap do |res|
491
- add_error("Expected #{name} to match #{val.inspect} but it was #{node[name]}") unless res
492
- end
493
- end
494
-
495
- describe_expression_filters do |**options|
496
- booleans, values = options.partition { |_k, v| [true, false].include? v }.map(&:to_h)
497
- desc = describe_all_expression_filters(values)
498
- desc + booleans.map do |k, v|
499
- v ? " with #{k} attribute" : "without #{k} attribute"
500
- end.join
501
- end
502
- end
503
- # rubocop:enable Metrics/BlockLength
474
+ require 'capybara/selector/definition/xpath'
475
+ require 'capybara/selector/definition/css'
476
+ require 'capybara/selector/definition/id'
477
+ require 'capybara/selector/definition/field'
478
+ require 'capybara/selector/definition/fieldset'
479
+ require 'capybara/selector/definition/link'
480
+ require 'capybara/selector/definition/button'
481
+ require 'capybara/selector/definition/link_or_button'
482
+ require 'capybara/selector/definition/fillable_field'
483
+ require 'capybara/selector/definition/radio_button'
484
+ require 'capybara/selector/definition/checkbox'
485
+ require 'capybara/selector/definition/select'
486
+ require 'capybara/selector/definition/datalist_input'
487
+ require 'capybara/selector/definition/option'
488
+ require 'capybara/selector/definition/datalist_option'
489
+ require 'capybara/selector/definition/file_field'
490
+ require 'capybara/selector/definition/label'
491
+ require 'capybara/selector/definition/table'
492
+ require 'capybara/selector/definition/table_row'
493
+ require 'capybara/selector/definition/frame'
494
+ require 'capybara/selector/definition/element'