ferrum 0.18.0 → 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 +63 -1
- data/lib/ferrum/browser/binary.rb +1 -3
- data/lib/ferrum/browser/command.rb +0 -2
- data/lib/ferrum/browser/options/base.rb +5 -6
- data/lib/ferrum/browser/options/chrome.rb +0 -3
- data/lib/ferrum/browser/options.rb +33 -12
- data/lib/ferrum/browser/process/killer.rb +188 -0
- data/lib/ferrum/browser/process/output.rb +143 -0
- data/lib/ferrum/browser/process.rb +122 -144
- data/lib/ferrum/browser.rb +24 -30
- data/lib/ferrum/client/subscriber.rb +7 -2
- data/lib/ferrum/client/web_socket.rb +124 -43
- data/lib/ferrum/client.rb +36 -9
- 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 +41 -20
- data/lib/ferrum/contexts.rb +110 -28
- data/lib/ferrum/downloads.rb +2 -1
- data/lib/ferrum/errors.rb +50 -4
- data/lib/ferrum/frame/dom.rb +100 -45
- data/lib/ferrum/frame/runtime/legacy.rb +336 -0
- data/lib/ferrum/frame/runtime.rb +417 -186
- data/lib/ferrum/frame/script.rb +289 -0
- data/lib/ferrum/frame.rb +33 -4
- data/lib/ferrum/interceptable.rb +35 -4
- 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 +22 -8
- data/lib/ferrum/node.rb +127 -52
- data/lib/ferrum/page/frames.rb +10 -3
- data/lib/ferrum/page/screenshot.rb +11 -10
- data/lib/ferrum/page/stream.rb +16 -1
- data/lib/ferrum/page.rb +117 -58
- data/lib/ferrum/proxy.rb +8 -7
- data/lib/ferrum/remote_object.rb +75 -0
- data/lib/ferrum/target.rb +14 -3
- data/lib/ferrum/utils/deprecate.rb +77 -0
- data/lib/ferrum/utils/thread.rb +3 -1
- data/lib/ferrum/version.rb +1 -1
- data/lib/ferrum/worker.rb +8 -0
- data/lib/ferrum.rb +4 -1
- metadata +18 -4
- data/lib/ferrum/browser/options/firefox.rb +0 -70
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
|
|
@@ -32,9 +32,9 @@ module Ferrum
|
|
|
32
32
|
A1: { width: 23.40, height: 33.10 },
|
|
33
33
|
A2: { width: 16.54, height: 23.40 },
|
|
34
34
|
A3: { width: 11.70, height: 16.54 },
|
|
35
|
-
A4: { width:
|
|
36
|
-
A5: { width:
|
|
37
|
-
A6: { width:
|
|
35
|
+
A4: { width: 8.27, height: 11.70 },
|
|
36
|
+
A5: { width: 5.83, height: 8.27 },
|
|
37
|
+
A6: { width: 4.13, height: 5.83 }
|
|
38
38
|
}.freeze
|
|
39
39
|
|
|
40
40
|
#
|
|
@@ -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/stream.rb
CHANGED
|
@@ -70,7 +70,8 @@ module Ferrum
|
|
|
70
70
|
|
|
71
71
|
#
|
|
72
72
|
# Reads a CDP `IO` stream in chunks, writing each chunk to the given
|
|
73
|
-
# output until the stream is exhausted
|
|
73
|
+
# output until the stream is exhausted, then releases the stream
|
|
74
|
+
# handle on the browser side.
|
|
74
75
|
#
|
|
75
76
|
# @param [#<<] output
|
|
76
77
|
# Anything that responds to `#<<`, e.g. an open `File` or a `String`.
|
|
@@ -88,6 +89,20 @@ module Ferrum
|
|
|
88
89
|
output << chunk
|
|
89
90
|
break if result["eof"]
|
|
90
91
|
end
|
|
92
|
+
close_stream(handle: handle)
|
|
93
|
+
end
|
|
94
|
+
|
|
95
|
+
#
|
|
96
|
+
# Releases a CDP `IO` stream handle, discarding any backing storage
|
|
97
|
+
# the browser is holding for it.
|
|
98
|
+
#
|
|
99
|
+
# @param [String] handle
|
|
100
|
+
# The CDP `IO` stream handle to close.
|
|
101
|
+
#
|
|
102
|
+
# @return [void]
|
|
103
|
+
#
|
|
104
|
+
def close_stream(handle:)
|
|
105
|
+
command("IO.close", handle: handle)
|
|
91
106
|
end
|
|
92
107
|
end
|
|
93
108
|
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"
|
|
@@ -31,9 +32,10 @@ module Ferrum
|
|
|
31
32
|
|
|
32
33
|
extend Forwardable
|
|
33
34
|
|
|
34
|
-
delegate %i[at_css at_xpath css xpath
|
|
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)
|
|
@@ -176,9 +191,15 @@ module Ferrum
|
|
|
176
191
|
#
|
|
177
192
|
# @param [Float] scale_factor device scale factor value. 0 disables the override
|
|
178
193
|
#
|
|
179
|
-
# @param [Boolean] mobile whether to emulate mobile device
|
|
194
|
+
# @param [Boolean] mobile whether to emulate mobile device and enable touch
|
|
180
195
|
#
|
|
181
|
-
def set_viewport(width
|
|
196
|
+
def set_viewport(width: 0, height: 0, scale_factor: 0, mobile: false)
|
|
197
|
+
command(
|
|
198
|
+
"Emulation.setTouchEmulationEnabled",
|
|
199
|
+
enabled: mobile,
|
|
200
|
+
maxTouchPoints: 1
|
|
201
|
+
)
|
|
202
|
+
|
|
182
203
|
command(
|
|
183
204
|
"Emulation.setDeviceMetricsOverride",
|
|
184
205
|
slowmoable: true,
|
|
@@ -378,6 +399,40 @@ module Ferrum
|
|
|
378
399
|
@event.set
|
|
379
400
|
end
|
|
380
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
|
+
|
|
381
436
|
#
|
|
382
437
|
# Enables/disables CSP bypass.
|
|
383
438
|
#
|
|
@@ -397,6 +452,45 @@ module Ferrum
|
|
|
397
452
|
enabled
|
|
398
453
|
end
|
|
399
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
|
+
|
|
400
494
|
#
|
|
401
495
|
# Activates (focuses) the target for the given page.
|
|
402
496
|
# When you have multiple tabs you work with, and you need to switch a given one.
|
|
@@ -437,25 +531,18 @@ module Ferrum
|
|
|
437
531
|
# page.command("Page.navigate", url: "https://github.com/")
|
|
438
532
|
#
|
|
439
533
|
def command(method, wait: 0, slowmoable: false, timeout: nil, **params)
|
|
440
|
-
iteration = @event.reset if wait.positive?
|
|
441
|
-
sleep(@options.slowmo) if slowmoable && @options.slowmo.positive?
|
|
442
|
-
result = client.command(method, timeout: timeout || self.timeout, **params)
|
|
443
|
-
|
|
444
534
|
if wait.positive?
|
|
445
|
-
|
|
446
|
-
|
|
447
|
-
|
|
448
|
-
@event.wait(wait)
|
|
449
|
-
if iteration != @event.iteration
|
|
450
|
-
set = @event.wait(self.timeout)
|
|
451
|
-
raise TimeoutError unless set
|
|
452
|
-
end
|
|
535
|
+
result = nil
|
|
536
|
+
wait_for_navigation!(wait) { result = command(method, slowmoable:, timeout:, **params) }
|
|
537
|
+
return result
|
|
453
538
|
end
|
|
454
|
-
|
|
539
|
+
|
|
540
|
+
sleep(@options.slowmo) if slowmoable && @options.slowmo.positive?
|
|
541
|
+
client.command(method, timeout: timeout || self.timeout, **params)
|
|
455
542
|
end
|
|
456
543
|
|
|
457
|
-
# Subscribes to a CDP event, or to `:dialog`, `:request`, `:
|
|
458
|
-
# latter
|
|
544
|
+
# Subscribes to a CDP event, or to `:dialog`, `:request`, `:response`,
|
|
545
|
+
# `:auth` (the latter three handled by {Interceptable}).
|
|
459
546
|
#
|
|
460
547
|
# @param [Symbol, String] name
|
|
461
548
|
#
|
|
@@ -508,9 +595,13 @@ module Ferrum
|
|
|
508
595
|
# The root node id, or `true` when sent asynchronously.
|
|
509
596
|
#
|
|
510
597
|
def document_node_id(async: false)
|
|
511
|
-
|
|
512
|
-
|
|
513
|
-
|
|
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
|
|
514
605
|
end
|
|
515
606
|
|
|
516
607
|
private
|
|
@@ -518,13 +609,10 @@ module Ferrum
|
|
|
518
609
|
def subscribe
|
|
519
610
|
frames_subscribe
|
|
520
611
|
network.subscribe
|
|
612
|
+
console.subscribe
|
|
521
613
|
downloads.subscribe
|
|
522
614
|
|
|
523
|
-
|
|
524
|
-
on("Runtime.consoleAPICalled") do |params|
|
|
525
|
-
log_console_api(params)
|
|
526
|
-
end
|
|
527
|
-
end
|
|
615
|
+
on("DOM.documentUpdated") { @document_version += 1 }
|
|
528
616
|
|
|
529
617
|
if @options.js_errors
|
|
530
618
|
on("Runtime.exceptionThrown") do |params|
|
|
@@ -554,19 +642,13 @@ module Ferrum
|
|
|
554
642
|
if use_authorized_proxy?
|
|
555
643
|
network.authorize(user: @proxy_user,
|
|
556
644
|
password: @proxy_password,
|
|
557
|
-
type: :proxy) do |request, _index,
|
|
558
|
-
request.continue
|
|
645
|
+
type: :proxy) do |request, _index, total|
|
|
646
|
+
request.continue if total == 1
|
|
559
647
|
end
|
|
560
648
|
end
|
|
561
649
|
|
|
562
650
|
downloads.set_behavior(save_path: @options.save_path) if @options.save_path
|
|
563
651
|
|
|
564
|
-
@options.extensions.each do |extension|
|
|
565
|
-
command("Page.addScriptToEvaluateOnNewDocument", source: extension)
|
|
566
|
-
end
|
|
567
|
-
|
|
568
|
-
inject_extensions
|
|
569
|
-
|
|
570
652
|
response = command("Page.getNavigationHistory")
|
|
571
653
|
transition_type = response.dig("entries", 0, "transitionType")
|
|
572
654
|
return if transition_type == "auto_toplevel"
|
|
@@ -579,29 +661,6 @@ module Ferrum
|
|
|
579
661
|
document_node_id
|
|
580
662
|
end
|
|
581
663
|
|
|
582
|
-
def log_console_api(params)
|
|
583
|
-
message = params.fetch("args", []).filter_map { |arg| arg["value"] || arg["description"] }.join(" ")
|
|
584
|
-
@options.logger.puts("[#{params['type']}] #{message}")
|
|
585
|
-
|
|
586
|
-
params.dig("stackTrace", "callFrames")&.each do |frame|
|
|
587
|
-
location = "#{frame['url']}:#{frame['lineNumber'].to_i + 1}:#{frame['columnNumber'].to_i + 1}"
|
|
588
|
-
@options.logger.puts(" at #{frame['functionName']} (#{location})")
|
|
589
|
-
end
|
|
590
|
-
end
|
|
591
|
-
|
|
592
|
-
def inject_extensions
|
|
593
|
-
@options.extensions.each do |extension|
|
|
594
|
-
# https://github.com/GoogleChrome/puppeteer/issues/1443
|
|
595
|
-
# https://github.com/ChromeDevTools/devtools-protocol/issues/77
|
|
596
|
-
# https://github.com/cyrus-and/chrome-remote-interface/issues/319
|
|
597
|
-
# We also evaluate script just in case because
|
|
598
|
-
# `Page.addScriptToEvaluateOnNewDocument` doesn't work in popups.
|
|
599
|
-
command("Runtime.evaluate", expression: extension,
|
|
600
|
-
executionContextId: execution_id!,
|
|
601
|
-
returnByValue: true)
|
|
602
|
-
end
|
|
603
|
-
end
|
|
604
|
-
|
|
605
664
|
def history_navigate(delta:)
|
|
606
665
|
history = command("Page.getNavigationHistory")
|
|
607
666
|
index, entries = history.values_at("currentIndex", "entries")
|