cuprite 0.21 → 0.22

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 (43) hide show
  1. checksums.yaml +4 -4
  2. data/README.md +27 -25
  3. data/lib/capybara/cuprite/deprecation.rb +20 -0
  4. data/lib/capybara/cuprite/driver/deprecations.rb +36 -0
  5. data/lib/capybara/cuprite/driver/extensions.rb +47 -1
  6. data/lib/capybara/cuprite/{browser → driver}/settings.rb +2 -2
  7. data/lib/capybara/cuprite/driver/window/frames.rb +35 -0
  8. data/lib/capybara/cuprite/driver/window/modals.rb +113 -0
  9. data/lib/capybara/cuprite/driver/window.rb +34 -0
  10. data/lib/capybara/cuprite/driver/windows.rb +90 -0
  11. data/lib/capybara/cuprite/driver.rb +171 -67
  12. data/lib/capybara/cuprite/errors.rb +1 -23
  13. data/lib/capybara/cuprite/finder.rb +4 -4
  14. data/lib/capybara/cuprite/node/extensions.rb +1 -1
  15. data/lib/capybara/cuprite/node/typing.rb +1 -1
  16. data/lib/capybara/cuprite/node.rb +23 -27
  17. data/lib/capybara/cuprite/version.rb +1 -1
  18. data/lib/capybara/cuprite.rb +0 -7
  19. data/sig/capybara/cuprite/deprecation.rbs +7 -0
  20. data/sig/capybara/cuprite/driver/deprecations.rbs +11 -0
  21. data/sig/capybara/cuprite/driver/extensions.rbs +20 -0
  22. data/sig/capybara/cuprite/{browser → driver}/settings.rbs +1 -1
  23. data/sig/capybara/cuprite/driver/window/frames.rbs +13 -0
  24. data/sig/capybara/cuprite/driver/window/modals.rbs +29 -0
  25. data/sig/capybara/cuprite/driver/window.rbs +22 -0
  26. data/sig/capybara/cuprite/driver/windows.rbs +27 -0
  27. data/sig/capybara/cuprite/driver.rbs +32 -15
  28. data/sig/capybara/cuprite/errors.rbs +0 -6
  29. data/sig/capybara/cuprite/finder.rbs +1 -1
  30. data/sig/capybara/cuprite/node.rbs +2 -2
  31. metadata +17 -17
  32. data/lib/capybara/cuprite/browser/extensions.rb +0 -56
  33. data/lib/capybara/cuprite/browser.rb +0 -142
  34. data/lib/capybara/cuprite/context.rb +0 -26
  35. data/lib/capybara/cuprite/page/frames.rb +0 -45
  36. data/lib/capybara/cuprite/page/modals.rb +0 -134
  37. data/lib/capybara/cuprite/page.rb +0 -77
  38. data/sig/capybara/cuprite/browser/extensions.rbs +0 -27
  39. data/sig/capybara/cuprite/browser.rbs +0 -41
  40. data/sig/capybara/cuprite/context.rbs +0 -7
  41. data/sig/capybara/cuprite/page/frames.rbs +0 -37
  42. data/sig/capybara/cuprite/page/modals.rbs +0 -39
  43. data/sig/capybara/cuprite/page.rbs +0 -26
@@ -3,12 +3,31 @@
3
3
  require "forwardable"
4
4
  require "rack"
5
5
  require "capybara/cuprite/finder"
6
+ require "capybara/cuprite/driver/deprecations"
6
7
  require "capybara/cuprite/driver/extensions"
8
+ require "capybara/cuprite/driver/settings"
9
+ require "capybara/cuprite/driver/window"
10
+ require "capybara/cuprite/driver/windows"
7
11
 
8
12
  module Capybara
9
13
  module Cuprite
10
14
  # Capybara driver for Chrome, built on Ferrum.
11
15
  #
