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
|
@@ -0,0 +1,128 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Ferrum
|
|
4
|
+
module Concurrency
|
|
5
|
+
#
|
|
6
|
+
# A thread-safe slot that threads wait on: it holds a value once set, can be cleared and set again, and wakes
|
|
7
|
+
# every waiter when it is set or closed. It serves as a one-shot response, the latest value of something that
|
|
8
|
+
# changes, or a latch, and every wait is bounded by a {Deadline}.
|
|
9
|
+
#
|
|
10
|
+
# @example
|
|
11
|
+
# cell = Ferrum::Concurrency::Cell.new
|
|
12
|
+
# Thread.new { cell.set(42) }
|
|
13
|
+
# cell.wait(5) # => true
|
|
14
|
+
# cell.value # => 42
|
|
15
|
+
#
|
|
16
|
+
class Cell
|
|
17
|
+
# How many times the cell has been cleared.
|
|
18
|
+
#
|
|
19
|
+
# @return [Integer]
|
|
20
|
+
attr_reader :generation
|
|
21
|
+
|
|
22
|
+
#
|
|
23
|
+
# Builds a cell that already holds the value.
|
|
24
|
+
#
|
|
25
|
+
# @param [Object] value
|
|
26
|
+
#
|
|
27
|
+
# @return [Cell]
|
|
28
|
+
#
|
|
29
|
+
def self.set(value)
|
|
30
|
+
new.tap { |c| c.set(value) }
|
|
31
|
+
end
|
|
32
|
+
|
|
33
|
+
def initialize
|
|
34
|
+
@mutex = Mutex.new
|
|
35
|
+
@condition = ConditionVariable.new
|
|
36
|
+
@set = false
|
|
37
|
+
@closed = false
|
|
38
|
+
@value = nil
|
|
39
|
+
@generation = 0
|
|
40
|
+
end
|
|
41
|
+
|
|
42
|
+
#
|
|
43
|
+
# Stores the value and wakes the waiters.
|
|
44
|
+
#
|
|
45
|
+
# @param [Object] value
|
|
46
|
+
#
|
|
47
|
+
# @return [Object]
|
|
48
|
+
#
|
|
49
|
+
def set(value)
|
|
50
|
+
@mutex.synchronize do
|
|
51
|
+
@value = value
|
|
52
|
+
@set = true
|
|
53
|
+
@condition.broadcast
|
|
54
|
+
end
|
|
55
|
+
value
|
|
56
|
+
end
|
|
57
|
+
|
|
58
|
+
#
|
|
59
|
+
# Empties the cell and counts it as a new generation.
|
|
60
|
+
#
|
|
61
|
+
# @return [Integer]
|
|
62
|
+
# The new generation.
|
|
63
|
+
#
|
|
64
|
+
def clear
|
|
65
|
+
@mutex.synchronize do
|
|
66
|
+
@value = nil
|
|
67
|
+
@set = false
|
|
68
|
+
@generation += 1
|
|
69
|
+
end
|
|
70
|
+
end
|
|
71
|
+
|
|
72
|
+
#
|
|
73
|
+
# Whether a value is stored.
|
|
74
|
+
#
|
|
75
|
+
# @return [Boolean]
|
|
76
|
+
#
|
|
77
|
+
def set?
|
|
78
|
+
@mutex.synchronize { @set }
|
|
79
|
+
end
|
|
80
|
+
|
|
81
|
+
#
|
|
82
|
+
# The stored value, `nil` when the cell is empty.
|
|
83
|
+
#
|
|
84
|
+
# @return [Object, nil]
|
|
85
|
+
#
|
|
86
|
+
def value
|
|
87
|
+
@mutex.synchronize { @value }
|
|
88
|
+
end
|
|
89
|
+
|
|
90
|
+
#
|
|
91
|
+
# Wakes the waiters for good: a closed cell never blocks, so a waiter learns at once that nothing is coming.
|
|
92
|
+
#
|
|
93
|
+
# @return [void]
|
|
94
|
+
#
|
|
95
|
+
def close
|
|
96
|
+
@mutex.synchronize do
|
|
97
|
+
@closed = true
|
|
98
|
+
@condition.broadcast
|
|
99
|
+
end
|
|
100
|
+
end
|
|
101
|
+
|
|
102
|
+
#
|
|
103
|
+
# Whether the cell is closed.
|
|
104
|
+
#
|
|
105
|
+
# @return [Boolean]
|
|
106
|
+
#
|
|
107
|
+
def closed?
|
|
108
|
+
@mutex.synchronize { @closed }
|
|
109
|
+
end
|
|
110
|
+
|
|
111
|
+
#
|
|
112
|
+
# Blocks until a value is stored, the cell is closed or the timeout is reached.
|
|
113
|
+
#
|
|
114
|
+
# @param [Numeric, Deadline] timeout
|
|
115
|
+
#
|
|
116
|
+
# @return [Boolean]
|
|
117
|
+
# Whether a value is stored.
|
|
118
|
+
#
|
|
119
|
+
def wait(timeout)
|
|
120
|
+
deadline = Deadline.wrap(timeout)
|
|
121
|
+
@mutex.synchronize do
|
|
122
|
+
@condition.wait(@mutex, deadline.remaining) until @set || @closed || deadline.expired?
|
|
123
|
+
@set
|
|
124
|
+
end
|
|
125
|
+
end
|
|
126
|
+
end
|
|
127
|
+
end
|
|
128
|
+
end
|
|
@@ -0,0 +1,146 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Ferrum
|
|
4
|
+
module Concurrency
|
|
5
|
+
#
|
|
6
|
+
# Delivers published payloads to the callbacks registered for a key. Every lane is a queue with its own
|
|
7
|
+
# consumer thread, so payloads of one lane are delivered in order and a slow callback never holds up another
|
|
8
|
+
# lane. A raising callback is reported and doesn't stop the delivery.
|
|
9
|
+
#
|
|
10
|
+
# @example
|
|
11
|
+
# dispatcher = Ferrum::Concurrency::Dispatcher.new(lanes: %i[regular priority])
|
|
12
|
+
# dispatcher.on("greeting") { |payload| puts payload }
|
|
13
|
+
# dispatcher.publish("greeting", "hello", lane: :priority)
|
|
14
|
+
#
|
|
15
|
+
class Dispatcher
|
|
16
|
+
DEFAULT_LANES = %i[regular priority].freeze
|
|
17
|
+
|
|
18
|
+
#
|
|
19
|
+
# @param [Array<Symbol>] lanes
|
|
20
|
+
# Names of the lanes, the first one is the default.
|
|
21
|
+
#
|
|
22
|
+
# @param [#call, nil] on_error
|
|
23
|
+
# Receives the key and the error a callback raised, warns when not given.
|
|
24
|
+
#
|
|
25
|
+
def initialize(lanes: DEFAULT_LANES, on_error: nil)
|
|
26
|
+
@queues = lanes.to_h { |l| [l, Queue.new] }
|
|
27
|
+
@on_error = on_error || method(:warn_error)
|
|
28
|
+
@on = Concurrent::Hash.new
|
|
29
|
+
@sequence = Concurrent::AtomicFixnum.new
|
|
30
|
+
|
|
31
|
+
start
|
|
32
|
+
end
|
|
33
|
+
|
|
34
|
+
#
|
|
35
|
+
# Enqueues a payload for delivery to the callbacks registered for the key at the time it is delivered.
|
|
36
|
+
#
|
|
37
|
+
# @param [String] key
|
|
38
|
+
#
|
|
39
|
+
# @param [Object] payload
|
|
40
|
+
#
|
|
41
|
+
# @param [Symbol] lane
|
|
42
|
+
# The first lane when not given.
|
|
43
|
+
#
|
|
44
|
+
# @return [void]
|
|
45
|
+
#
|
|
46
|
+
# @raise [KeyError]
|
|
47
|
+
# When there is no such lane.
|
|
48
|
+
#
|
|
49
|
+
def publish(key, payload, lane: @queues.each_key.first)
|
|
50
|
+
@queues.fetch(lane).push([key, payload])
|
|
51
|
+
end
|
|
52
|
+
|
|
53
|
+
#
|
|
54
|
+
# Registers a callback for a key. It receives the payload.
|
|
55
|
+
#
|
|
56
|
+
# @param [String] key
|
|
57
|
+
#
|
|
58
|
+
# @return [Integer]
|
|
59
|
+
# The callback's id, used to unsubscribe via {#off}. Ids are never reused, so removing a callback doesn't
|
|
60
|
+
# change the others'.
|
|
61
|
+
#
|
|
62
|
+
def on(key, &block)
|
|
63
|
+
id = @sequence.increment
|
|
64
|
+
@on[key] ||= Concurrent::Array.new
|
|
65
|
+
@on[key] << [id, block]
|
|
66
|
+
id
|
|
67
|
+
end
|
|
68
|
+
|
|
69
|
+
#
|
|
70
|
+
# Unregisters a callback.
|
|
71
|
+
#
|
|
72
|
+
# @param [String] key
|
|
73
|
+
#
|
|
74
|
+
# @param [Integer] id
|
|
75
|
+
# The callback's id, as returned by {#on}.
|
|
76
|
+
#
|
|
77
|
+
# @return [Boolean]
|
|
78
|
+
#
|
|
79
|
+
def off(key, id)
|
|
80
|
+
@on[key]&.delete_if { |i, _| i == id }
|
|
81
|
+
true
|
|
82
|
+
end
|
|
83
|
+
|
|
84
|
+
#
|
|
85
|
+
# Whether there's at least one callback registered for the key.
|
|
86
|
+
#
|
|
87
|
+
# @param [String] key
|
|
88
|
+
#
|
|
89
|
+
# @return [Boolean]
|
|
90
|
+
#
|
|
91
|
+
def subscribed?(key)
|
|
92
|
+
@on[key].to_a.any?
|
|
93
|
+
end
|
|
94
|
+
|
|
95
|
+
#
|
|
96
|
+
# Stops the consumer threads.
|
|
97
|
+
#
|
|
98
|
+
# @return [void]
|
|
99
|
+
#
|
|
100
|
+
def close
|
|
101
|
+
@threads.each(&:kill)
|
|
102
|
+
end
|
|
103
|
+
|
|
104
|
+
#
|
|
105
|
+
# Removes the callbacks of every key the block returns a truthy value for.
|
|
106
|
+
#
|
|
107
|
+
# @yieldparam [String] key
|
|
108
|
+
#
|
|
109
|
+
# @return [void]
|
|
110
|
+
#
|
|
111
|
+
def clear
|
|
112
|
+
@on.delete_if { |k, _| yield(k) }
|
|
113
|
+
end
|
|
114
|
+
|
|
115
|
+
private
|
|
116
|
+
|
|
117
|
+
def start
|
|
118
|
+
@threads = @queues.values.map do |queue|
|
|
119
|
+
Utils::Thread.spawn do
|
|
120
|
+
loop do
|
|
121
|
+
item = queue.pop
|
|
122
|
+
break unless item
|
|
123
|
+
|
|
124
|
+
call(*item)
|
|
125
|
+
end
|
|
126
|
+
end
|
|
127
|
+
end
|
|
128
|
+
end
|
|
129
|
+
|
|
130
|
+
def call(key, payload)
|
|
131
|
+
@on[key]&.each do |(_, block)|
|
|
132
|
+
block.call(payload)
|
|
133
|
+
rescue StandardError => e
|
|
134
|
+
# A raising callback used to terminate the dispatch thread, and with it every
|
|
135
|
+
# event for the rest of the browser's life: targets stopped being registered
|
|
136
|
+
# and any page created later raised `NoSuchTargetError`. Report and carry on.
|
|
137
|
+
@on_error.call(key, e)
|
|
138
|
+
end
|
|
139
|
+
end
|
|
140
|
+
|
|
141
|
+
def warn_error(key, error)
|
|
142
|
+
warn("Ferrum: #{key} callback raised #{error.class}: #{error.message}\n #{error.backtrace&.first}")
|
|
143
|
+
end
|
|
144
|
+
end
|
|
145
|
+
end
|
|
146
|
+
end
|
data/lib/ferrum/console.rb
CHANGED
|
@@ -80,11 +80,11 @@ module Ferrum
|
|
|
80
80
|
#
|
|
81
81
|
# Forgets the collected messages.
|
|
82
82
|
#
|
|
83
|
-
# @return [
|
|
83
|
+
# @return [nil]
|
|
84
84
|
#
|
|
85
85
|
def clear
|
|
86
86
|
@mutex.synchronize { @messages.clear }
|
|
87
|
-
|
|
87
|
+
nil
|
|
88
88
|
end
|
|
89
89
|
|
|
90
90
|
#
|
|
@@ -115,6 +115,7 @@ module Ferrum
|
|
|
115
115
|
#
|
|
116
116
|
# @return [void]
|
|
117
117
|
#
|
|
118
|
+
# @api private
|
|
118
119
|
def subscribe
|
|
119
120
|
@page.on("Runtime.consoleAPICalled") do |params|
|
|
120
121
|
add(Message.from_console_api(params)) unless replayed?(params)
|
data/lib/ferrum/context.rb
CHANGED
|
@@ -12,16 +12,19 @@ module Ferrum
|
|
|
12
12
|
#
|
|
13
13
|
class Context
|
|
14
14
|
POSITION = %i[first last].freeze
|
|
15
|
+
WINDOWS_DEPRECATED = "windows will be removed in Ferrum 0.22. Use await_page { ... } for the page a click " \
|
|
16
|
+
"opens, or pages(&:opener_id) for all of them."
|
|
15
17
|
|
|
16
|
-
attr_reader :id
|
|
18
|
+
attr_reader :id
|
|
17
19
|
|
|
18
20
|
def initialize(client, contexts, id, implicit: false)
|
|
19
21
|
@id = id
|
|
20
22
|
@implicit = implicit
|
|
21
23
|
@client = client
|
|
22
24
|
@contexts = contexts
|
|
23
|
-
@targets =
|
|
24
|
-
@
|
|
25
|
+
@targets = Concurrency::AwaitableMap.new
|
|
26
|
+
@page_waiters = []
|
|
27
|
+
@page_waiters_mutex = Mutex.new
|
|
25
28
|
end
|
|
26
29
|
|
|
27
30
|
# Whether this is the browser's implicit context, the one it puts its
|
|
@@ -38,28 +41,61 @@ module Ferrum
|
|
|
38
41
|
# `Target.createTarget` if none has attached yet.
|
|
39
42
|
#
|
|
40
43
|
# @return [Target]
|
|
44
|
+
#
|
|
45
|
+
# @api private
|
|
41
46
|
def default_target
|
|
42
47
|
@default_target ||= create_target
|
|
43
48
|
end
|
|
44
49
|
|
|
50
|
+
# The cookies of the context, which every page in it shares.
|
|
51
|
+
#
|
|
52
|
+
# @return [Cookies]
|
|
53
|
+
def cookies
|
|
54
|
+
@cookies ||= Cookies.new(@client, @id)
|
|
55
|
+
end
|
|
56
|
+
|
|
45
57
|
# Connects to and returns the {#default_target}'s page.
|
|
46
58
|
#
|
|
47
59
|
# @return [Page]
|
|
60
|
+
#
|
|
61
|
+
# @raise [TimeoutError]
|
|
48
62
|
def page
|
|
49
63
|
default_target.page
|
|
50
64
|
end
|
|
51
65
|
|
|
66
|
+
# The targets in this context by id, as they are at the time of the call.
|
|
67
|
+
#
|
|
68
|
+
# @return [Hash{String => Target}]
|
|
69
|
+
def targets
|
|
70
|
+
@targets.to_h
|
|
71
|
+
end
|
|
72
|
+
|
|
52
73
|
# All page targets in this context, connected to as {Page}s.
|
|
53
74
|
#
|
|
75
|
+
# @yield [target]
|
|
76
|
+
# Only the pages of the targets the block returns truthy for are
|
|
77
|
+
# connected to and returned.
|
|
78
|
+
#
|
|
79
|
+
# @yieldparam [Target] target
|
|
80
|
+
#
|
|
54
81
|
# @return [Array<Page>]
|
|
55
|
-
|
|
56
|
-
|
|
82
|
+
#
|
|
83
|
+
# @raise [TimeoutError]
|
|
84
|
+
#
|
|
85
|
+
# @example The pages opened by another page
|
|
86
|
+
# browser.pages(&:opener_id)
|
|
87
|
+
def pages(&)
|
|
88
|
+
targets = @targets.values.select(&:page?)
|
|
89
|
+
targets = targets.select(&) if block_given?
|
|
90
|
+
targets.map(&:page)
|
|
57
91
|
end
|
|
58
92
|
|
|
59
93
|
# Dedicated and shared workers spawned by any page in this context,
|
|
60
94
|
# leaving out the ones we have no session with, e.g. detached ones.
|
|
61
95
|
#
|
|
62
96
|
# @return [Array<Worker>]
|
|
97
|
+
#
|
|
98
|
+
# @raise [TimeoutError]
|
|
63
99
|
def workers
|
|
64
100
|
@targets.values.select { |t| (t.worker? || t.shared_worker?) && t.session_id }.map(&:worker)
|
|
65
101
|
end
|
|
@@ -74,24 +110,93 @@ module Ferrum
|
|
|
74
110
|
@targets.values.select(&:service_worker?)
|
|
75
111
|
end
|
|
76
112
|
|
|
77
|
-
#
|
|
78
|
-
#
|
|
79
|
-
#
|
|
80
|
-
#
|
|
113
|
+
# The pages of this context opened by another page.
|
|
114
|
+
#
|
|
115
|
+
# @deprecated Will be removed in Ferrum 0.22. Use {#await_page} for the page a click opens, or
|
|
116
|
+
# `pages(&:opener_id)` for all of them.
|
|
117
|
+
#
|
|
118
|
+
# @param [:first, :last, nil] pos
|
|
119
|
+
#
|
|
120
|
+
# @param [Integer] size
|
|
121
|
+
#
|
|
122
|
+
# @return [Array<Page>]
|
|
123
|
+
#
|
|
124
|
+
# @raise [ArgumentError]
|
|
125
|
+
#
|
|
126
|
+
# @raise [TimeoutError]
|
|
81
127
|
def windows(pos = nil, size = 1)
|
|
128
|
+
Utils::Deprecate.warn(WINDOWS_DEPRECATED, user_code: true)
|
|
82
129
|
raise ArgumentError if pos && !POSITION.include?(pos)
|
|
83
130
|
|
|
84
|
-
windows = @targets.values.select(&:
|
|
131
|
+
windows = @targets.values.select(&:opener_id)
|
|
85
132
|
windows = windows.send(pos, size) if pos
|
|
86
133
|
windows.map(&:page)
|
|
87
134
|
end
|
|
88
135
|
|
|
136
|
+
# Runs the block and returns the page it opens, in a tab or a window,
|
|
137
|
+
# e.g. through `window.open` or a link with `target="_blank"`.
|
|
138
|
+
#
|
|
139
|
+
# Only a page that another page opened is seen. Chrome reports no opener
|
|
140
|
+
# for a page the user opens from a link, i.e. a click with Ctrl, Cmd or
|
|
141
|
+
# Shift held, or a middle click, so such a page is not returned. Compare
|
|
142
|
+
# {#targets} before and after the click to find it.
|
|
143
|
+
#
|
|
144
|
+
# @param [Page, nil] opener
|
|
145
|
+
# Only return a page opened by this page. By default a page opened by
|
|
146
|
+
# any page of the context is returned.
|
|
147
|
+
#
|
|
148
|
+
# @param [Numeric, Deadline, nil] timeout
|
|
149
|
+
# How long the block and the new page may take together. Defaults to
|
|
150
|
+
# the browser's `timeout`.
|
|
151
|
+
#
|
|
152
|
+
# @yield The code that opens the page.
|
|
153
|
+
#
|
|
154
|
+
# @return [Page, nil]
|
|
155
|
+
# The first page opened since the call, `nil` if none opens in time.
|
|
156
|
+
#
|
|
157
|
+
# @raise [ArgumentError]
|
|
158
|
+
# If no block is given.
|
|
159
|
+
#
|
|
160
|
+
# @example
|
|
161
|
+
# opened = browser.await_page { browser.at_css("a[target=_blank]").click }
|
|
162
|
+
# opened.url # => "https://example.com/"
|
|
163
|
+
def await_page(opener: nil, timeout: nil)
|
|
164
|
+
raise ArgumentError, "await_page needs a block that opens the page" unless block_given?
|
|
165
|
+
|
|
166
|
+
deadline = Deadline.wrap(timeout || @client.timeout)
|
|
167
|
+
waiter = [opener&.target_id, Concurrency::Cell.new]
|
|
168
|
+
@page_waiters_mutex.synchronize { @page_waiters << waiter }
|
|
169
|
+
yield
|
|
170
|
+
return unless waiter.last.wait(deadline)
|
|
171
|
+
|
|
172
|
+
waiter.last.value.page
|
|
173
|
+
ensure
|
|
174
|
+
@page_waiters_mutex.synchronize { @page_waiters.delete(waiter) }
|
|
175
|
+
end
|
|
176
|
+
|
|
177
|
+
# Same as {#await_page}, but raises if no page opens in time.
|
|
178
|
+
#
|
|
179
|
+
# @param [Hash] options
|
|
180
|
+
# The options of {#await_page}.
|
|
181
|
+
#
|
|
182
|
+
# @yield The code that opens the page.
|
|
183
|
+
#
|
|
184
|
+
# @return [Page]
|
|
185
|
+
#
|
|
186
|
+
# @raise [TimeoutError]
|
|
187
|
+
# If no page opens in time.
|
|
188
|
+
def await_page!(**options, &)
|
|
189
|
+
await_page(**options, &) || raise(TimeoutError)
|
|
190
|
+
end
|
|
191
|
+
|
|
89
192
|
# Creates a new target in this context and eagerly connects to it as a
|
|
90
193
|
# {Page}, forwarding `options` to {Target#build_page}.
|
|
91
194
|
#
|
|
92
195
|
# @param [Hash] options
|
|
93
196
|
#
|
|
94
197
|
# @return [Page]
|
|
198
|
+
#
|
|
199
|
+
# @raise [TimeoutError]
|
|
95
200
|
def create_page(**options)
|
|
96
201
|
target = create_target
|
|
97
202
|
target.page = target.build_page(**options)
|
|
@@ -103,17 +208,13 @@ module Ferrum
|
|
|
103
208
|
# @return [Target]
|
|
104
209
|
#
|
|
105
210
|
# @raise [NoSuchTargetError]
|
|
211
|
+
#
|
|
212
|
+
# @raise [TimeoutError]
|
|
106
213
|
def create_target
|
|
107
214
|
options = { browserContextId: @id } unless implicit?
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
pending = @pendings.put_if_absent(target_id, new_pending) || new_pending
|
|
112
|
-
resolved = pending.value(@client.protocol_timeout)
|
|
113
|
-
raise NoSuchTargetError unless resolved
|
|
114
|
-
|
|
115
|
-
@pendings.delete(target_id)
|
|
116
|
-
@targets[target_id]
|
|
215
|
+
deadline = Deadline.wrap(@client.timeout)
|
|
216
|
+
response = @client.command("Target.createTarget", timeout: deadline, url: "about:blank", **Hash(options))
|
|
217
|
+
@targets.await(response["targetId"], deadline) || raise(NoSuchTargetError)
|
|
117
218
|
end
|
|
118
219
|
|
|
119
220
|
# Registers a target discovered via a CDP `Target.*` event, or updates
|
|
@@ -129,16 +230,16 @@ module Ferrum
|
|
|
129
230
|
# The session comes from `Target.attachedToTarget`, so it replaces the one a target got from its frame owner.
|
|
130
231
|
#
|
|
131
232
|
# @return [Target]
|
|
233
|
+
#
|
|
234
|
+
# @api private
|
|
132
235
|
def add_target(params:, session_id: nil, attached: false)
|
|
133
236
|
new_target = Target.new(@client, session_id, params, implicit: implicit?)
|
|
134
|
-
target = @targets[new_target.id]
|
|
237
|
+
target = @targets[new_target.id] || new_target
|
|
135
238
|
# on first iteration session_id may be nil, then if session is present here we must set it to the target
|
|
136
239
|
target.session_id = session_id if session_id && (attached || target.session_id.nil?)
|
|
137
240
|
@default_target ||= target
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
pending = @pendings.put_if_absent(target.id, new_pending) || new_pending
|
|
141
|
-
pending.try_set(true)
|
|
241
|
+
@targets[target.id] = target
|
|
242
|
+
announce_page(target) if target.equal?(new_target)
|
|
142
243
|
target
|
|
143
244
|
end
|
|
144
245
|
|
|
@@ -149,6 +250,8 @@ module Ferrum
|
|
|
149
250
|
# @param [Hash] params
|
|
150
251
|
#
|
|
151
252
|
# @return [void]
|
|
253
|
+
#
|
|
254
|
+
# @api private
|
|
152
255
|
def update_target(target_id, params)
|
|
153
256
|
@targets[target_id]&.update(params)
|
|
154
257
|
end
|
|
@@ -158,6 +261,8 @@ module Ferrum
|
|
|
158
261
|
# @param [String] target_id
|
|
159
262
|
#
|
|
160
263
|
# @return [Target, nil]
|
|
264
|
+
#
|
|
265
|
+
# @api private
|
|
161
266
|
def delete_target(target_id)
|
|
162
267
|
@targets.delete(target_id)
|
|
163
268
|
end
|
|
@@ -175,13 +280,16 @@ module Ferrum
|
|
|
175
280
|
# @return [Target]
|
|
176
281
|
#
|
|
177
282
|
# @raise [NoSuchTargetError] if the context has no such target.
|
|
283
|
+
#
|
|
284
|
+
# @raise [TimeoutError]
|
|
178
285
|
def attach_target(target_id)
|
|
179
286
|
target = @targets[target_id]
|
|
180
287
|
raise NoSuchTargetError unless target
|
|
181
288
|
return target if target.session_id
|
|
182
289
|
|
|
183
290
|
@contexts.manually_attached(target_id)
|
|
184
|
-
session = @client.command("Target.attachToTarget",
|
|
291
|
+
session = @client.command("Target.attachToTarget", timeout: Client::PROTOCOL_TIMEOUT,
|
|
292
|
+
targetId: target_id, flatten: true)
|
|
185
293
|
target.session_id = session["sessionId"]
|
|
186
294
|
target
|
|
187
295
|
end
|
|
@@ -189,6 +297,8 @@ module Ferrum
|
|
|
189
297
|
# Returns the first target for which the block returns truthy.
|
|
190
298
|
#
|
|
191
299
|
# @return [Target, nil]
|
|
300
|
+
#
|
|
301
|
+
# @api private
|
|
192
302
|
def find_target
|
|
193
303
|
targets = @targets.values
|
|
194
304
|
targets.each { |t| return t if yield(t) }
|
|
@@ -200,6 +310,8 @@ module Ferrum
|
|
|
200
310
|
# disposing the targets themselves.
|
|
201
311
|
#
|
|
202
312
|
# @return [void]
|
|
313
|
+
#
|
|
314
|
+
# @api private
|
|
203
315
|
def close_targets_connection
|
|
204
316
|
@targets.values.select(&:connected?).each(&:close_connection)
|
|
205
317
|
end
|
|
@@ -207,18 +319,32 @@ module Ferrum
|
|
|
207
319
|
# Disposes this browser context and all of its targets. The browser's
|
|
208
320
|
# implicit context cannot be disposed, see {#implicit?}.
|
|
209
321
|
#
|
|
210
|
-
# @return [
|
|
322
|
+
# @return [nil]
|
|
323
|
+
#
|
|
324
|
+
# @raise [TimeoutError]
|
|
211
325
|
def dispose
|
|
212
326
|
@contexts.dispose(@id)
|
|
213
327
|
end
|
|
214
328
|
|
|
329
|
+
# The target with the given id.
|
|
330
|
+
#
|
|
331
|
+
# @param [String] target_id
|
|
332
|
+
#
|
|
333
|
+
# @return [Target, nil]
|
|
334
|
+
# `nil` when the context has no such target.
|
|
335
|
+
def target(target_id)
|
|
336
|
+
@targets[target_id]
|
|
337
|
+
end
|
|
338
|
+
|
|
215
339
|
# Whether a target with the given id is known in this context.
|
|
216
340
|
#
|
|
217
341
|
# @param [String] target_id
|
|
218
342
|
#
|
|
219
343
|
# @return [Boolean]
|
|
344
|
+
#
|
|
345
|
+
# @api private
|
|
220
346
|
def target?(target_id)
|
|
221
|
-
|
|
347
|
+
!!target(target_id)
|
|
222
348
|
end
|
|
223
349
|
|
|
224
350
|
# Debug representation of the context, including its known targets.
|
|
@@ -227,5 +353,14 @@ module Ferrum
|
|
|
227
353
|
def inspect
|
|
228
354
|
%(#<#{self.class} @id=#{@id.inspect} @targets=#{@targets.inspect} @default_target=#{@default_target.inspect}>)
|
|
229
355
|
end
|
|
356
|
+
|
|
357
|
+
private
|
|
358
|
+
|
|
359
|
+
def announce_page(target)
|
|
360
|
+
return unless target.page? && target.opener_id
|
|
361
|
+
|
|
362
|
+
waiters = @page_waiters_mutex.synchronize { @page_waiters.select { |id, _| id.nil? || id == target.opener_id } }
|
|
363
|
+
waiters.each { |w| w.last.set(target) }
|
|
364
|
+
end
|
|
230
365
|
end
|
|
231
366
|
end
|