ferrum 0.20.2 → 0.21.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/README.md +2 -4
- data/lib/ferrum/accessibility.rb +40 -6
- data/lib/ferrum/browser/command.rb +2 -11
- data/lib/ferrum/browser/options/chrome.rb +58 -1
- data/lib/ferrum/browser/options.rb +63 -9
- data/lib/ferrum/browser/process.rb +12 -4
- data/lib/ferrum/browser/xvfb.rb +22 -2
- data/lib/ferrum/browser.rb +74 -35
- data/lib/ferrum/client/web_socket.rb +13 -6
- data/lib/ferrum/client.rb +119 -81
- data/lib/ferrum/concurrency/awaitable_map.rb +171 -0
- data/lib/ferrum/concurrency/cell.rb +128 -0
- data/lib/ferrum/concurrency/dispatcher.rb +146 -0
- data/lib/ferrum/console.rb +3 -2
- data/lib/ferrum/context.rb +162 -27
- data/lib/ferrum/contexts.rb +45 -22
- data/lib/ferrum/cookies/collection.rb +93 -0
- data/lib/ferrum/cookies/cookie.rb +0 -11
- data/lib/ferrum/cookies.rb +79 -86
- data/lib/ferrum/deadline.rb +140 -0
- data/lib/ferrum/dialog.rb +22 -0
- data/lib/ferrum/downloads.rb +83 -10
- data/lib/ferrum/errors.rb +96 -6
- data/lib/ferrum/frame/dom.rb +147 -38
- data/lib/ferrum/frame/runtime.rb +40 -32
- data/lib/ferrum/frame/script.rb +7 -38
- data/lib/ferrum/frame.rb +57 -20
- data/lib/ferrum/headers.rb +32 -27
- data/lib/ferrum/interceptable/chain.rb +99 -0
- data/lib/ferrum/interceptable.rb +44 -27
- data/lib/ferrum/isolated_world.rb +9 -10
- data/lib/ferrum/keyboard.rb +27 -6
- data/lib/ferrum/mouse.rb +139 -52
- data/lib/ferrum/network/auth_request.rb +42 -7
- data/lib/ferrum/network/error.rb +34 -4
- data/lib/ferrum/network/exchange.rb +35 -7
- data/lib/ferrum/network/intercepted_request.rb +42 -9
- data/lib/ferrum/network/request.rb +33 -9
- data/lib/ferrum/network/request_params.rb +11 -2
- data/lib/ferrum/network/response.rb +23 -2
- data/lib/ferrum/network.rb +267 -64
- data/lib/ferrum/node.rb +430 -62
- data/lib/ferrum/page/animation.rb +4 -0
- data/lib/ferrum/page/cookies.rb +133 -0
- data/lib/ferrum/page/frames.rb +7 -7
- data/lib/ferrum/page/navigation.rb +430 -0
- data/lib/ferrum/page/screencast.rb +4 -0
- data/lib/ferrum/page/screenshot.rb +13 -1
- data/lib/ferrum/page/stream.rb +13 -5
- data/lib/ferrum/page/tracing.rb +12 -7
- data/lib/ferrum/page.rb +168 -190
- data/lib/ferrum/remote_object.rb +4 -0
- data/lib/ferrum/target.rb +49 -11
- data/lib/ferrum/utils/deprecate.rb +73 -2
- data/lib/ferrum/utils/poll.rb +57 -0
- data/lib/ferrum/version.rb +1 -1
- data/lib/ferrum/worker.rb +34 -19
- data/lib/ferrum.rb +5 -1
- metadata +11 -6
- data/lib/ferrum/browser/options/base.rb +0 -112
- data/lib/ferrum/client/subscriber.rb +0 -145
- data/lib/ferrum/frame/runtime/legacy.rb +0 -336
- data/lib/ferrum/utils/event.rb +0 -33
data/lib/ferrum/contexts.rb
CHANGED
|
@@ -14,8 +14,12 @@ module Ferrum
|
|
|
14
14
|
ALLOWED_TARGET_TYPES = %w[page iframe worker shared_worker service_worker].freeze
|
|
15
15
|
RECURSIVE_AUTO_ATTACH_TYPES = %w[page iframe worker shared_worker].freeze
|
|
16
16
|
|
|
17
|
+
EACH_PAIRS_DEPRECATED = "contexts.each { |id, context| } will be removed in Ferrum 0.22. It yields the context, " \
|
|
18
|
+
"use context.id for the id."
|
|
19
|
+
|
|
17
20
|
include Enumerable
|
|
18
21
|
|
|
22
|
+
# @api private
|
|
19
23
|
attr_reader :contexts
|
|
20
24
|
|
|
21
25
|
def initialize(client)
|
|
@@ -30,6 +34,8 @@ module Ferrum
|
|
|
30
34
|
|
|
31
35
|
# Marks a target, so the next time we see it attached, we leave its session alone instead of {#detach}ing it.
|
|
32
36
|
# Used by {Context#attach_target} right before it manually attaches to a service worker on the caller's behalf.
|
|
37
|
+
#
|
|
38
|
+
# @api private
|
|
33
39
|
def manually_attached(target_id)
|
|
34
40
|
@manually_attached[target_id] = true
|
|
35
41
|
end
|
|
@@ -43,14 +49,23 @@ module Ferrum
|
|
|
43
49
|
@default_context ||= create
|
|
44
50
|
end
|
|
45
51
|
|
|
46
|
-
# Iterates over
|
|
47
|
-
# block is
|
|
52
|
+
# Iterates over the contexts; returns an `Enumerator` if no block is given.
|
|
53
|
+
# A block taking two arguments still gets `[id, context]` pairs, which is
|
|
54
|
+
# deprecated and will be removed in Ferrum 0.22.
|
|
55
|
+
#
|
|
56
|
+
# @yieldparam [Context] context
|
|
48
57
|
#
|
|
49
|
-
# @return [
|
|
50
|
-
def each(&)
|
|
51
|
-
return enum_for(__method__) unless
|
|
58
|
+
# @return [self, Enumerator]
|
|
59
|
+
def each(&block)
|
|
60
|
+
return enum_for(__method__) unless block
|
|
52
61
|
|
|
53
|
-
|
|
62
|
+
if block.arity == 2
|
|
63
|
+
Utils::Deprecate.warn(EACH_PAIRS_DEPRECATED, user_code: true)
|
|
64
|
+
@contexts.each(&block)
|
|
65
|
+
else
|
|
66
|
+
@contexts.each_value(&block)
|
|
67
|
+
end
|
|
68
|
+
self
|
|
54
69
|
end
|
|
55
70
|
|
|
56
71
|
# The context with the given id, if any.
|
|
@@ -67,6 +82,8 @@ module Ferrum
|
|
|
67
82
|
# @param [String] target_id
|
|
68
83
|
#
|
|
69
84
|
# @return [Context, nil]
|
|
85
|
+
#
|
|
86
|
+
# @api private
|
|
70
87
|
def find_by(target_id:)
|
|
71
88
|
context = nil
|
|
72
89
|
@contexts.each_value { |c| context = c if c.target?(target_id) }
|
|
@@ -79,8 +96,10 @@ module Ferrum
|
|
|
79
96
|
# Keyword arguments forwarded to `Target.createBrowserContext`.
|
|
80
97
|
#
|
|
81
98
|
# @return [Context]
|
|
99
|
+
#
|
|
100
|
+
# @raise [TimeoutError]
|
|
82
101
|
def create(**options)
|
|
83
|
-
response = @client.command("Target.createBrowserContext", **options)
|
|
102
|
+
response = @client.command("Target.createBrowserContext", timeout: @client.timeout, **options)
|
|
84
103
|
context_id = response["browserContextId"]
|
|
85
104
|
context = Context.new(@client, self, context_id)
|
|
86
105
|
@contexts[context_id] = context
|
|
@@ -96,7 +115,7 @@ module Ferrum
|
|
|
96
115
|
#
|
|
97
116
|
# @param [String] context_id
|
|
98
117
|
#
|
|
99
|
-
# @return [
|
|
118
|
+
# @return [nil]
|
|
100
119
|
#
|
|
101
120
|
# @raise [TimeoutError, BrowserError]
|
|
102
121
|
def dispose(context_id)
|
|
@@ -106,17 +125,20 @@ module Ferrum
|
|
|
106
125
|
|
|
107
126
|
begin
|
|
108
127
|
context.close_targets_connection
|
|
109
|
-
@client.command("Target.disposeBrowserContext", browserContextId: context.id)
|
|
128
|
+
@client.command("Target.disposeBrowserContext", timeout: @client.timeout, browserContextId: context.id)
|
|
110
129
|
ensure
|
|
111
130
|
@contexts.delete(context_id)
|
|
112
131
|
end
|
|
113
|
-
|
|
132
|
+
|
|
133
|
+
nil
|
|
114
134
|
end
|
|
115
135
|
|
|
116
136
|
# Closes the WebSocket connection of every target in every context,
|
|
117
137
|
# without disposing the contexts themselves.
|
|
118
138
|
#
|
|
119
139
|
# @return [void]
|
|
140
|
+
#
|
|
141
|
+
# @api private
|
|
120
142
|
def close_connections
|
|
121
143
|
@contexts.each_value(&:close_targets_connection)
|
|
122
144
|
end
|
|
@@ -130,7 +152,7 @@ module Ferrum
|
|
|
130
152
|
# @raise [TimeoutError, BrowserError]
|
|
131
153
|
def reset
|
|
132
154
|
error = nil
|
|
133
|
-
context_ids = @client.command("Target.getBrowserContexts")["browserContextIds"]
|
|
155
|
+
context_ids = @client.command("Target.getBrowserContexts", timeout: @client.timeout)["browserContextIds"]
|
|
134
156
|
@default_context = nil if context_ids.include?(@default_context&.id)
|
|
135
157
|
(@contexts.keys & context_ids).each do |id|
|
|
136
158
|
dispose(id)
|
|
@@ -218,7 +240,7 @@ module Ferrum
|
|
|
218
240
|
return unless RECURSIVE_AUTO_ATTACH_TYPES.include?(type)
|
|
219
241
|
|
|
220
242
|
client = @client.session(session_id)
|
|
221
|
-
client.
|
|
243
|
+
client.notify("Target.setAutoAttach", autoAttach: true, waitForDebuggerOnStart: true, flatten: true)
|
|
222
244
|
subscribe_attached_target(client)
|
|
223
245
|
end
|
|
224
246
|
|
|
@@ -240,9 +262,9 @@ module Ferrum
|
|
|
240
262
|
def add_extensions_and_resume(session_id, waiting)
|
|
241
263
|
extensions = @client.options.extensions
|
|
242
264
|
session = @client.session(session_id)
|
|
243
|
-
session.
|
|
265
|
+
session.notify("Page.enable") if extensions.any?
|
|
244
266
|
extensions.each do |source|
|
|
245
|
-
session.
|
|
267
|
+
session.notify("Page.addScriptToEvaluateOnNewDocument", source: source)
|
|
246
268
|
end
|
|
247
269
|
resume(session_id) if waiting
|
|
248
270
|
end
|
|
@@ -275,29 +297,30 @@ module Ferrum
|
|
|
275
297
|
end
|
|
276
298
|
|
|
277
299
|
def resume(session_id)
|
|
278
|
-
@client.session(session_id).
|
|
300
|
+
@client.session(session_id).notify("Runtime.runIfWaitingForDebugger")
|
|
279
301
|
end
|
|
280
302
|
|
|
281
303
|
def forget_session(target_id, session_id)
|
|
282
|
-
target = find_by(target_id: target_id)&.
|
|
304
|
+
target = find_by(target_id: target_id)&.target(target_id)
|
|
283
305
|
target.session_id = nil if target&.session_id == session_id
|
|
284
306
|
end
|
|
285
307
|
|
|
286
308
|
def silent_detach(session_id, parent)
|
|
287
309
|
Utils::Thread.spawn do
|
|
288
|
-
@client.session(session_id).command("Runtime.runIfWaitingForDebugger")
|
|
289
|
-
parent.command("Target.detachFromTarget", sessionId: session_id)
|
|
310
|
+
@client.session(session_id).command("Runtime.runIfWaitingForDebugger", timeout: Client::PROTOCOL_TIMEOUT)
|
|
311
|
+
parent.command("Target.detachFromTarget", timeout: Client::PROTOCOL_TIMEOUT, sessionId: session_id)
|
|
290
312
|
rescue Error
|
|
291
313
|
nil
|
|
292
314
|
end
|
|
293
315
|
end
|
|
294
316
|
|
|
295
317
|
def discover
|
|
296
|
-
@client.command("Target.setDiscoverTargets", discover: true)
|
|
318
|
+
@client.command("Target.setDiscoverTargets", timeout: Client::PROTOCOL_TIMEOUT, discover: true)
|
|
297
319
|
end
|
|
298
320
|
|
|
299
321
|
def auto_attach
|
|
300
|
-
@client.command("Target.setAutoAttach",
|
|
322
|
+
@client.command("Target.setAutoAttach", timeout: Client::PROTOCOL_TIMEOUT, autoAttach: true,
|
|
323
|
+
waitForDebuggerOnStart: true, flatten: true)
|
|
301
324
|
end
|
|
302
325
|
|
|
303
326
|
def add_context(context_id)
|
|
@@ -320,8 +343,8 @@ module Ferrum
|
|
|
320
343
|
#
|
|
321
344
|
# @return [Context, nil]
|
|
322
345
|
def find_implicit_context
|
|
323
|
-
created_ids = @client.command("Target.getBrowserContexts")["browserContextIds"]
|
|
324
|
-
target_infos = @client.command("Target.getTargets")["targetInfos"]
|
|
346
|
+
created_ids = @client.command("Target.getBrowserContexts", timeout: Client::PROTOCOL_TIMEOUT)["browserContextIds"]
|
|
347
|
+
target_infos = @client.command("Target.getTargets", timeout: Client::PROTOCOL_TIMEOUT)["targetInfos"]
|
|
325
348
|
implicit_id = (target_infos.filter_map { |t| t["browserContextId"] } - created_ids).first
|
|
326
349
|
return unless implicit_id
|
|
327
350
|
|
|
@@ -0,0 +1,93 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Ferrum
|
|
4
|
+
class Cookies
|
|
5
|
+
#
|
|
6
|
+
# Reading cookies by name, shared by {Cookies} and {Page::Cookies}, both
|
|
7
|
+
# of which define `each`.
|
|
8
|
+
#
|
|
9
|
+
module Collection
|
|
10
|
+
include Enumerable
|
|
11
|
+
|
|
12
|
+
#
|
|
13
|
+
# @deprecated Will be removed in Ferrum 0.22, it keeps one cookie per
|
|
14
|
+
# name. Use `to_a`, or `map(&:name)` for the names.
|
|
15
|
+
#
|
|
16
|
+
# Returns cookies hash.
|
|
17
|
+
#
|
|
18
|
+
# @param [Hash] options
|
|
19
|
+
# Passed to `each`.
|
|
20
|
+
#
|
|
21
|
+
# @return [Hash{String => Cookie}]
|
|
22
|
+
#
|
|
23
|
+
# @example
|
|
24
|
+
# page.cookies.all # => {
|
|
25
|
+
# # "NID" => #<Ferrum::Cookies::Cookie:0x0000558624b37a40 @attributes={
|
|
26
|
+
# # "name"=>"NID", "value"=>"...", "domain"=>".google.com", "path"=>"/",
|
|
27
|
+
# # "expires"=>1583211046.575681, "size"=>178, "httpOnly"=>true, "secure"=>false, "session"=>false
|
|
28
|
+
# # }>
|
|
29
|
+
# # }
|
|
30
|
+
#
|
|
31
|
+
def all(**options)
|
|
32
|
+
Utils::Deprecate.warn(
|
|
33
|
+
"cookies.all will be removed in Ferrum 0.22, it keeps one cookie per name. " \
|
|
34
|
+
"Use cookies.to_a, or cookies.map(&:name) for the names.",
|
|
35
|
+
user_code: true
|
|
36
|
+
)
|
|
37
|
+
each(**options).to_h do |cookie|
|
|
38
|
+
[cookie.name, cookie]
|
|
39
|
+
end
|
|
40
|
+
end
|
|
41
|
+
|
|
42
|
+
#
|
|
43
|
+
# Returns the first cookie with the given name, narrowed down by domain
|
|
44
|
+
# and path when the name is used by more than one cookie.
|
|
45
|
+
#
|
|
46
|
+
# @param [String] name
|
|
47
|
+
# The cookie name to fetch.
|
|
48
|
+
#
|
|
49
|
+
# @param [String, nil] domain
|
|
50
|
+
# The exact domain of the cookie, e.g. `".example.com"` for one sent to
|
|
51
|
+
# every subdomain.
|
|
52
|
+
#
|
|
53
|
+
# @param [String, nil] path
|
|
54
|
+
# The exact path of the cookie.
|
|
55
|
+
#
|
|
56
|
+
# @return [Cookie, nil]
|
|
57
|
+
# The matching cookie.
|
|
58
|
+
#
|
|
59
|
+
# @example
|
|
60
|
+
# page.cookies["NID"] # =>
|
|
61
|
+
# # <Ferrum::Cookies::Cookie:0x0000558624b67a88 @attributes={
|
|
62
|
+
# # "name"=>"NID", "value"=>"...", "domain"=>".google.com",
|
|
63
|
+
# # "path"=>"/", "expires"=>1583211046.575681, "size"=>178,
|
|
64
|
+
# # "httpOnly"=>true, "secure"=>false, "session"=>false
|
|
65
|
+
# # }>
|
|
66
|
+
#
|
|
67
|
+
# @example
|
|
68
|
+
# page.cookies["id", domain: "www.example.com", path: "/app"]
|
|
69
|
+
#
|
|
70
|
+
def [](name, domain: nil, path: nil)
|
|
71
|
+
find do |cookie|
|
|
72
|
+
cookie.name == name && (domain.nil? || cookie.domain == domain) && (path.nil? || cookie.path == path)
|
|
73
|
+
end
|
|
74
|
+
end
|
|
75
|
+
|
|
76
|
+
private
|
|
77
|
+
|
|
78
|
+
def cookie_params(options)
|
|
79
|
+
cookie = (options.is_a?(Cookie) ? options.attributes : options).transform_keys(&:to_sym)
|
|
80
|
+
cookie.transform_keys!(httponly: :httpOnly, http_only: :httpOnly, samesite: :sameSite, same_site: :sameSite)
|
|
81
|
+
|
|
82
|
+
expires = cookie.delete(:expires).to_i
|
|
83
|
+
cookie[:expires] = expires if expires.positive?
|
|
84
|
+
|
|
85
|
+
cookie
|
|
86
|
+
end
|
|
87
|
+
|
|
88
|
+
def base_domain(base_url)
|
|
89
|
+
URI.parse(base_url).host if base_url
|
|
90
|
+
end
|
|
91
|
+
end
|
|
92
|
+
end
|
|
93
|
+
end
|
|
@@ -122,17 +122,6 @@ module Ferrum
|
|
|
122
122
|
@attributes["priority"]
|
|
123
123
|
end
|
|
124
124
|
|
|
125
|
-
#
|
|
126
|
-
# Specifies whether the cookie is a first-party set cookie or not.
|
|
127
|
-
#
|
|
128
|
-
# @return [Boolean]
|
|
129
|
-
#
|
|
130
|
-
def sameparty?
|
|
131
|
-
@attributes["sameParty"]
|
|
132
|
-
end
|
|
133
|
-
|
|
134
|
-
alias same_party? sameparty?
|
|
135
|
-
|
|
136
125
|
#
|
|
137
126
|
# The scheme (`"Unset"`, `"NonSecure"`, `"Secure"`) that the cookie was set with.
|
|
138
127
|
#
|
data/lib/ferrum/cookies.rb
CHANGED
|
@@ -2,82 +2,49 @@
|
|
|
2
2
|
|
|
3
3
|
require "yaml"
|
|
4
4
|
require "ferrum/cookies/cookie"
|
|
5
|
+
require "ferrum/cookies/collection"
|
|
5
6
|
|
|
6
7
|
module Ferrum
|
|
7
8
|
#
|
|
8
|
-
# Manages a
|
|
9
|
-
# the
|
|
10
|
-
# set, remove and clear them, as well
|
|
9
|
+
# Manages the cookies of a browser {Context}, which every page in it shares,
|
|
10
|
+
# via the CDP `Storage` domain. Enumerable over all of them whatever their
|
|
11
|
+
# domain, and provides methods to read, set, remove and clear them, as well
|
|
12
|
+
# as persist them to/from a YAML file. {Page::Cookies} narrows them down to
|
|
13
|
+
# what one page sees.
|
|
11
14
|
#
|
|
12
15
|
class Cookies
|
|
13
|
-
include
|
|
16
|
+
include Collection
|
|
14
17
|
|
|
15
|
-
def initialize(
|
|
16
|
-
@
|
|
18
|
+
def initialize(client, context_id)
|
|
19
|
+
@client = client
|
|
20
|
+
@context_id = context_id
|
|
17
21
|
end
|
|
18
22
|
|
|
19
23
|
#
|
|
20
|
-
# Enumerates over all cookies.
|
|
24
|
+
# Enumerates over all cookies in the context.
|
|
21
25
|
#
|
|
22
26
|
# @yield [cookie]
|
|
23
27
|
# The given block will be passed each cookie.
|
|
24
28
|
#
|
|
25
29
|
# @yieldparam [Cookie] cookie
|
|
26
|
-
# A cookie in the
|
|
30
|
+
# A cookie in the context.
|
|
27
31
|
#
|
|
28
32
|
# @return [Enumerator]
|
|
29
33
|
# If no block is given, an Enumerator object will be returned.
|
|
30
34
|
#
|
|
35
|
+
# @raise [TimeoutError]
|
|
36
|
+
#
|
|
31
37
|
def each
|
|
32
38
|
return enum_for(__method__) unless block_given?
|
|
33
39
|
|
|
34
|
-
|
|
40
|
+
response = @client.command("Storage.getCookies", timeout: @client.timeout, browserContextId: @context_id)
|
|
41
|
+
cookies = response["cookies"]
|
|
35
42
|
|
|
36
43
|
cookies.each do |c|
|
|
37
44
|
yield Cookie.new(c)
|
|
38
45
|
end
|
|
39
46
|
end
|
|
40
47
|
|
|
41
|
-
#
|
|
42
|
-
# Returns cookies hash.
|
|
43
|
-
#
|
|
44
|
-
# @return [Hash{String => Cookie}]
|
|
45
|
-
#
|
|
46
|
-
# @example
|
|
47
|
-
# browser.cookies.all # => {
|
|
48
|
-
# # "NID" => #<Ferrum::Cookies::Cookie:0x0000558624b37a40 @attributes={
|
|
49
|
-
# # "name"=>"NID", "value"=>"...", "domain"=>".google.com", "path"=>"/",
|
|
50
|
-
# # "expires"=>1583211046.575681, "size"=>178, "httpOnly"=>true, "secure"=>false, "session"=>false
|
|
51
|
-
# # }>
|
|
52
|
-
# # }
|
|
53
|
-
#
|
|
54
|
-
def all
|
|
55
|
-
each.to_h do |cookie|
|
|
56
|
-
[cookie.name, cookie]
|
|
57
|
-
end
|
|
58
|
-
end
|
|
59
|
-
|
|
60
|
-
#
|
|
61
|
-
# Returns cookie.
|
|
62
|
-
#
|
|
63
|
-
# @param [String] name
|
|
64
|
-
# The cookie name to fetch.
|
|
65
|
-
#
|
|
66
|
-
# @return [Cookie, nil]
|
|
67
|
-
# The cookie with the matching name.
|
|
68
|
-
#
|
|
69
|
-
# @example
|
|
70
|
-
# browser.cookies["NID"] # =>
|
|
71
|
-
# # <Ferrum::Cookies::Cookie:0x0000558624b67a88 @attributes={
|
|
72
|
-
# # "name"=>"NID", "value"=>"...", "domain"=>".google.com",
|
|
73
|
-
# # "path"=>"/", "expires"=>1583211046.575681, "size"=>178,
|
|
74
|
-
# # "httpOnly"=>true, "secure"=>false, "session"=>false
|
|
75
|
-
# # }>
|
|
76
|
-
#
|
|
77
|
-
def [](name)
|
|
78
|
-
find { |cookie| cookie.name == name }
|
|
79
|
-
end
|
|
80
|
-
|
|
81
48
|
#
|
|
82
49
|
# Sets a cookie.
|
|
83
50
|
#
|
|
@@ -90,7 +57,10 @@ module Ferrum
|
|
|
90
57
|
# The cookie param value.
|
|
91
58
|
#
|
|
92
59
|
# @option options [String] :domain
|
|
93
|
-
# The domain the cookie belongs to.
|
|
60
|
+
# The domain the cookie belongs to, the `:base_url` host by default.
|
|
61
|
+
#
|
|
62
|
+
# @option options [String] :url
|
|
63
|
+
# The URL to derive the domain and path from instead.
|
|
94
64
|
#
|
|
95
65
|
# @option options [String] :path
|
|
96
66
|
# The path that the cookie is bound to.
|
|
@@ -98,20 +68,20 @@ module Ferrum
|
|
|
98
68
|
# @option options [Integer] :expires
|
|
99
69
|
# When the cookie will expire.
|
|
100
70
|
#
|
|
101
|
-
# @option options [Integer] :size
|
|
102
|
-
# The size of the cookie.
|
|
103
|
-
#
|
|
104
71
|
# @option options [Boolean] :httponly
|
|
105
|
-
# Specifies whether the cookie `HttpOnly`.
|
|
72
|
+
# Specifies whether the cookie `HttpOnly`, also accepted as `:http_only`.
|
|
106
73
|
#
|
|
107
74
|
# @option options [Boolean] :secure
|
|
108
75
|
# Specifies whether the cookie is marked as `Secure`.
|
|
109
76
|
#
|
|
110
77
|
# @option options [String] :samesite
|
|
111
|
-
# Specifies whether the cookie is `SameSite`.
|
|
78
|
+
# Specifies whether the cookie is `SameSite`, also accepted as `:same_site`.
|
|
112
79
|
#
|
|
113
|
-
# @
|
|
114
|
-
#
|
|
80
|
+
# @return [nil]
|
|
81
|
+
#
|
|
82
|
+
# @raise [BrowserError] if neither `:domain`, `:url` nor `:base_url` is set.
|
|
83
|
+
#
|
|
84
|
+
# @raise [TimeoutError]
|
|
115
85
|
#
|
|
116
86
|
# @example
|
|
117
87
|
# browser.cookies.set(name: "stealth", value: "omg", domain: "google.com") # => true
|
|
@@ -121,23 +91,16 @@ module Ferrum
|
|
|
121
91
|
# browser.cookies.set(nid_cookie) # => true
|
|
122
92
|
#
|
|
123
93
|
def set(options)
|
|
124
|
-
cookie = (
|
|
125
|
-
|
|
126
|
-
).dup.transform_keys(&:to_sym)
|
|
127
|
-
|
|
128
|
-
cookie[:domain] ||= default_domain
|
|
129
|
-
|
|
130
|
-
cookie[:httpOnly] = cookie.delete(:httponly) if cookie.key?(:httponly)
|
|
131
|
-
cookie[:sameSite] = cookie.delete(:samesite) if cookie.key?(:samesite)
|
|
132
|
-
|
|
133
|
-
expires = cookie.delete(:expires).to_i
|
|
134
|
-
cookie[:expires] = expires if expires.positive?
|
|
94
|
+
cookie = cookie_params(options)
|
|
95
|
+
cookie[:domain] ||= default_domain unless cookie[:url]
|
|
135
96
|
|
|
136
|
-
|
|
97
|
+
write(cookie)
|
|
98
|
+
nil
|
|
137
99
|
end
|
|
138
100
|
|
|
139
101
|
#
|
|
140
|
-
# Removes given
|
|
102
|
+
# Removes the cookies with the given name, on the given domain or the ones
|
|
103
|
+
# sent to the given URL.
|
|
141
104
|
#
|
|
142
105
|
# @param [String] name
|
|
143
106
|
#
|
|
@@ -145,41 +108,58 @@ module Ferrum
|
|
|
145
108
|
# Additional keyword arguments.
|
|
146
109
|
#
|
|
147
110
|
# @option options [String] :domain
|
|
111
|
+
# The exact domain of the cookie, the `:base_url` host by default.
|
|
148
112
|
#
|
|
149
113
|
# @option options [String] :url
|
|
114
|
+
# The URL the cookie is sent to.
|
|
115
|
+
#
|
|
116
|
+
# @option options [String] :path
|
|
117
|
+
# The exact path of the cookie.
|
|
118
|
+
#
|
|
119
|
+
# @return [nil]
|
|
120
|
+
#
|
|
121
|
+
# @raise [TimeoutError]
|
|
150
122
|
#
|
|
151
123
|
# @example
|
|
152
124
|
# browser.cookies.remove(name: "stealth", domain: "google.com") # => true
|
|
153
125
|
#
|
|
154
126
|
def remove(name:, **options)
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
options[:
|
|
159
|
-
|
|
160
|
-
|
|
127
|
+
domain = options[:domain] || (default_domain unless options[:url])
|
|
128
|
+
raise "Specify :domain or :url option" if !domain && !options[:url]
|
|
129
|
+
|
|
130
|
+
uri = URI.parse(options[:url]) if options[:url]
|
|
131
|
+
cookies = select do |cookie|
|
|
132
|
+
cookie.name == name &&
|
|
133
|
+
(options[:path].nil? || cookie.path == options[:path]) &&
|
|
134
|
+
(domain ? cookie.domain == domain : sent_to?(cookie, uri))
|
|
135
|
+
end
|
|
136
|
+
cookies.each { |cookie| write(cookie.attributes.merge("expires" => 1)) }
|
|
161
137
|
|
|
162
|
-
|
|
138
|
+
nil
|
|
163
139
|
end
|
|
164
140
|
|
|
165
141
|
#
|
|
166
|
-
# Removes all cookies
|
|
142
|
+
# Removes all cookies in the context.
|
|
143
|
+
#
|
|
144
|
+
# @return [nil]
|
|
167
145
|
#
|
|
168
|
-
# @
|
|
146
|
+
# @raise [TimeoutError]
|
|
169
147
|
#
|
|
170
148
|
# @example
|
|
171
149
|
# browser.cookies.clear # => true
|
|
172
150
|
#
|
|
173
151
|
def clear
|
|
174
|
-
@
|
|
175
|
-
|
|
152
|
+
@client.command("Storage.clearCookies", timeout: @client.timeout, browserContextId: @context_id)
|
|
153
|
+
nil
|
|
176
154
|
end
|
|
177
155
|
|
|
178
156
|
#
|
|
179
|
-
# Stores all cookies
|
|
157
|
+
# Stores all cookies in the context in a file.
|
|
180
158
|
#
|
|
181
159
|
# @return [Integer]
|
|
182
160
|
#
|
|
161
|
+
# @raise [TimeoutError]
|
|
162
|
+
#
|
|
183
163
|
# @example
|
|
184
164
|
# browser.cookies.store # => Integer
|
|
185
165
|
#
|
|
@@ -188,9 +168,11 @@ module Ferrum
|
|
|
188
168
|
end
|
|
189
169
|
|
|
190
170
|
#
|
|
191
|
-
# Loads all cookies from the file and sets them
|
|
171
|
+
# Loads all cookies from the file and sets them in the context.
|
|
172
|
+
#
|
|
173
|
+
# @return [nil]
|
|
192
174
|
#
|
|
193
|
-
# @
|
|
175
|
+
# @raise [TimeoutError]
|
|
194
176
|
#
|
|
195
177
|
# @example
|
|
196
178
|
# browser.cookies.load # => true
|
|
@@ -198,13 +180,24 @@ module Ferrum
|
|
|
198
180
|
def load(path = "cookies.yml")
|
|
199
181
|
cookies = YAML.load_file(path)
|
|
200
182
|
cookies.each { |c| set(c) }
|
|
201
|
-
|
|
183
|
+
nil
|
|
202
184
|
end
|
|
203
185
|
|
|
204
186
|
private
|
|
205
187
|
|
|
188
|
+
def write(cookie)
|
|
189
|
+
@client.command("Storage.setCookies", timeout: @client.timeout, cookies: [cookie], browserContextId: @context_id)
|
|
190
|
+
end
|
|
191
|
+
|
|
206
192
|
def default_domain
|
|
207
|
-
|
|
193
|
+
base_domain(@client.options.base_url)
|
|
194
|
+
end
|
|
195
|
+
|
|
196
|
+
def sent_to?(cookie, uri)
|
|
197
|
+
domain = cookie.domain
|
|
198
|
+
host_match = uri.host == domain.delete_prefix(".") || (domain.start_with?(".") && uri.host.end_with?(domain))
|
|
199
|
+
path = uri.path.to_s.empty? ? "/" : uri.path
|
|
200
|
+
host_match && path.start_with?(cookie.path.to_s)
|
|
208
201
|
end
|
|
209
202
|
end
|
|
210
203
|
end
|