16
+ # The driver keeps what Capybara needs and Ferrum doesn't have, and leaves the rest to a plain Ferrum browser:
17
+ #
18
+ # Driver
19
+ # ├─ browser Ferrum::Browser
20
+ # │ └─ on_page sets up every new page: Cuprite's JS, viewport, user agent, URL lists, JS errors
21
+ # ├─ current_page Ferrum::Page of Capybara's current window
22
+ # ├─ window Window, that window's frames and dialogs
23
+ # │ ├─ Frames the frame switched to, current_frame
24
+ # │ └─ Modals the dialogs to answer, the unhandled dialog error
25
+ # ├─ Windows window handles, opening, switching, closing
26
+ # ├─ Settings the options only Cuprite handles
27
+ # └─ Extensions debug, URL lists, raise_on_unhandled_modal
28
+ #
29
+ # Node wraps a Ferrum::Node and works in current_frame and current_page, Finder finds nodes in a frame.
30
+ #
12
31
  # @example Register the driver
13
32
  # Capybara.register_driver(:cuprite) do |app|
14
33
  # Capybara::Cuprite::Driver.new(app, window_size: [1200, 800])
@@ -19,6 +38,12 @@ module Capybara
19
38
  # The path of the JavaScript Cuprite adds to every page.
20
39
  # @api private
21
40
  EXTENSION = File.expand_path("javascripts/index.js", __dir__)
41
+ # The JavaScript Cuprite adds to every page.
42
+ # @api private
43
+ EXTENSION_JS = File.read(EXTENSION).freeze
44
+ # The options Cuprite handles itself and doesn't pass to Ferrum.
45
+ # @api private
46
+ CUPRITE_OPTIONS = %i[js_errors pending_connection_errors].freeze
22
47
  # The case-insensitive Hash class `response_headers` returns.
23
48
  # @api private
24
49
  RESPONSE_HEADERS = defined?(Rack::Headers) ? Rack::Headers : Rack::Utils::HeaderHash
@@ -40,27 +65,14 @@ module Capybara
40
65
 
41
66
  extend Forwardable
42
67
  include Extensions
68
+ include Windows
43
69
 
44
70
  # @!method title
45
71
  # @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
72
  # @!method refresh
61
73
  # Reloads the current page.
62
74
  # @return [void]
63
- delegate %i[title window_handles current_window_handle close_window switch_to_window refresh] => :browser
75
+ delegate %i[title refresh] => :current_page
64
76
  # @!method send_keys(*keys)
65
77
  # Types keys into the focused element, see {Node#send_keys}.
66
78
  # @param keys [Array<String, Symbol, Array>]
@@ -68,18 +80,18 @@ module Capybara
68
80
  delegate %i[send_keys] => :active_element
69
81
  # @!method current_url
70
82
  # @return [String] the URL of the current page
71
- def_delegator :browser, :url, :current_url
83
+ def_delegator :current_page, :url, :current_url
72
84
  # @!method html
73
85
  # @return [String] the HTML of the current page or frame
74
- def_delegator :browser, :html
86
+ delegate %i[html] => :current_frame
75
87
  # @!method go_back
76
88
  # Navigates back in history.
77
89
  # @return [void]
78
- def_delegator :browser, :back, :go_back
90
+ def_delegator :current_page, :back, :go_back
79
91
  # @!method go_forward
80
92
  # Navigates forward in history.
81
93
  # @return [void]
82
- def_delegator :browser, :forward, :go_forward
94
+ def_delegator :current_page, :forward, :go_forward
83
95
 
84
96
  # @return [#call] the Rack application under test
85
97
  attr_reader :app
@@ -107,9 +119,11 @@ module Capybara
107
119
  @app = app
108
120
  @options = options.dup
109
121
  @started = false
122
+ @current_page = nil
123
+ @page_closed = false
124
+ @windows = {}
110
125
 
111
- @options[:extensions] ||= []
112
- @options[:extensions] << EXTENSION
126
+ @options[:init_scripts] = [*@options[:init_scripts], EXTENSION]
113
127
 
