cuprite 0.20.1 → 0.21
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.
- checksums.yaml +4 -4
- data/README.md +18 -16
- data/lib/capybara/cuprite/browser/extensions.rb +25 -28
- data/lib/capybara/cuprite/browser/settings.rb +102 -0
- data/lib/capybara/cuprite/browser.rb +47 -13
- data/lib/capybara/cuprite/context.rb +26 -0
- data/lib/capybara/cuprite/devices.rb +8 -0
- data/lib/capybara/cuprite/driver/extensions.rb +4 -0
- data/lib/capybara/cuprite/driver.rb +189 -28
- data/lib/capybara/cuprite/errors.rb +70 -1
- data/lib/capybara/cuprite/finder/hints.rb +51 -0
- data/lib/capybara/cuprite/finder.rb +18 -3
- data/lib/capybara/cuprite/javascripts/index.js +123 -58
- data/lib/capybara/cuprite/node/extensions.rb +13 -3
- data/lib/capybara/cuprite/node/typing.rb +75 -0
- data/lib/capybara/cuprite/node.rb +193 -44
- data/lib/capybara/cuprite/page/frames.rb +5 -7
- data/lib/capybara/cuprite/page/modals.rb +51 -35
- data/lib/capybara/cuprite/page.rb +49 -8
- data/lib/capybara/cuprite/version.rb +3 -1
- data/lib/capybara/cuprite.rb +6 -2
- data/sig/capybara/cuprite/browser/extensions.rbs +27 -0
- data/sig/capybara/cuprite/browser/settings.rbs +42 -0
- data/sig/capybara/cuprite/browser.rbs +41 -0
- data/sig/capybara/cuprite/context.rbs +7 -0
- data/sig/capybara/cuprite/devices.rbs +19 -0
- data/sig/capybara/cuprite/driver/extensions.rbs +9 -0
- data/sig/capybara/cuprite/driver.rbs +123 -0
- data/sig/capybara/cuprite/errors.rbs +62 -0
- data/sig/capybara/cuprite/finder/hints.rbs +28 -0
- data/sig/capybara/cuprite/finder.rbs +7 -0
- data/sig/capybara/cuprite/node/extensions.rbs +9 -0
- data/sig/capybara/cuprite/node/typing.rbs +30 -0
- data/sig/capybara/cuprite/node.rbs +112 -0
- data/sig/capybara/cuprite/page/frames.rbs +37 -0
- data/sig/capybara/cuprite/page/modals.rbs +39 -0
- data/sig/capybara/cuprite/page.rbs +26 -0
- data/sig/capybara/cuprite/version.rbs +5 -0
- metadata +24 -8
- data/lib/capybara/cuprite/browser/deprecations.rb +0 -26
- data/lib/capybara/cuprite/deprecation.rb +0 -13
- data/lib/capybara/cuprite/driver/deprecations.rb +0 -198
- data/lib/capybara/cuprite/node/deprecations.rb +0 -43
- data/lib/capybara/cuprite/options.rb +0 -33
|
@@ -1,15 +1,29 @@
|
|
|
1
1
|
# frozen_string_literal: true
|
|
2
2
|
|
|
3
3
|
require "forwardable"
|
|
4
|
+
require "rack"
|
|
4
5
|
require "capybara/cuprite/finder"
|
|
5
|
-
require "capybara/cuprite/driver/deprecations"
|
|
6
6
|
require "capybara/cuprite/driver/extensions"
|
|
7
7
|
|
|
8
8
|
module Capybara
|
|
9
9
|
module Cuprite
|
|
10
|
+
# Capybara driver for Chrome, built on Ferrum.
|
|
11
|
+
#
|
|
12
|
+
# @example Register the driver
|
|
13
|
+
# Capybara.register_driver(:cuprite) do |app|
|
|
14
|
+
# Capybara::Cuprite::Driver.new(app, window_size: [1200, 800])
|
|
15
|
+
# end
|
|
10
16
|
class Driver < Capybara::Driver::Base
|
|
17
|
+
# The window size `maximize_window` resizes to unless `screen_size:` is given.
|
|
11
18
|
DEFAULT_MAXIMIZE_SCREEN_SIZE = [1366, 768].freeze
|
|
19
|
+
# The path of the JavaScript Cuprite adds to every page.
|
|
20
|
+
# @api private
|
|
12
21
|
EXTENSION = File.expand_path("javascripts/index.js", __dir__)
|
|
22
|
+
# The case-insensitive Hash class `response_headers` returns.
|
|
23
|
+
# @api private
|
|
24
|
+
RESPONSE_HEADERS = defined?(Rack::Headers) ? Rack::Headers : Rack::Utils::HeaderHash
|
|
25
|
+
# The wrapper that turns an async script's callback into a Promise.
|
|
26
|
+
# @api private
|
|
13
27
|
CALLBACK_JS = <<~JS
|
|
14
28
|
function(...args) {
|
|
15
29
|
return new Promise((resolve, reject) => {
|
|
@@ -25,19 +39,70 @@ module Capybara
|
|
|
25
39
|
JS
|
|
26
40
|
|
|
27
41
|
extend Forwardable
|
|
28
|
-
include Deprecations
|
|
29
42
|
include Extensions
|
|
30
43
|
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
44
|
+
# @!method title
|
|
45
|
+
# @return [String] the title of the current page
|
|
46
|
+
# @!method window_handles
|
|
47
|
+
# @return [Array<String>] the handles of all open windows and tabs
|
|
48
|
+
# @!method current_window_handle
|
|
49
|
+
# @return [String] the handle of the current window
|
|
50
|
+
# @!method close_window(handle)
|
|
51
|
+
# Closes a window or tab.
|
|
52
|
+
# @param handle [String]
|
|
53
|
+
# @return [void]
|
|
54
|
+
# @raise [Ferrum::NoSuchPageError] when there's no such window
|
|
55
|
+
# @!method switch_to_window(handle)
|
|
56
|
+
# Makes a window or tab the current one.
|
|
57
|
+
# @param handle [String]
|
|
58
|
+
# @return [void]
|
|
59
|
+
# @raise [Ferrum::NoSuchPageError] when there's no such window
|
|
60
|
+
# @!method refresh
|
|
61
|
+
# Reloads the current page.
|
|
62
|
+
# @return [void]
|
|
63
|
+
delegate %i[title window_handles current_window_handle close_window switch_to_window refresh] => :browser
|
|
64
|
+
# @!method send_keys(*keys)
|
|
65
|
+
# Types keys into the focused element, see {Node#send_keys}.
|
|
66
|
+
# @param keys [Array<String, Symbol, Array>]
|
|
67
|
+
# @return [void]
|
|
34
68
|
delegate %i[send_keys] => :active_element
|
|
35
|
-
|
|
69
|
+
# @!method current_url
|
|
70
|
+
# @return [String] the URL of the current page
|
|
71
|
+
def_delegator :browser, :url, :current_url
|
|
72
|
+
# @!method html
|
|
73
|
+
# @return [String] the HTML of the current page or frame
|
|
74
|
+
def_delegator :browser, :html
|
|
75
|
+
# @!method go_back
|
|
76
|
+
# Navigates back in history.
|
|
77
|
+
# @return [void]
|
|
36
78
|
def_delegator :browser, :back, :go_back
|
|
79
|
+
# @!method go_forward
|
|
80
|
+
# Navigates forward in history.
|
|
81
|
+
# @return [void]
|
|
37
82
|
def_delegator :browser, :forward, :go_forward
|
|
38
83
|
|
|
39
|
-
|
|
40
|
-
|
|
84
|
+
# @return [#call] the Rack application under test
|
|
85
|
+
attr_reader :app
|
|
86
|
+
# @return [Hash] the options the browser is started with
|
|
87
|
+
attr_reader :options
|
|
88
|
+
|
|
89
|
+
# Every Ferrum option is accepted as well, see Ferrum's docs.
|
|
90
|
+
#
|
|
91
|
+
# @param app [#call] the Rack application under test
|
|
92
|
+
# @param options [Hash]
|
|
93
|
+
# @option options [Boolean] :js_errors re-raise JavaScript errors from the page as `Ferrum::JavaScriptError`,
|
|
94
|
+
# `false` by default
|
|
95
|
+
# @option options [Boolean] :pending_connection_errors raise {PendingConnectionsError} from `visit` when the
|
|
96
|
+
# page doesn't load in time, `true` by default
|
|
97
|
+
# @option options [Array(Integer, Integer)] :screen_size the size `maximize_window` resizes to,
|
|
98
|
+
# {DEFAULT_MAXIMIZE_SCREEN_SIZE} by default
|
|
99
|
+
# @option options [Boolean] :raise_on_unhandled_modal raise {UnhandledModalError} instead of warning when a
|
|
100
|
+
# dialog isn't handled, `false` by default
|
|
101
|
+
# @option options [Array<Regexp>] :url_blocklist requests to block, alias `:url_blacklist`
|
|
102
|
+
# @option options [Array<Regexp>] :url_allowlist requests to allow, alias `:url_whitelist`
|
|
103
|
+
# @option options [Symbol] :emulate a device from {Devices::LIST} to emulate
|
|
104
|
+
# @option options [Boolean] :mobile emulate a mobile device, `false` by default
|
|
105
|
+
# @option options [Numeric] :scale_factor the device scale factor to emulate, `0` (browser default) by default
|
|
41
106
|
def initialize(app, options = {})
|
|
42
107
|
@app = app
|
|
43
108
|
@options = options.dup
|
|
@@ -50,7 +115,8 @@ module Capybara
|
|
|
50
115
|
@screen_size ||= DEFAULT_MAXIMIZE_SCREEN_SIZE
|
|
51
116
|
@options[:save_path] ||= File.expand_path(Capybara.save_path) if Capybara.save_path
|
|
52
117
|
|
|
53
|
-
@options[:
|
|
118
|
+
@options[:url_blacklist] = @options.delete(:url_blocklist) if @options.key?(:url_blocklist)
|
|
119
|
+
@options[:url_whitelist] = @options.delete(:url_allowlist) if @options.key?(:url_allowlist)
|
|
54
120
|
|
|
55
121
|
# It's set for debug() to make devtools tab open correctly.
|
|
56
122
|
@options[:browser_options] ||= {}
|
|
@@ -62,59 +128,99 @@ module Capybara
|
|
|
62
128
|
super()
|
|
63
129
|
end
|
|
64
130
|
|
|
131
|
+
# @return [true] Cuprite runs the app in a server
|
|
65
132
|
def needs_server?
|
|
66
133
|
true
|
|
67
134
|
end
|
|
68
135
|
|
|
136
|
+
# The Ferrum browser, started on first use. Reach for it to use what Capybara doesn't cover.
|
|
137
|
+
#
|
|
138
|
+
# @return [Browser]
|
|
69
139
|
def browser
|
|
70
140
|
@browser ||= Browser.new({ base_url: capybara_base_url }.compact.merge(@options))
|
|
71
141
|
end
|
|
72
142
|
|
|
143
|
+
# Navigates to a URL, relative ones against Capybara's `app_host` or server.
|
|
144
|
+
#
|
|
145
|
+
# @param url [String]
|
|
146
|
+
# @return [void]
|
|
147
|
+
# @raise [PendingConnectionsError] with `pending_connection_errors` when the page doesn't load in time
|
|
73
148
|
def visit(url)
|
|
74
149
|
@started = true
|
|
75
150
|
browser.base_url = url if url.match?(%r{\Ahttps?://})
|
|
76
|
-
browser.
|
|
151
|
+
browser.go_to(url)
|
|
77
152
|
end
|
|
78
153
|
|
|
154
|
+
# @return [Integer, nil] the HTTP status of the last response
|
|
79
155
|
def status_code
|
|
80
156
|
browser.network.status
|
|
81
157
|
end
|
|
82
158
|
|
|
159
|
+
# @return [String] the URL of the current frame
|
|
83
160
|
def frame_url
|
|
84
161
|
evaluate_script("window.location.href")
|
|
85
162
|
end
|
|
86
163
|
|
|
164
|
+
# @return [String] the title of the current frame
|
|
87
165
|
def frame_title
|
|
88
166
|
evaluate_script("document.title")
|
|
89
167
|
end
|
|
90
168
|
|
|
169
|
+
# @return [Node] the focused element
|
|
91
170
|
def active_element
|
|
92
171
|
Node.new(self, browser.evaluate("document.activeElement"))
|
|
93
172
|
end
|
|
94
173
|
|
|
95
|
-
|
|
96
|
-
|
|
174
|
+
# @param selector [String]
|
|
175
|
+
# @param hints [Hash] Capybara's finder hints
|
|
176
|
+
# @return [Array<Node>] the elements matching the XPath
|
|
177
|
+
# @raise [InvalidSelector]
|
|
178
|
+
def find_xpath(selector, **hints)
|
|
179
|
+
find(:xpath, selector, **hints)
|
|
97
180
|
end
|
|
98
181
|
|
|
99
|
-
|
|
100
|
-
|
|
182
|
+
# @param selector [String]
|
|
183
|
+
# @param hints [Hash] Capybara's finder hints
|
|
184
|
+
# @return [Array<Node>] the elements matching the CSS selector
|
|
185
|
+
# @raise [InvalidSelector]
|
|
186
|
+
def find_css(selector, **hints)
|
|
187
|
+
find(:css, selector, **hints)
|
|
101
188
|
end
|
|
102
189
|
|
|
190
|
+
# Evaluates a JavaScript expression, with `arguments` set to `args`.
|
|
191
|
+
#
|
|
192
|
+
# @param script [String]
|
|
193
|
+
# @param args [Array] values passed to the script, nodes as elements
|
|
194
|
+
# @return [Object] the result, elements as {Node}
|
|
103
195
|
def evaluate_script(script, *args)
|
|
104
196
|
result = browser.evaluate(script, *native_args(args))
|
|
105
197
|
unwrap_script_result(result)
|
|
106
198
|
end
|
|
107
199
|
|
|
200
|
+
# Evaluates JavaScript that calls the callback passed as its last argument, waiting up to Capybara's
|
|
201
|
+
# `default_max_wait_time`.
|
|
202
|
+
#
|
|
203
|
+
# @param script [String]
|
|
204
|
+
# @param args [Array] values passed to the script, nodes as elements
|
|
205
|
+
# @return [Object] the value passed to the callback, elements as {Node}
|
|
108
206
|
def evaluate_async_script(script, *args)
|
|
109
207
|
result = browser.evaluate(CALLBACK_JS.sub("SCRIPT") { script }, *native_args(args), timeout: session_wait_time)
|
|
110
208
|
unwrap_script_result(result)
|
|
111
209
|
end
|
|
112
210
|
|
|
211
|
+
# Executes JavaScript, with `arguments` set to `args`.
|
|
212
|
+
#
|
|
213
|
+
# @param script [String]
|
|
214
|
+
# @param args [Array] values passed to the script, nodes as elements
|
|
215
|
+
# @return [nil]
|
|
113
216
|
def execute_script(script, *args)
|
|
114
217
|
browser.execute(script, *native_args(args))
|
|
115
218
|
nil
|
|
116
219
|
end
|
|
117
220
|
|
|
221
|
+
# @param locator [Capybara::Node::Element, Node, :parent, :top] the frame element, or the parent or top frame
|
|
222
|
+
# @return [void]
|
|
223
|
+
# @raise [ArgumentError] for any other locator
|
|
118
224
|
def switch_to_frame(locator)
|
|
119
225
|
handle = case locator
|
|
120
226
|
when Capybara::Node::Element
|
|
@@ -130,20 +236,33 @@ module Capybara
|
|
|
130
236
|
browser.page.switch_to_frame(handle)
|
|
131
237
|
end
|
|
132
238
|
|
|
133
|
-
|
|
134
|
-
|
|
239
|
+
# Opens a blank page without switching to it.
|
|
240
|
+
#
|
|
241
|
+
# @param kind [:tab, :window] what Capybara's `open_new_window` asks for
|
|
242
|
+
# @return [Page]
|
|
243
|
+
# @raise [ArgumentError] for any other kind
|
|
244
|
+
def open_new_window(kind = :tab)
|
|
245
|
+
browser.open_new_window(kind)
|
|
135
246
|
end
|
|
136
247
|
|
|
248
|
+
# @return [Class] the error raised for a closed window
|
|
137
249
|
def no_such_window_error
|
|
138
250
|
Ferrum::NoSuchPageError
|
|
139
251
|
end
|
|
140
252
|
|
|
253
|
+
# Closes the browser.
|
|
254
|
+
#
|
|
255
|
+
# @return [void]
|
|
256
|
+
def quit
|
|
257
|
+
@browser&.quit
|
|
258
|
+
end
|
|
259
|
+
|
|
260
|
+
# Resets the browser between tests: starts a fresh browser context and restores the options.
|
|
261
|
+
#
|
|
262
|
+
# @return [void]
|
|
141
263
|
def reset!
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
browser.url_blacklist = @options[:url_blacklist]
|
|
145
|
-
browser.url_whitelist = @options[:url_whitelist]
|
|
146
|
-
browser.raise_on_unhandled_modal = @options.fetch(:raise_on_unhandled_modal, false)
|
|
264
|
+
return unless @browser
|
|
265
|
+
|
|
147
266
|
browser.reset
|
|
148
267
|
base_url = @options[:base_url] || capybara_base_url
|
|
149
268
|
browser.base_url = base_url if base_url
|
|
@@ -151,48 +270,71 @@ module Capybara
|
|
|
151
270
|
@started = false
|
|
152
271
|
end
|
|
153
272
|
|
|
273
|
+
# Saves a screenshot, or a PDF when the path ends in `.pdf`.
|
|
274
|
+
#
|
|
275
|
+
# @param path [String]
|
|
276
|
+
# @param options [Hash] the options of Ferrum's `screenshot` or `pdf`, like `full:` or `selector:`
|
|
277
|
+
# @return [void]
|
|
154
278
|
def save_screenshot(path, options = {})
|
|
155
|
-
options[:scale] = @zoom_factor if @zoom_factor
|
|
156
|
-
|
|
157
279
|
if File.extname(path) == ".pdf"
|
|
158
|
-
options[:paperWidth] = @paper_size[:width].to_f if @paper_size
|
|
159
|
-
options[:paperHeight] = @paper_size[:height].to_f if @paper_size
|
|
160
280
|
browser.pdf(path: path, **options)
|
|
161
281
|
else
|
|
162
282
|
browser.screenshot(path: path, **options)
|
|
163
283
|
end
|
|
164
284
|
end
|
|
165
285
|
|
|
286
|
+
# @param handle [String]
|
|
287
|
+
# @param width [Integer]
|
|
288
|
+
# @param height [Integer]
|
|
289
|
+
# @return [void]
|
|
290
|
+
# @raise [Ferrum::NoSuchPageError] when there's no such window
|
|
166
291
|
def resize_window_to(handle, width, height)
|
|
167
292
|
within_window(handle) do
|
|
168
293
|
browser.resize(width: width, height: height)
|
|
169
294
|
end
|
|
170
295
|
end
|
|
171
296
|
|
|
297
|
+
# Resizes a window to `screen_size:`.
|
|
298
|
+
#
|
|
299
|
+
# @param handle [String]
|
|
300
|
+
# @return [void]
|
|
301
|
+
# @raise [Ferrum::NoSuchPageError] when there's no such window
|
|
172
302
|
def maximize_window(handle)
|
|
173
303
|
resize_window_to(handle, *screen_size)
|
|
174
304
|
end
|
|
175
305
|
|
|
306
|
+
# @param handle [String]
|
|
307
|
+
# @return [Array(Integer, Integer)] the inner width and height of the window
|
|
308
|
+
# @raise [Ferrum::NoSuchPageError] when there's no such window
|
|
176
309
|
def window_size(handle)
|
|
177
310
|
within_window(handle) do
|
|
178
311
|
evaluate_script("[window.innerWidth, window.innerHeight]")
|
|
179
312
|
end
|
|
180
313
|
end
|
|
181
314
|
|
|
315
|
+
# @param handle [String]
|
|
316
|
+
# @return [void]
|
|
317
|
+
# @raise [Ferrum::NoSuchPageError] when there's no such window
|
|
182
318
|
def fullscreen_window(handle)
|
|
183
319
|
within_window(handle) do
|
|
184
320
|
browser.resize(fullscreen: true)
|
|
185
321
|
end
|
|
186
322
|
end
|
|
187
323
|
|
|
324
|
+
# The headers of the last response, looked up case-insensitively as with rack_test.
|
|
325
|
+
#
|
|
326
|
+
# @return [Hash{String => String}, nil] `Rack::Headers`, or `Rack::Utils::HeaderHash` on Rack 2
|
|
188
327
|
def response_headers
|
|
189
|
-
browser.network.response&.headers
|
|
328
|
+
headers = browser.network.response&.headers
|
|
329
|
+
RESPONSE_HEADERS[headers] if headers
|
|
190
330
|
end
|
|
191
331
|
|
|
332
|
+
# @return [true] Capybara waits for elements with Cuprite
|
|
192
333
|
def wait?
|
|
193
334
|
true
|
|
194
335
|
end
|
|
195
336
|
|
|
337
|
+
# @return [Array<Class>] the errors after which Capybara looks the element up again
|
|
196
338
|
def invalid_element_errors
|
|
197
339
|
[Capybara::Cuprite::ObsoleteNode,
|
|
198
340
|
Capybara::Cuprite::MouseEventFailed,
|
|
@@ -201,6 +343,16 @@ module Capybara
|
|
|
201
343
|
Ferrum::NodeNotFoundError]
|
|
202
344
|
end
|
|
203
345
|
|
|
346
|
+
# Accepts the dialog the block opens.
|
|
347
|
+
#
|
|
348
|
+
# @param type [:alert, :confirm, :prompt]
|
|
349
|
+
# @param options [Hash]
|
|
350
|
+
# @option options [String, Regexp] :text the text the dialog has to match
|
|
351
|
+
# @option options [String] :with the response to a prompt
|
|
352
|
+
# @option options [Numeric] :wait how long to wait for the dialog
|
|
353
|
+
# @yield the action that opens the dialog
|
|
354
|
+
# @return [String] the dialog's message
|
|
355
|
+
# @raise [Capybara::ModalNotFound] when no matching dialog opens in time
|
|
204
356
|
def accept_modal(type, options = {})
|
|
205
357
|
case type
|
|
206
358
|
when :alert, :confirm
|
|
@@ -214,6 +366,15 @@ module Capybara
|
|
|
214
366
|
browser.page.find_modal(options)
|
|
215
367
|
end
|
|
216
368
|
|
|
369
|
+
# Dismisses the dialog the block opens.
|
|
370
|
+
#
|
|
371
|
+
# @param type [:confirm, :prompt]
|
|
372
|
+
# @param options [Hash]
|
|
373
|
+
# @option options [String, Regexp] :text the text the dialog has to match
|
|
374
|
+
# @option options [Numeric] :wait how long to wait for the dialog
|
|
375
|
+
# @yield the action that opens the dialog
|
|
376
|
+
# @return [String] the dialog's message
|
|
377
|
+
# @raise [Capybara::ModalNotFound] when no matching dialog opens in time
|
|
217
378
|
def dismiss_modal(type, options = {})
|
|
218
379
|
case type
|
|
219
380
|
when :confirm
|
|
@@ -231,8 +392,8 @@ module Capybara
|
|
|
231
392
|
|
|
232
393
|
attr_reader :screen_size
|
|
233
394
|
|
|
234
|
-
def find(method, selector)
|
|
235
|
-
Finder.find(browser, method, selector).map { |native| Node.new(self, native) }
|
|
395
|
+
def find(method, selector, **hints)
|
|
396
|
+
Finder.find(browser, method, selector, **hints).map { |native, cache| Node.new(self, native, cache) }
|
|
236
397
|
end
|
|
237
398
|
|
|
238
399
|
def within_window(handle)
|
|
@@ -2,32 +2,48 @@
|
|
|
2
2
|
|
|
3
3
|
module Capybara
|
|
4
4
|
module Cuprite
|
|
5
|
+
# The base of Cuprite's errors.
|
|
5
6
|
class Error < StandardError; end
|
|
6
7
|
|
|
8
|
+
# An error the browser reported.
|
|
7
9
|
class ClientError < Error
|
|
10
|
+
# @return [Object] what the browser responded with
|
|
8
11
|
attr_reader :response
|
|
9
12
|
|
|
13
|
+
# @param response [Object] what the browser responded with
|
|
10
14
|
def initialize(response)
|
|
11
15
|
@response = response
|
|
12
16
|
super()
|
|
13
17
|
end
|
|
14
18
|
end
|
|
15
19
|
|
|
20
|
+
# Raised by the finders for a selector the browser can't parse.
|
|
16
21
|
class InvalidSelector < ClientError
|
|
22
|
+
# @param response [Object] what the browser responded with
|
|
23
|
+
# @param method [:css, :xpath]
|
|
24
|
+
# @param selector [String]
|
|
17
25
|
def initialize(response, method, selector)
|
|
18
26
|
super(response)
|
|
19
27
|
@method = method
|
|
20
28
|
@selector = selector
|
|
21
29
|
end
|
|
22
30
|
|
|
31
|
+
# @return [String]
|
|
23
32
|
def message
|
|
24
33
|
"Browser raised error trying to find #{@method}: #{@selector.inspect}"
|
|
25
34
|
end
|
|
26
35
|
end
|
|
27
36
|
|
|
37
|
+
# Raised by a click when another element, overlapping this one, would receive it.
|
|
28
38
|
class MouseEventFailed < ClientError
|
|
29
|
-
|
|
39
|
+
# @return [String] the mouse event, like `"click"`
|
|
40
|
+
attr_reader :name
|
|
41
|
+
# @return [String] the CSS selector of the overlapping element
|
|
42
|
+
attr_reader :selector
|
|
43
|
+
# @return [Array(Float, Float)] the coordinates of the event
|
|
44
|
+
attr_reader :position
|
|
30
45
|
|
|
46
|
+
# Parses the browser's `"MouseEventFailed: <name>, <selector>, <x>, <y>"` response.
|
|
31
47
|
def initialize(*)
|
|
32
48
|
super
|
|
33
49
|
data = /\A\w+: (\w+), (.+?), ([\d.-]+), ([\d.-]+)/.match(@response)
|
|
@@ -35,6 +51,7 @@ module Capybara
|
|
|
35
51
|
@position = data.values_at(3, 4).map(&:to_f)
|
|
36
52
|
end
|
|
37
53
|
|
|
54
|
+
# @return [String]
|
|
38
55
|
def message
|
|
39
56
|
"Firing a #{name} at coordinates [#{position.join(', ')}] failed. Cuprite detected " \
|
|
40
57
|
"another element with CSS selector \"#{selector}\" at this position. " \
|
|
@@ -43,23 +60,75 @@ module Capybara
|
|
|
43
60
|
end
|
|
44
61
|
end
|
|
45
62
|
|
|
63
|
+
# Raised with `raise_on_unhandled_modal: true` when a dialog opens outside `accept_*` or `dismiss_*`.
|
|
46
64
|
class UnhandledModalError < Error
|
|
65
|
+
# @return [String]
|
|
47
66
|
attr_reader :message
|
|
48
67
|
|
|
68
|
+
# @param message [String]
|
|
49
69
|
def initialize(message)
|
|
50
70
|
@message = message
|
|
51
71
|
super()
|
|
52
72
|
end
|
|
53
73
|
end
|
|
54
74
|
|
|
75
|
+
# Whether this Ferrum still has `Ferrum::PendingConnectionsError`.
|
|
76
|
+
# @api private
|
|
77
|
+
FERRUM_PENDING_CONNECTIONS_ERROR = Ferrum.const_defined?(:PendingConnectionsError, false)
|
|
78
|
+
|
|
79
|
+
# Raised by `visit` with `pending_connection_errors: true` when the page doesn't load in time.
|
|
80
|
+
class PendingConnectionsError < (FERRUM_PENDING_CONNECTIONS_ERROR ? Ferrum::PendingConnectionsError : Ferrum::StatusError)
|
|
81
|
+
# @return [String] the URL being loaded
|
|
82
|
+
attr_reader :url
|
|
83
|
+
# @return [Array<String>] the URLs of the requests still pending
|
|
84
|
+
attr_reader :pendings
|
|
85
|
+
|
|
86
|
+
# @param url [String] the URL being loaded
|
|
87
|
+
# @param pendings [Array<String>] the URLs of the requests still pending
|
|
88
|
+
def initialize(url, pendings = [])
|
|
89
|
+
super(url)
|
|
90
|
+
@url = url
|
|
91
|
+
@pendings = pendings
|
|
92
|
+
end
|
|
93
|
+
|
|
94
|
+
# @return [String]
|
|
95
|
+
def message
|
|
96
|
+
message = "Request to #{@url} reached server, but there are still pending connections"
|
|
97
|
+
@pendings.empty? ? message : "#{message}: #{@pendings.join(', ')}"
|
|
98
|
+
end
|
|
99
|
+
end
|
|
100
|
+
|
|
101
|
+
# Warns when `rescue Ferrum::PendingConnectionsError` catches Cuprite's error.
|
|
102
|
+
# @api private
|
|
103
|
+
module DeprecatedPendingConnectionsError
|
|
104
|
+
# @param other [Object]
|
|
105
|
+
# @return [Boolean]
|
|
106
|
+
def ===(other)
|
|
107
|
+
matched = super
|
|
108
|
+
if matched && equal?(Ferrum::PendingConnectionsError) && other.is_a?(Cuprite::PendingConnectionsError)
|
|
109
|
+
warn "Ferrum::PendingConnectionsError is deprecated, use Capybara::Cuprite::PendingConnectionsError",
|
|
110
|
+
uplevel: 1
|
|
111
|
+
end
|
|
112
|
+
matched
|
|
113
|
+
end
|
|
114
|
+
end
|
|
115
|
+
|
|
116
|
+
Ferrum::PendingConnectionsError.singleton_class.prepend(DeprecatedPendingConnectionsError) if
|
|
117
|
+
FERRUM_PENDING_CONNECTIONS_ERROR
|
|
118
|
+
|
|
119
|
+
# Raised when an element is no longer in the DOM. Capybara finds it again and retries.
|
|
55
120
|
class ObsoleteNode < ClientError
|
|
121
|
+
# @return [Node] the element
|
|
56
122
|
attr_reader :node
|
|
57
123
|
|
|
124
|
+
# @param node [Node] the element
|
|
125
|
+
# @param response [Object, nil] what the browser responded with
|
|
58
126
|
def initialize(node, response)
|
|
59
127
|
@node = node
|
|
60
128
|
super(response)
|
|
61
129
|
end
|
|
62
130
|
|
|
131
|
+
# @return [String]
|
|
63
132
|
def message
|
|
64
133
|
"The element you are trying to interact with is either not part of the DOM, or is " \
|
|
65
134
|
"not currently visible on the page (perhaps display: none is set). " \
|
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Capybara
|
|
4
|
+
module Cuprite
|
|
5
|
+
module Finder
|
|
6
|
+
# Capybara's finder hints for one query: what the browser is asked to answer along with
|
|
7
|
+
# the nodes it finds, and the initial cache of each node built from that answer.
|
|
8
|
+
# @api private
|
|
9
|
+
class Hints
|
|
10
|
+
# @param uses_visibility [Boolean] whether the query filters on visibility
|
|
11
|
+
# @param texts [Array<String>] the fragments every matching node's text has to contain
|
|
12
|
+
# @param styles [Hash, nil] the computed styles the query filters on
|
|
13
|
+
# @param position [Boolean] whether the query filters on the position of the nodes
|
|
14
|
+
def initialize(uses_visibility: false, texts: [], styles: nil, position: false, **)
|
|
15
|
+
@visible = uses_visibility
|
|
16
|
+
@texts = texts
|
|
17
|
+
@styles = styles.keys.map(&:to_s) if styles.is_a?(Hash)
|
|
18
|
+
@position = position
|
|
19
|
+
end
|
|
20
|
+
|
|
21
|
+
# @return [Hash] the hints as `_cuprite.findWithHints` takes them
|
|
22
|
+
def to_h
|
|
23
|
+
{ visible: @visible, texts: @texts, styles: @styles, position: @position }
|
|
24
|
+
end
|
|
25
|
+
|
|
26
|
+
# Builds the initial cache of each node from the browser's answer, which holds one
|
|
27
|
+
# list per hint instead of one cache per node, as Chrome serializes that faster.
|
|
28
|
+
#
|
|
29
|
+
# @param count [Integer] the number of nodes found
|
|
30
|
+
# @param hidden [Array<Integer>, nil] the indexes of the hidden nodes
|
|
31
|
+
# @param style [Hash{String => Array<String>}, nil] each style's value for every node
|
|
32
|
+
# @param position [Array<Hash>, nil] the bounding rectangle of every node
|
|
33
|
+
# @param texts [Array<Array>, nil] all text and visible text of every node
|
|
34
|
+
# @return [Array<Hash>] one cache per node, in the order of the nodes
|
|
35
|
+
def caches(count, hidden, style, position, texts)
|
|
36
|
+
hidden = hidden&.to_set
|
|
37
|
+
all_texts, visible_texts = texts
|
|
38
|
+
|
|
39
|
+
Array.new(count) do |index|
|
|
40
|
+
cache = {}
|
|
41
|
+
cache[:visible] = !hidden.include?(index) if hidden
|
|
42
|
+
cache[:style] = style.transform_values { |values| values[index] } if style
|
|
43
|
+
cache[:position] = position[index] if position
|
|
44
|
+
cache.merge!(all_text: all_texts[index], visible_text: visible_texts[index]) if texts
|
|
45
|
+
cache
|
|
46
|
+
end
|
|
47
|
+
end
|
|
48
|
+
end
|
|
49
|
+
end
|
|
50
|
+
end
|
|
51
|
+
end
|
|
@@ -1,17 +1,32 @@
|
|
|
1
1
|
# frozen_string_literal: true
|
|
2
2
|
|
|
3
|
+
require "capybara/cuprite/finder/hints"
|
|
4
|
+
|
|
3
5
|
module Capybara
|
|
4
6
|
module Cuprite
|
|
7
|
+
# Looks up the elements for {Driver} and {Node}.
|
|
8
|
+
# @api private
|
|
5
9
|
module Finder
|
|
6
10
|
module_function
|
|
7
11
|
|
|
8
|
-
|
|
12
|
+
# Finds the nodes matching a selector and answers Capybara's finder hints in the same call.
|
|
13
|
+
#
|
|
14
|
+
# @param browser [Browser]
|
|
15
|
+
# @param method [Symbol] `:css` or `:xpath`
|
|
16
|
+
# @param selector [String]
|
|
17
|
+
# @param within [Ferrum::Node, nil] the node to search in, the document by default
|
|
18
|
+
# @param hints [Hash] the hints Capybara passes to `find_css` and `find_xpath`
|
|
19
|
+
# @return [Array<Array(Ferrum::Node, Hash)>] each node with its initial cache
|
|
20
|
+
# @raise [InvalidSelector]
|
|
21
|
+
def find(browser, method, selector, within = nil, **hints)
|
|
9
22
|
# Capybara appends its text-match hint as `(expr)[...]`, but with `exact_text: true` the hint
|
|
10
23
|
# is empty and the result `(expr)[]` is invalid XPath. An empty predicate is never valid, so
|
|
11
24
|
# dropping it can't change a valid selector. Remove once teamcapybara/capybara#2860 is released.
|
|
12
25
|
selector = selector.delete_suffix("[]") if method == :xpath
|
|
13
|
-
|
|
14
|
-
nodes.
|
|
26
|
+
hints = Hints.new(**hints)
|
|
27
|
+
nodes, *answers = browser.evaluate("_cuprite.findWithHints(method, selector, within, hints)",
|
|
28
|
+
method: method, selector: selector, within: within, hints: hints.to_h)
|
|
29
|
+
nodes.zip(hints.caches(nodes.size, *answers))
|
|
15
30
|
rescue Ferrum::JavaScriptError => e
|
|
16
31
|
raise InvalidSelector.new(e.response, method, selector) if e.class_name == "InvalidSelector"
|
|
17
32
|
|