ferrum 0.19.2 → 0.20.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 +60 -0
- data/lib/ferrum/browser/binary.rb +1 -2
- data/lib/ferrum/browser/options.rb +33 -12
- data/lib/ferrum/browser/process/output.rb +143 -0
- data/lib/ferrum/browser/process.rb +42 -69
- data/lib/ferrum/browser.rb +9 -23
- data/lib/ferrum/client/web_socket.rb +4 -15
- data/lib/ferrum/client.rb +1 -1
- data/lib/ferrum/console/location.rb +42 -0
- data/lib/ferrum/console/message.rb +230 -0
- data/lib/ferrum/console.rb +157 -0
- data/lib/ferrum/context.rb +23 -15
- data/lib/ferrum/contexts.rb +41 -18
- data/lib/ferrum/errors.rb +7 -5
- data/lib/ferrum/frame/dom.rb +50 -44
- data/lib/ferrum/frame/runtime/legacy.rb +336 -0
- data/lib/ferrum/frame/runtime.rb +416 -186
- data/lib/ferrum/frame/script.rb +289 -0
- data/lib/ferrum/frame.rb +33 -4
- data/lib/ferrum/interceptable.rb +19 -9
- data/lib/ferrum/isolated_world.rb +94 -0
- data/lib/ferrum/keyboard.rb +95 -8
- data/lib/ferrum/log/annotated.rb +77 -0
- data/lib/ferrum/log/default.rb +55 -0
- data/lib/ferrum/log/json.rb +32 -0
- data/lib/ferrum/log/paint.rb +28 -0
- data/lib/ferrum/log.rb +247 -0
- data/lib/ferrum/mouse.rb +16 -8
- data/lib/ferrum/node.rb +127 -52
- data/lib/ferrum/page/frames.rb +10 -3
- data/lib/ferrum/page/screenshot.rb +8 -7
- data/lib/ferrum/page.rb +106 -53
- data/lib/ferrum/proxy.rb +8 -7
- data/lib/ferrum/remote_object.rb +75 -0
- data/lib/ferrum/target.rb +7 -0
- data/lib/ferrum/utils/deprecate.rb +77 -0
- data/lib/ferrum/version.rb +1 -1
- data/lib/ferrum/worker.rb +8 -0
- data/lib/ferrum.rb +3 -0
- metadata +16 -2
data/lib/ferrum/node.rb
CHANGED
|
@@ -14,45 +14,86 @@ module Ferrum
|
|
|
14
14
|
MOVING_WAIT_DELAY = ENV.fetch("FERRUM_NODE_MOVING_WAIT", 0.01).to_f
|
|
15
15
|
MOVING_WAIT_ATTEMPTS = ENV.fetch("FERRUM_NODE_MOVING_ATTEMPTS", 50).to_i
|
|
16
16
|
|
|
17
|
-
|
|
17
|
+
# @return [Page] the page the node belongs to.
|
|
18
|
+
attr_reader :page
|
|
18
19
|
|
|
19
|
-
|
|
20
|
+
# @return [String] the id of the page's target.
|
|
21
|
+
attr_reader :target_id
|
|
22
|
+
|
|
23
|
+
# @return [Integer] Chrome's id of the node, stable for the node's lifetime.
|
|
24
|
+
attr_reader :backend_node_id
|
|
25
|
+
|
|
26
|
+
# @return [Integer] the DOM `nodeType`, e.g. `1` for an element, `3` for a text node.
|
|
27
|
+
attr_reader :node_type
|
|
28
|
+
|
|
29
|
+
# @return [String, nil] the id of the frame an `iframe` or `frame` element contains, `nil` for other nodes.
|
|
30
|
+
attr_reader :frame_id
|
|
31
|
+
|
|
32
|
+
#
|
|
33
|
+
# @param [Frame, IsolatedWorld] frame
|
|
34
|
+
# The runtime the node was returned from.
|
|
35
|
+
#
|
|
36
|
+
# @param [Hash{String => Object}] properties
|
|
37
|
+
# The node's properties from Chrome's deep serialization.
|
|
38
|
+
#
|
|
39
|
+
def initialize(frame, properties)
|
|
40
|
+
@runtime = frame
|
|
20
41
|
@page = frame.page
|
|
21
|
-
@target_id = target_id
|
|
22
|
-
@
|
|
23
|
-
@
|
|
24
|
-
@
|
|
25
|
-
@
|
|
42
|
+
@target_id = @page.target_id
|
|
43
|
+
@backend_node_id = properties["backendNodeId"]
|
|
44
|
+
@node_type = properties["nodeType"]
|
|
45
|
+
@tag_name = properties["localName"]&.downcase
|
|
46
|
+
@frame_id = properties["frameId"]
|
|
47
|
+
@document_version = @page.document_version
|
|
48
|
+
end
|
|
49
|
+
|
|
50
|
+
#
|
|
51
|
+
# Chrome's description of the node from `DOM.describeNode`, fetched on
|
|
52
|
+
# first use.
|
|
53
|
+
#
|
|
54
|
+
# @return [Hash{String => Object}]
|
|
55
|
+
#
|
|
56
|
+
def description
|
|
57
|
+
@description ||= page.command("DOM.describeNode", backendNodeId: backend_node_id)["node"]
|
|
58
|
+
end
|
|
59
|
+
|
|
60
|
+
#
|
|
61
|
+
# The lowercased tag name of an element, or the node name of any other
|
|
62
|
+
# node, e.g. `"#text"`.
|
|
63
|
+
#
|
|
64
|
+
# @return [String]
|
|
65
|
+
#
|
|
66
|
+
def tag_name
|
|
67
|
+
@tag_name ||= description["nodeName"].downcase
|
|
26
68
|
end
|
|
27
69
|
|
|
28
70
|
# Frontend node id is resolved lazily, on first actual need (focus, click, scroll_into_view, etc.)
|
|
29
71
|
# We can try to subscribe to `DOM.childNodeRemoved` and `DOM.childNodeInserted` in the future
|
|
30
72
|
# to keep track of nodes.
|
|
31
73
|
def node_id
|
|
32
|
-
@node_id
|
|
33
|
-
|
|
34
|
-
|
|
74
|
+
return @node_id if @node_id && @document_version == page.document_version
|
|
75
|
+
|
|
76
|
+
document_version = page.document_version
|
|
77
|
+
@node_id = begin
|
|
78
|
+
id = page.command("DOM.pushNodesByBackendIdsToFrontend",
|
|
79
|
+
backendNodeIds: [backend_node_id])["nodeIds"].first
|
|
80
|
+
raise NodeNotFoundError, { "message" => "node is not trackable" } if id.zero?
|
|
35
81
|
|
|
36
82
|
id
|
|
37
|
-
rescue
|
|
38
|
-
raise
|
|
83
|
+
rescue BrowserError => e
|
|
84
|
+
raise unless e.message == "Document needs to be requested first"
|
|
85
|
+
|
|
86
|
+
raise NodeNotFoundError, { "message" => "node is not trackable, the page is still loading a new document" }
|
|
39
87
|
end
|
|
88
|
+
@document_version = document_version
|
|
89
|
+
@node_id
|
|
40
90
|
end
|
|
41
91
|
|
|
42
92
|
# Whether this is an element node, as opposed to e.g. a text node.
|
|
43
93
|
#
|
|
44
94
|
# @return [Boolean]
|
|
45
95
|
def node?
|
|
46
|
-
|
|
47
|
-
end
|
|
48
|
-
|
|
49
|
-
#
|
|
50
|
-
# The id of the frame this node belongs to.
|
|
51
|
-
#
|
|
52
|
-
# @return [String]
|
|
53
|
-
#
|
|
54
|
-
def frame_id
|
|
55
|
-
description["frameId"]
|
|
96
|
+
node_type == 1 # nodeType: 3, nodeName: "#text" e.g.
|
|
56
97
|
end
|
|
57
98
|
|
|
58
99
|
#
|
|
@@ -202,9 +243,9 @@ module Ferrum
|
|
|
202
243
|
#
|
|
203
244
|
# @return [Boolean]
|
|
204
245
|
def in_viewport?(of: nil)
|
|
205
|
-
|
|
206
|
-
function(
|
|
207
|
-
const rect =
|
|
246
|
+
evaluate(<<~JS, of)
|
|
247
|
+
function(scope) {
|
|
248
|
+
const rect = this.getBoundingClientRect();
|
|
208
249
|
const [height, width] = scope
|
|
209
250
|
? [scope.offsetHeight, scope.offsetWidth]
|
|
210
251
|
: [window.innerHeight, window.innerWidth];
|
|
@@ -214,7 +255,6 @@ module Ferrum
|
|
|
214
255
|
rect.right <= width;
|
|
215
256
|
}
|
|
216
257
|
JS
|
|
217
|
-
page.evaluate_func(function, self, of)
|
|
218
258
|
end
|
|
219
259
|
|
|
220
260
|
#
|
|
@@ -232,7 +272,7 @@ module Ferrum
|
|
|
232
272
|
page.command(
|
|
233
273
|
"DOM.setFileInputFiles",
|
|
234
274
|
slowmoable: true,
|
|
235
|
-
backendNodeId:
|
|
275
|
+
backendNodeId: backend_node_id,
|
|
236
276
|
files: Array(value)
|
|
237
277
|
)
|
|
238
278
|
end
|
|
@@ -335,7 +375,7 @@ module Ferrum
|
|
|
335
375
|
# page.at_css("input").property("value") # => "Foo"
|
|
336
376
|
#
|
|
337
377
|
def property(name)
|
|
338
|
-
evaluate("this[
|
|
378
|
+
evaluate("this[name]", name: name)
|
|
339
379
|
end
|
|
340
380
|
alias [] property
|
|
341
381
|
|
|
@@ -352,7 +392,7 @@ module Ferrum
|
|
|
352
392
|
# page.at_css("input").attribute("value") # => "Foo"
|
|
353
393
|
#
|
|
354
394
|
def attribute(name)
|
|
355
|
-
evaluate("this.getAttribute(
|
|
395
|
+
evaluate("this.getAttribute(name)", name: name)
|
|
356
396
|
end
|
|
357
397
|
|
|
358
398
|
#
|
|
@@ -361,15 +401,14 @@ module Ferrum
|
|
|
361
401
|
# @return [Array<Node>]
|
|
362
402
|
#
|
|
363
403
|
def selected
|
|
364
|
-
|
|
365
|
-
function(
|
|
366
|
-
if (
|
|
404
|
+
evaluate(<<~JS)
|
|
405
|
+
function() {
|
|
406
|
+
if (this.nodeName.toLowerCase() !== 'select') {
|
|
367
407
|
throw new Error('Element is not a <select> element.');
|
|
368
408
|
}
|
|
369
|
-
return Array.from(
|
|
409
|
+
return Array.from(this).filter(option => option.selected);
|
|
370
410
|
}
|
|
371
411
|
JS
|
|
372
|
-
page.evaluate_func(function, self, on: self)
|
|
373
412
|
end
|
|
374
413
|
|
|
375
414
|
#
|
|
@@ -397,38 +436,73 @@ module Ferrum
|
|
|
397
436
|
#
|
|
398
437
|
def select(*values, by: :value)
|
|
399
438
|
tap do
|
|
400
|
-
|
|
401
|
-
function(
|
|
402
|
-
if (
|
|
439
|
+
execute(<<~JS, values.flatten, by)
|
|
440
|
+
function(values, by) {
|
|
441
|
+
if (this.nodeName.toLowerCase() !== 'select') {
|
|
403
442
|
throw new Error('Element is not a <select> element.');
|
|
404
443
|
}
|
|
405
|
-
const options = Array.from(
|
|
406
|
-
|
|
444
|
+
const options = Array.from(this.options);
|
|
445
|
+
this.value = undefined;
|
|
407
446
|
for (const option of options) {
|
|
408
447
|
option.selected = values.some((value) => option[by] === value);
|
|
409
|
-
if (option.selected && !
|
|
448
|
+
if (option.selected && !this.multiple) break;
|
|
410
449
|
}
|
|
411
|
-
|
|
412
|
-
|
|
450
|
+
this.dispatchEvent(new Event('input', { bubbles: true }));
|
|
451
|
+
this.dispatchEvent(new Event('change', { bubbles: true }));
|
|
413
452
|
}
|
|
414
453
|
JS
|
|
415
|
-
page.evaluate_func(function, self, values.flatten, by, on: self)
|
|
416
454
|
end
|
|
417
455
|
end
|
|
418
456
|
|
|
419
457
|
#
|
|
420
|
-
# Evaluates
|
|
421
|
-
#
|
|
458
|
+
# Evaluates JavaScript with `this` bound to the node, and returns the
|
|
459
|
+
# result serialized into Ruby. Takes the same script shapes and arguments
|
|
460
|
+
# as {Frame::Runtime#evaluate}. Arrow functions don't get `this`, so use a
|
|
461
|
+
# `function` declaration when the script needs it.
|
|
422
462
|
#
|
|
423
|
-
# @param
|
|
463
|
+
# @param (see Frame::Runtime#evaluate)
|
|
464
|
+
# @option (see Frame::Runtime#evaluate)
|
|
424
465
|
#
|
|
425
466
|
# @return [Object]
|
|
426
467
|
#
|
|
427
468
|
# @example
|
|
428
469
|
# page.at_css("input").evaluate("this.value")
|
|
470
|
+
# page.at_css("input").evaluate("this.value + suffix", suffix: "!")
|
|
471
|
+
#
|
|
472
|
+
def evaluate(expression, *positional, **keywords)
|
|
473
|
+
@runtime.evaluate_in(self, expression, positional, keywords, mode: :value)
|
|
474
|
+
end
|
|
475
|
+
|
|
476
|
+
#
|
|
477
|
+
# Same as {#evaluate}, but returns a {RemoteObject} that stays in the
|
|
478
|
+
# browser instead of being serialized.
|
|
479
|
+
#
|
|
480
|
+
# @param (see Frame::Runtime#evaluate)
|
|
481
|
+
# @option (see Frame::Runtime#evaluate)
|
|
482
|
+
#
|
|
483
|
+
# @return [RemoteObject, Node, Object]
|
|
429
484
|
#
|
|
430
|
-
def
|
|
431
|
-
|
|
485
|
+
def evaluate_handle(expression, *positional, **keywords)
|
|
486
|
+
@runtime.evaluate_in(self, expression, positional, keywords, mode: :handle)
|
|
487
|
+
end
|
|
488
|
+
|
|
489
|
+
#
|
|
490
|
+
# Runs JavaScript with `this` bound to the node, for its side effects.
|
|
491
|
+
# The script is used as a function body, so multiple statements need no
|
|
492
|
+
# wrapping.
|
|
493
|
+
#
|
|
494
|
+
# @param (see Frame::Runtime#evaluate)
|
|
495
|
+
# @option (see Frame::Runtime#evaluate)
|
|
496
|
+
#
|
|
497
|
+
# @return [Boolean]
|
|
498
|
+
# Always `true`.
|
|
499
|
+
#
|
|
500
|
+
# @example
|
|
501
|
+
# page.at_css("input").execute("this.value = text", text: "hello")
|
|
502
|
+
#
|
|
503
|
+
def execute(expression, *positional, **keywords)
|
|
504
|
+
@runtime.evaluate_in(self, expression, positional, keywords, mode: :none)
|
|
505
|
+
true
|
|
432
506
|
end
|
|
433
507
|
|
|
434
508
|
#
|
|
@@ -445,7 +519,7 @@ module Ferrum
|
|
|
445
519
|
# We compare backendNodeId because once nodeId is sent to frontend backend
|
|
446
520
|
# never returns same nodeId sending 0. In other words frontend is
|
|
447
521
|
# responsible for keeping track of node ids.
|
|
448
|
-
target_id == other.target_id &&
|
|
522
|
+
target_id == other.target_id && backend_node_id == other.backend_node_id
|
|
449
523
|
end
|
|
450
524
|
|
|
451
525
|
#
|
|
@@ -454,7 +528,8 @@ module Ferrum
|
|
|
454
528
|
# @return [String]
|
|
455
529
|
#
|
|
456
530
|
def inspect
|
|
457
|
-
%(#<#{self.class} @target_id=#{@target_id.inspect} @node_id=#{@node_id}
|
|
531
|
+
%(#<#{self.class} @target_id=#{@target_id.inspect} @node_id=#{@node_id} ) +
|
|
532
|
+
%(@backend_node_id=#{@backend_node_id} @tag_name=#{@tag_name.inspect}>)
|
|
458
533
|
end
|
|
459
534
|
|
|
460
535
|
#
|
|
@@ -523,8 +598,8 @@ module Ferrum
|
|
|
523
598
|
|
|
524
599
|
def bounding_rect_coordinates
|
|
525
600
|
evaluate <<~JS
|
|
526
|
-
[this.getBoundingClientRect().left +
|
|
527
|
-
this.getBoundingClientRect().top +
|
|
601
|
+
[this.getBoundingClientRect().left + (this.offsetWidth / 2),
|
|
602
|
+
this.getBoundingClientRect().top + (this.offsetHeight / 2)]
|
|
528
603
|
JS
|
|
529
604
|
end
|
|
530
605
|
|
data/lib/ferrum/page/frames.rb
CHANGED
|
@@ -203,8 +203,9 @@ module Ferrum
|
|
|
203
203
|
|
|
204
204
|
def subscribe_execution_context_created
|
|
205
205
|
on("Runtime.executionContextCreated") do |params|
|
|
206
|
-
|
|
207
|
-
|
|
206
|
+
context = params["context"]
|
|
207
|
+
context_id = context["id"]
|
|
208
|
+
frame_id = context.dig("auxData", "frameId")
|
|
208
209
|
|
|
209
210
|
unless @main_frame.id
|
|
210
211
|
root_frame = command("Page.getFrameTree").dig("frameTree", "frame", "id")
|
|
@@ -215,7 +216,11 @@ module Ferrum
|
|
|
215
216
|
end
|
|
216
217
|
|
|
217
218
|
frame = @frames.fetch_or_store(frame_id, Frame.new(frame_id, self))
|
|
218
|
-
|
|
219
|
+
if context.dig("auxData", "isDefault")
|
|
220
|
+
frame.execution_id = context_id
|
|
221
|
+
elsif !context["name"].to_s.empty?
|
|
222
|
+
frame.isolated_world(context["name"]).execution_id = context_id
|
|
223
|
+
end
|
|
219
224
|
end
|
|
220
225
|
end
|
|
221
226
|
|
|
@@ -225,6 +230,7 @@ module Ferrum
|
|
|
225
230
|
frame = frame_by(execution_id: execution_id)
|
|
226
231
|
frame&.execution_id = nil
|
|
227
232
|
frame&.state = :canceled
|
|
233
|
+
frames.flat_map(&:isolated_worlds).find { |w| w.execution_id == execution_id }&.execution_id = nil
|
|
228
234
|
end
|
|
229
235
|
end
|
|
230
236
|
|
|
@@ -240,6 +246,7 @@ module Ferrum
|
|
|
240
246
|
@frames.each do |frame_id, f|
|
|
241
247
|
if f.main?
|
|
242
248
|
f.execution_id = nil
|
|
249
|
+
f.isolated_worlds.each { |w| w.execution_id = nil }
|
|
243
250
|
f.loader_id = nil
|
|
244
251
|
f.lifecycle_events.clear
|
|
245
252
|
f.state = :canceled
|
|
@@ -307,13 +307,14 @@ module Ferrum
|
|
|
307
307
|
end
|
|
308
308
|
|
|
309
309
|
def bounding_rect(selector)
|
|
310
|
-
rect =
|
|
311
|
-
|
|
312
|
-
|
|
313
|
-
|
|
314
|
-
|
|
315
|
-
|
|
316
|
-
|
|
310
|
+
rect = evaluate(<<~JS, selector)
|
|
311
|
+
function(selector) {
|
|
312
|
+
const {x, y, width, height} = document
|
|
313
|
+
.querySelector(selector)
|
|
314
|
+
.getBoundingClientRect();
|
|
315
|
+
return [x, y, width, height];
|
|
316
|
+
}
|
|
317
|
+
JS
|
|
317
318
|
|
|
318
319
|
{ x: rect[0], y: rect[1], width: rect[2], height: rect[3] }
|
|
319
320
|
end
|
data/lib/ferrum/page.rb
CHANGED
|
@@ -8,6 +8,7 @@ require "ferrum/headers"
|
|
|
8
8
|
require "ferrum/cookies"
|
|
9
9
|
require "ferrum/dialog"
|
|
10
10
|
require "ferrum/network"
|
|
11
|
+
require "ferrum/console"
|
|
11
12
|
require "ferrum/accessibility"
|
|
12
13
|
require "ferrum/interceptable"
|
|
13
14
|
require "ferrum/downloads"
|
|
@@ -33,7 +34,8 @@ module Ferrum
|
|
|
33
34
|
|
|
34
35
|
delegate %i[at_css at_xpath css xpath wait_for_selector
|
|
35
36
|
current_url current_title url title body doctype content=
|
|
36
|
-
execution_id execution_id! evaluate
|
|
37
|
+
execution_id execution_id! evaluate evaluate_handle execute
|
|
38
|
+
evaluate_on evaluate_async evaluate_func isolated_world
|
|
37
39
|
add_script_tag add_style_tag] => :main_frame
|
|
38
40
|
delegate %i[base_url default_user_agent timeout timeout=] => :@options
|
|
39
41
|
|
|
@@ -67,6 +69,11 @@ module Ferrum
|
|
|
67
69
|
# @return [Network]
|
|
68
70
|
attr_reader :network
|
|
69
71
|
|
|
72
|
+
# Console messages.
|
|
73
|
+
#
|
|
74
|
+
# @return [Console]
|
|
75
|
+
attr_reader :console
|
|
76
|
+
|
|
70
77
|
# Accessibility object.
|
|
71
78
|
#
|
|
72
79
|
# @return [Accessibility]
|
|
@@ -87,6 +94,12 @@ module Ferrum
|
|
|
87
94
|
# @return [Downloads]
|
|
88
95
|
attr_reader :downloads
|
|
89
96
|
|
|
97
|
+
# Bumped every time Chrome reports `DOM.documentUpdated` and after every
|
|
98
|
+
# `DOM.getDocument`, both invalidate every node id handed out before them.
|
|
99
|
+
#
|
|
100
|
+
# @return [Integer]
|
|
101
|
+
attr_reader :document_version
|
|
102
|
+
|
|
90
103
|
def initialize(client, context_id:, target_id:, proxy: nil)
|
|
91
104
|
@client = client
|
|
92
105
|
@context_id = context_id
|
|
@@ -96,6 +109,7 @@ module Ferrum
|
|
|
96
109
|
@frames = Concurrent::Map.new
|
|
97
110
|
@main_frame = Frame.new(nil, self)
|
|
98
111
|
@event = Utils::Event.new.tap(&:set)
|
|
112
|
+
@document_version = 0
|
|
99
113
|
self.proxy = proxy
|
|
100
114
|
|
|
101
115
|
@mouse = Mouse.new(self)
|
|
@@ -103,6 +117,7 @@ module Ferrum
|
|
|
103
117
|
@headers = Headers.new(self)
|
|
104
118
|
@cookies = Cookies.new(self)
|
|
105
119
|
@network = Network.new(self)
|
|
120
|
+
@console = Console.new(self)
|
|
106
121
|
@accessibility = Accessibility.new(self)
|
|
107
122
|
@tracing = Tracing.new(self)
|
|
108
123
|
@downloads = Downloads.new(self)
|
|
@@ -384,6 +399,40 @@ module Ferrum
|
|
|
384
399
|
@event.set
|
|
385
400
|
end
|
|
386
401
|
|
|
402
|
+
#
|
|
403
|
+
# Runs the block and, if it starts a navigation of the main frame within
|
|
404
|
+
# `wait` seconds, waits for that navigation to finish, e.g. after a click
|
|
405
|
+
# on a link made from JavaScript.
|
|
406
|
+
#
|
|
407
|
+
# @param [Numeric] wait
|
|
408
|
+
# How many seconds to watch for a navigation after the block returns.
|
|
409
|
+
#
|
|
410
|
+
# @yield The code that may start a navigation.
|
|
411
|
+
#
|
|
412
|
+
# @return [Boolean]
|
|
413
|
+
# `false` when a navigation started but didn't finish within the page's
|
|
414
|
+
# `timeout`.
|
|
415
|
+
#
|
|
416
|
+
# @example
|
|
417
|
+
# page.wait_for_navigation { page.at_css("a").evaluate("this.click()") } # => true
|
|
418
|
+
#
|
|
419
|
+
def wait_for_navigation(wait = Mouse::CLICK_WAIT)
|
|
420
|
+
iteration = @event.reset
|
|
421
|
+
yield
|
|
422
|
+
@event.wait(wait)
|
|
423
|
+
iteration == @event.iteration || @event.wait(timeout)
|
|
424
|
+
end
|
|
425
|
+
|
|
426
|
+
#
|
|
427
|
+
# Waits for a navigation the block starts or raises {Ferrum::TimeoutError}.
|
|
428
|
+
# Accepts the same arguments as {#wait_for_navigation}.
|
|
429
|
+
#
|
|
430
|
+
# @raise [Ferrum::TimeoutError]
|
|
431
|
+
#
|
|
432
|
+
def wait_for_navigation!(...)
|
|
433
|
+
raise TimeoutError unless wait_for_navigation(...)
|
|
434
|
+
end
|
|
435
|
+
|
|
387
436
|
#
|
|
388
437
|
# Enables/disables CSP bypass.
|
|
389
438
|
#
|
|
@@ -403,6 +452,45 @@ module Ferrum
|
|
|
403
452
|
enabled
|
|
404
453
|
end
|
|
405
454
|
|
|
455
|
+
#
|
|
456
|
+
# Adds JavaScript to run in every new document of this page and its in-process frames, before the document's own
|
|
457
|
+
# scripts. For every page and iframe of the browser, see {Browser::Options#add_extension}.
|
|
458
|
+
#
|
|
459
|
+
# @param [String] source
|
|
460
|
+
#
|
|
461
|
+
# @param [String, nil] world_name
|
|
462
|
+
# Runs the script in an isolated world with this name instead of the page's main world.
|
|
463
|
+
#
|
|
464
|
+
# @param [Boolean] run_immediately
|
|
465
|
+
# Runs the script in the current document as well.
|
|
466
|
+
#
|
|
467
|
+
# @return [String]
|
|
468
|
+
# The script identifier, for {#remove_script_on_new_document}.
|
|
469
|
+
#
|
|
470
|
+
# @example
|
|
471
|
+
# page.evaluate_on_new_document <<~JS
|
|
472
|
+
# Object.defineProperty(navigator, "languages", {
|
|
473
|
+
# get: function() { return ["tlh"]; }
|
|
474
|
+
# });
|
|
475
|
+
# JS
|
|
476
|
+
#
|
|
477
|
+
def evaluate_on_new_document(source, world_name: nil, run_immediately: false)
|
|
478
|
+
params = { source: source, worldName: world_name, runImmediately: run_immediately }.compact
|
|
479
|
+
command("Page.addScriptToEvaluateOnNewDocument", **params)["identifier"]
|
|
480
|
+
end
|
|
481
|
+
|
|
482
|
+
#
|
|
483
|
+
# Stops running a script added with {#evaluate_on_new_document} in new documents.
|
|
484
|
+
#
|
|
485
|
+
# @param [String] identifier
|
|
486
|
+
#
|
|
487
|
+
# @return [true]
|
|
488
|
+
#
|
|
489
|
+
def remove_script_on_new_document(identifier)
|
|
490
|
+
command("Page.removeScriptToEvaluateOnNewDocument", identifier: identifier)
|
|
491
|
+
true
|
|
492
|
+
end
|
|
493
|
+
|
|
406
494
|
#
|
|
407
495
|
# Activates (focuses) the target for the given page.
|
|
408
496
|
# When you have multiple tabs you work with, and you need to switch a given one.
|
|
@@ -443,21 +531,14 @@ module Ferrum
|
|
|
443
531
|
# page.command("Page.navigate", url: "https://github.com/")
|
|
444
532
|
#
|
|
445
533
|
def command(method, wait: 0, slowmoable: false, timeout: nil, **params)
|
|
446
|
-
iteration = @event.reset if wait.positive?
|
|
447
|
-
sleep(@options.slowmo) if slowmoable && @options.slowmo.positive?
|
|
448
|
-
result = client.command(method, timeout: timeout || self.timeout, **params)
|
|
449
|
-
|
|
450
534
|
if wait.positive?
|
|
451
|
-
|
|
452
|
-
|
|
453
|
-
|
|
454
|
-
@event.wait(wait)
|
|
455
|
-
if iteration != @event.iteration
|
|
456
|
-
set = @event.wait(self.timeout)
|
|
457
|
-
raise TimeoutError unless set
|
|
458
|
-
end
|
|
535
|
+
result = nil
|
|
536
|
+
wait_for_navigation!(wait) { result = command(method, slowmoable:, timeout:, **params) }
|
|
537
|
+
return result
|
|
459
538
|
end
|
|
460
|
-
|
|
539
|
+
|
|
540
|
+
sleep(@options.slowmo) if slowmoable && @options.slowmo.positive?
|
|
541
|
+
client.command(method, timeout: timeout || self.timeout, **params)
|
|
461
542
|
end
|
|
462
543
|
|
|
463
544
|
# Subscribes to a CDP event, or to `:dialog`, `:request`, `:response`,
|
|
@@ -514,9 +595,13 @@ module Ferrum
|
|
|
514
595
|
# The root node id, or `true` when sent asynchronously.
|
|
515
596
|
#
|
|
516
597
|
def document_node_id(async: false)
|
|
517
|
-
|
|
518
|
-
|
|
519
|
-
|
|
598
|
+
result = if async
|
|
599
|
+
client.command("DOM.getDocument", async: true, depth: 0)
|
|
600
|
+
else
|
|
601
|
+
command("DOM.getDocument", depth: 0).dig("root", "nodeId")
|
|
602
|
+
end
|
|
603
|
+
@document_version += 1
|
|
604
|
+
result
|
|
520
605
|
end
|
|
521
606
|
|
|
522
607
|
private
|
|
@@ -524,13 +609,10 @@ module Ferrum
|
|
|
524
609
|
def subscribe
|
|
525
610
|
frames_subscribe
|
|
526
611
|
network.subscribe
|
|
612
|
+
console.subscribe
|
|
527
613
|
downloads.subscribe
|
|
528
614
|
|
|
529
|
-
|
|
530
|
-
on("Runtime.consoleAPICalled") do |params|
|
|
531
|
-
log_console_api(params)
|
|
532
|
-
end
|
|
533
|
-
end
|
|
615
|
+
on("DOM.documentUpdated") { @document_version += 1 }
|
|
534
616
|
|
|
535
617
|
if @options.js_errors
|
|
536
618
|
on("Runtime.exceptionThrown") do |params|
|
|
@@ -560,19 +642,13 @@ module Ferrum
|
|
|
560
642
|
if use_authorized_proxy?
|
|
561
643
|
network.authorize(user: @proxy_user,
|
|
562
644
|
password: @proxy_password,
|
|
563
|
-
type: :proxy) do |request, _index,
|
|
564
|
-
request.continue
|
|
645
|
+
type: :proxy) do |request, _index, total|
|
|
646
|
+
request.continue if total == 1
|
|
565
647
|
end
|
|
566
648
|
end
|
|
567
649
|
|
|
568
650
|
downloads.set_behavior(save_path: @options.save_path) if @options.save_path
|
|
569
651
|
|
|
570
|
-
@options.extensions.each do |extension|
|
|
571
|
-
command("Page.addScriptToEvaluateOnNewDocument", source: extension)
|
|
572
|
-
end
|
|
573
|
-
|
|
574
|
-
inject_extensions
|
|
575
|
-
|
|
576
652
|
response = command("Page.getNavigationHistory")
|
|
577
653
|
transition_type = response.dig("entries", 0, "transitionType")
|
|
578
654
|
return if transition_type == "auto_toplevel"
|
|
@@ -585,29 +661,6 @@ module Ferrum
|
|
|
585
661
|
document_node_id
|
|
586
662
|
end
|
|
587
663
|
|
|
588
|
-
def log_console_api(params)
|
|
589
|
-
message = params.fetch("args", []).filter_map { |arg| arg["value"] || arg["description"] }.join(" ")
|
|
590
|
-
@options.logger.puts("[#{params['type']}] #{message}")
|
|
591
|
-
|
|
592
|
-
params.dig("stackTrace", "callFrames")&.each do |frame|
|
|
593
|
-
location = "#{frame['url']}:#{frame['lineNumber'].to_i + 1}:#{frame['columnNumber'].to_i + 1}"
|
|
594
|
-
@options.logger.puts(" at #{frame['functionName']} (#{location})")
|
|
595
|
-
end
|
|
596
|
-
end
|
|
597
|
-
|
|
598
|
-
def inject_extensions
|
|
599
|
-
@options.extensions.each do |extension|
|
|
600
|
-
# https://github.com/GoogleChrome/puppeteer/issues/1443
|
|
601
|
-
# https://github.com/ChromeDevTools/devtools-protocol/issues/77
|
|
602
|
-
# https://github.com/cyrus-and/chrome-remote-interface/issues/319
|
|
603
|
-
# We also evaluate script just in case because
|
|
604
|
-
# `Page.addScriptToEvaluateOnNewDocument` doesn't work in popups.
|
|
605
|
-
command("Runtime.evaluate", expression: extension,
|
|
606
|
-
executionContextId: execution_id!,
|
|
607
|
-
returnByValue: true)
|
|
608
|
-
end
|
|
609
|
-
end
|
|
610
|
-
|
|
611
664
|
def history_navigate(delta:)
|
|
612
665
|
history = command("Page.getNavigationHistory")
|
|
613
666
|
index, entries = history.values_at("currentIndex", "entries")
|
data/lib/ferrum/proxy.rb
CHANGED
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
# frozen_string_literal: true
|
|
2
2
|
|
|
3
|
+
require "logger"
|
|
3
4
|
require "tempfile"
|
|
4
5
|
|
|
5
6
|
begin
|
|
@@ -133,19 +134,19 @@ module Ferrum
|
|
|
133
134
|
|
|
134
135
|
if proxy
|
|
135
136
|
@logger.debug("CONNECT: sending a Request-Line")
|
|
136
|
-
os << proxy_request_line << CRLF
|
|
137
|
+
os << proxy_request_line << WEBrick::CRLF
|
|
137
138
|
@logger.debug("CONNECT: > #{proxy_request_line}")
|
|
138
139
|
if credentials
|
|
139
140
|
@logger.debug("CONNECT: sending credentials")
|
|
140
|
-
os << "Proxy-Authorization: " << credentials << CRLF
|
|
141
|
+
os << "Proxy-Authorization: " << credentials << WEBrick::CRLF
|
|
141
142
|
end
|
|
142
|
-
os << CRLF
|
|
143
|
-
proxy_status_line = os.gets(LF)
|
|
143
|
+
os << WEBrick::CRLF
|
|
144
|
+
proxy_status_line = os.gets(WEBrick::LF)
|
|
144
145
|
@logger.debug("CONNECT: read Status-Line from the upstream server")
|
|
145
146
|
@logger.debug("CONNECT: < #{proxy_status_line}")
|
|
146
147
|
if %r{^HTTP/\d+\.\d+\s+200\s*} =~ proxy_status_line
|
|
147
|
-
while line = os.gets(LF)
|
|
148
|
-
break if /\A(#{CRLF}|#{LF})\z/om =~ line
|
|
148
|
+
while line = os.gets(WEBrick::LF)
|
|
149
|
+
break if /\A(#{WEBrick::CRLF}|#{WEBrick::LF})\z/om =~ line
|
|
149
150
|
end
|
|
150
151
|
else
|
|
151
152
|
raise WEBrick::HTTPStatus::BadGateway
|
|
@@ -169,7 +170,7 @@ module Ferrum
|
|
|
169
170
|
|
|
170
171
|
# Should clear request-line not to send the response twice.
|
|
171
172
|
# see: HTTPServer#run
|
|
172
|
-
req.parse(NullReader) rescue nil
|
|
173
|
+
req.parse(WEBrick::NullReader) rescue nil
|
|
173
174
|
end
|
|
174
175
|
|
|
175
176
|
begin
|