114
128
  @screen_size = @options.delete(:screen_size)
115
129
  @screen_size ||= DEFAULT_MAXIMIZE_SCREEN_SIZE
@@ -133,11 +147,40 @@ module Capybara
133
147
  true
134
148
  end
135
149
 
136
- # The Ferrum browser, started on first use. Reach for it to use what Capybara doesn't cover.
150
+ # The Ferrum browser, started on first use. Reach for it for browser-wide settings such as `timeout`, and for
151
+ # pages through {#current_page}.
137
152
  #
138
- # @return [Browser]
153
+ # @return [Ferrum::Browser]
154
+ # @raise [ArgumentError] for a URL pattern that isn't a Regexp, both URL lists, or an unknown device
139
155
  def browser
140
- @browser ||= Browser.new({ base_url: capybara_base_url }.compact.merge(@options))
156
+ @browser ||= start_browser
157
+ end
158
+
159
+ # The Ferrum page of Capybara's current window, the one it's switched to at the moment. The driver runs Capybara's
160
+ # calls on it, and it's the way to use Ferrum in that window, because `browser.page` is always the first window,
161
+ # even inside `within_window`.
162
+ #
163
+ # @example
164
+ # within_window(new_window) { page.driver.current_page.network.traffic }
165
+ #
166
+ # @return [Ferrum::Page] the first window's page until Capybara switches to another one
167
+ # @raise [Ferrum::NoSuchPageError] when the current window was closed
168
+ def current_page
169
+ raise Ferrum::NoSuchPageError if @page_closed
170
+
171
+ @current_page ||= browser.page
172
+ end
173
+
174
+ # @api private
175
+ # @return [Window] the frames and dialogs Cuprite keeps for the current window
176
+ def window
177
+ @windows.fetch(current_page.target_id)
178
+ end
179
+
180
+ # @api private
181
+ # @return [Ferrum::Frame] the frame switched to in the current page
182
+ def current_frame
183
+ window.current_frame
141
184
  end
142
185
 
143
186
  # Navigates to a URL, relative ones against Capybara's `app_host` or server.
@@ -148,12 +191,12 @@ module Capybara
148
191
  def visit(url)
149
192
  @started = true
