capybara-lightpanda 0.19.1 → 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/CHANGELOG.md +32 -0
- data/NOTICE.md +9 -3
- data/README.md +226 -37
- data/lib/capybara/lightpanda/browser/console.rb +21 -7
- data/lib/capybara/lightpanda/browser/finder.rb +9 -10
- data/lib/capybara/lightpanda/browser/navigation.rb +39 -16
- data/lib/capybara/lightpanda/browser/runtime.rb +23 -17
- data/lib/capybara/lightpanda/browser/selenium_compat.rb +162 -9
- data/lib/capybara/lightpanda/browser.rb +28 -6
- data/lib/capybara/lightpanda/client/web_socket.rb +15 -2
- data/lib/capybara/lightpanda/client.rb +35 -3
- data/lib/capybara/lightpanda/cookies.rb +8 -8
- data/lib/capybara/lightpanda/cuprite_cookies.rb +47 -0
- data/lib/capybara/lightpanda/cuprite_driver.rb +95 -0
- data/lib/capybara/lightpanda/cuprite_page.rb +62 -0
- data/lib/capybara/lightpanda/downloads.rb +119 -32
- data/lib/capybara/lightpanda/driver.rb +39 -8
- data/lib/capybara/lightpanda/errors.rb +5 -6
- data/lib/capybara/lightpanda/keyboard.rb +38 -0
- data/lib/capybara/lightpanda/network/exchange.rb +41 -0
- data/lib/capybara/lightpanda/network.rb +90 -58
- data/lib/capybara/lightpanda/node.rb +137 -17
- data/lib/capybara/lightpanda/options.rb +16 -1
- data/lib/capybara/lightpanda/process.rb +13 -3
- data/lib/capybara/lightpanda/railtie.rb +7 -4
- data/lib/capybara/lightpanda/request_headers.rb +76 -0
- data/lib/capybara/lightpanda/utils/wait.rb +3 -2
- data/lib/capybara/lightpanda/version.rb +1 -1
- data/lib/capybara-lightpanda.rb +2 -0
- metadata +6 -1
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
# frozen_string_literal: true
|
|
2
2
|
|
|
3
3
|
require "singleton"
|
|
4
|
+
require "securerandom"
|
|
4
5
|
|
|
5
6
|
module Capybara
|
|
6
7
|
module Lightpanda
|
|
@@ -61,9 +62,8 @@ module Capybara
|
|
|
61
62
|
#
|
|
62
63
|
# Like `evaluate`, the no-args path uses `replMode: true` so top-level
|
|
63
64
|
# `const`/`let` redeclarations across calls don't raise. Also raises
|
|
64
|
-
# on JS exceptions so silent failures
|
|
65
|
-
#
|
|
66
|
-
# checked but `exceptionDetails` was not).
|
|
65
|
+
# on JS exceptions (it checks `exceptionDetails`) so silent failures
|
|
66
|
+
# don't mask test bugs.
|
|
67
67
|
def execute(expression, *args, frame: nil)
|
|
68
68
|
if frame && args.empty?
|
|
69
69
|
call_with_args(FRAME_EXECUTE_JS, [expression], return_by_value: true, frame: frame)
|
|
@@ -104,21 +104,27 @@ module Capybara
|
|
|
104
104
|
# the callback as its last argument (`arguments[arguments.length - 1]`),
|
|
105
105
|
# matching Capybara's evaluate_async_script contract.
|
|
106
106
|
def evaluate_async(expression, *args, wait: @options.timeout, frame: nil)
|
|
107
|
-
|
|
107
|
+
# A private scalar lets the timer report expiry without interpreting
|
|
108
|
+
# an application's exception class or changing its callback value.
|
|
109
|
+
expired = SecureRandom.uuid
|
|
108
110
|
wrapped = <<~JS
|
|
109
111
|
function() {
|
|
110
|
-
var __args = Array.
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
var __done = function(val) { clearTimeout(__timer); __resolve(val); };
|
|
112
|
+
var __args = Array.from(arguments), __timer;
|
|
113
|
+
var __deadline = new Promise(__resolve => {
|
|
114
|
+
__timer = setTimeout(() => __resolve('#{expired}'), #{(wait * 1000).to_i});
|
|
115
|
+
});
|
|
116
|
+
var __result = new Promise(__done => {
|
|
116
117
|
__args.push(__done);
|
|
117
118
|
(function() { #{expression} }).apply(null, __args);
|
|
118
119
|
});
|
|
120
|
+
return Promise.race([__result, __deadline]).finally(() => clearTimeout(__timer));
|
|
119
121
|
}
|
|
120
122
|
JS
|
|
121
|
-
|
|
123
|
+
# Give the browser time to return the result of the race over CDP.
|
|
124
|
+
result = call_with_args(wrapped, args, frame: frame, command_timeout: wait + 1)
|
|
125
|
+
raise ScriptTimeoutError, "Async script callback timed out after #{wait}s" if result == expired
|
|
126
|
+
|
|
127
|
+
result
|
|
122
128
|
end
|
|
123
129
|
|
|
124
130
|
# Evaluate JS and return a RemoteObject reference (for DOM nodes, arrays).
|
|
@@ -214,7 +220,7 @@ module Capybara
|
|
|
214
220
|
# callFunctionOn in the top context, so node handles found inside a
|
|
215
221
|
# frame belong to that context and can't be passed into another
|
|
216
222
|
# ("Argument should belong to the same JavaScript world").
|
|
217
|
-
def call_with_args(function_declaration, args, return_by_value: false, frame: nil)
|
|
223
|
+
def call_with_args(function_declaration, args, return_by_value: false, frame: nil, command_timeout: nil)
|
|
218
224
|
arguments = args.map { |a| serialize_argument(a) }
|
|
219
225
|
if frame
|
|
220
226
|
target = frame.remote_object_id
|
|
@@ -233,7 +239,8 @@ module Capybara
|
|
|
233
239
|
awaitPromise: true,
|
|
234
240
|
arguments: arguments,
|
|
235
241
|
}
|
|
236
|
-
response =
|
|
242
|
+
response = @client.command("Runtime.callFunctionOn", params, session_id: @session_id,
|
|
243
|
+
timeout: command_timeout)
|
|
237
244
|
raise_on_js_error!("call_with_args", function_declaration, response)
|
|
238
245
|
|
|
239
246
|
return unwrap_call_result(response["result"]) unless return_by_value
|
|
@@ -271,10 +278,9 @@ module Capybara
|
|
|
271
278
|
# Cheaper than walking properties and good enough for shared specs. Releases
|
|
272
279
|
# the original handle so long-lived sessions don't accumulate leaked objectIds.
|
|
273
280
|
#
|
|
274
|
-
# V8 refuses to return a cyclic object by value.
|
|
275
|
-
#
|
|
276
|
-
#
|
|
277
|
-
# and a plain self-referencing object return CyclicObject.
|
|
281
|
+
# V8 refuses to return a cyclic object by value. `window` lands here
|
|
282
|
+
# (only real nodes carry subtype node) and is cyclic, as is a plain
|
|
283
|
+
# self-referencing object: both return CyclicObject instead of raising.
|
|
278
284
|
def serialize_remote_object(object_id)
|
|
279
285
|
json = page_command(
|
|
280
286
|
"Runtime.callFunctionOn",
|
|
@@ -1,5 +1,7 @@
|
|
|
1
1
|
# frozen_string_literal: true
|
|
2
2
|
|
|
3
|
+
require "date"
|
|
4
|
+
|
|
3
5
|
module Capybara
|
|
4
6
|
module Lightpanda
|
|
5
7
|
class Browser
|
|
@@ -16,17 +18,62 @@ module Capybara
|
|
|
16
18
|
# spec files (decidim's `account_spec.rb` + the shared "accessible page"
|
|
17
19
|
# examples, real-apps run 30116365373).
|
|
18
20
|
#
|
|
19
|
-
# Scope
|
|
20
|
-
#
|
|
21
|
-
#
|
|
22
|
-
#
|
|
21
|
+
# Scope grows only when a real-app helper needs an entry point, each one
|
|
22
|
+
# pinned by test/compatibility/selenium_contract.rb: alias what maps
|
|
23
|
+
# cleanly onto an existing API, and raise something *actionable* where
|
|
24
|
+
# the underlying model genuinely differs (see #switch_to). Mirroring
|
|
25
|
+
# Selenium's API for its own sake is not a goal.
|
|
23
26
|
module SeleniumCompat
|
|
24
|
-
#
|
|
27
|
+
# Alchemy's picture overlay queues navigation keys for whichever
|
|
28
|
+
# element has focus when the caller performs the actions.
|
|
29
|
+
class KeyboardActions
|
|
30
|
+
NAVIGATION_KEYS = { arrow_left: :left, arrow_right: :right, arrow_up: :up, arrow_down: :down,
|
|
31
|
+
left: :left, right: :right, up: :up, down: :down, enter: :numpad_enter, }.freeze
|
|
32
|
+
|
|
33
|
+
def initialize(browser)
|
|
34
|
+
@browser = browser
|
|
35
|
+
@keys = []
|
|
36
|
+
end
|
|
37
|
+
|
|
38
|
+
def send_keys(*keys)
|
|
39
|
+
@keys.concat(keys.flatten.map do |key|
|
|
40
|
+
NAVIGATION_KEYS.fetch(key) do
|
|
41
|
+
raise ::Capybara::NotSupportedByDriverError, "Keyboard actions support the arrow keys and Enter"
|
|
42
|
+
end
|
|
43
|
+
end)
|
|
44
|
+
self
|
|
45
|
+
end
|
|
46
|
+
|
|
47
|
+
def perform
|
|
48
|
+
unless @keys.empty?
|
|
49
|
+
@browser.keyboard.type(*@keys)
|
|
50
|
+
@keys.clear
|
|
51
|
+
@browser.wait_for_idle
|
|
52
|
+
end
|
|
53
|
+
nil
|
|
54
|
+
end
|
|
55
|
+
end
|
|
56
|
+
|
|
57
|
+
def action
|
|
58
|
+
KeyboardActions.new(self)
|
|
59
|
+
end
|
|
60
|
+
|
|
61
|
+
# Selenium’s browser log includes console output and uncaught JavaScript errors.
|
|
25
62
|
# `level` is the Selenium severity string ("SEVERE"/"WARNING"/…), not
|
|
26
63
|
# the CDP console type, because that is what callers compare against.
|
|
64
|
+
# `timestamp` is wall-clock epoch milliseconds, as in Selenium, and
|
|
65
|
+
# `time`/`as_json`/`to_s` mirror Selenium::WebDriver::LogEntry.
|
|
27
66
|
LogEntry = Struct.new(:level, :message, :timestamp) do
|
|
67
|
+
def time
|
|
68
|
+
Time.at(timestamp / 1000)
|
|
69
|
+
end
|
|
70
|
+
|
|
71
|
+
def as_json(*)
|
|
72
|
+
{ "timestamp" => timestamp, "level" => level, "message" => message }
|
|
73
|
+
end
|
|
74
|
+
|
|
28
75
|
def to_s
|
|
29
|
-
"#{
|
|
76
|
+
"#{time} #{level}: #{message}"
|
|
30
77
|
end
|
|
31
78
|
end
|
|
32
79
|
|
|
@@ -55,13 +102,19 @@ module Capybara
|
|
|
55
102
|
def get(type = :browser)
|
|
56
103
|
return [] unless type.to_sym == :browser
|
|
57
104
|
|
|
58
|
-
@browser.console_logs.map do |entry|
|
|
105
|
+
console = @browser.console_logs.map do |entry|
|
|
59
106
|
LogEntry.new(
|
|
60
107
|
CONSOLE_LEVELS.fetch(entry[:type], "INFO"),
|
|
61
108
|
entry[:text],
|
|
62
|
-
entry[:
|
|
109
|
+
entry[:received_at]
|
|
63
110
|
)
|
|
64
111
|
end
|
|
112
|
+
errors = @browser.page_errors.map do |error|
|
|
113
|
+
prefix = error[:kind] == "unhandledrejection" ? "Uncaught (in promise)" : "Uncaught"
|
|
114
|
+
message = [error[:url], prefix, error[:message]].reject(&:empty?).join(" ")
|
|
115
|
+
LogEntry.new("SEVERE", message, error[:received_at])
|
|
116
|
+
end
|
|
117
|
+
(console + errors).sort_by { |entry| entry.timestamp || 0 }
|
|
65
118
|
end
|
|
66
119
|
|
|
67
120
|
def available_types
|
|
@@ -73,7 +126,8 @@ module Capybara
|
|
|
73
126
|
# repeated calls re-report the same entries. Draining would let an
|
|
74
127
|
# earlier reader silently swallow a JS error from a later assertion,
|
|
75
128
|
# and the buffer is already scoped to the session (cleared by
|
|
76
|
-
# Driver#reset!).
|
|
129
|
+
# Driver#reset!). Native callers can clear each source with
|
|
130
|
+
# #clear_console_logs and #clear_page_errors.
|
|
77
131
|
#
|
|
78
132
|
# Not memoized on purpose: Logs holds nothing but a back-reference, and
|
|
79
133
|
# an ivar here would have to be declared in Browser#initialize per the
|
|
@@ -82,6 +136,105 @@ module Capybara
|
|
|
82
136
|
Logs.new(self)
|
|
83
137
|
end
|
|
84
138
|
|
|
139
|
+
# Lightpanda's single virtual window has a fixed origin. Resizing
|
|
140
|
+
# changes the viewport used by matchMedia and by the next document.
|
|
141
|
+
class Window
|
|
142
|
+
def initialize(browser)
|
|
143
|
+
@browser = browser
|
|
144
|
+
end
|
|
145
|
+
|
|
146
|
+
def resize_to(width, height)
|
|
147
|
+
width = Integer(width)
|
|
148
|
+
height = Integer(height)
|
|
149
|
+
unless [width, height].all? { |value| value.between?(0, (2**31) - 1) }
|
|
150
|
+
error = if defined?(::Selenium::WebDriver::Error::InvalidArgumentError)
|
|
151
|
+
::Selenium::WebDriver::Error::InvalidArgumentError
|
|
152
|
+
else
|
|
153
|
+
ArgumentError
|
|
154
|
+
end
|
|
155
|
+
raise error, "Window dimensions must be nonnegative 32-bit signed integers"
|
|
156
|
+
end
|
|
157
|
+
if width.zero? || height.zero?
|
|
158
|
+
raise ::Capybara::NotSupportedByDriverError, "Lightpanda cannot represent a zero-sized viewport"
|
|
159
|
+
end
|
|
160
|
+
|
|
161
|
+
@browser.set_viewport(width, height)
|
|
162
|
+
{ "x" => 0, "y" => 0, "width" => width, "height" => height }
|
|
163
|
+
end
|
|
164
|
+
end
|
|
165
|
+
|
|
166
|
+
# Chromium's per-page destination, used by Rails download helpers.
|
|
167
|
+
def download_path=(path)
|
|
168
|
+
path = path.to_path if path.respond_to?(:to_path)
|
|
169
|
+
path = path.to_s if path.is_a?(Symbol)
|
|
170
|
+
unless path.is_a?(String)
|
|
171
|
+
error = if defined?(::Selenium::WebDriver::Error::InvalidArgumentError)
|
|
172
|
+
::Selenium::WebDriver::Error::InvalidArgumentError
|
|
173
|
+
else
|
|
174
|
+
ArgumentError
|
|
175
|
+
end
|
|
176
|
+
raise error, "download_path must be a directory path"
|
|
177
|
+
end
|
|
178
|
+
|
|
179
|
+
downloads.download_path = File.absolute_path(path)
|
|
180
|
+
{}
|
|
181
|
+
end
|
|
182
|
+
|
|
183
|
+
# WebDriver's cookie commands operate on the active document. The native
|
|
184
|
+
# cookie jar spans the whole browser context, including other hosts.
|
|
185
|
+
class Manager
|
|
186
|
+
def initialize(browser)
|
|
187
|
+
@browser = browser
|
|
188
|
+
end
|
|
189
|
+
|
|
190
|
+
def window
|
|
191
|
+
Window.new(@browser)
|
|
192
|
+
end
|
|
193
|
+
|
|
194
|
+
def all_cookies
|
|
195
|
+
document_cookies.map do |raw|
|
|
196
|
+
cookie = Cookies::Cookie.new(raw)
|
|
197
|
+
{ name: cookie.name, value: cookie.value, domain: cookie.domain, path: cookie.path,
|
|
198
|
+
expires: cookie.expires&.utc&.to_datetime, secure: cookie.secure?, http_only: cookie.httponly?,
|
|
199
|
+
same_site: cookie.same_site || "Lax", }
|
|
200
|
+
end
|
|
201
|
+
end
|
|
202
|
+
|
|
203
|
+
def cookie_named(name)
|
|
204
|
+
cookie = all_cookies.find { |entry| entry[:name] == name }
|
|
205
|
+
return cookie if cookie
|
|
206
|
+
|
|
207
|
+
error = if defined?(::Selenium::WebDriver::Error::NoSuchCookieError)
|
|
208
|
+
::Selenium::WebDriver::Error::NoSuchCookieError
|
|
209
|
+
else
|
|
210
|
+
NoSuchCookieError
|
|
211
|
+
end
|
|
212
|
+
raise error, "No cookie named #{name.inspect} for the current document"
|
|
213
|
+
end
|
|
214
|
+
|
|
215
|
+
def delete_all_cookies
|
|
216
|
+
# Network.deleteCookies strips the leading dot and can remove a
|
|
217
|
+
# parent host's private cookie with the same name and path.
|
|
218
|
+
url = @browser.frame_url
|
|
219
|
+
expired = document_cookies(url).map do |cookie|
|
|
220
|
+
scope = cookie.fetch("domain").start_with?(".") ? { domain: cookie["domain"] } : { url: url }
|
|
221
|
+
cookie.slice("name", "value", "path", "secure", "httpOnly").merge(scope, expires: 1)
|
|
222
|
+
end
|
|
223
|
+
@browser.execute_cdp("Network.setCookies", cookies: expired) unless expired.empty?
|
|
224
|
+
nil
|
|
225
|
+
end
|
|
226
|
+
|
|
227
|
+
private
|
|
228
|
+
|
|
229
|
+
def document_cookies(url = @browser.frame_url)
|
|
230
|
+
@browser.execute_cdp("Network.getCookies", urls: [url]).fetch("cookies")
|
|
231
|
+
end
|
|
232
|
+
end
|
|
233
|
+
|
|
234
|
+
def manage
|
|
235
|
+
Manager.new(self)
|
|
236
|
+
end
|
|
237
|
+
|
|
85
238
|
# Selenium's `execute_async_script`: the script receives a completion
|
|
86
239
|
# callback as its last argument. Same contract as
|
|
87
240
|
# Driver#evaluate_async_script, minus the DOM-node unwrapping — callers
|
|
@@ -2,6 +2,7 @@
|
|
|
2
2
|
|
|
3
3
|
require "forwardable"
|
|
4
4
|
|
|
5
|
+
require_relative "cuprite_page"
|
|
5
6
|
require_relative "browser/runtime"
|
|
6
7
|
require_relative "browser/finder"
|
|
7
8
|
require_relative "browser/navigation"
|
|
@@ -21,7 +22,7 @@ module Capybara
|
|
|
21
22
|
include Console
|
|
22
23
|
include SeleniumCompat
|
|
23
24
|
|
|
24
|
-
attr_reader :options, :process, :client, :target_id, :session_id, :browser_context_id, :frame_stack
|
|
25
|
+
attr_reader :options, :process, :client, :target_id, :session_id, :browser_context_id, :frame_stack, :headers
|
|
25
26
|
|
|
26
27
|
delegate %i[on off] => :client
|
|
27
28
|
|
|
@@ -94,8 +95,10 @@ module Capybara
|
|
|
94
95
|
@process&.release || @remote_release
|
|
95
96
|
end
|
|
96
97
|
|
|
97
|
-
def initialize(options = {})
|
|
98
|
+
def initialize(options = {}, &cookie_jar)
|
|
98
99
|
@options = Options.new(options)
|
|
100
|
+
@headers = RequestHeaders.new(self)
|
|
101
|
+
@cookies = cookie_jar&.call(self)
|
|
99
102
|
@process = nil
|
|
100
103
|
@client = nil
|
|
101
104
|
@remote_version = nil
|
|
@@ -111,10 +114,7 @@ module Capybara
|
|
|
111
114
|
@modal_handler_installed = false
|
|
112
115
|
@modal_armed = false
|
|
113
116
|
@unhandled_modal = nil
|
|
114
|
-
|
|
115
|
-
@console_logs_mutex = Mutex.new
|
|
116
|
-
@page_errors = []
|
|
117
|
-
@page_errors_mutex = Mutex.new
|
|
117
|
+
initialize_console_buffers
|
|
118
118
|
@frame_stack = []
|
|
119
119
|
@turbo_event = Utils::Event.new
|
|
120
120
|
@turbo_event.set
|
|
@@ -245,6 +245,7 @@ module Capybara
|
|
|
245
245
|
# short-circuit on the new client, so find_modal silently sees no
|
|
246
246
|
# javascriptDialogOpening events.
|
|
247
247
|
def clear_session_state
|
|
248
|
+
@headers.reset
|
|
248
249
|
@page_events_enabled = false
|
|
249
250
|
@modal_handler_installed = false
|
|
250
251
|
@modal_messages_mutex.synchronize do
|
|
@@ -311,6 +312,10 @@ module Capybara
|
|
|
311
312
|
@client.command(method, params)
|
|
312
313
|
end
|
|
313
314
|
|
|
315
|
+
def page
|
|
316
|
+
CupritePage.new(self)
|
|
317
|
+
end
|
|
318
|
+
|
|
314
319
|
def page_command(method, **params)
|
|
315
320
|
@client.command(method, params, session_id: @session_id)
|
|
316
321
|
end
|
|
@@ -543,6 +548,23 @@ module Capybara
|
|
|
543
548
|
|
|
544
549
|
private
|
|
545
550
|
|
|
551
|
+
# Keep capability checks such as respond_to?(:mouse) accurate for suites
|
|
552
|
+
# that already fall back to another driver.
|
|
553
|
+
def method_missing(name, *args) # rubocop:disable Style/MissingRespondToMissing
|
|
554
|
+
return super unless name == :mouse
|
|
555
|
+
|
|
556
|
+
message = "undefined method 'mouse' for #{self.class}. Coordinate mouse input needs rendered layout. " \
|
|
557
|
+
"Run this example with Cuprite (Chrome)."
|
|
558
|
+
raise NoMethodError.new(message, name, args, receiver: self)
|
|
559
|
+
end
|
|
560
|
+
|
|
561
|
+
def initialize_console_buffers
|
|
562
|
+
@console_logs = []
|
|
563
|
+
@console_logs_mutex = Mutex.new
|
|
564
|
+
@page_errors = []
|
|
565
|
+
@page_errors_mutex = Mutex.new
|
|
566
|
+
end
|
|
567
|
+
|
|
546
568
|
# Keep iframes and workers loading.
|
|
547
569
|
#
|
|
548
570
|
# Upstream #3305 (build 8946) turned `Config.LoadResources` into a
|
|
@@ -2,6 +2,7 @@
|
|
|
2
2
|
|
|
3
3
|
require "json"
|
|
4
4
|
require "socket"
|
|
5
|
+
require "uri"
|
|
5
6
|
require "websocket/driver"
|
|
6
7
|
|
|
7
8
|
require_relative "../utils/attempt"
|
|
@@ -13,6 +14,18 @@ module Capybara
|
|
|
13
14
|
class WebSocket
|
|
14
15
|
attr_reader :url, :messages
|
|
15
16
|
|
|
17
|
+
# The transport opens a TCP socket directly and cannot negotiate TLS.
|
|
18
|
+
def self.endpoint_uri(url)
|
|
19
|
+
uri = URI.parse(url)
|
|
20
|
+
if uri.scheme == "ws" && uri.host && !uri.host.empty? && !uri.fragment && (1..65_535).cover?(uri.port)
|
|
21
|
+
return uri
|
|
22
|
+
end
|
|
23
|
+
|
|
24
|
+
raise ArgumentError, "ws_url: requires a ws:// endpoint with a host and valid port (TLS is unsupported)"
|
|
25
|
+
rescue URI::InvalidURIError
|
|
26
|
+
raise ArgumentError, "ws_url: requires a valid ws:// endpoint"
|
|
27
|
+
end
|
|
28
|
+
|
|
16
29
|
def initialize(url, options)
|
|
17
30
|
@url = url
|
|
18
31
|
@options = options
|
|
@@ -160,9 +173,9 @@ module Capybara
|
|
|
160
173
|
end
|
|
161
174
|
|
|
162
175
|
def connect
|
|
163
|
-
uri =
|
|
176
|
+
uri = self.class.endpoint_uri(@url)
|
|
164
177
|
|
|
165
|
-
@socket = connect_with_retry(uri.
|
|
178
|
+
@socket = connect_with_retry(uri.hostname, uri.port)
|
|
166
179
|
@driver = ::WebSocket::Driver.client(self)
|
|
167
180
|
|
|
168
181
|
setup_callbacks
|
|
@@ -8,7 +8,24 @@ require_relative "client/subscriber"
|
|
|
8
8
|
|
|
9
9
|
module Capybara
|
|
10
10
|
module Lightpanda
|
|
11
|
-
class Client
|
|
11
|
+
class Client # rubocop:disable Metrics/ClassLength
|
|
12
|
+
# Apply a state change on the message thread before waking the caller or
|
|
13
|
+
# delivering the next CDP event. The callback must never issue CDP commands.
|
|
14
|
+
class AcknowledgedCommand < Concurrent::IVar
|
|
15
|
+
def initialize(on_success)
|
|
16
|
+
super()
|
|
17
|
+
@on_success = on_success
|
|
18
|
+
end
|
|
19
|
+
|
|
20
|
+
def try_set(message)
|
|
21
|
+
super() do
|
|
22
|
+
@on_success.call if message&.key?("result")
|
|
23
|
+
message
|
|
24
|
+
end
|
|
25
|
+
end
|
|
26
|
+
end
|
|
27
|
+
private_constant :AcknowledgedCommand
|
|
28
|
+
|
|
12
29
|
def initialize(ws_url, options)
|
|
13
30
|
@options = options
|
|
14
31
|
@ws = WebSocket.new(ws_url, options)
|
|
@@ -23,7 +40,7 @@ module Capybara
|
|
|
23
40
|
# `timeout` bounds the whole command: waiting for the socket lock, writing
|
|
24
41
|
# the frame and waiting for the response all draw on one deadline, so a
|
|
25
42
|
# slow send can't stretch a command past its budget.
|
|
26
|
-
def command(method, params = {}, async: false, session_id: nil, timeout: nil)
|
|
43
|
+
def command(method, params = {}, async: false, session_id: nil, timeout: nil, on_success: nil)
|
|
27
44
|
message = build_message(method, params, session_id: session_id)
|
|
28
45
|
effective_timeout = timeout || @options.timeout
|
|
29
46
|
deadline = Utils::Wait.deadline(effective_timeout)
|
|
@@ -33,7 +50,7 @@ module Capybara
|
|
|
33
50
|
return true
|
|
34
51
|
end
|
|
35
52
|
|
|
36
|
-
pending = Concurrent::IVar.new
|
|
53
|
+
pending = on_success ? AcknowledgedCommand.new(on_success) : Concurrent::IVar.new
|
|
37
54
|
@pendings[message[:id]] = pending
|
|
38
55
|
|
|
39
56
|
@ws.send_message(JSON.generate(message), deadline: deadline)
|
|
@@ -54,6 +71,21 @@ module Capybara
|
|
|
54
71
|
@pendings.delete(message[:id]) if message
|
|
55
72
|
end
|
|
56
73
|
|
|
74
|
+
# Let navigation arm its event handlers before sending and use its own
|
|
75
|
+
# readiness deadline. A reply that arrives later must never delay the
|
|
76
|
+
# page or get mistaken for the next visit's response.
|
|
77
|
+
def observe_async_command(method, params, session_id:)
|
|
78
|
+
message = build_message(method, params, session_id: session_id)
|
|
79
|
+
pending = Concurrent::IVar.new
|
|
80
|
+
@pendings[message[:id]] = pending
|
|
81
|
+
yield lambda {
|
|
82
|
+
@ws.send_message(JSON.generate(message), deadline: Utils::Wait.deadline(@options.timeout))
|
|
83
|
+
}
|
|
84
|
+
pending.value(0)&.dig("result")
|
|
85
|
+
ensure
|
|
86
|
+
@pendings.delete(message[:id]) if message
|
|
87
|
+
end
|
|
88
|
+
|
|
57
89
|
def on(event, &)
|
|
58
90
|
@subscriber.subscribe(event, &)
|
|
59
91
|
end
|
|
@@ -60,8 +60,7 @@ module Capybara
|
|
|
60
60
|
end
|
|
61
61
|
|
|
62
62
|
def all
|
|
63
|
-
|
|
64
|
-
(result["cookies"] || []).map { |c| Cookie.new(c) }
|
|
63
|
+
to_a
|
|
65
64
|
end
|
|
66
65
|
|
|
67
66
|
# Yields each Cookie. Powers `Enumerable` (so callers can do
|
|
@@ -70,7 +69,8 @@ module Capybara
|
|
|
70
69
|
def each(&block)
|
|
71
70
|
return enum_for(:each) unless block
|
|
72
71
|
|
|
73
|
-
|
|
72
|
+
result = browser.command("Network.getAllCookies")
|
|
73
|
+
(result["cookies"] || []).each { |attributes| block.call(Cookie.new(attributes)) }
|
|
74
74
|
end
|
|
75
75
|
|
|
76
76
|
def get(name)
|
|
@@ -88,7 +88,7 @@ module Capybara
|
|
|
88
88
|
# BrowserError instead: since upstream #3791 (build 10212) the browser
|
|
89
89
|
# answers `-32602 Sanitizing cookie failed`, as Chrome does.
|
|
90
90
|
def set(name:, value:, domain: nil, path: "/", secure: false, http_only: false, # rubocop:disable Metrics/ParameterLists, Naming/PredicateMethod
|
|
91
|
-
same_site: nil, expires: nil)
|
|
91
|
+
same_site: nil, expires: nil, url: nil)
|
|
92
92
|
params = {
|
|
93
93
|
name: name,
|
|
94
94
|
value: value,
|
|
@@ -98,6 +98,7 @@ module Capybara
|
|
|
98
98
|
}
|
|
99
99
|
|
|
100
100
|
params[:domain] = domain if domain
|
|
101
|
+
params[:url] = url if url
|
|
101
102
|
# CDP rejects unknown SameSite values; pass through only the canonical
|
|
102
103
|
# spec strings ("Strict" / "Lax" / "None") so YAML noise from a hand-
|
|
103
104
|
# edited file doesn't reach the browser.
|
|
@@ -108,9 +109,8 @@ module Capybara
|
|
|
108
109
|
!(result.is_a?(Hash) && result["success"] == false)
|
|
109
110
|
end
|
|
110
111
|
|
|
111
|
-
def remove(name:, domain: nil, path: "/")
|
|
112
|
-
params = { name: name, path: path }
|
|
113
|
-
params[:domain] = domain if domain
|
|
112
|
+
def remove(name:, domain: nil, path: "/", url: nil)
|
|
113
|
+
params = { name: name, path: path, domain: domain, url: url }.compact
|
|
114
114
|
|
|
115
115
|
browser.command("Network.deleteCookies", **params)
|
|
116
116
|
end
|
|
@@ -122,7 +122,7 @@ module Capybara
|
|
|
122
122
|
# Persist all current cookies to a YAML file (ferrum parity).
|
|
123
123
|
# Returns the number of bytes written.
|
|
124
124
|
def store(path = "cookies.yml")
|
|
125
|
-
File.write(path,
|
|
125
|
+
File.write(path, map(&:to_h).to_yaml)
|
|
126
126
|
end
|
|
127
127
|
|
|
128
128
|
# Load cookies from a YAML file produced by `store` and re-set them.
|
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Capybara
|
|
4
|
+
module Lightpanda
|
|
5
|
+
class CupriteCookies < Cookies
|
|
6
|
+
def initialize(browser, &default_domain)
|
|
7
|
+
super(browser)
|
|
8
|
+
@default_domain = default_domain
|
|
9
|
+
end
|
|
10
|
+
|
|
11
|
+
# Enumeration still includes cookies with the same name at different paths.
|
|
12
|
+
def all
|
|
13
|
+
to_h { |cookie| [cookie.name, cookie] }
|
|
14
|
+
end
|
|
15
|
+
|
|
16
|
+
COOKIE_KEYS = { httponly: :http_only, httpOnly: :http_only, samesite: :same_site, sameSite: :same_site }.freeze
|
|
17
|
+
private_constant :COOKIE_KEYS
|
|
18
|
+
|
|
19
|
+
def set(options)
|
|
20
|
+
return restore_cookie(options.to_h) if options.is_a?(Cookie)
|
|
21
|
+
|
|
22
|
+
attributes = options.transform_keys { |key| COOKIE_KEYS.fetch(key.to_sym, key.to_sym) }
|
|
23
|
+
attributes[:domain] ||= @default_domain.call unless attributes[:url]
|
|
24
|
+
super(**attributes)
|
|
25
|
+
end
|
|
26
|
+
|
|
27
|
+
def remove(name:, domain: nil, url: nil, path: nil) # rubocop:disable Naming/PredicateMethod
|
|
28
|
+
domain ||= @default_domain.call unless url
|
|
29
|
+
if path || url
|
|
30
|
+
super
|
|
31
|
+
else
|
|
32
|
+
# Lightpanda applies "/" when deleteCookies omits the path. Chrome
|
|
33
|
+
# removes every path for this name/domain. Keep other hosts intact.
|
|
34
|
+
paths_for(name, domain).each { |scope| super(name: name, domain: domain, path: scope) }
|
|
35
|
+
end
|
|
36
|
+
true
|
|
37
|
+
end
|
|
38
|
+
|
|
39
|
+
private
|
|
40
|
+
|
|
41
|
+
def paths_for(name, domain)
|
|
42
|
+
host = domain.delete_prefix(".").downcase
|
|
43
|
+
filter_map { |cookie| cookie.path if cookie.name == name && cookie.domain.delete_prefix(".").downcase == host }
|
|
44
|
+
end
|
|
45
|
+
end
|
|
46
|
+
end
|
|
47
|
+
end
|