capybara 3.8.1 → 3.33.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (242) hide show
  1. checksums.yaml +4 -4
  2. data/.yardopts +1 -0
  3. data/History.md +465 -0
  4. data/License.txt +1 -1
  5. data/README.md +58 -57
  6. data/lib/capybara/config.rb +10 -4
  7. data/lib/capybara/cucumber.rb +1 -1
  8. data/lib/capybara/driver/base.rb +2 -2
  9. data/lib/capybara/driver/node.rb +26 -5
  10. data/lib/capybara/dsl.rb +12 -4
  11. data/lib/capybara/helpers.rb +8 -4
  12. data/lib/capybara/minitest/spec.rb +162 -85
  13. data/lib/capybara/minitest.rb +248 -148
  14. data/lib/capybara/node/actions.rb +149 -96
  15. data/lib/capybara/node/base.rb +27 -10
  16. data/lib/capybara/node/document.rb +12 -0
  17. data/lib/capybara/node/document_matchers.rb +9 -5
  18. data/lib/capybara/node/element.rb +254 -109
  19. data/lib/capybara/node/finders.rb +83 -76
  20. data/lib/capybara/node/matchers.rb +279 -141
  21. data/lib/capybara/node/simple.rb +25 -6
  22. data/lib/capybara/queries/ancestor_query.rb +5 -7
  23. data/lib/capybara/queries/base_query.rb +11 -5
  24. data/lib/capybara/queries/current_path_query.rb +3 -3
  25. data/lib/capybara/queries/match_query.rb +1 -0
  26. data/lib/capybara/queries/selector_query.rb +467 -103
  27. data/lib/capybara/queries/sibling_query.rb +5 -4
  28. data/lib/capybara/queries/style_query.rb +6 -2
  29. data/lib/capybara/queries/text_query.rb +17 -3
  30. data/lib/capybara/queries/title_query.rb +2 -2
  31. data/lib/capybara/rack_test/browser.rb +22 -15
  32. data/lib/capybara/rack_test/driver.rb +10 -1
  33. data/lib/capybara/rack_test/errors.rb +6 -0
  34. data/lib/capybara/rack_test/form.rb +33 -28
  35. data/lib/capybara/rack_test/node.rb +74 -6
  36. data/lib/capybara/registration_container.rb +44 -0
  37. data/lib/capybara/registrations/drivers.rb +36 -0
  38. data/lib/capybara/registrations/patches/puma_ssl.rb +27 -0
  39. data/lib/capybara/registrations/servers.rb +44 -0
  40. data/lib/capybara/result.rb +55 -23
  41. data/lib/capybara/rspec/features.rb +4 -4
  42. data/lib/capybara/rspec/matcher_proxies.rb +36 -15
  43. data/lib/capybara/rspec/matchers/base.rb +111 -0
  44. data/lib/capybara/rspec/matchers/become_closed.rb +33 -0
  45. data/lib/capybara/rspec/matchers/compound.rb +88 -0
  46. data/lib/capybara/rspec/matchers/count_sugar.rb +37 -0
  47. data/lib/capybara/rspec/matchers/have_ancestor.rb +28 -0
  48. data/lib/capybara/rspec/matchers/have_current_path.rb +29 -0
  49. data/lib/capybara/rspec/matchers/have_selector.rb +77 -0
  50. data/lib/capybara/rspec/matchers/have_sibling.rb +27 -0
  51. data/lib/capybara/rspec/matchers/have_text.rb +33 -0
  52. data/lib/capybara/rspec/matchers/have_title.rb +29 -0
  53. data/lib/capybara/rspec/matchers/match_selector.rb +27 -0
  54. data/lib/capybara/rspec/matchers/match_style.rb +38 -0
  55. data/lib/capybara/rspec/matchers/spatial_sugar.rb +39 -0
  56. data/lib/capybara/rspec/matchers.rb +117 -311
  57. data/lib/capybara/selector/builders/css_builder.rb +84 -0
  58. data/lib/capybara/selector/builders/xpath_builder.rb +69 -0
  59. data/lib/capybara/selector/css.rb +17 -15
  60. data/lib/capybara/selector/definition/button.rb +52 -0
  61. data/lib/capybara/selector/definition/checkbox.rb +26 -0
  62. data/lib/capybara/selector/definition/css.rb +10 -0
  63. data/lib/capybara/selector/definition/datalist_input.rb +35 -0
  64. data/lib/capybara/selector/definition/datalist_option.rb +25 -0
  65. data/lib/capybara/selector/definition/element.rb +27 -0
  66. data/lib/capybara/selector/definition/field.rb +40 -0
  67. data/lib/capybara/selector/definition/fieldset.rb +14 -0
  68. data/lib/capybara/selector/definition/file_field.rb +13 -0
  69. data/lib/capybara/selector/definition/fillable_field.rb +33 -0
  70. data/lib/capybara/selector/definition/frame.rb +17 -0
  71. data/lib/capybara/selector/definition/id.rb +6 -0
  72. data/lib/capybara/selector/definition/label.rb +62 -0
  73. data/lib/capybara/selector/definition/link.rb +54 -0
  74. data/lib/capybara/selector/definition/link_or_button.rb +16 -0
  75. data/lib/capybara/selector/definition/option.rb +27 -0
  76. data/lib/capybara/selector/definition/radio_button.rb +27 -0
  77. data/lib/capybara/selector/definition/select.rb +81 -0
  78. data/lib/capybara/selector/definition/table.rb +109 -0
  79. data/lib/capybara/selector/definition/table_row.rb +21 -0
  80. data/lib/capybara/selector/definition/xpath.rb +5 -0
  81. data/lib/capybara/selector/definition.rb +277 -0
  82. data/lib/capybara/selector/filter.rb +1 -0
  83. data/lib/capybara/selector/filter_set.rb +26 -19
  84. data/lib/capybara/selector/filters/base.rb +24 -5
  85. data/lib/capybara/selector/filters/expression_filter.rb +3 -3
  86. data/lib/capybara/selector/filters/locator_filter.rb +29 -0
  87. data/lib/capybara/selector/filters/node_filter.rb +16 -2
  88. data/lib/capybara/selector/regexp_disassembler.rb +214 -0
  89. data/lib/capybara/selector/selector.rb +73 -367
  90. data/lib/capybara/selector/xpath_extensions.rb +17 -0
  91. data/lib/capybara/selector.rb +221 -480
  92. data/lib/capybara/selenium/atoms/getAttribute.min.js +1 -0
  93. data/lib/capybara/selenium/atoms/isDisplayed.min.js +1 -0
  94. data/lib/capybara/selenium/atoms/src/getAttribute.js +161 -0
  95. data/lib/capybara/selenium/atoms/src/isDisplayed.js +454 -0
  96. data/lib/capybara/selenium/driver.rb +203 -86
  97. data/lib/capybara/selenium/driver_specializations/chrome_driver.rb +88 -14
  98. data/lib/capybara/selenium/driver_specializations/edge_driver.rb +124 -0
  99. data/lib/capybara/selenium/driver_specializations/firefox_driver.rb +89 -0
  100. data/lib/capybara/selenium/driver_specializations/internet_explorer_driver.rb +26 -0
  101. data/lib/capybara/selenium/driver_specializations/safari_driver.rb +24 -0
  102. data/lib/capybara/selenium/extensions/file_input_click_emulation.rb +34 -0
  103. data/lib/capybara/selenium/extensions/find.rb +110 -0
  104. data/lib/capybara/selenium/extensions/html5_drag.rb +191 -22
  105. data/lib/capybara/selenium/extensions/modifier_keys_stack.rb +28 -0
  106. data/lib/capybara/selenium/extensions/scroll.rb +78 -0
  107. data/lib/capybara/selenium/logger_suppressor.rb +34 -0
  108. data/lib/capybara/selenium/node.rb +298 -93
  109. data/lib/capybara/selenium/nodes/chrome_node.rb +100 -8
  110. data/lib/capybara/selenium/nodes/edge_node.rb +104 -0
  111. data/lib/capybara/selenium/nodes/firefox_node.rb +131 -0
  112. data/lib/capybara/selenium/nodes/ie_node.rb +22 -0
  113. data/lib/capybara/selenium/nodes/safari_node.rb +118 -0
  114. data/lib/capybara/selenium/patches/action_pauser.rb +26 -0
  115. data/lib/capybara/selenium/patches/atoms.rb +18 -0
  116. data/lib/capybara/selenium/patches/is_displayed.rb +16 -0
  117. data/lib/capybara/selenium/patches/logs.rb +45 -0
  118. data/lib/capybara/selenium/patches/pause_duration_fix.rb +1 -3
  119. data/lib/capybara/selenium/patches/persistent_client.rb +20 -0
  120. data/lib/capybara/server/animation_disabler.rb +4 -3
  121. data/lib/capybara/server/checker.rb +6 -2
  122. data/lib/capybara/server/middleware.rb +23 -13
  123. data/lib/capybara/server.rb +30 -7
  124. data/lib/capybara/session/config.rb +14 -10
  125. data/lib/capybara/session/matchers.rb +11 -7
  126. data/lib/capybara/session.rb +152 -111
  127. data/lib/capybara/spec/public/offset.js +6 -0
  128. data/lib/capybara/spec/public/test.js +101 -10
  129. data/lib/capybara/spec/session/all_spec.rb +96 -6
  130. data/lib/capybara/spec/session/ancestor_spec.rb +5 -0
  131. data/lib/capybara/spec/session/assert_all_of_selectors_spec.rb +29 -0
  132. data/lib/capybara/spec/session/assert_current_path_spec.rb +5 -2
  133. data/lib/capybara/spec/session/assert_selector_spec.rb +0 -10
  134. data/lib/capybara/spec/session/assert_style_spec.rb +4 -4
  135. data/lib/capybara/spec/session/assert_text_spec.rb +9 -5
  136. data/lib/capybara/spec/session/attach_file_spec.rb +63 -36
  137. data/lib/capybara/spec/session/check_spec.rb +10 -4
  138. data/lib/capybara/spec/session/choose_spec.rb +8 -2
  139. data/lib/capybara/spec/session/click_button_spec.rb +117 -61
  140. data/lib/capybara/spec/session/click_link_or_button_spec.rb +16 -0
  141. data/lib/capybara/spec/session/click_link_spec.rb +17 -6
  142. data/lib/capybara/spec/session/element/matches_selector_spec.rb +40 -39
  143. data/lib/capybara/spec/session/evaluate_script_spec.rb +13 -0
  144. data/lib/capybara/spec/session/execute_script_spec.rb +1 -0
  145. data/lib/capybara/spec/session/fill_in_spec.rb +47 -6
  146. data/lib/capybara/spec/session/find_field_spec.rb +1 -1
  147. data/lib/capybara/spec/session/find_spec.rb +74 -4
  148. data/lib/capybara/spec/session/first_spec.rb +1 -1
  149. data/lib/capybara/spec/session/frame/switch_to_frame_spec.rb +13 -1
  150. data/lib/capybara/spec/session/frame/within_frame_spec.rb +12 -1
  151. data/lib/capybara/spec/session/has_all_selectors_spec.rb +1 -1
  152. data/lib/capybara/spec/session/has_ancestor_spec.rb +46 -0
  153. data/lib/capybara/spec/session/has_any_selectors_spec.rb +25 -0
  154. data/lib/capybara/spec/session/has_button_spec.rb +16 -0
  155. data/lib/capybara/spec/session/has_css_spec.rb +122 -12
  156. data/lib/capybara/spec/session/has_current_path_spec.rb +6 -4
  157. data/lib/capybara/spec/session/has_field_spec.rb +55 -0
  158. data/lib/capybara/spec/session/has_select_spec.rb +34 -6
  159. data/lib/capybara/spec/session/has_selector_spec.rb +11 -4
  160. data/lib/capybara/spec/session/has_sibling_spec.rb +50 -0
  161. data/lib/capybara/spec/session/has_table_spec.rb +166 -0
  162. data/lib/capybara/spec/session/has_text_spec.rb +48 -1
  163. data/lib/capybara/spec/session/has_xpath_spec.rb +17 -0
  164. data/lib/capybara/spec/session/html_spec.rb +7 -0
  165. data/lib/capybara/spec/session/matches_style_spec.rb +35 -0
  166. data/lib/capybara/spec/session/node_spec.rb +643 -18
  167. data/lib/capybara/spec/session/node_wrapper_spec.rb +1 -1
  168. data/lib/capybara/spec/session/refresh_spec.rb +4 -0
  169. data/lib/capybara/spec/session/reset_session_spec.rb +23 -8
  170. data/lib/capybara/spec/session/save_and_open_screenshot_spec.rb +2 -2
  171. data/lib/capybara/spec/session/save_screenshot_spec.rb +4 -4
  172. data/lib/capybara/spec/session/scroll_spec.rb +117 -0
  173. data/lib/capybara/spec/session/select_spec.rb +10 -10
  174. data/lib/capybara/spec/session/selectors_spec.rb +36 -5
  175. data/lib/capybara/spec/session/uncheck_spec.rb +2 -2
  176. data/lib/capybara/spec/session/unselect_spec.rb +1 -1
  177. data/lib/capybara/spec/session/window/become_closed_spec.rb +20 -17
  178. data/lib/capybara/spec/session/window/switch_to_window_spec.rb +4 -0
  179. data/lib/capybara/spec/session/window/window_opened_by_spec.rb +4 -0
  180. data/lib/capybara/spec/session/window/window_spec.rb +59 -58
  181. data/lib/capybara/spec/session/window/windows_spec.rb +4 -0
  182. data/lib/capybara/spec/session/within_spec.rb +23 -0
  183. data/lib/capybara/spec/spec_helper.rb +16 -6
  184. data/lib/capybara/spec/test_app.rb +28 -23
  185. data/lib/capybara/spec/views/animated.erb +49 -0
  186. data/lib/capybara/spec/views/form.erb +48 -7
  187. data/lib/capybara/spec/views/frame_child.erb +3 -2
  188. data/lib/capybara/spec/views/frame_one.erb +1 -0
  189. data/lib/capybara/spec/views/obscured.erb +47 -0
  190. data/lib/capybara/spec/views/offset.erb +32 -0
  191. data/lib/capybara/spec/views/react.erb +45 -0
  192. data/lib/capybara/spec/views/scroll.erb +20 -0
  193. data/lib/capybara/spec/views/spatial.erb +31 -0
  194. data/lib/capybara/spec/views/tables.erb +67 -0
  195. data/lib/capybara/spec/views/with_animation.erb +29 -1
  196. data/lib/capybara/spec/views/with_dragula.erb +24 -0
  197. data/lib/capybara/spec/views/with_hover.erb +1 -0
  198. data/lib/capybara/spec/views/with_hover1.erb +10 -0
  199. data/lib/capybara/spec/views/with_html.erb +32 -6
  200. data/lib/capybara/spec/views/with_js.erb +3 -1
  201. data/lib/capybara/spec/views/with_jstree.erb +26 -0
  202. data/lib/capybara/spec/views/with_scope_other.erb +6 -0
  203. data/lib/capybara/spec/views/with_sortable_js.erb +21 -0
  204. data/lib/capybara/version.rb +1 -1
  205. data/lib/capybara/window.rb +11 -11
  206. data/lib/capybara.rb +118 -111
  207. data/spec/basic_node_spec.rb +14 -3
  208. data/spec/capybara_spec.rb +29 -29
  209. data/spec/css_builder_spec.rb +101 -0
  210. data/spec/dsl_spec.rb +46 -21
  211. data/spec/filter_set_spec.rb +5 -5
  212. data/spec/fixtures/selenium_driver_rspec_failure.rb +1 -1
  213. data/spec/fixtures/selenium_driver_rspec_success.rb +1 -1
  214. data/spec/minitest_spec.rb +18 -4
  215. data/spec/minitest_spec_spec.rb +59 -44
  216. data/spec/rack_test_spec.rb +117 -89
  217. data/spec/regexp_dissassembler_spec.rb +250 -0
  218. data/spec/result_spec.rb +51 -49
  219. data/spec/rspec/features_spec.rb +3 -0
  220. data/spec/rspec/shared_spec_matchers.rb +112 -97
  221. data/spec/rspec_spec.rb +35 -17
  222. data/spec/sauce_spec_chrome.rb +43 -0
  223. data/spec/selector_spec.rb +244 -28
  224. data/spec/selenium_spec_chrome.rb +125 -54
  225. data/spec/selenium_spec_chrome_remote.rb +26 -12
  226. data/spec/selenium_spec_edge.rb +23 -8
  227. data/spec/selenium_spec_firefox.rb +208 -0
  228. data/spec/selenium_spec_firefox_remote.rb +15 -18
  229. data/spec/selenium_spec_ie.rb +82 -13
  230. data/spec/selenium_spec_safari.rb +148 -0
  231. data/spec/server_spec.rb +118 -77
  232. data/spec/session_spec.rb +19 -3
  233. data/spec/shared_selenium_node.rb +83 -0
  234. data/spec/shared_selenium_session.rb +110 -65
  235. data/spec/spec_helper.rb +57 -9
  236. data/spec/xpath_builder_spec.rb +93 -0
  237. metadata +257 -17
  238. data/lib/capybara/rspec/compound.rb +0 -94
  239. data/lib/capybara/selenium/driver_specializations/marionette_driver.rb +0 -49
  240. data/lib/capybara/selenium/nodes/marionette_node.rb +0 -121
  241. data/lib/capybara/spec/session/has_style_spec.rb +0 -25
  242. data/spec/selenium_spec_marionette.rb +0 -172