150
193
  browser.base_url = url if url.match?(%r{\Ahttps?://})
151
- browser.go_to(url)
194
+ check_modals { navigate(url) }
152
195
  end
153
196
 
154
197
  # @return [Integer, nil] the HTTP status of the last response
155
198
  def status_code
156
- browser.network.status
199
+ current_page.network.status
157
200
  end
158
201
 
159
202
  # @return [String] the URL of the current frame
@@ -168,7 +211,7 @@ module Capybara
168
211
 
169
212
  # @return [Node] the focused element
170
213
  def active_element
171
- Node.new(self, browser.evaluate("document.activeElement"))
214
+ Node.new(self, current_frame.evaluate("document.activeElement"))
172
215
  end
173
216
 
174
217
  # @param selector [String]
@@ -193,7 +236,7 @@ module Capybara
193
236
  # @param args [Array] values passed to the script, nodes as elements
194
237
  # @return [Object] the result, elements as {Node}
195
238
  def evaluate_script(script, *args)
196
- result = browser.evaluate(script, *native_args(args))
239
+ result = check_modals { current_frame.evaluate(script, *native_args(args)) }
197
240
  unwrap_script_result(result)
198
241
  end
199
242
 
@@ -204,7 +247,9 @@ module Capybara
204
247
  # @param args [Array] values passed to the script, nodes as elements
205
248
  # @return [Object] the value passed to the callback, elements as {Node}
206
249
  def evaluate_async_script(script, *args)
207
- result = browser.evaluate(CALLBACK_JS.sub("SCRIPT") { script }, *native_args(args), timeout: session_wait_time)
250
+ result = check_modals do
251
+ current_frame.evaluate(CALLBACK_JS.sub("SCRIPT") { script }, *native_args(args), timeout: session_wait_time)
252
+ end
208
253
  unwrap_script_result(result)
209
254
  end
210
255
 
@@ -214,7 +259,7 @@ module Capybara
214
259
  # @param args [Array] values passed to the script, nodes as elements
215
260
  # @return [nil]
216
261
  def execute_script(script, *args)
217
- browser.execute(script, *native_args(args))
262
+ check_modals { current_frame.execute(script, *native_args(args)) }
218
263
  nil
219
264
  end
220
265
 
@@ -224,30 +269,16 @@ module Capybara
224
269
  def switch_to_frame(locator)
225
270
  handle = case locator
226
271
  when Capybara::Node::Element
227
- locator.native.node.description["frameId"]
272
+ locator.native.node.frame_id
228
273
  when Capybara::Cuprite::Node
229
- locator.node.description["frameId"]
274
+ locator.node.frame_id
230
275
  when :parent, :top
231
276
  locator
232
277
  end
233
278
 
234
279
  raise ArgumentError, "Unable to switch to frame from #{locator.class}" unless handle
235
280
 
236
- browser.page.switch_to_frame(handle)
237
- end
238
-
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)
246
- end
247
-
248
- # @return [Class] the error raised for a closed window
249
- def no_such_window_error
250
- Ferrum::NoSuchPageError
281
+ window.switch_to_frame(handle)
251
282
  end
252
283
 
253
284
  # Closes the browser.
@@ -255,6 +286,9 @@ module Capybara
255
286
  # @return [void]
256
287
  def quit
257
288
  @browser&.quit
289
+ ensure
290
+ @windows.clear
291
+ forget_page
258
292
  end
259
293
 
260
294
  # Resets the browser between tests: starts a fresh browser context and restores the options.
@@ -263,7 +297,7 @@ module Capybara
263
297
  def reset!
264
298
  return unless @browser
265
299
 
266
- browser.reset
300
+ reset_browser
267
301
  base_url = @options[:base_url] || capybara_base_url
268
302
  browser.base_url = base_url if base_url
269
303
  ensure
@@ -277,9 +311,9 @@ module Capybara
277
311
  # @return [void]
278
312
  def save_screenshot(path, options = {})
279
313
  if File.extname(path) == ".pdf"
280
- browser.pdf(path: path, **options)
314
+ current_page.pdf(path: path, **options)
281
315
  else
282
- browser.screenshot(path: path, **options)
316
+ current_page.screenshot(path: path, **options)
283
317
  end
284
318
  end
285
319
 
@@ -290,7 +324,9 @@ module Capybara
290
324
  # @raise [Ferrum::NoSuchPageError] when there's no such window
291
325
  def resize_window_to(handle, width, height)
292
326
  within_window(handle) do
293
- browser.resize(width: width, height: height)
327
+ settings.window_size = [width, height]
328
+ current_page.resize(width: width, height: height)
329
+ emulate_viewport(current_page, width, height)
294
330
  end
295
331
  end
296
332
 
@@ -317,7 +353,9 @@ module Capybara
317
353
  # @raise [Ferrum::NoSuchPageError] when there's no such window
318
354
  def fullscreen_window(handle)
319
355
  within_window(handle) do
320
- browser.resize(fullscreen: true)
356
+ settings.window_size = [nil, nil]
357
+ current_page.resize(fullscreen: true)
358
+ emulate_viewport(current_page, *current_page.document_size)
321
359
  end
322
360
  end
323
361
 
@@ -325,7 +363,7 @@ module Capybara
325
363
  #
326
364
  # @return [Hash{String => String}, nil] `Rack::Headers`, or `Rack::Utils::HeaderHash` on Rack 2
327
365
  def response_headers
328
- headers = browser.network.response&.headers
366
+ headers = current_page.network.response&.headers
329
367
  RESPONSE_HEADERS[headers] if headers
330
368
  end
331
369
 
@@ -340,7 +378,9 @@ module Capybara
340
378
  Capybara::Cuprite::MouseEventFailed,
341
379
  Ferrum::CoordinatesNotFoundError,
342
380
  Ferrum::NoExecutionContextError,
343
- Ferrum::NodeNotFoundError]
381
+ Ferrum::ExecutionContextDestroyedError,
382
+ Ferrum::NodeNotFoundError,
383
+ Ferrum::ForeignObjectError]
344
384
  end
