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
|
@@ -0,0 +1,157 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require "ferrum/console/message"
|
|
4
|
+
|
|
5
|
+
module Ferrum
|
|
6
|
+
#
|
|
7
|
+
# Collects what the DevTools console would show for a page or worker:
|
|
8
|
+
# `console.*` calls, browser log entries and uncaught exceptions, as
|
|
9
|
+
# {Console::Message}s. Keeps the last {LIMIT} messages and is `Enumerable`
|
|
10
|
+
# over them, oldest first.
|
|
11
|
+
#
|
|
12
|
+
# @example
|
|
13
|
+
# browser.go_to("https://example.com")
|
|
14
|
+
# browser.console.select(&:error?) # => [#<Ferrum::Console::Message type=:error ...>]
|
|
15
|
+
# browser.console.clear
|
|
16
|
+
#
|
|
17
|
+
class Console
|
|
18
|
+
include Enumerable
|
|
19
|
+
|
|
20
|
+
LIMIT = 1000
|
|
21
|
+
|
|
22
|
+
def initialize(page)
|
|
23
|
+
@page = page
|
|
24
|
+
@messages = []
|
|
25
|
+
@listeners = Concurrent::Map.new
|
|
26
|
+
@listener_id = Concurrent::AtomicFixnum.new
|
|
27
|
+
@mutex = Mutex.new
|
|
28
|
+
end
|
|
29
|
+
|
|
30
|
+
#
|
|
31
|
+
# Yields each collected message, oldest first.
|
|
32
|
+
#
|
|
33
|
+
# @yieldparam [Message] message
|
|
34
|
+
#
|
|
35
|
+
# @return [Enumerator, self]
|
|
36
|
+
#
|
|
37
|
+
def each(&)
|
|
38
|
+
return to_enum(:each) { size } unless block_given?
|
|
39
|
+
|
|
40
|
+
to_a.each(&)
|
|
41
|
+
self
|
|
42
|
+
end
|
|
43
|
+
|
|
44
|
+
#
|
|
45
|
+
# The collected messages, oldest first.
|
|
46
|
+
#
|
|
47
|
+
# @return [Array<Message>]
|
|
48
|
+
#
|
|
49
|
+
def to_a
|
|
50
|
+
@mutex.synchronize { @messages.dup }
|
|
51
|
+
end
|
|
52
|
+
|
|
53
|
+
#
|
|
54
|
+
# The most recent message.
|
|
55
|
+
#
|
|
56
|
+
# @return [Message, nil]
|
|
57
|
+
#
|
|
58
|
+
def last
|
|
59
|
+
@mutex.synchronize { @messages.last }
|
|
60
|
+
end
|
|
61
|
+
|
|
62
|
+
#
|
|
63
|
+
# Number of collected messages.
|
|
64
|
+
#
|
|
65
|
+
# @return [Integer]
|
|
66
|
+
#
|
|
67
|
+
def size
|
|
68
|
+
@mutex.synchronize { @messages.size }
|
|
69
|
+
end
|
|
70
|
+
|
|
71
|
+
#
|
|
72
|
+
# Whether no messages were collected.
|
|
73
|
+
#
|
|
74
|
+
# @return [Boolean]
|
|
75
|
+
#
|
|
76
|
+
def empty?
|
|
77
|
+
size.zero?
|
|
78
|
+
end
|
|
79
|
+
|
|
80
|
+
#
|
|
81
|
+
# Forgets the collected messages.
|
|
82
|
+
#
|
|
83
|
+
# @return [self]
|
|
84
|
+
#
|
|
85
|
+
def clear
|
|
86
|
+
@mutex.synchronize { @messages.clear }
|
|
87
|
+
self
|
|
88
|
+
end
|
|
89
|
+
|
|
90
|
+
#
|
|
91
|
+
# Calls the block for every new message. Prefer `page.on(:console)`.
|
|
92
|
+
#
|
|
93
|
+
# @yieldparam [Message] message
|
|
94
|
+
#
|
|
95
|
+
# @return [Integer]
|
|
96
|
+
# The listener id, used to remove it via {#off}.
|
|
97
|
+
#
|
|
98
|
+
def on(&block)
|
|
99
|
+
@listener_id.increment.tap { |id| @listeners[id] = block }
|
|
100
|
+
end
|
|
101
|
+
|
|
102
|
+
#
|
|
103
|
+
# Removes a listener added via {#on}.
|
|
104
|
+
#
|
|
105
|
+
# @param [Integer] id
|
|
106
|
+
#
|
|
107
|
+
# @return [void]
|
|
108
|
+
#
|
|
109
|
+
def off(id)
|
|
110
|
+
@listeners.delete(id)
|
|
111
|
+
end
|
|
112
|
+
|
|
113
|
+
#
|
|
114
|
+
# Subscribes to the CDP events messages are built from.
|
|
115
|
+
#
|
|
116
|
+
# @return [void]
|
|
117
|
+
#
|
|
118
|
+
def subscribe
|
|
119
|
+
@page.on("Runtime.consoleAPICalled") do |params|
|
|
120
|
+
add(Message.from_console_api(params)) unless replayed?(params)
|
|
121
|
+
end
|
|
122
|
+
|
|
123
|
+
@page.on("Log.entryAdded") do |params|
|
|
124
|
+
entry = params["entry"]
|
|
125
|
+
add(Message.from_log_entry(entry)) unless entry["source"] == "worker"
|
|
126
|
+
end
|
|
127
|
+
|
|
128
|
+
@page.on("Runtime.exceptionThrown") do |params|
|
|
129
|
+
add(Message.from_exception(params))
|
|
130
|
+
end
|
|
131
|
+
end
|
|
132
|
+
|
|
133
|
+
#
|
|
134
|
+
# Debug representation of the console.
|
|
135
|
+
#
|
|
136
|
+
# @return [String]
|
|
137
|
+
#
|
|
138
|
+
def inspect
|
|
139
|
+
"#<#{self.class.name} size=#{size}>"
|
|
140
|
+
end
|
|
141
|
+
|
|
142
|
+
private
|
|
143
|
+
|
|
144
|
+
def replayed?(params)
|
|
145
|
+
params["executionContextId"].to_i.zero?
|
|
146
|
+
end
|
|
147
|
+
|
|
148
|
+
def add(message)
|
|
149
|
+
@mutex.synchronize do
|
|
150
|
+
@messages << message
|
|
151
|
+
@messages.shift if @messages.size > LIMIT
|
|
152
|
+
end
|
|
153
|
+
|
|
154
|
+
@listeners.each_value { |listener| listener.call(message) }
|
|
155
|
+
end
|
|
156
|
+
end
|
|
157
|
+
end
|
data/lib/ferrum/context.rb
CHANGED
|
@@ -15,14 +15,25 @@ module Ferrum
|
|
|
15
15
|
|
|
16
16
|
attr_reader :id, :targets
|
|
17
17
|
|
|
18
|
-
def initialize(client, contexts, id)
|
|
18
|
+
def initialize(client, contexts, id, implicit: false)
|
|
19
19
|
@id = id
|
|
20
|
+
@implicit = implicit
|
|
20
21
|
@client = client
|
|
21
22
|
@contexts = contexts
|
|
22
|
-
@targets = Concurrent::
|
|
23
|
+
@targets = Concurrent::Hash.new
|
|
23
24
|
@pendings = Concurrent::Map.new
|
|
24
25
|
end
|
|
25
26
|
|
|
27
|
+
# Whether this is the browser's implicit context, the one it puts its
|
|
28
|
+
# startup window in. We didn't create it, so we can't dispose it, and
|
|
29
|
+
# Chrome doesn't let us address it by id either: targets are created in
|
|
30
|
+
# it by omitting `browserContextId` altogether.
|
|
31
|
+
#
|
|
32
|
+
# @return [Boolean]
|
|
33
|
+
def implicit?
|
|
34
|
+
@implicit
|
|
35
|
+
end
|
|
36
|
+
|
|
26
37
|
# The context's first known target, creating one via
|
|
27
38
|
# `Target.createTarget` if none has attached yet.
|
|
28
39
|
#
|
|
@@ -45,11 +56,12 @@ module Ferrum
|
|
|
45
56
|
@targets.values.select(&:page?).map(&:page)
|
|
46
57
|
end
|
|
47
58
|
|
|
48
|
-
# Dedicated and shared workers spawned by any page in this context
|
|
59
|
+
# Dedicated and shared workers spawned by any page in this context,
|
|
60
|
+
# leaving out the ones we have no session with, e.g. detached ones.
|
|
49
61
|
#
|
|
50
62
|
# @return [Array<Worker>]
|
|
51
63
|
def workers
|
|
52
|
-
@targets.values.select { |t| t.worker? || t.shared_worker? }.map(&:worker)
|
|
64
|
+
@targets.values.select { |t| (t.worker? || t.shared_worker?) && t.session_id }.map(&:worker)
|
|
53
65
|
end
|
|
54
66
|
|
|
55
67
|
# Service worker targets registered in this context. Unlike {#workers},
|
|
@@ -92,7 +104,8 @@ module Ferrum
|
|
|
92
104
|
#
|
|
93
105
|
# @raise [NoSuchTargetError]
|
|
94
106
|
def create_target
|
|
95
|
-
|
|
107
|
+
options = { browserContextId: @id } unless implicit?
|
|
108
|
+
target_id = @client.command("Target.createTarget", url: "about:blank", **Hash(options))["targetId"]
|
|
96
109
|
|
|
97
110
|
new_pending = Concurrent::IVar.new
|
|
98
111
|
pending = @pendings.put_if_absent(target_id, new_pending) || new_pending
|
|
@@ -112,13 +125,15 @@ module Ferrum
|
|
|
112
125
|
#
|
|
113
126
|
# @param [String, nil] session_id
|
|
114
127
|
#
|
|
128
|
+
# @param [Boolean] attached
|
|
129
|
+
# The session comes from `Target.attachedToTarget`, so it replaces the one a target got from its frame owner.
|
|
130
|
+
#
|
|
115
131
|
# @return [Target]
|
|
116
|
-
def add_target(params:, session_id: nil)
|
|
117
|
-
new_target = Target.new(@client, session_id, params)
|
|
118
|
-
|
|
119
|
-
target = @targets.put_if_absent(new_target.id, new_target) || new_target
|
|
132
|
+
def add_target(params:, session_id: nil, attached: false)
|
|
133
|
+
new_target = Target.new(@client, session_id, params, implicit: implicit?)
|
|
134
|
+
target = @targets[new_target.id] ||= new_target
|
|
120
135
|
# on first iteration session_id may be nil, then if session is present here we must set it to the target
|
|
121
|
-
target.session_id = session_id if session_id && target.session_id.nil?
|
|
136
|
+
target.session_id = session_id if session_id && (attached || target.session_id.nil?)
|
|
122
137
|
@default_target ||= target
|
|
123
138
|
|
|
124
139
|
new_pending = Concurrent::IVar.new
|
|
@@ -149,25 +164,34 @@ module Ferrum
|
|
|
149
164
|
|
|
150
165
|
# Manually attaches to a target, e.g. a service worker discovered via
|
|
151
166
|
# {#service_workers}. Once attached, `target.worker`/`target.page`
|
|
152
|
-
# returns a connected {Worker}/{Page} for it.
|
|
167
|
+
# returns a connected {Worker}/{Page} for it. A target that already has
|
|
168
|
+
# a session keeps it.
|
|
153
169
|
#
|
|
154
170
|
# Note: attaching to a service worker's session prevents Chrome from
|
|
155
171
|
# ever terminating it while the connection is open.
|
|
172
|
+
#
|
|
173
|
+
# @param [String] target_id
|
|
174
|
+
#
|
|
175
|
+
# @return [Target]
|
|
176
|
+
#
|
|
177
|
+
# @raise [NoSuchTargetError] if the context has no such target.
|
|
156
178
|
def attach_target(target_id)
|
|
157
179
|
target = @targets[target_id]
|
|
158
180
|
raise NoSuchTargetError unless target
|
|
181
|
+
return target if target.session_id
|
|
159
182
|
|
|
160
183
|
@contexts.manually_attached(target_id)
|
|
161
184
|
session = @client.command("Target.attachToTarget", targetId: target_id, flatten: true)
|
|
162
185
|
target.session_id = session["sessionId"]
|
|
163
|
-
|
|
186
|
+
target
|
|
164
187
|
end
|
|
165
188
|
|
|
166
189
|
# Returns the first target for which the block returns truthy.
|
|
167
190
|
#
|
|
168
191
|
# @return [Target, nil]
|
|
169
192
|
def find_target
|
|
170
|
-
@targets.
|
|
193
|
+
targets = @targets.values
|
|
194
|
+
targets.each { |t| return t if yield(t) }
|
|
171
195
|
|
|
172
196
|
nil
|
|
173
197
|
end
|
|
@@ -177,16 +201,13 @@ module Ferrum
|
|
|
177
201
|
#
|
|
178
202
|
# @return [void]
|
|
179
203
|
def close_targets_connection
|
|
180
|
-
@targets.
|
|
181
|
-
next unless target.connected?
|
|
182
|
-
|
|
183
|
-
target.close_connection
|
|
184
|
-
end
|
|
204
|
+
@targets.values.select(&:connected?).each(&:close_connection)
|
|
185
205
|
end
|
|
186
206
|
|
|
187
|
-
# Disposes this browser context and all of its targets.
|
|
207
|
+
# Disposes this browser context and all of its targets. The browser's
|
|
208
|
+
# implicit context cannot be disposed, see {#implicit?}.
|
|
188
209
|
#
|
|
189
|
-
# @return [Boolean]
|
|
210
|
+
# @return [Boolean, nil]
|
|
190
211
|
def dispose
|
|
191
212
|
@contexts.dispose(@id)
|
|
192
213
|
end
|
data/lib/ferrum/contexts.rb
CHANGED
|
@@ -22,6 +22,7 @@ module Ferrum
|
|
|
22
22
|
@client = client
|
|
23
23
|
@contexts = Concurrent::Map.new
|
|
24
24
|
@manually_attached = Concurrent::Map.new
|
|
25
|
+
@default_context = find_implicit_context
|
|
25
26
|
subscribe
|
|
26
27
|
auto_attach
|
|
27
28
|
discover
|
|
@@ -33,7 +34,9 @@ module Ferrum
|
|
|
33
34
|
@manually_attached[target_id] = true
|
|
34
35
|
end
|
|
35
36
|
|
|
36
|
-
# The browser's
|
|
37
|
+
# The context we work in unless told otherwise: the browser's implicit
|
|
38
|
+
# context when it came up with a startup window (see {#find_implicit_context}),
|
|
39
|
+
# otherwise one of our own, created lazily.
|
|
37
40
|
#
|
|
38
41
|
# @return [Context]
|
|
39
42
|
def default_context
|
|
@@ -84,18 +87,29 @@ module Ferrum
|
|
|
84
87
|
context
|
|
85
88
|
end
|
|
86
89
|
|
|
87
|
-
# Disposes a browser context and all of its targets.
|
|
90
|
+
# Disposes a browser context and all of its targets. The browser's implicit
|
|
91
|
+
# context is not ours to dispose, see {Context#implicit?}.
|
|
92
|
+
#
|
|
93
|
+
# The context is forgotten even if Chrome fails to dispose it in time: its
|
|
94
|
+
# targets are disconnected by then, and asking again only gets
|
|
95
|
+
# `Disposal of browser context ... is already pending` back.
|
|
88
96
|
#
|
|
89
97
|
# @param [String] context_id
|
|
90
98
|
#
|
|
91
|
-
# @return [Boolean]
|
|
99
|
+
# @return [Boolean, nil]
|
|
100
|
+
#
|
|
101
|
+
# @raise [TimeoutError, BrowserError]
|
|
92
102
|
def dispose(context_id)
|
|
93
103
|
context = @contexts[context_id]
|
|
94
104
|
return unless context
|
|
105
|
+
return if context.implicit?
|
|
95
106
|
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
107
|
+
begin
|
|
108
|
+
context.close_targets_connection
|
|
109
|
+
@client.command("Target.disposeBrowserContext", browserContextId: context.id)
|
|
110
|
+
ensure
|
|
111
|
+
@contexts.delete(context_id)
|
|
112
|
+
end
|
|
99
113
|
true
|
|
100
114
|
end
|
|
101
115
|
|
|
@@ -107,13 +121,23 @@ module Ferrum
|
|
|
107
121
|
@contexts.each_value(&:close_targets_connection)
|
|
108
122
|
end
|
|
109
123
|
|
|
110
|
-
# Disposes every context still known to the browser.
|
|
124
|
+
# Disposes every context still known to the browser. A context that fails
|
|
125
|
+
# to dispose doesn't stop the others from being disposed; the first error
|
|
126
|
+
# is raised once all of them have been tried.
|
|
111
127
|
#
|
|
112
128
|
# @return [void]
|
|
129
|
+
#
|
|
130
|
+
# @raise [TimeoutError, BrowserError]
|
|
113
131
|
def reset
|
|
132
|
+
error = nil
|
|
114
133
|
context_ids = @client.command("Target.getBrowserContexts")["browserContextIds"]
|
|
115
134
|
@default_context = nil if context_ids.include?(@default_context&.id)
|
|
116
|
-
@contexts.
|
|
135
|
+
(@contexts.keys & context_ids).each do |id|
|
|
136
|
+
dispose(id)
|
|
137
|
+
rescue BrowserError, TimeoutError => e
|
|
138
|
+
error ||= e
|
|
139
|
+
end
|
|
140
|
+
raise error if error
|
|
117
141
|
end
|
|
118
142
|
|
|
119
143
|
# Number of known contexts.
|
|
@@ -135,14 +159,18 @@ module Ferrum
|
|
|
135
159
|
def subscribe_attached_target(client)
|
|
136
160
|
client.on("Target.attachedToTarget") do |params|
|
|
137
161
|
info, session_id = params.values_at("targetInfo", "sessionId")
|
|
138
|
-
next unless ALLOWED_TARGET_TYPES.include?(info["type"])
|
|
162
|
+
next detach_untracked(session_id, client) unless ALLOWED_TARGET_TYPES.include?(info["type"])
|
|
139
163
|
|
|
140
164
|
context_id = info["browserContextId"]
|
|
141
165
|
add_context(context_id)
|
|
142
|
-
target = @contexts[context_id]&.add_target(session_id: session_id, params: info)
|
|
166
|
+
target = @contexts[context_id]&.add_target(session_id: session_id, params: info, attached: true)
|
|
143
167
|
|
|
144
168
|
rearm_auto_attach(session_id, info["type"])
|
|
145
|
-
handle_attach(target, session_id, params)
|
|
169
|
+
handle_attach(target, session_id, params, client)
|
|
170
|
+
end
|
|
171
|
+
|
|
172
|
+
client.on("Target.detachedFromTarget") do |params|
|
|
173
|
+
forget_session(params["targetId"], params["sessionId"])
|
|
146
174
|
end
|
|
147
175
|
end
|
|
148
176
|
|
|
@@ -154,8 +182,7 @@ module Ferrum
|
|
|
154
182
|
context_id = info["browserContextId"]
|
|
155
183
|
add_context(context_id)
|
|
156
184
|
|
|
157
|
-
if info["type"] == "iframe" &&
|
|
158
|
-
(target = @contexts[context_id]&.find_target { |t| t.connected? && t.page.frame_by(id: info["targetId"]) })
|
|
185
|
+
if info["type"] == "iframe" && (target = find_frame_owner(context_id, info["targetId"]))
|
|
159
186
|
@contexts[context_id]&.add_target(session_id: target.session_id, params: info)
|
|
160
187
|
else
|
|
161
188
|
@contexts[context_id]&.add_target(params: info)
|
|
@@ -181,6 +208,12 @@ module Ferrum
|
|
|
181
208
|
end
|
|
182
209
|
end
|
|
183
210
|
|
|
211
|
+
def find_frame_owner(context_id, frame_id)
|
|
212
|
+
@contexts[context_id]&.find_target do |target|
|
|
213
|
+
(target.page? || target.iframe?) && target.connected? && target.page.frame_by(id: frame_id)
|
|
214
|
+
end
|
|
215
|
+
end
|
|
216
|
+
|
|
184
217
|
def rearm_auto_attach(session_id, type)
|
|
185
218
|
return unless RECURSIVE_AUTO_ATTACH_TYPES.include?(type)
|
|
186
219
|
|
|
@@ -189,27 +222,47 @@ module Ferrum
|
|
|
189
222
|
subscribe_attached_target(client)
|
|
190
223
|
end
|
|
191
224
|
|
|
192
|
-
def handle_attach(target, session_id, params)
|
|
225
|
+
def handle_attach(target, session_id, params, parent)
|
|
193
226
|
return unless target
|
|
194
227
|
|
|
195
228
|
if target.service_worker?
|
|
196
|
-
detach_unless_manually_attached(target, session_id)
|
|
229
|
+
detach_unless_manually_attached(target, session_id, parent)
|
|
197
230
|
elsif target.worker? || target.shared_worker?
|
|
198
231
|
connect_worker(target)
|
|
199
|
-
|
|
200
|
-
|
|
232
|
+
else
|
|
233
|
+
add_extensions_and_resume(session_id, params["waitingForDebugger"])
|
|
234
|
+
end
|
|
235
|
+
end
|
|
236
|
+
|
|
237
|
+
# A page or iframe is paused until we resume it, so the extensions we
|
|
238
|
+
# register now run before any script of its first document. An
|
|
239
|
+
# out-of-process iframe ignores them until the Page domain is enabled.
|
|
240
|
+
def add_extensions_and_resume(session_id, waiting)
|
|
241
|
+
extensions = @client.options.extensions
|
|
242
|
+
session = @client.session(session_id)
|
|
243
|
+
session.command("Page.enable", async: true) if extensions.any?
|
|
244
|
+
extensions.each do |source|
|
|
245
|
+
session.command("Page.addScriptToEvaluateOnNewDocument", async: true, source: source)
|
|
201
246
|
end
|
|
247
|
+
resume(session_id) if waiting
|
|
248
|
+
end
|
|
249
|
+
|
|
250
|
+
# Auto-attach pauses every target it attaches to, including the types
|
|
251
|
+
# we don't track: Chrome's own `browser_ui` targets, for instance. We
|
|
252
|
+
# never resume those, so left alone they stay attached and paused for
|
|
253
|
+
# the lifetime of the browser. Resume them and drop the session.
|
|
254
|
+
def detach_untracked(session_id, parent)
|
|
255
|
+
silent_detach(session_id, parent)
|
|
202
256
|
end
|
|
203
257
|
|
|
204
258
|
# Attaching keeps a service worker alive forever, so unless the caller
|
|
205
259
|
# explicitly asked to connect to it (via Context#attach_target), we
|
|
206
260
|
# just resume it and let go.
|
|
207
|
-
def detach_unless_manually_attached(target, session_id)
|
|
261
|
+
def detach_unless_manually_attached(target, session_id, parent)
|
|
208
262
|
return if @manually_attached.delete(target.id)
|
|
209
263
|
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
nil
|
|
264
|
+
forget_session(target.id, session_id)
|
|
265
|
+
silent_detach(session_id, parent)
|
|
213
266
|
end
|
|
214
267
|
|
|
215
268
|
# Workers have no events to notify us when they're ready, so we
|
|
@@ -217,7 +270,7 @@ module Ferrum
|
|
|
217
270
|
# only then resumes the debugger itself.
|
|
218
271
|
def connect_worker(target)
|
|
219
272
|
target.worker
|
|
220
|
-
rescue BrowserError
|
|
273
|
+
rescue BrowserError, TimeoutError
|
|
221
274
|
nil
|
|
222
275
|
end
|
|
223
276
|
|
|
@@ -225,9 +278,18 @@ module Ferrum
|
|
|
225
278
|
@client.session(session_id).command("Runtime.runIfWaitingForDebugger", async: true)
|
|
226
279
|
end
|
|
227
280
|
|
|
228
|
-
def
|
|
229
|
-
|
|
230
|
-
|
|
281
|
+
def forget_session(target_id, session_id)
|
|
282
|
+
target = find_by(target_id: target_id)&.targets&.[](target_id)
|
|
283
|
+
target.session_id = nil if target&.session_id == session_id
|
|
284
|
+
end
|
|
285
|
+
|
|
286
|
+
def silent_detach(session_id, parent)
|
|
287
|
+
Utils::Thread.spawn do
|
|
288
|
+
@client.session(session_id).command("Runtime.runIfWaitingForDebugger")
|
|
289
|
+
parent.command("Target.detachFromTarget", sessionId: session_id)
|
|
290
|
+
rescue Error
|
|
291
|
+
nil
|
|
292
|
+
end
|
|
231
293
|
end
|
|
232
294
|
|
|
233
295
|
def discover
|
|
@@ -241,9 +303,29 @@ module Ferrum
|
|
|
241
303
|
def add_context(context_id)
|
|
242
304
|
return if @contexts[context_id]
|
|
243
305
|
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
|
|
306
|
+
@contexts[context_id] = Context.new(@client, self, context_id)
|
|
307
|
+
end
|
|
308
|
+
|
|
309
|
+
# The browser's implicit context, the context Chrome uses for its startup window.
|
|
310
|
+
# We register it so events for that window are routed to it.
|
|
311
|
+
# `Target.getBrowserContexts` reports every context created with
|
|
312
|
+
# `Target.createBrowserContext`, ours and those of anyone else driving the same
|
|
313
|
+
# browser, so the context holding targets that it doesn't report is the implicit
|
|
314
|
+
# one. Chrome also reports its id as `defaultBrowserContextId`, but only since
|
|
315
|
+
# 145 and only as an experimental field, so we don't rely on it.
|
|
316
|
+
#
|
|
317
|
+
# Returns nil if the browser has no startup window, which happens when it is
|
|
318
|
+
# launched with `--no-startup-window` (used for `incognito: true`). In that case,
|
|
319
|
+
# we create our own context instead.
|
|
320
|
+
#
|
|
321
|
+
# @return [Context, nil]
|
|
322
|
+
def find_implicit_context
|
|
323
|
+
created_ids = @client.command("Target.getBrowserContexts")["browserContextIds"]
|
|
324
|
+
target_infos = @client.command("Target.getTargets")["targetInfos"]
|
|
325
|
+
implicit_id = (target_infos.filter_map { |t| t["browserContextId"] } - created_ids).first
|
|
326
|
+
return unless implicit_id
|
|
327
|
+
|
|
328
|
+
@contexts[implicit_id] = Context.new(@client, self, implicit_id, implicit: true)
|
|
247
329
|
end
|
|
248
330
|
end
|
|
249
331
|
end
|
data/lib/ferrum/downloads.rb
CHANGED
|
@@ -62,8 +62,9 @@ module Ferrum
|
|
|
62
62
|
raise ArgumentError unless VALID_BEHAVIOR.include?(behavior.to_sym)
|
|
63
63
|
raise Error, "supply absolute path for `:save_path` option" unless Pathname.new(save_path.to_s).absolute?
|
|
64
64
|
|
|
65
|
+
options = { browserContextId: @page.context_id } if @page.context_id
|
|
65
66
|
@page.command("Browser.setDownloadBehavior",
|
|
66
|
-
|
|
67
|
+
**Hash(options),
|
|
67
68
|
downloadPath: save_path,
|
|
68
69
|
behavior: behavior,
|
|
69
70
|
eventsEnabled: true)
|
data/lib/ferrum/errors.rb
CHANGED
|
@@ -40,22 +40,54 @@ module Ferrum
|
|
|
40
40
|
|
|
41
41
|
# Raised when waiting for a response from the browser times out.
|
|
42
42
|
class TimeoutError < Error
|
|
43
|
+
# The CDP method that didn't answer in time, `nil` when the wait wasn't for a command.
|
|
43
44
|
#
|
|
44
|
-
#
|
|
45
|
+
# @return [String, nil]
|
|
46
|
+
attr_reader :command
|
|
47
|
+
|
|
48
|
+
# The session the command was sent to, `nil` for the browser-wide session.
|
|
49
|
+
#
|
|
50
|
+
# @return [String, nil]
|
|
51
|
+
attr_reader :session_id
|
|
52
|
+
|
|
53
|
+
#
|
|
54
|
+
# @param [String, nil] command
|
|
55
|
+
# The CDP method that timed out.
|
|
56
|
+
#
|
|
57
|
+
# @param [String, nil] session_id
|
|
58
|
+
# The session it was sent to.
|
|
59
|
+
#
|
|
60
|
+
def initialize(command = nil, session_id: nil)
|
|
61
|
+
@command = command
|
|
62
|
+
@session_id = session_id
|
|
63
|
+
super()
|
|
64
|
+
end
|
|
65
|
+
|
|
66
|
+
#
|
|
67
|
+
# Explains that waiting for a response timed out, naming the command when
|
|
68
|
+
# there was one.
|
|
45
69
|
#
|
|
46
70
|
# @return [String]
|
|
47
71
|
#
|
|
48
72
|
def message
|
|
49
|
-
"Timed out waiting for
|
|
73
|
+
"Timed out waiting for #{awaited}. It's possible that this happened " \
|
|
50
74
|
"because something took a very long time (for example a page load " \
|
|
51
75
|
"was slow). If so, setting the :timeout option to a higher value might " \
|
|
52
76
|
"help. If this happened on an internal protocol call instead, try " \
|
|
53
77
|
"raising :protocol_timeout."
|
|
54
78
|
end
|
|
79
|
+
|
|
80
|
+
private
|
|
81
|
+
|
|
82
|
+
def awaited
|
|
83
|
+
return "response" unless @command
|
|
84
|
+
|
|
85
|
+
@session_id ? "a response to #{@command} (session #{@session_id})" : "a response to #{@command}"
|
|
86
|
+
end
|
|
55
87
|
end
|
|
56
88
|
|
|
57
89
|
# Raised when an evaluated script takes too long to return a value.
|
|
58
|
-
class ScriptTimeoutError <
|
|
90
|
+
class ScriptTimeoutError < TimeoutError
|
|
59
91
|
#
|
|
60
92
|
# Explains that the evaluated script timed out.
|
|
61
93
|
#
|
|
@@ -77,6 +109,20 @@ module Ferrum
|
|
|
77
109
|
end
|
|
78
110
|
end
|
|
79
111
|
|
|
112
|
+
# Raised when connecting to an already running browser, but its
|
|
113
|
+
# `/json/version` endpoint doesn't answer with the websocket url.
|
|
114
|
+
class NoWebSocketUrlError < Error
|
|
115
|
+
attr_reader :url, :status
|
|
116
|
+
|
|
117
|
+
def initialize(url, status = nil)
|
|
118
|
+
@url = url
|
|
119
|
+
@status = status
|
|
120
|
+
reason = status ? "answered #{status}" : "didn't answer with JSON"
|
|
121
|
+
super("Couldn't get a websocket url out of #{url}, `/json/version` there #{reason}. " \
|
|
122
|
+
"Check the browser is reachable at that url and that any credentials it needs are part of it")
|
|
123
|
+
end
|
|
124
|
+
end
|
|
125
|
+
|
|
80
126
|
# Raised when trying to interact with a browser process that has died or
|
|
81
127
|
# a window that has already been closed.
|
|
82
128
|
class DeadBrowserError < Error
|
|
@@ -170,7 +216,7 @@ module Ferrum
|
|
|
170
216
|
|
|
171
217
|
def initialize(response)
|
|
172
218
|
@class_name, @message = response["exception"]&.values_at("className", "description")
|
|
173
|
-
@message ||= response["text"]
|
|
219
|
+
@message ||= [response["text"], response.dig("exception", "value")].compact.join(" ")
|
|
174
220
|
@stack_trace = response["stackTrace"]
|
|
175
221
|
super(response.merge("message" => @message))
|
|
176
222
|
end
|