@@ -1,395 +1,201 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- require 'capybara/rspec/compound'
3
+ require 'capybara/rspec/matchers/have_selector'
4
+ require 'capybara/rspec/matchers/have_ancestor'
5
+ require 'capybara/rspec/matchers/have_sibling'
6
+ require 'capybara/rspec/matchers/match_selector'
7
+ require 'capybara/rspec/matchers/have_current_path'
8
+ require 'capybara/rspec/matchers/match_style'
9
+ require 'capybara/rspec/matchers/have_text'
10
+ require 'capybara/rspec/matchers/have_title'
11
+ require 'capybara/rspec/matchers/become_closed'
4
12
 
5
13
  module Capybara
6
14
  module RSpecMatchers
7
- class Matcher
8
- include ::Capybara::RSpecMatchers::Compound if defined?(::Capybara::RSpecMatchers::Compound)
9
-
10
- attr_reader :failure_message, :failure_message_when_negated
11
-
12
- def initialize(*args, &filter_block)
13
- @args = args.dup
14
- @filter_block = filter_block
15
- end
16
-
17
- def wrap(actual)
18
- actual = actual.to_capybara_node if actual.respond_to?(:to_capybara_node)
19
- @context_el = if actual.respond_to?(:has_selector?)
20
- actual
21
- else
22
- Capybara.string(actual.to_s)
23
- end
24
- end
25
-
26
- private
27
-
28
- def wrap_matches?(actual)
29
- yield(wrap(actual))
30
- rescue Capybara::ExpectationNotMet => err
31
- @failure_message = err.message
32
- false
33
- end
34
-
35
- def wrap_does_not_match?(actual)
36
- yield(wrap(actual))
37
- rescue Capybara::ExpectationNotMet => err
38
- @failure_message_when_negated = err.message
39
- false
40
- end
41
-
42
- def session_query_args
43
- if @args.last.is_a? Hash
44
- @args.last[:session_options] = session_options
45
- else
46
- @args.push(session_options: session_options)
47
- end
48
- @args
49
- end
50
-
51
- def session_options
52
- @context_el ||= nil
53
- if @context_el.respond_to? :session_options
54
- @context_el.session_options
55
- elsif @context_el.respond_to? :current_scope
56
- @context_el.current_scope.session_options
57
- else
58
- Capybara.session_options
59
- end
60
- end
15
+ # RSpec matcher for whether the element(s) matching a given selector exist.
16
+ #
17
+ # @see Capybara::Node::Matchers#assert_selector
18
+ def have_selector(*args, **kw_args, &optional_filter_block)
19
+ Matchers::HaveSelector.new(*args, **kw_args, &optional_filter_block)
61
20
  end