345
385
 
346
386
  # Accepts the dialog the block opens.
@@ -356,14 +396,14 @@ module Capybara
356
396
  def accept_modal(type, options = {})
357
397
  case type
358
398
  when :alert, :confirm
359
- browser.page.accept_confirm
399
+ window.accept_confirm
360
400
  when :prompt
361
- browser.page.accept_prompt(options[:with])
401
+ window.accept_prompt(options[:with])
362
402
  end
363
403
 
364
404
  yield if block_given?
365
405
 
366
- browser.page.find_modal(options)
406
+ window.find_modal(options)
367
407
  end
368
408
 
369
409
  # Dismisses the dialog the block opens.
@@ -378,14 +418,14 @@ module Capybara
378
418
  def dismiss_modal(type, options = {})
379
419
  case type
380
420
  when :confirm
381
- browser.page.dismiss_confirm
421
+ window.dismiss_confirm
382
422
  when :prompt
383
- browser.page.dismiss_prompt
423
+ window.dismiss_prompt
384
424
  end
385
425
 
386
426
  yield if block_given?
387
427
 
388
- browser.page.find_modal(options)
428
+ window.find_modal(options)
389
429
  end
390
430
 
391
431
  private
@@ -393,17 +433,81 @@ module Capybara
393
433
  attr_reader :screen_size
394
434
 
395
435
  def find(method, selector, **hints)
396
- Finder.find(browser, method, selector, **hints).map { |native, cache| Node.new(self, native, cache) }
436
+ nodes = check_modals { Finder.find(current_frame, method, selector, **hints) }
437
+ nodes.map { |native, cache| Node.new(self, native, cache) }
438
+ end
439
+
440
+ def check_modals(&)
441
+ window.check_modals(&)
397
442
  end
398
443
 
399
- def within_window(handle)
400
- original = browser.current_window_handle
401
- raise Ferrum::NoSuchPageError unless browser.window_handles.include?(handle)
444
+ def start_browser
445
+ options = { base_url: capybara_base_url }.compact.merge(@options)
446
+ device = options[:emulate]
447
+ options = Devices.fetch(device).except(:user_agent).merge(options) if device
448
+
449
+ browser = Ferrum::Browser.new(options.except(*CUPRITE_OPTIONS))
450
+ @settings = Settings.new(options, browser.options.window_size)
451
+ browser.on_page { |page| prepare_page(page) }
452
+ Deprecations.extend_browser(browser, self)
453
+ browser
454
+ rescue StandardError
455
+ browser&.quit
456
+ raise
457
+ end
402
458
 
403
- browser.switch_to_window(handle)
404
- yield
459
+ def reset_browser
460
+ @windows.clear
461
+ browser.contexts.reset
462
+ rescue Ferrum::DeadBrowserError
463
+ browser.restart
405
464
  ensure
