ferrum 0.20.2 → 0.21.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/README.md +2 -4
- data/lib/ferrum/accessibility.rb +40 -6
- data/lib/ferrum/browser/command.rb +2 -11
- data/lib/ferrum/browser/options/chrome.rb +58 -1
- data/lib/ferrum/browser/options.rb +63 -9
- data/lib/ferrum/browser/process.rb +12 -4
- data/lib/ferrum/browser/xvfb.rb +22 -2
- data/lib/ferrum/browser.rb +74 -35
- data/lib/ferrum/client/web_socket.rb +13 -6
- data/lib/ferrum/client.rb +119 -81
- data/lib/ferrum/concurrency/awaitable_map.rb +171 -0
- data/lib/ferrum/concurrency/cell.rb +128 -0
- data/lib/ferrum/concurrency/dispatcher.rb +146 -0
- data/lib/ferrum/console.rb +3 -2
- data/lib/ferrum/context.rb +162 -27
- data/lib/ferrum/contexts.rb +45 -22
- data/lib/ferrum/cookies/collection.rb +93 -0
- data/lib/ferrum/cookies/cookie.rb +0 -11
- data/lib/ferrum/cookies.rb +79 -86
- data/lib/ferrum/deadline.rb +140 -0
- data/lib/ferrum/dialog.rb +22 -0
- data/lib/ferrum/downloads.rb +83 -10
- data/lib/ferrum/errors.rb +96 -6
- data/lib/ferrum/frame/dom.rb +147 -38
- data/lib/ferrum/frame/runtime.rb +40 -32
- data/lib/ferrum/frame/script.rb +7 -38
- data/lib/ferrum/frame.rb +57 -20
- data/lib/ferrum/headers.rb +32 -27
- data/lib/ferrum/interceptable/chain.rb +99 -0
- data/lib/ferrum/interceptable.rb +44 -27
- data/lib/ferrum/isolated_world.rb +9 -10
- data/lib/ferrum/keyboard.rb +27 -6
- data/lib/ferrum/mouse.rb +139 -52
- data/lib/ferrum/network/auth_request.rb +42 -7
- data/lib/ferrum/network/error.rb +34 -4
- data/lib/ferrum/network/exchange.rb +35 -7
- data/lib/ferrum/network/intercepted_request.rb +42 -9
- data/lib/ferrum/network/request.rb +33 -9
- data/lib/ferrum/network/request_params.rb +11 -2
- data/lib/ferrum/network/response.rb +23 -2
- data/lib/ferrum/network.rb +267 -64
- data/lib/ferrum/node.rb +430 -62
- data/lib/ferrum/page/animation.rb +4 -0
- data/lib/ferrum/page/cookies.rb +133 -0
- data/lib/ferrum/page/frames.rb +7 -7
- data/lib/ferrum/page/navigation.rb +430 -0
- data/lib/ferrum/page/screencast.rb +4 -0
- data/lib/ferrum/page/screenshot.rb +13 -1
- data/lib/ferrum/page/stream.rb +13 -5
- data/lib/ferrum/page/tracing.rb +12 -7
- data/lib/ferrum/page.rb +168 -190
- data/lib/ferrum/remote_object.rb +4 -0
- data/lib/ferrum/target.rb +49 -11
- data/lib/ferrum/utils/deprecate.rb +73 -2
- data/lib/ferrum/utils/poll.rb +57 -0
- data/lib/ferrum/version.rb +1 -1
- data/lib/ferrum/worker.rb +34 -19
- data/lib/ferrum.rb +5 -1
- metadata +11 -6
- data/lib/ferrum/browser/options/base.rb +0 -112
- data/lib/ferrum/client/subscriber.rb +0 -145
- data/lib/ferrum/frame/runtime/legacy.rb +0 -336
- data/lib/ferrum/utils/event.rb +0 -33
data/lib/ferrum/frame/dom.rb
CHANGED
|
@@ -26,6 +26,9 @@ module Ferrum
|
|
|
26
26
|
# `<link>` tags.
|
|
27
27
|
#
|
|
28
28
|
module DOM
|
|
29
|
+
WAIT_INTERVAL = 0.05
|
|
30
|
+
WAIT_FOR_SELECTOR_DEPRECATED = "wait_for_selector will be removed in Ferrum 0.22. Use wait_css(selector) or " \
|
|
31
|
+
"wait_xpath(selector) instead."
|
|
29
32
|
SCRIPT_SRC_TAG = <<~JS
|
|
30
33
|
function(url, type) {
|
|
31
34
|
const script = document.createElement("script");
|
|
@@ -61,31 +64,41 @@ module Ferrum
|
|
|
61
64
|
}
|
|
62
65
|
JS
|
|
63
66
|
|
|
67
|
+
#
|
|
68
|
+
# @deprecated Will be removed in Ferrum 0.22. Use `page.url`, or
|
|
69
|
+
# `frame.page.url` from a frame.
|
|
64
70
|
#
|
|
65
71
|
# Returns current top window `location href`.
|
|
66
72
|
#
|
|
67
73
|
# @return [String]
|
|
68
74
|
# The window's current URL.
|
|
69
75
|
#
|
|
70
|
-
# @
|
|
71
|
-
# browser.go_to("https://google.com/")
|
|
72
|
-
# browser.current_url # => "https://www.google.com/"
|
|
76
|
+
# @raise [TimeoutError]
|
|
73
77
|
#
|
|
74
78
|
def current_url
|
|
79
|
+
Utils::Deprecate.warn(
|
|
80
|
+
"#current_url will be removed in Ferrum 0.22. Use page.url, or frame.page.url from a frame, " \
|
|
81
|
+
"frame.url is the frame's own URL."
|
|
82
|
+
)
|
|
75
83
|
evaluate("window.top.location.href")
|
|
76
84
|
end
|
|
77
85
|
|
|
86
|
+
#
|
|
87
|
+
# @deprecated Will be removed in Ferrum 0.22. Use `page.title`, or
|
|
88
|
+
# `frame.page.title` from a frame.
|
|
78
89
|
#
|
|
79
90
|
# Returns current top window title.
|
|
80
91
|
#
|
|
81
92
|
# @return [String]
|
|
82
93
|
# The window's current title.
|
|
83
94
|
#
|
|
84
|
-
# @
|
|
85
|
-
# browser.go_to("https://google.com/")
|
|
86
|
-
# browser.current_title # => "Google"
|
|
95
|
+
# @raise [TimeoutError]
|
|
87
96
|
#
|
|
88
97
|
def current_title
|
|
98
|
+
Utils::Deprecate.warn(
|
|
99
|
+
"#current_title will be removed in Ferrum 0.22. Use page.title, or frame.page.title from a frame, " \
|
|
100
|
+
"frame.title is the frame's own title."
|
|
101
|
+
)
|
|
89
102
|
evaluate("window.top.document.title")
|
|
90
103
|
end
|
|
91
104
|
|
|
@@ -96,6 +109,8 @@ module Ferrum
|
|
|
96
109
|
# The serialized `<!DOCTYPE ...>` declaration, or +nil+ if the
|
|
97
110
|
# document has none.
|
|
98
111
|
#
|
|
112
|
+
# @raise [TimeoutError]
|
|
113
|
+
#
|
|
99
114
|
# @example
|
|
100
115
|
# browser.go_to("https://example.com")
|
|
101
116
|
# browser.doctype # => "<!DOCTYPE html>"
|
|
@@ -105,19 +120,34 @@ module Ferrum
|
|
|
105
120
|
end
|
|
106
121
|
|
|
107
122
|
#
|
|
108
|
-
# Returns
|
|
123
|
+
# Returns the HTML of the whole document, `<html>` included.
|
|
109
124
|
#
|
|
110
125
|
# @return [String]
|
|
111
|
-
# The
|
|
126
|
+
# The document's `outerHTML`, empty when it has no root element.
|
|
127
|
+
#
|
|
128
|
+
# @raise [TimeoutError]
|
|
112
129
|
#
|
|
113
130
|
# @example
|
|
114
131
|
# browser.go_to("https://google.com/")
|
|
115
|
-
# browser.
|
|
132
|
+
# browser.html # => '<html itemscope="" itemtype="http://schema.org/WebPage" lang="ru"><head>...
|
|
116
133
|
#
|
|
117
|
-
def
|
|
134
|
+
def html
|
|
118
135
|
evaluate("document.documentElement?.outerHTML") || ""
|
|
119
136
|
end
|
|
120
137
|
|
|
138
|
+
#
|
|
139
|
+
# @deprecated Will be removed in Ferrum 0.22. Use {#html}, it was never
|
|
140
|
+
# only `<body>`.
|
|
141
|
+
#
|
|
142
|
+
# @return [String]
|
|
143
|
+
#
|
|
144
|
+
# @raise [TimeoutError]
|
|
145
|
+
#
|
|
146
|
+
def body
|
|
147
|
+
Utils::Deprecate.warn("body will be removed in Ferrum 0.22. Use html instead.", user_code: true)
|
|
148
|
+
html
|
|
149
|
+
end
|
|
150
|
+
|
|
121
151
|
#
|
|
122
152
|
# Returns the element in which the window is embedded.
|
|
123
153
|
#
|
|
@@ -125,6 +155,8 @@ module Ferrum
|
|
|
125
155
|
# The element in which the window is embedded, `nil` for the main frame
|
|
126
156
|
# and for a frame from a different origin than its parent.
|
|
127
157
|
#
|
|
158
|
+
# @raise [TimeoutError]
|
|
159
|
+
#
|
|
128
160
|
# @example
|
|
129
161
|
# browser.go_to("https://developer.mozilla.org/en-US/docs/Web/HTML/Element/iframe")
|
|
130
162
|
# frame = browser.frames.last
|
|
@@ -147,6 +179,8 @@ module Ferrum
|
|
|
147
179
|
# @return [Array<Node>]
|
|
148
180
|
# The matching nodes.
|
|
149
181
|
#
|
|
182
|
+
# @raise [TimeoutError]
|
|
183
|
+
#
|
|
150
184
|
# @example
|
|
151
185
|
# browser.go_to("https://github.com/")
|
|
152
186
|
# browser.xpath("//a[@aria-label='Issues you created']") # => [Node]
|
|
@@ -181,6 +215,8 @@ module Ferrum
|
|
|
181
215
|
# @return [Node, nil]
|
|
182
216
|
# The matching node.
|
|
183
217
|
#
|
|
218
|
+
# @raise [TimeoutError]
|
|
219
|
+
#
|
|
184
220
|
# @example
|
|
185
221
|
# browser.go_to("https://github.com/")
|
|
186
222
|
# browser.at_xpath("//a[@aria-label='Issues you created']") # => Node
|
|
@@ -208,6 +244,8 @@ module Ferrum
|
|
|
208
244
|
# @return [Array<Node>]
|
|
209
245
|
# The matching nodes.
|
|
210
246
|
#
|
|
247
|
+
# @raise [TimeoutError]
|
|
248
|
+
#
|
|
211
249
|
# @example
|
|
212
250
|
# browser.go_to("https://github.com/")
|
|
213
251
|
# browser.css("a[aria-label='Issues you created']") # => [Node]
|
|
@@ -235,6 +273,8 @@ module Ferrum
|
|
|
235
273
|
# @return [Node, nil]
|
|
236
274
|
# The matching node.
|
|
237
275
|
#
|
|
276
|
+
# @raise [TimeoutError]
|
|
277
|
+
#
|
|
238
278
|
# @example
|
|
239
279
|
# browser.go_to("https://github.com/")
|
|
240
280
|
# browser.at_css("a[aria-label='Issues you created']") # => Node
|
|
@@ -250,52 +290,117 @@ module Ferrum
|
|
|
250
290
|
evaluate(expr, selector, within)
|
|
251
291
|
end
|
|
252
292
|
|
|
293
|
+
#
|
|
294
|
+
# Polls {#at_css} until a node matches the selector.
|
|
295
|
+
#
|
|
296
|
+
# @param [String] selector
|
|
297
|
+
# The CSS selector.
|
|
298
|
+
#
|
|
299
|
+
# @param [Node, nil] within
|
|
300
|
+
# The parent node to search within.
|
|
301
|
+
#
|
|
302
|
+
# @param [Numeric, Deadline, nil] timeout
|
|
303
|
+
# How long to keep trying, the page's `timeout` by default.
|
|
304
|
+
#
|
|
305
|
+
# @param [Numeric] interval
|
|
306
|
+
# Seconds to sleep between the attempts.
|
|
307
|
+
#
|
|
308
|
+
# @return [Node, nil]
|
|
309
|
+
# The matching node, `nil` if none shows up in time.
|
|
310
|
+
#
|
|
311
|
+
# @example
|
|
312
|
+
# browser.wait_css("#appears_later") # => Node
|
|
313
|
+
#
|
|
314
|
+
def wait_css(selector, within: nil, timeout: nil, interval: WAIT_INTERVAL)
|
|
315
|
+
Utils::Poll.call(timeout || @page.timeout, interval) { at_css(selector, within: within) }
|
|
316
|
+
end
|
|
317
|
+
|
|
318
|
+
#
|
|
319
|
+
# Same as {#wait_css}, but raises if no node matches in time.
|
|
320
|
+
#
|
|
321
|
+
# @param [String] selector
|
|
322
|
+
#
|
|
323
|
+
# @param [Hash] options
|
|
324
|
+
# The options of {#wait_css}.
|
|
325
|
+
#
|
|
326
|
+
# @return [Node]
|
|
327
|
+
#
|
|
328
|
+
# @raise [TimeoutError]
|
|
329
|
+
# If no node matches in time.
|
|
330
|
+
#
|
|
331
|
+
def wait_css!(selector, **options)
|
|
332
|
+
wait_css(selector, **options) || raise(TimeoutError)
|
|
333
|
+
end
|
|
334
|
+
|
|
335
|
+
#
|
|
336
|
+
# Polls {#at_xpath} until a node matches the selector.
|
|
337
|
+
#
|
|
338
|
+
# @param [String] selector
|
|
339
|
+
# The XPath selector.
|
|
340
|
+
#
|
|
341
|
+
# @param [Node, nil] within
|
|
342
|
+
# The parent node to search within.
|
|
343
|
+
#
|
|
344
|
+
# @param [Numeric, Deadline, nil] timeout
|
|
345
|
+
# How long to keep trying, the page's `timeout` by default.
|
|
346
|
+
#
|
|
347
|
+
# @param [Numeric] interval
|
|
348
|
+
# Seconds to sleep between the attempts.
|
|
349
|
+
#
|
|
350
|
+
# @return [Node, nil]
|
|
351
|
+
# The matching node, `nil` if none shows up in time.
|
|
352
|
+
#
|
|
353
|
+
# @example
|
|
354
|
+
# browser.wait_xpath("//div[@id='appears_later']") # => Node
|
|
355
|
+
#
|
|
356
|
+
def wait_xpath(selector, within: nil, timeout: nil, interval: WAIT_INTERVAL)
|
|
357
|
+
Utils::Poll.call(timeout || @page.timeout, interval) { at_xpath(selector, within: within) }
|
|
358
|
+
end
|
|
359
|
+
|
|
360
|
+
#
|
|
361
|
+
# Same as {#wait_xpath}, but raises if no node matches in time.
|
|
362
|
+
#
|
|
363
|
+
# @param [String] selector
|
|
364
|
+
#
|
|
365
|
+
# @param [Hash] options
|
|
366
|
+
# The options of {#wait_xpath}.
|
|
367
|
+
#
|
|
368
|
+
# @return [Node]
|
|
369
|
+
#
|
|
370
|
+
# @raise [TimeoutError]
|
|
371
|
+
# If no node matches in time.
|
|
372
|
+
#
|
|
373
|
+
def wait_xpath!(selector, **options)
|
|
374
|
+
wait_xpath(selector, **options) || raise(TimeoutError)
|
|
375
|
+
end
|
|
376
|
+
|
|
253
377
|
#
|
|
254
378
|
# Finds a node by using a CSS path or XPath selector, retrying until
|
|
255
|
-
# it appears or `timeout` elapses.
|
|
256
|
-
#
|
|
257
|
-
#
|
|
379
|
+
# it appears or `timeout` elapses.
|
|
380
|
+
#
|
|
381
|
+
# @deprecated Will be removed in Ferrum 0.22. Use {#wait_css} or {#wait_xpath}.
|
|
258
382
|
#
|
|
259
383
|
# @param [String, nil] css
|
|
260
|
-
# The CSS path selector. Mutually exclusive with `xpath`.
|
|
261
384
|
#
|
|
262
385
|
# @param [String, nil] xpath
|
|
263
|
-
# The XPath selector. Mutually exclusive with `css`.
|
|
264
386
|
#
|
|
265
387
|
# @param [Node, nil] within
|
|
266
|
-
# The parent node to search within.
|
|
267
388
|
#
|
|
268
|
-
# @param [
|
|
269
|
-
# How long to keep retrying, in seconds.
|
|
389
|
+
# @param [Numeric, Deadline] timeout
|
|
270
390
|
#
|
|
271
391
|
# @param [Float, Integer] interval
|
|
272
|
-
# How long to sleep between attempts, in seconds.
|
|
273
392
|
#
|
|
274
393
|
# @return [Node, nil]
|
|
275
|
-
# The matching node, or `nil` if `timeout` elapses without a match.
|
|
276
394
|
#
|
|
277
395
|
# @raise [ArgumentError]
|
|
278
396
|
# If neither or both of `css`/`xpath` are given.
|
|
279
397
|
#
|
|
280
|
-
|
|
281
|
-
|
|
282
|
-
|
|
283
|
-
#
|
|
284
|
-
def wait_for_selector(css: nil, xpath: nil, within: nil, timeout: @page.timeout, interval: 0.05)
|
|
285
|
-
raise ArgumentError, "pass either `css:` or `xpath:`" if css && xpath
|
|
286
|
-
|
|
287
|
-
selector, method_name = css ? [css, :at_css] : [xpath, :at_xpath]
|
|
288
|
-
raise ArgumentError, "pass either `css:` or `xpath:`" unless selector
|
|
289
|
-
|
|
290
|
-
start = Utils::ElapsedTime.monotonic_time
|
|
398
|
+
def wait_for_selector(css: nil, xpath: nil, within: nil, timeout: @page.timeout, interval: WAIT_INTERVAL)
|
|
399
|
+
Utils::Deprecate.warn(WAIT_FOR_SELECTOR_DEPRECATED, user_code: true)
|
|
400
|
+
raise ArgumentError, "pass either `css:` or `xpath:`" if (css && xpath) || !(css || xpath)
|
|
291
401
|
|
|
292
|
-
|
|
293
|
-
|
|
294
|
-
|
|
295
|
-
sleep(interval)
|
|
296
|
-
end
|
|
297
|
-
|
|
298
|
-
node
|
|
402
|
+
options = { within: within, timeout: timeout, interval: interval }
|
|
403
|
+
css ? wait_css(css, **options) : wait_xpath(xpath, **options)
|
|
299
404
|
end
|
|
300
405
|
|
|
301
406
|
#
|
|
@@ -309,6 +414,8 @@ module Ferrum
|
|
|
309
414
|
#
|
|
310
415
|
# @param [String] type
|
|
311
416
|
#
|
|
417
|
+
# @raise [TimeoutError]
|
|
418
|
+
#
|
|
312
419
|
# @example
|
|
313
420
|
# browser.add_script_tag(url: "http://example.com/stylesheet.css") # => true
|
|
314
421
|
#
|
|
@@ -335,6 +442,8 @@ module Ferrum
|
|
|
335
442
|
#
|
|
336
443
|
# @param [String, nil] content
|
|
337
444
|
#
|
|
445
|
+
# @raise [TimeoutError]
|
|
446
|
+
#
|
|
338
447
|
# @example
|
|
339
448
|
# browser.add_style_tag(content: "h1 { font-size: 40px; }") # => true
|
|
340
449
|
#
|
data/lib/ferrum/frame/runtime.rb
CHANGED
|
@@ -2,7 +2,6 @@
|
|
|
2
2
|
|
|
3
3
|
require "singleton"
|
|
4
4
|
require "ferrum/frame/script"
|
|
5
|
-
require "ferrum/frame/runtime/legacy"
|
|
6
5
|
|
|
7
6
|
module Ferrum
|
|
8
7
|
#
|
|
@@ -140,21 +139,11 @@ module Ferrum
|
|
|
140
139
|
# is sent unchanged, with `this` bound to the node and the arguments
|
|
141
140
|
# `["#", "!"]`, and comes back as `"#main!"`.
|
|
142
141
|
#
|
|
143
|
-
# ## Scripts written for the old API
|
|
144
|
-
#
|
|
145
|
-
# Until Ferrum 0.21, a call that only the old API could have made runs
|
|
146
|
-
# the old way through {Legacy} and warns when its result will change.
|
|
147
|
-
# That's a call without named arguments, or one whose script reads
|
|
148
|
-
# `arguments` or also passes positional ones, unless the script is a
|
|
149
|
-
# declaration or uses `await`, or the method is {#evaluate_handle} or
|
|
150
|
-
# {Node#execute}.
|
|
151
|
-
# `FERRUM_JS_API=new` sends every call through the steps above.
|
|
152
|
-
#
|
|
153
142
|
module Runtime
|
|
154
|
-
include Legacy
|
|
155
|
-
|
|
156
143
|
INTERMITTENT_ATTEMPTS = ENV.fetch("FERRUM_INTERMITTENT_ATTEMPTS", 6).to_i
|
|
157
144
|
INTERMITTENT_SLEEP = ENV.fetch("FERRUM_INTERMITTENT_SLEEP", 0.1).to_f
|
|
145
|
+
OBJECT_ID_DEPRECATED = "Passing a Hash with \"objectId\" as a remote object will be removed in Ferrum 0.22, " \
|
|
146
|
+
"pass a Ferrum::RemoteObject instead."
|
|
158
147
|
DEEP_SERIALIZATION = {
|
|
159
148
|
serialization: "deep",
|
|
160
149
|
additionalParameters: { maxNodeDepth: 0, includeShadowTree: "none" }
|
|
@@ -216,6 +205,8 @@ module Ferrum
|
|
|
216
205
|
#
|
|
217
206
|
# @return [RemoteObject, Node, Object]
|
|
218
207
|
#
|
|
208
|
+
# @raise [TimeoutError]
|
|
209
|
+
#
|
|
219
210
|
# @example
|
|
220
211
|
# list = page.evaluate_handle("document.querySelectorAll('li')")
|
|
221
212
|
# page.evaluate("Array.from(nodes).map(n => n.textContent)", nodes: list)
|
|
@@ -232,9 +223,11 @@ module Ferrum
|
|
|
232
223
|
# @param (see #evaluate)
|
|
233
224
|
# @option (see #evaluate)
|
|
234
225
|
#
|
|
235
|
-
# @return [
|
|
226
|
+
# @return [nil]
|
|
236
227
|
# Always `true`.
|
|
237
228
|
#
|
|
229
|
+
# @raise [TimeoutError]
|
|
230
|
+
#
|
|
238
231
|
# @example
|
|
239
232
|
# browser.execute("window.scrollBy(0, 100)") # => true
|
|
240
233
|
# browser.execute(<<~JS, url: "/next")
|
|
@@ -244,7 +237,7 @@ module Ferrum
|
|
|
244
237
|
#
|
|
245
238
|
def execute(expression, *positional, **keywords)
|
|
246
239
|
run(expression, positional, keywords, mode: :none)
|
|
247
|
-
|
|
240
|
+
nil
|
|
248
241
|
end
|
|
249
242
|
|
|
250
243
|
#
|
|
@@ -262,6 +255,8 @@ module Ferrum
|
|
|
262
255
|
#
|
|
263
256
|
# @return [Object]
|
|
264
257
|
#
|
|
258
|
+
# @raise [TimeoutError]
|
|
259
|
+
#
|
|
265
260
|
def evaluate_in(object, expression, positional, keywords, mode: :value)
|
|
266
261
|
run(expression, positional, keywords, mode: mode, on: object)
|
|
267
262
|
end
|
|
@@ -276,13 +271,10 @@ module Ferrum
|
|
|
276
271
|
#
|
|
277
272
|
def run(expression, positional, keywords, mode:, on: nil)
|
|
278
273
|
script = Script.new(expression)
|
|
279
|
-
if legacy?(script, positional, keywords, mode: mode, on: on)
|
|
280
|
-
return run_legacy(script, positional, keywords, mode: mode, on: on)
|
|
281
|
-
end
|
|
282
|
-
|
|
283
274
|
named = keywords.except(:timeout)
|
|
284
275
|
params, values = arguments_for(script, positional, named)
|
|
285
|
-
|
|
276
|
+
deadline = script_deadline(keywords[:timeout])
|
|
277
|
+
Deadline.within(deadline) { call(wrap(script, params, mode), values, on: on, mode: mode, deadline: deadline) }
|
|
286
278
|
end
|
|
287
279
|
|
|
288
280
|
#
|
|
@@ -340,8 +332,8 @@ module Ferrum
|
|
|
340
332
|
# Waiting for the response is what bounds the script, so a page that
|
|
341
333
|
# replaces `Promise` or `setTimeout`, or throttles timers, can't defeat
|
|
342
334
|
# it.
|
|
343
|
-
def
|
|
344
|
-
(timeout || @page.timeout)
|
|
335
|
+
def script_deadline(timeout)
|
|
336
|
+
Deadline.wrap(timeout || @page.timeout)
|
|
345
337
|
end
|
|
346
338
|
|
|
347
339
|
#
|
|
@@ -353,18 +345,18 @@ module Ferrum
|
|
|
353
345
|
# `:value` resolves the result into Ruby, `:handle` returns a
|
|
354
346
|
# {RemoteObject}, and `:none` discards it.
|
|
355
347
|
#
|
|
356
|
-
def call(declaration, values, on:, mode:,
|
|
348
|
+
def call(declaration, values, on:, mode:, deadline:)
|
|
357
349
|
# do not rescue -> retry if we operate on an existing node
|
|
358
350
|
errors = on ? [] : [NodeNotFoundError, NoExecutionContextError]
|
|
359
351
|
|
|
360
352
|
serialization = mode == :value ? DEEP_SERIALIZATION : SHALLOW_SERIALIZATION
|
|
361
353
|
response = Utils::Attempt.with_retry(errors: errors, max: INTERMITTENT_ATTEMPTS, wait: INTERMITTENT_SLEEP) do
|
|
362
|
-
call_function(declaration, values, call_target(on), serialization,
|
|
354
|
+
call_function(declaration, values, call_target(on, deadline), serialization, deadline)
|
|
363
355
|
end
|
|
364
356
|
handle_error(response, declaration)
|
|
365
357
|
|
|
366
358
|
result = response["result"]
|
|
367
|
-
result = await_promise(result, serialization,
|
|
359
|
+
result = await_promise(result, serialization, deadline) if thenable?(result)
|
|
368
360
|
case mode
|
|
369
361
|
when :none then nil
|
|
370
362
|
when :handle then handle_remote_object(result)
|
|
@@ -372,12 +364,14 @@ module Ferrum
|
|
|
372
364
|
end
|
|
373
365
|
end
|
|
374
366
|
|
|
375
|
-
def call_function(declaration, values, target, serialization,
|
|
367
|
+
def call_function(declaration, values, target, serialization, deadline)
|
|
376
368
|
@page.command("Runtime.callFunctionOn",
|
|
377
|
-
timeout:
|
|
369
|
+
timeout: deadline, slowmoable: true, serializationOptions: serialization,
|
|
378
370
|
functionDeclaration: declaration, arguments: prepare_args(values), **target)
|
|
379
371
|
rescue TimeoutError
|
|
380
372
|
raise ScriptTimeoutError
|
|
373
|
+
rescue BrowserError => e
|
|
374
|
+
raise_deep_error(e)
|
|
381
375
|
end
|
|
382
376
|
|
|
383
377
|
# A promise, or a plain object that may be one, such as the promises of
|
|
@@ -389,23 +383,31 @@ module Ferrum
|
|
|
389
383
|
# Awaited outside the retries: by now the script has run, and running
|
|
390
384
|
# it again after a navigation would repeat what it did. Awaiting an
|
|
391
385
|
# object that isn't thenable returns it as it is.
|
|
392
|
-
def await_promise(promise, serialization,
|
|
386
|
+
def await_promise(promise, serialization, deadline)
|
|
393
387
|
response = @page.command("Runtime.callFunctionOn",
|
|
394
|
-
timeout:
|
|
388
|
+
timeout: deadline, awaitPromise: true, serializationOptions: serialization,
|
|
395
389
|
functionDeclaration: "function() { return this }", objectId: promise["objectId"])
|
|
396
390
|
handle_error(response)
|
|
397
391
|
response["result"]
|
|
398
392
|
rescue TimeoutError
|
|
399
393
|
raise ScriptTimeoutError
|
|
394
|
+
rescue BrowserError => e
|
|
395
|
+
raise_deep_error(e)
|
|
396
|
+
end
|
|
397
|
+
|
|
398
|
+
def raise_deep_error(error)
|
|
399
|
+
raise error unless error.message.to_s.include?("CBOR: stack limit exceeded")
|
|
400
|
+
|
|
401
|
+
raise ResultTooDeepError, error.response
|
|
400
402
|
end
|
|
401
403
|
|
|
402
|
-
def call_target(on)
|
|
404
|
+
def call_target(on, deadline)
|
|
403
405
|
if on.is_a?(RemoteObject)
|
|
404
406
|
{ objectId: on.remote_id }
|
|
405
407
|
elsif on
|
|
406
408
|
{ objectId: resolve_node(on) }
|
|
407
409
|
else
|
|
408
|
-
{ executionContextId: execution_id! }
|
|
410
|
+
{ executionContextId: execution_id!(deadline) }
|
|
409
411
|
end
|
|
410
412
|
end
|
|
411
413
|
|
|
@@ -466,6 +468,9 @@ module Ferrum
|
|
|
466
468
|
value.to_h { |k, v| [k, deserialize(v, references, path, converted)] }
|
|
467
469
|
when "node"
|
|
468
470
|
Node.new(self, value)
|
|
471
|
+
when "map", "set"
|
|
472
|
+
value.flatten(1).each { |v| deserialize(v, references, path, converted) if v.is_a?(Hash) }
|
|
473
|
+
{}
|
|
469
474
|
else
|
|
470
475
|
{}
|
|
471
476
|
end
|
|
@@ -492,7 +497,10 @@ module Ferrum
|
|
|
492
497
|
when RemoteObject
|
|
493
498
|
{ objectId: arg.remote_id }
|
|
494
499
|
when Hash
|
|
495
|
-
|
|
500
|
+
next { value: arg } unless arg["objectId"]
|
|
501
|
+
|
|
502
|
+
Utils::Deprecate.warn(OBJECT_ID_DEPRECATED, user_code: true)
|
|
503
|
+
{ objectId: arg["objectId"] }
|
|
496
504
|
else
|
|
497
505
|
{ value: arg }
|
|
498
506
|
end
|
data/lib/ferrum/frame/script.rb
CHANGED
|
@@ -77,7 +77,7 @@ module Ferrum
|
|
|
77
77
|
def initialize(source)
|
|
78
78
|
@source = source.to_s
|
|
79
79
|
@code_start = @code_end = 0
|
|
80
|
-
@
|
|
80
|
+
@awaits = false
|
|
81
81
|
@scanned = @source.valid_encoding? && (@source.encoding == Encoding::UTF_8 || @source.ascii_only?)
|
|
82
82
|
@scanned ? scan : @code_end = @source.bytesize
|
|
83
83
|
@code = @source.byteslice(@code_start...@code_end)
|
|
@@ -105,54 +105,23 @@ module Ferrum
|
|
|
105
105
|
# @return [Boolean]
|
|
106
106
|
#
|
|
107
107
|
def awaits?
|
|
108
|
-
|
|
109
|
-
end
|
|
110
|
-
|
|
111
|
-
#
|
|
112
|
-
# Whether the script mentions the given word as code, rather than as a
|
|
113
|
-
# property name or inside a string or comment.
|
|
114
|
-
#
|
|
115
|
-
# @param [String] word
|
|
116
|
-
#
|
|
117
|
-
# @return [Boolean]
|
|
118
|
-
#
|
|
119
|
-
def uses?(word)
|
|
120
|
-
@words.include?(word)
|
|
121
|
-
end
|
|
122
|
-
|
|
123
|
-
#
|
|
124
|
-
# Whether the code is several statements, separated by a top-level
|
|
125
|
-
# semicolon, as in `"a(); b"`, or by a line break that can only end a
|
|
126
|
-
# statement, as in `"a()\nb"`, unlike `"a\n.b()"`.
|
|
127
|
-
#
|
|
128
|
-
# @return [Boolean]
|
|
129
|
-
#
|
|
130
|
-
def several_statements?
|
|
131
|
-
!@last_statement_start.nil?
|
|
108
|
+
@awaits
|
|
132
109
|
end
|
|
133
110
|
|
|
134
111
|
#
|
|
135
112
|
# The code split before its last statement, so that the value of that
|
|
136
|
-
# one can be returned
|
|
113
|
+
# one can be returned. Statements are separated by a top-level
|
|
114
|
+
# semicolon, as in `"a(); b"`, or by a line break that can only end a
|
|
115
|
+
# statement, as in `"a = 1\nb"`, which gives `["a = 1\n", "b"]`.
|
|
137
116
|
#
|
|
138
117
|
# @return [Array(String, String)]
|
|
139
118
|
#
|
|
140
119
|
def split_last_statement
|
|
141
|
-
return ["", @code] unless
|
|
120
|
+
return ["", @code] unless @last_statement_start
|
|
142
121
|
|
|
143
122
|
[@source.byteslice(@code_start...@last_statement_start), @source.byteslice(@last_statement_start...@code_end)]
|
|
144
123
|
end
|
|
145
124
|
|
|
146
|
-
#
|
|
147
|
-
# Whether a line break comes before the code, which right after a
|
|
148
|
-
# `return` ends that statement.
|
|
149
|
-
#
|
|
150
|
-
# @return [Boolean]
|
|
151
|
-
#
|
|
152
|
-
def leading_line_break?
|
|
153
|
-
@scanned && !@code.empty? && @source.byteslice(0...@code_start).include?("\n")
|
|
154
|
-
end
|
|
155
|
-
|
|
156
125
|
private
|
|
157
126
|
|
|
158
127
|
# Whether the code is one parenthesized group, as in `(function() {})`.
|
|
@@ -230,7 +199,7 @@ module Ferrum
|
|
|
230
199
|
|
|
231
200
|
def scan_token(scanner, stack, previous)
|
|
232
201
|
if scanner.scan(WORD)
|
|
233
|
-
@
|
|
202
|
+
@awaits ||= scanner.matched == "await" && previous != "."
|
|
234
203
|
scanner.matched
|
|
235
204
|
elsif scanner.scan(STRING) || (regexp_allowed?(previous) && scanner.scan(REGEXP))
|
|
236
205
|
scanner.matched
|