62
21
 
63
- class HaveSelector < Matcher
64
- def matches?(actual)
65
- wrap_matches?(actual) { |el| el.assert_selector(*@args, &@filter_block) }
66
- end
67
-
68
- def does_not_match?(actual)
69
- wrap_does_not_match?(actual) { |el| el.assert_no_selector(*@args, &@filter_block) }
70
- end
71
-
72
- def description
73
- "have #{query.description}"
74
- end
75
-
76
- def query
77
- @query ||= Capybara::Queries::SelectorQuery.new(*session_query_args, &@filter_block)
78
- end
79
- end
80
-
81
- class HaveAllSelectors < Matcher
82
- def matches?(actual)
83
- wrap_matches?(actual) { |el| el.assert_all_of_selectors(*@args, &@filter_block) }
84
- end
85
-
86
- def does_not_match?(_actual)
87
- raise ArgumentError, 'The have_all_selectors matcher does not support use with not_to/should_not'
88
- end
89
-
90
- def description
91
- 'have all selectors'
92
- end
22
+ # RSpec matcher for whether the element(s) matching a group of selectors exist.
23
+ #
24
+ # @see Capybara::Node::Matchers#assert_all_of_selectors
25
+ def have_all_of_selectors(*args, **kw_args, &optional_filter_block)
26
+ Matchers::HaveAllSelectors.new(*args, **kw_args, &optional_filter_block)
93
27
  end