406
- browser.switch_to_window(original)
465
+ @settings.reset
466
+ forget_page
467
+ end
468
+
469
+ def settings
470
+ browser
471
+ @settings
472
+ end
473
+
474
+ def navigate(url)
475
+ return current_page.go_to(url) unless settings.pending_connection_errors?
476
+
477
+ current_page.go_to!(url)
478
+ rescue Ferrum::NavigationTimeoutError => e
479
+ raise if Ferrum::Deadline.current&.expired?
480
+
481
+ raise PendingConnectionsError.new(e.url, e.pendings)
482
+ end
483
+
484
+ def prepare_page(page)
485
+ @windows[page.target_id] = Window.new(page, @settings)
486
+ subscribe_to_js_errors(page) if @settings.js_errors?
487
+ page.execute(EXTENSION_JS)
488
+
489
+ width, height = @settings.window_size
490
+ page.resize(width: width, height: height) if width && height
491
+ emulate_viewport(page, width, height)
492
+ emulate_device_user_agent(page) if @settings.device_user_agent?
493
+
494
+ page.network.blacklist = @settings.url_blacklist if @settings.url_blacklist.any?
495
+ page.network.whitelist = @settings.url_whitelist if @settings.url_whitelist.any?
496
+ end
497
+
498
+ def subscribe_to_js_errors(page)
499
+ page.on("Runtime.exceptionThrown") do |params|
500
+ Thread.main.raise Ferrum::JavaScriptError, params["exceptionDetails"]
501
+ end
502
+ end
503
+
504
+ def emulate_viewport(page, width, height)
505
+ page.set_viewport(width: width || 0, height: height || 0, scale_factor: @settings.scale_factor,
506
+ mobile: @settings.mobile)
507
+ end
508
+
509
+ def emulate_device_user_agent(page)
510
+ page.network.emulate(user_agent: @settings.user_agent(browser.default_user_agent))
407
511
  end
408
512
 
409
513
  def capybara_base_url
@@ -72,12 +72,8 @@ module Capybara
72
72
  end
73
73
  end
74
74
 
75
- # Whether this Ferrum still has `Ferrum::PendingConnectionsError`.
76
- # @api private
77
- FERRUM_PENDING_CONNECTIONS_ERROR = Ferrum.const_defined?(:PendingConnectionsError, false)
78
-
79
75
  # 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)
76
+ class PendingConnectionsError < Ferrum::StatusError
81
77
  # @return [String] the URL being loaded
82
78
  attr_reader :url
83
79
  # @return [Array<String>] the URLs of the requests still pending
@@ -98,24 +94,6 @@ module Capybara
98
94
  end
99
95
  end
100
96
 
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
97
  # Raised when an element is no longer in the DOM. Capybara finds it again and retries.
120
98
  class ObsoleteNode < ClientError
121
99
  # @return [Node] the element
@@ -11,21 +11,21 @@ module Capybara
11
11
 
12
12
  # Finds the nodes matching a selector and answers Capybara's finder hints in the same call.
13
13
  #
14
- # @param browser [Browser]
14
+ # @param frame [Ferrum::Frame] the frame to search in
15
15
  # @param method [Symbol] `:css` or `:xpath`
16
16
  # @param selector [String]
17
17
  # @param within [Ferrum::Node, nil] the node to search in, the document by default
18
18
  # @param hints [Hash] the hints Capybara passes to `find_css` and `find_xpath`
19
19
  # @return [Array<Array(Ferrum::Node, Hash)>] each node with its initial cache
20
20
  # @raise [InvalidSelector]
21
- def find(browser, method, selector, within = nil, **hints)
21
+ def find(frame, method, selector, within = nil, **hints)
22
22
  # Capybara appends its text-match hint as `(expr)[...]`, but with `exact_text: true` the hint
23
23
  # is empty and the result `(expr)[]` is invalid XPath. An empty predicate is never valid, so
24
24
  # dropping it can't change a valid selector. Remove once teamcapybara/capybara#2860 is released.
25
25
  selector = selector.delete_suffix("[]") if method == :xpath
26
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)
27
+ nodes, *answers = frame.evaluate("_cuprite.findWithHints(method, selector, within, hints)",
28
+ method: method, selector: selector, within: within, hints: hints.to_h)
29
29
  nodes.zip(hints.caches(nodes.size, *answers))
30
30
  rescue Ferrum::JavaScriptError => e
31
31
  raise InvalidSelector.new(e.response, method, selector) if e.class_name == "InvalidSelector"
@@ -15,7 +15,7 @@ module Capybara
15
15
  # @return [void]
16
16
  # @raise [ObsoleteNode] when the element is no longer in the DOM