94
28
 
95
- class HaveNoSelectors < Matcher
96
- def matches?(actual)
97
- wrap_matches?(actual) { |el| el.assert_none_of_selectors(*@args, &@filter_block) }
98
- end
99
-
100
- def does_not_match?(_actual)
101
- raise ArgumentError, 'The have_none_of_selectors matcher does not support use with not_to/should_not'
102
- end
103
-
104
- def description
105
- 'have no selectors'
106
- end
29
+ # RSpec matcher for whether no element(s) matching a group of selectors exist.
30
+ #
31
+ # @see Capybara::Node::Matchers#assert_none_of_selectors
32
+ def have_none_of_selectors(*args, **kw_args, &optional_filter_block)
33
+ Matchers::HaveNoSelectors.new(*args, **kw_args, &optional_filter_block)
107
34
  end
108
35
 
109
- class MatchSelector < HaveSelector
110
- def matches?(actual)
111
- wrap_matches?(actual) { |el| el.assert_matches_selector(*@args, &@filter_block) }
112
- end
113
-
114
- def does_not_match?(actual)
115
- wrap_does_not_match?(actual) { |el| el.assert_not_matches_selector(*@args, &@filter_block) }
116
- end
117
-
118
- def description
119
- "match #{query.description}"
120
- end
121
-
122
- def query
123
- @query ||= Capybara::Queries::MatchQuery.new(*session_query_args, &@filter_block)
124
- end
36
+ # RSpec matcher for whether the element(s) matching any of a group of selectors exist.
37
+ #
38
+ # @see Capybara::Node::Matchers#assert_any_of_selectors
39
+ def have_any_of_selectors(*args, **kw_args, &optional_filter_block)
40
+ Matchers::HaveAnySelectors.new(*args, **kw_args, &optional_filter_block)
125
41
  end
126
42
 
127
- class HaveText < Matcher
128
- def matches?(actual)
129
- wrap_matches?(actual) { |el| el.assert_text(*@args) }
130
- end
131
-
132
- def does_not_match?(actual)
133
- wrap_does_not_match?(actual) { |el| el.assert_no_text(*@args) }
134
- end
135
-
136
- def description
137
- "text #{format(text)}"
138
- end
139
-
140
- def format(content)
141
- content.inspect
142
- end
143
-
144
- private
145
-
146
- def text
147
- @args[0].is_a?(Symbol) ? @args[1] : @args[0]
148
- end
149
- end
150
-
151
- class HaveTitle < Matcher
152
- def matches?(actual)
153
- wrap_matches?(actual) { |el| el.assert_title(*@args) }
154
- end
155
-
156
- def does_not_match?(actual)
157
- wrap_does_not_match?(actual) { |el| el.assert_no_title(*@args) }
158
- end
159
-
160
- def description
161
- "have title #{title.inspect}"
162
- end
163
-
164
- private
165
-
166
- def title
167
- @args.first
168
- end
169
- end
170
-
171
- class HaveCurrentPath < Matcher
172
- def matches?(actual)
173
- wrap_matches?(actual) { |el| el.assert_current_path(*@args) }
174
- end
175
-
176
- def does_not_match?(actual)
177
- wrap_does_not_match?(actual) { |el| el.assert_no_current_path(*@args) }
178
- end
179
-
180
- def description
181
- "have current path #{current_path.inspect}"
182
- end
183
-
184
- private
185
-
186
- def current_path
187
- @args.first
188
- end
189
- end
190
-
191
- class NegatedMatcher
192
- include ::Capybara::RSpecMatchers::Compound if defined?(::Capybara::RSpecMatchers::Compound)
193
-
194
- def initialize(matcher)
195
- super()
196
- @matcher = matcher
197
- end
198
-
199
- def matches?(actual)
200
- @matcher.does_not_match?(actual)
201
- end
202
-
203
- def does_not_match?(actual)
204
- @matcher.matches?(actual)
205
- end
206
-
207
- def description
208
- "not #{@matcher.description}"
209
- end
210
-
211
- def failure_message
212
- @matcher.failure_message_when_negated
213
- end
214
-
215
- def failure_message_when_negated
216
- @matcher.failure_message
217
- end
218
- end
219
-
220
- class HaveStyle < Matcher
221
- def matches?(actual)
222
- wrap_matches?(actual) { |el| el.assert_style(*@args) }
223
- end
224
-
225
- def does_not_match?(_actual)
226
- raise ArgumentError, 'The have_style matcher does not support use with not_to/should_not'
227
- end
228
-
229
- def description
230
- 'have style'
231
- end
232
- end
233
-
234
- class BecomeClosed
235
- def initialize(options)
236
- @options = options
237
- end
238
-
239
- def matches?(window)
240
- @window = window
241
- @wait_time = Capybara::Queries::BaseQuery.wait(@options, window.session.config.default_max_wait_time)
242
- timer = Capybara::Helpers.timer(expire_in: @wait_time)
243
- while window.exists?
244
- return false if timer.expired?
245
- sleep 0.05
246
- end
247
- true
248
- end
249
-
250
- def failure_message
251
- "expected #{@window.inspect} to become closed after #{@wait_time} seconds"
252
- end
253
-
254
- def failure_message_when_negated
255
- "expected #{@window.inspect} not to become closed after #{@wait_time} seconds"
256
- end
257
- end
258
-
259
- # RSpec matcher for whether the element(s) matching a given selector exist
260
- # See {Capybara::Node::Matcher#assert_selector}
261
- def have_selector(*args, &optional_filter_block)
262
- HaveSelector.new(*args, &optional_filter_block)
263
- end
264
-
265
- # RSpec matcher for whether the element(s) matching a group of selectors exist
266
- # See {Capybara::Node::Matcher#assert_all_of_selectors}
267
- def have_all_of_selectors(*args, &optional_filter_block)
268
- HaveAllSelectors.new(*args, &optional_filter_block)
269
- end
270
-
271
- # RSpec matcher for whether no element(s) matching a group of selectors exist
272
- # See {Capybara::Node::Matcher#assert_none_of_selectors}
273
- def have_none_of_selectors(*args, &optional_filter_block)
274
- HaveNoSelectors.new(*args, &optional_filter_block)
275
- end
276
-
277
- # RSpec matcher for whether the current element matches a given selector
278
- # See {Capybara::Node::Matchers#assert_matches_selector}
279
- def match_selector(*args, &optional_filter_block)
280
- MatchSelector.new(*args, &optional_filter_block)
43
+ # RSpec matcher for whether the current element matches a given selector.
44
+ #
45
+ # @see Capybara::Node::Matchers#assert_matches_selector
46
+ def match_selector(*args, **kw_args, &optional_filter_block)
47
+ Matchers::MatchSelector.new(*args, **kw_args, &optional_filter_block)
281
48
  end