17
17
  def drag_by(x, y, steps: 1, delay: nil, scroll: true)
18
- mouse = driver.browser.mouse
18
+ mouse = driver.current_page.mouse
19
19
 
20
20
  command do
21
21
  x1, y1 = node.pinpoint
@@ -66,7 +66,7 @@ module Capybara
66
66
 
67
67
  def refocus
68
68
  node.evaluate("this.blur(); this.focus()")
69
- rescue Ferrum::NodeNotFoundError, Ferrum::NoExecutionContextError
69
+ rescue Ferrum::NodeNotFoundError, Ferrum::NoExecutionContextError, Ferrum::ExecutionContextDestroyedError
70
70
  nil
71
71
  end
72
72
  end
@@ -175,7 +175,7 @@ module Capybara
175
175
 
176
176
  # @return [String] the lowercase tag name, `"ShadowRoot"` for a shadow root
177
177
  def tag_name
178
- @tag_name ||= node.description["shadowRootType"] ? "ShadowRoot" : node.description["nodeName"].downcase
178
+ @tag_name ||= node.node_type == 11 ? "ShadowRoot" : node.tag_name
179
179
  end
180
180
 
181
181
  # @return [Boolean]
@@ -244,8 +244,8 @@ module Capybara
244
244
  def hover
245
245
  command do
246
246
  js(:scrollIntoViewport)
247
- x, y = pinpoint
248
- driver.browser.page.command("Input.dispatchMouseEvent", type: "mouseMoved", x: x, y: y)
247
+ x, y = node.pinpoint
248
+ driver.current_page.mouse.move(x: x, y: y)
249
249
  end
250
250
  end
251
251
 
@@ -284,8 +284,8 @@ module Capybara
284
284
  def drop(*args)
285
285
  command do
286
286
  if args[0].is_a?(String)
287
- driver.browser.execute("_cuprite.attachDropInput()")
288
- driver.browser.at_css("#_cuprite_drop_file").select_file(args)
287
+ driver.current_frame.execute("_cuprite.attachDropInput()")
288
+ driver.current_frame.at_css("#_cuprite_drop_file").select_file(args)
289
289
  js(:dropFile)
290
290
  else
291
291
  strings = args.flat_map { |arg| arg.map { |type, data| { "type" => type, "data" => data } } }
@@ -305,7 +305,7 @@ module Capybara
305
305
  wait = Ferrum::Page::NAVIGATION_WAIT
306
306
  next js(:trigger, event.to_s) unless event.to_s == "click" && wait.positive?
307
307
 
308
- driver.browser.await_navigation!(watch: wait) { js(:trigger, "click") }
308
+ driver.current_page.await_navigation!(watch: wait) { js(:trigger, "click") }
309
309
  end
310
310
  end
311
311
 
@@ -350,7 +350,7 @@ module Capybara
350
350
  return if keys.empty?
351
351
 
352
352
  command do
353
- keyboard = driver.browser.keyboard
353
+ keyboard = driver.current_page.keyboard
354
354
  keyboard.type(:end) if js(:focusForTyping)
355
355
  keyboard.type(keys)
356
356
  end
@@ -370,7 +370,7 @@ module Capybara
370
370
 
371
371
  # @return [Node, nil] the open shadow root of the element
372
372
  def shadow_root
373
- root = driver.browser.evaluate("node.shadowRoot", node: node)
373
+ root = node.evaluate("this.shadowRoot")
374
374
  root && self.class.new(driver, root)
375
375
  end
376
376
 
@@ -388,7 +388,7 @@ module Capybara
388
388
  private
389
389
 
390
390
  def find(method, selector, **hints)
391
- command { Finder.find(driver.browser, method, selector, node, **hints) }.map do |native, cache|
391
+ command { Finder.find(driver.current_frame, method, selector, node, **hints) }.map do |native, cache|
392
392
  self.class.new(driver, native, cache)
393
393
  end
394
394
  end
@@ -396,8 +396,8 @@ module Capybara
396
396
  def command(function = nil, *args)