282
49
 
283
50
  %i[css xpath].each do |selector|
284
51
  define_method "have_#{selector}" do |expr, **options, &optional_filter_block|
285
- HaveSelector.new(selector, expr, options, &optional_filter_block)
52
+ Matchers::HaveSelector.new(selector, expr, **options, &optional_filter_block)
286
53
  end
287
54
 
288
55
  define_method "match_#{selector}" do |expr, **options, &optional_filter_block|
289
- MatchSelector.new(selector, expr, options, &optional_filter_block)
56
+ Matchers::MatchSelector.new(selector, expr, **options, &optional_filter_block)
290
57
  end
291
58
  end
292
59
 
293
60
  # @!method have_xpath(xpath, **options, &optional_filter_block)
294
- # RSpec matcher for whether elements(s) matching a given xpath selector exist
295
- # See {Capybara::Node::Matchers#has_xpath?}
61
+ # RSpec matcher for whether elements(s) matching a given xpath selector exist.
62
+ #
63
+ # @see Capybara::Node::Matchers#has_xpath?
296
64
 
297
65
  # @!method have_css(css, **options, &optional_filter_block)
298
66
  # RSpec matcher for whether elements(s) matching a given css selector exist
299
- # See {Capybara::Node::Matchers#has_css?}
67
+ #
68
+ # @see Capybara::Node::Matchers#has_css?
300
69
 
301
70
  # @!method match_xpath(xpath, **options, &optional_filter_block)
302
- # RSpec matcher for whether the current element matches a given xpath selector
303
- # See {Capybara::Node::Matchers#matches_xpath?}
71
+ # RSpec matcher for whether the current element matches a given xpath selector.
72
+ #
73
+ # @see Capybara::Node::Matchers#matches_xpath?
304
74
 
305
75
  # @!method match_css(css, **options, &optional_filter_block)
306
- # RSpec matcher for whether the current element matches a given css selector
307
- # See {Capybara::Node::Matchers#matches_css?}
76
+ # RSpec matcher for whether the current element matches a given css selector.
77
+ #
78
+ # @see Capybara::Node::Matchers#matches_css?
308
79
 
309
80
  %i[link button field select table].each do |selector|
310
81
  define_method "have_#{selector}" do |locator = nil, **options, &optional_filter_block|
311
- HaveSelector.new(selector, locator, options, &optional_filter_block)
82
+ Matchers::HaveSelector.new(selector, locator, **options, &optional_filter_block)
312
83
  end
313
84
  end
314
85
 
315
86
  # @!method have_link(locator = nil, **options, &optional_filter_block)
316
- # RSpec matcher for links
317
- # See {Capybara::Node::Matchers#has_link?}
87
+ # RSpec matcher for links.
88
+ #
89
+ # @see Capybara::Node::Matchers#has_link?
318
90
 
319
91
  # @!method have_button(locator = nil, **options, &optional_filter_block)
320
- # RSpec matcher for buttons
321
- # See {Capybara::Node::Matchers#has_button?}
92
+ # RSpec matcher for buttons.
93
+ #
94
+ # @see Capybara::Node::Matchers#has_button?
322
95
 
323
96
  # @!method have_field(locator = nil, **options, &optional_filter_block)
324
- # RSpec matcher for links
325
- # See {Capybara::Node::Matchers#has_field?}
97
+ # RSpec matcher for links.
98
+ #
99
+ # @see Capybara::Node::Matchers#has_field?
326
100
 
327
101
  # @!method have_select(locator = nil, **options, &optional_filter_block)
328
- # RSpec matcher for select elements
329
- # See {Capybara::Node::Matchers#has_select?}
102
+ # RSpec matcher for select elements.
103
+ #
104
+ # @see Capybara::Node::Matchers#has_select?
330
105
 
331
106
  # @!method have_table(locator = nil, **options, &optional_filter_block)
332
- # RSpec matcher for table elements
333
- # See {Capybara::Node::Matchers#has_table?}
107
+ # RSpec matcher for table elements.
108
+ #
109
+ # @see Capybara::Node::Matchers#has_table?
334
110
 
335
111
  %i[checked unchecked].each do |state|
336
112
  define_method "have_#{state}_field" do |locator = nil, **options, &optional_filter_block|
337
- HaveSelector.new(:field, locator, options.merge(state => true), &optional_filter_block)
113
+ Matchers::HaveSelector.new(:field, locator, **options.merge(state => true), &optional_filter_block)
338
114
  end
339
115
  end
340
116
 
341
117
  # @!method have_checked_field(locator = nil, **options, &optional_filter_block)
342
- # RSpec matcher for checked fields
343
- # See {Capybara::Node::Matchers#has_checked_field?}
118
+ # RSpec matcher for checked fields.
119
+ #
120
+ # @see Capybara::Node::Matchers#has_checked_field?
344
121
 
345
122
  # @!method have_unchecked_field(locator = nil, **options, &optional_filter_block)
346
- # RSpec matcher for unchecked fields
347
- # See {Capybara::Node::Matchers#has_unchecked_field?}
123
+ # RSpec matcher for unchecked fields.
124
+ #
125
+ # @see Capybara::Node::Matchers#has_unchecked_field?
348
126
 
349
- # RSpec matcher for text content
350
- # See {Capybara::SessionMatchers#assert_text}
351
- def have_text(*args)
352
- HaveText.new(*args)
127
+ # RSpec matcher for text content.
128
+ #
129
+ # @see Capybara::Node::Matchers#assert_text
130
+ def have_text(text_or_type, *args, **options)
131
+ Matchers::HaveText.new(text_or_type, *args, **options)
353
132
  end
354
133
  alias_method :have_content, :have_text
355
134
 
356
135
  def have_title(title, **options)
357
- HaveTitle.new(title, options)
136
+ Matchers::HaveTitle.new(title, **options)
358
137
  end
359
138
 
360
- # RSpec matcher for the current path
361
- # See {Capybara::SessionMatchers#assert_current_path}
139
+ # RSpec matcher for the current path.
140
+ #
141
+ # @see Capybara::SessionMatchers#assert_current_path
362
142
  def have_current_path(path, **options)
363
- HaveCurrentPath.new(path, options)
143
+ Matchers::HaveCurrentPath.new(path, **options)
364
144
  end
365
145
 
366
- # RSpec matcher for element style
367
- # See {Capybara::Node::Matchers#has_style?}
146
+ # RSpec matcher for element style.
147
+ #
148
+ # @see Capybara::Node::Matchers#matches_style?
149
+ def match_style(styles, **options)
150
+ Matchers::MatchStyle.new(styles, **options)
151
+ end
152
+
153
+ ##
154
+ # @deprecated
155
+ #
368
156
  def have_style(styles, **options)
369
- HaveStyle.new(styles, options)
157
+ warn 'DEPRECATED: have_style is deprecated, please use match_style'
158
+ match_style(styles, **options)
370
159
  end
371
160
 
372
- %w[selector css xpath text title current_path link button field checked_field unchecked_field select table].each do |matcher_type|
373
- define_method "have_no_#{matcher_type}" do |*args, &optional_filter_block|
374
- NegatedMatcher.new(send("have_#{matcher_type}", *args, &optional_filter_block))
161
+ %w[selector css xpath text title current_path link button
162
+ field checked_field unchecked_field select table
163
+ sibling ancestor].each do |matcher_type|
164
+ define_method "have_no_#{matcher_type}" do |*args, **kw_args, &optional_filter_block|
165
+ Matchers::NegatedMatcher.new(send("have_#{matcher_type}", *args, **kw_args, &optional_filter_block))
375
166
  end
376
167
  end
377
168
  alias_method :have_no_content, :have_no_text
378
169
 
379
170
  %w[selector css xpath].each do |matcher_type|
380
- define_method "not_match_#{matcher_type}" do |*args, &optional_filter_block|
381
- NegatedMatcher.new(send("match_#{matcher_type}", *args, &optional_filter_block))
171
+ define_method "not_match_#{matcher_type}" do |*args, **kw_args, &optional_filter_block|
172
+ Matchers::NegatedMatcher.new(send("match_#{matcher_type}", *args, **kw_args, &optional_filter_block))
382
173
  end
383
174
  end
384
175
 
176
+ # RSpec matcher for whether sibling element(s) matching a given selector exist.
177
+ #
178
+ # @see Capybara::Node::Matchers#assert_sibling
179
+ def have_sibling(*args, **kw_args, &optional_filter_block)
180
+ Matchers::HaveSibling.new(*args, **kw_args, &optional_filter_block)
181
+ end
182
+
183
+ # RSpec matcher for whether ancestor element(s) matching a given selector exist.
184
+ #
185
+ # @see Capybara::Node::Matchers#assert_ancestor
186
+ def have_ancestor(*args, **kw_args, &optional_filter_block)
187
+ Matchers::HaveAncestor.new(*args, **kw_args, &optional_filter_block)
188
+ end
189
+
385
190
  ##
386
191
  # Wait for window to become closed.
192
+ #
387
193
  # @example
388
194
  # expect(window).to become_closed(wait: 0.8)
389
- # @param options [Hash] optional param
390
- # @option options [Numeric] :wait (Capybara.default_max_wait_time) Maximum wait time
195
+ #
196
+ # @option options [Numeric] :wait Maximum wait time. Defaults to {Capybara.configure default_max_wait_time}
391
197
  def become_closed(**options)