397
397
  raise ObsoleteNode.new(self, nil) unless node.evaluate("this.isConnected")
398
398
 
399
- block_given? ? yield : js(function, *args)
400
- rescue Ferrum::NodeNotFoundError => e
399
+ check_modals { block_given? ? yield : js(function, *args) }
400
+ rescue Ferrum::NodeNotFoundError, Ferrum::ForeignObjectError, Ferrum::ExecutionContextDestroyedError => e
401
401
  raise ObsoleteNode.new(self, e.response)
402
402
  end
403
403
 
@@ -406,7 +406,11 @@ module Capybara
406
406
  x, y = options.values_at(:x, :y)
407
407
  offset = x && y ? { x: x, y: y, position: options[:offset] || :top } : {}
408
408
  command { before_click(name, offset) }
409
- node.click(modifiers: keys, offset: offset, delay: delay, **click)
409
+ check_modals { node.click(modifiers: keys, offset: offset, delay: delay, **click) }
410
+ end
411
+
412
+ def check_modals(&)
413
+ driver.window.check_modals(&)
410
414
  end
411
415
 
412
416
  def js(function, *args)
@@ -420,7 +424,7 @@ module Capybara
420
424
  # will be raised that we detected another element at this position.
421
425
  return if offset.any?
422
426
 
423
- x, y = pinpoint
427
+ x, y = node.pinpoint
424
428
  js(:mouseEventTest, name.to_s, x, y)
425
429
  rescue Ferrum::JavaScriptError => e
426
430
  raise MouseEventFailed, e.message if e.class_name == "MouseEventFailed"
@@ -429,24 +433,16 @@ module Capybara
429
433
  raise
430
434
  end
431
435
 
432
- def pinpoint(**options)
433
- node.pinpoint(**options)
434
- rescue Ferrum::BrowserError => e
435
- raise MouseEventFailed, "MouseEventFailed: click, none, 0, 0" if e.message == "Could not compute content quads."
436
-
437
- raise
438
- end
439
-
440
436
  def drag(other, modifiers, options)
441
- browser = driver.browser
437
+ page = driver.current_page
442
438
  delay, scroll, html5, drop_offset = options.values_at(:delay, :scroll, :html5, :drop_offset)
443
- browser.execute("_cuprite.dragMousedownTracker()")
439
+ driver.current_frame.execute("_cuprite.dragMousedownTracker()")
444
440
 
445
441
  node.scroll if scroll
446
442
  x1, y1 = node.pinpoint
447
443
 
448
- browser.mouse.move(x: x1, y: y1)
449
- browser.mouse.down
444
+ page.mouse.move(x: x1, y: y1)
445
+ page.mouse.down
450
446
  sleep delay if delay
451
447
 
452
448
  html5 = !js(:legacyDragCheck) if html5.nil?
@@ -454,12 +450,12 @@ module Capybara
454
450
  if html5
455
451
  keys = modifiers.map(&:to_s)
456
452
  driver.evaluate_async_script(DRAG_HTML5_JS, node, other, (delay || 0.05) * 1000, keys, drop_offset)
457
- browser.mouse.up
453
+ page.mouse.up
458
454
  else
459
455
  other.scroll if scroll
460
456
  x2, y2 = other.pinpoint(**drop_offset.to_h)
461
457
 
462
- browser.keyboard.hold(*modifiers) { browser.mouse.move(x: x2, y: y2, steps: options[:steps]).up }
458
+ page.keyboard.hold(*modifiers) { page.mouse.move(x: x2, y: y2, steps: options[:steps]).up }
463
459
  end
464
460
  end
465
461
 
@@ -4,6 +4,6 @@ module Capybara
4
4
  # Capybara driver for Chrome, built on Ferrum. See {Driver}.
5
5
  module Cuprite
6
6
  # The version of the gem.
7
- VERSION = "0.21"
7
+ VERSION = "0.22"
8
8
  end
9
9
  end