392
- BecomeClosed.new(options)
198
+ Matchers::BecomeClosed.new(options)
393
199
  end
394
200
  end
395
201
  end
@@ -0,0 +1,84 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'xpath'
4
+
5
+ module Capybara
6
+ class Selector
7
+ # @api private
8
+ class CSSBuilder
9
+ def initialize(expression)
10
+ @expression = expression || ''
11
+ end
12
+
13
+ attr_reader :expression
14
+
15
+ def add_attribute_conditions(**attributes)
16
+ @expression = attributes.inject(expression) do |css, (name, value)|
17
+ conditions = if name == :class
18
+ class_conditions(value)
19
+ elsif value.is_a? Regexp
20
+ regexp_conditions(name, value)
21
+ else
22
+ [attribute_conditions(name => value)]
23
+ end
24
+
25
+ ::Capybara::Selector::CSS.split(css).map do |sel|
26
+ next sel if conditions.empty?
27
+
28
+ conditions.map { |cond| sel + cond }.join(', ')
29
+ end.join(', ')
30
+ end
31
+ end
32
+
33
+ private
34
+
35
+ def regexp_conditions(name, value)
36
+ Selector::RegexpDisassembler.new(value).alternated_substrings.map do |strs|
37
+ strs.map do |str|
38
+ "[#{name}*='#{str}'#{' i' if value.casefold?}]"
39
+ end.join
40
+ end
41
+ end
42
+
43
+ def attribute_conditions(attributes)
44
+ attributes.map do |attribute, value|
45
+ case value
46
+ when XPath::Expression
47
+ raise ArgumentError, "XPath expressions are not supported for the :#{attribute} filter with CSS based selectors"
48
+ when Regexp
49
+ Selector::RegexpDisassembler.new(value).substrings.map do |str|
50
+ "[#{attribute}*='#{str}'#{' i' if value.casefold?}]"
51
+ end.join
52
+ when true
53
+ "[#{attribute}]"
54
+ when false
55
+ ':not([attribute])'
56
+ else
57
+ if attribute == :id
58
+ "##{::Capybara::Selector::CSS.escape(value)}"
59
+ else
60
+ "[#{attribute}='#{value}']"
61
+ end
62
+ end
63
+ end.join
64
+ end
65
+
66
+ def class_conditions(classes)
67
+ case classes
68
+ when XPath::Expression
69
+ raise ArgumentError, 'XPath expressions are not supported for the :class filter with CSS based selectors'
70
+ when Regexp
71
+ Selector::RegexpDisassembler.new(classes).alternated_substrings.map do |strs|
72
+ strs.map do |str|
73
+ "[class*='#{str}'#{' i' if classes.casefold?}]"
74
+ end.join
75
+ end
76
+ else
77
+ cls = Array(classes).group_by { |cl| cl.match?(/^!(?!!!)/) }
78
+ [(cls[false].to_a.map { |cl| ".#{Capybara::Selector::CSS.escape(cl.sub(/^!!/, ''))}" } +
79
+ cls[true].to_a.map { |cl| ":not(.#{Capybara::Selector::CSS.escape(cl.slice(1..-1))})" }).join]
80
+ end
81
+ end
82
+ end
83
+ end
84
+ end
@@ -0,0 +1,69 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'xpath'
4
+
5
+ module Capybara
6
+ class Selector
7
+ # @api private
8
+ class XPathBuilder
9
+ def initialize(expression)
10
+ @expression = expression || ''
11
+ end
12
+
13
+ attr_reader :expression
14
+
15
+ def add_attribute_conditions(**conditions)
16
+ @expression = conditions.inject(expression) do |xp, (name, value)|
17
+ conditions = name == :class ? class_conditions(value) : attribute_conditions(name => value)
18
+ if xp.is_a? XPath::Expression
19
+ xp[conditions]
20
+ else
21
+ "(#{xp})[#{conditions}]"
22
+ end
23
+ end
24
+ end
25
+
26
+ private
27
+
28
+ def attribute_conditions(attributes)
29
+ attributes.map do |attribute, value|
30
+ case value
31
+ when XPath::Expression
32
+ XPath.attr(attribute)[value]
33
+ when Regexp
34
+ XPath.attr(attribute)[regexp_to_xpath_conditions(value)]
35
+ when true
36
+ XPath.attr(attribute)
37
+ when false, nil
38
+ !XPath.attr(attribute)
39
+ else
40
+ XPath.attr(attribute) == value.to_s
41
+ end
42
+ end.reduce(:&)
43
+ end
44
+
45
+ def class_conditions(classes)
46
+ case classes
47
+ when XPath::Expression, Regexp
48
+ attribute_conditions(class: classes)
49
+ else
50
+ Array(classes).map do |klass|
51
+ if klass.match?(/^!(?!!!)/)
52
+ !XPath.attr(:class).contains_word(klass.slice(1..-1))
53
+ else
54
+ XPath.attr(:class).contains_word(klass.sub(/^!!/, ''))
55
+ end
56
+ end.reduce(:&)
57
+ end
58
+ end
59
+
60
+ def regexp_to_xpath_conditions(regexp)
61
+ condition = XPath.current
62
+ condition = condition.uppercase if regexp.casefold?
63
+ Selector::RegexpDisassembler.new(regexp).alternated_substrings.map do |strs|
64
+ strs.map { |str| condition.contains(str) }.reduce(:&)
65
+ end.reduce(:|)
66
+ end
67
+ end
68
+ end
69
+ end