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
|
@@ -0,0 +1,95 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require_relative "cuprite_cookies"
|
|
4
|
+
|
|
5
|
+
module Capybara
|
|
6
|
+
module Lightpanda
|
|
7
|
+
# An application-owned registration lets a suite keep its driven_by name
|
|
8
|
+
# while using real Cuprite for examples that require Chrome.
|
|
9
|
+
def self.register_cuprite_driver(name, **settings)
|
|
10
|
+
raise ArgumentError, "choose an application-owned driver name" if %i[cuprite lightpanda].include?(name.to_sym)
|
|
11
|
+
|
|
12
|
+
# No custom Chrome flags and no error on a pending navigation are
|
|
13
|
+
# already Lightpanda's behavior. Other values need a different driver.
|
|
14
|
+
settings.delete(:browser_options) if settings[:browser_options] == {}
|
|
15
|
+
settings.delete(:pending_connection_errors) if settings[:pending_connection_errors] == false
|
|
16
|
+
settings.delete(:slowmo) if [nil, 0].include?(settings[:slowmo])
|
|
17
|
+
settings.delete(:inspector) if [nil, false].include?(settings[:inspector])
|
|
18
|
+
CupriteDriver.validate_options!(settings)
|
|
19
|
+
Capybara.register_driver(name) { |app| CupriteDriver.new(app, options.to_h.merge(settings)) }
|
|
20
|
+
end
|
|
21
|
+
|
|
22
|
+
class CupriteDriver < Driver
|
|
23
|
+
def visit(url)
|
|
24
|
+
@started = true
|
|
25
|
+
browser.go_to(url) do |result|
|
|
26
|
+
error = result&.fetch("errorText", nil)
|
|
27
|
+
next if error.to_s.empty? || error == "net::ERR_ABORTED"
|
|
28
|
+
|
|
29
|
+
message = "Request to #{url} failed (#{error})"
|
|
30
|
+
raise ::Ferrum::StatusError.new(url, message) if defined?(::Ferrum::StatusError)
|
|
31
|
+
|
|
32
|
+
raise StatusError, message
|
|
33
|
+
end
|
|
34
|
+
end
|
|
35
|
+
|
|
36
|
+
def wait_for_reload(timeout = 1)
|
|
37
|
+
browser.wait_for_reload(timeout)
|
|
38
|
+
end
|
|
39
|
+
|
|
40
|
+
# Cuprite returns innerText's tab between table cells ("Total due\t$0.00").
|
|
41
|
+
def keep_cell_tabs?
|
|
42
|
+
true
|
|
43
|
+
end
|
|
44
|
+
|
|
45
|
+
def network_traffic(type = nil)
|
|
46
|
+
if type.to_s == "blocked"
|
|
47
|
+
raise Capybara::NotSupportedByDriverError, "Lightpanda does not track Cuprite request interception"
|
|
48
|
+
end
|
|
49
|
+
|
|
50
|
+
network.traffic
|
|
51
|
+
end
|
|
52
|
+
|
|
53
|
+
def clear_network_traffic
|
|
54
|
+
network.clear(:traffic)
|
|
55
|
+
end
|
|
56
|
+
|
|
57
|
+
def clear_memory_cache
|
|
58
|
+
network.clear(:cache)
|
|
59
|
+
end
|
|
60
|
+
|
|
61
|
+
def cookies
|
|
62
|
+
browser.cookies.all
|
|
63
|
+
end
|
|
64
|
+
|
|
65
|
+
def set_cookie(name, value, options = {})
|
|
66
|
+
browser.cookies.set(options.merge(name: name, value: value))
|
|
67
|
+
end
|
|
68
|
+
|
|
69
|
+
def self.validate_options!(settings)
|
|
70
|
+
if settings.key?(:url)
|
|
71
|
+
raise ArgumentError, "url: is a Cuprite HTTP endpoint. Pass ws_url: with a Lightpanda WebSocket endpoint."
|
|
72
|
+
end
|
|
73
|
+
|
|
74
|
+
Client::WebSocket.endpoint_uri(settings[:ws_url]) if settings.key?(:ws_url)
|
|
75
|
+
raise ArgumentError, "Lightpanda requires headless mode" if settings[:headless] == false
|
|
76
|
+
|
|
77
|
+
unsupported = settings.keys - Options.new.to_h.keys - %i[ws_url base_url]
|
|
78
|
+
return if unsupported.empty?
|
|
79
|
+
|
|
80
|
+
raise ArgumentError, "Unsupported Lightpanda driver options: #{unsupported.map(&:inspect).join(', ')}"
|
|
81
|
+
end
|
|
82
|
+
|
|
83
|
+
private
|
|
84
|
+
|
|
85
|
+
def build_browser
|
|
86
|
+
Browser.new(options) do |instance|
|
|
87
|
+
CupriteCookies.new(instance) do
|
|
88
|
+
safe_uri_host(instance.current_url) || safe_uri_host(options[:base_url]) ||
|
|
89
|
+
safe_uri_host(session_options.app_host) || session&.server&.host || "127.0.0.1"
|
|
90
|
+
end
|
|
91
|
+
end
|
|
92
|
+
end
|
|
93
|
+
end
|
|
94
|
+
end
|
|
95
|
+
end
|
|
@@ -0,0 +1,62 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require "json"
|
|
4
|
+
|
|
5
|
+
module Capybara
|
|
6
|
+
module Lightpanda
|
|
7
|
+
# Ferrum page handles address one target for their entire lifetime.
|
|
8
|
+
class CupritePage
|
|
9
|
+
def initialize(browser)
|
|
10
|
+
@client = browser.client
|
|
11
|
+
@session_id = browser.session_id
|
|
12
|
+
@remote = browser.options.ws_url?
|
|
13
|
+
end
|
|
14
|
+
|
|
15
|
+
def command(method, **params)
|
|
16
|
+
if params.keys.any? { |key| %w[wait timeout slowmoable].include?(key.to_s) }
|
|
17
|
+
raise ::Capybara::NotSupportedByDriverError, "Ferrum transport options are unsupported on page.command"
|
|
18
|
+
end
|
|
19
|
+
return send_command(method, params) unless method.to_s == "Emulation.setTimezoneOverride"
|
|
20
|
+
|
|
21
|
+
if @remote
|
|
22
|
+
raise ::Capybara::NotSupportedByDriverError, "Runtime timezones require a dedicated Lightpanda process"
|
|
23
|
+
end
|
|
24
|
+
|
|
25
|
+
set_timezone(method, params)
|
|
26
|
+
end
|
|
27
|
+
|
|
28
|
+
private
|
|
29
|
+
|
|
30
|
+
def send_command(method, params)
|
|
31
|
+
@client.command(method, params, session_id: @session_id)
|
|
32
|
+
end
|
|
33
|
+
|
|
34
|
+
def set_timezone(method, params)
|
|
35
|
+
zone = params.fetch(:timezoneId) { params["timezoneId"] }
|
|
36
|
+
raise ArgumentError, "timezoneId must be a string" unless zone.is_a?(String)
|
|
37
|
+
|
|
38
|
+
result = send_command(method, params)
|
|
39
|
+
# Older engines acknowledge this command without changing ICU's zone.
|
|
40
|
+
# An empty ID restores the process default on engines that support it.
|
|
41
|
+
return result if zone.empty? || timezone_matches?(zone)
|
|
42
|
+
|
|
43
|
+
raise ::Capybara::NotSupportedByDriverError,
|
|
44
|
+
"Lightpanda did not apply timezone #{zone.inspect}. " \
|
|
45
|
+
"Set timezone: at launch or use a browser with runtime timezone support."
|
|
46
|
+
end
|
|
47
|
+
|
|
48
|
+
def timezone_matches?(zone)
|
|
49
|
+
expression = <<~JS
|
|
50
|
+
(() => {
|
|
51
|
+
const actual = Intl.DateTimeFormat().resolvedOptions().timeZone;
|
|
52
|
+
const requested = Intl.DateTimeFormat("en", {timeZone: #{JSON.generate(zone)}}).resolvedOptions().timeZone;
|
|
53
|
+
// V8 can report an ICU GMT override as a fixed zero offset.
|
|
54
|
+
const normalize = name => name === "+00:00" ? "UTC" : name;
|
|
55
|
+
return normalize(actual) === normalize(requested);
|
|
56
|
+
})()
|
|
57
|
+
JS
|
|
58
|
+
send_command("Runtime.evaluate", { expression: expression, returnByValue: true }).dig("result", "value") == true
|
|
59
|
+
end
|
|
60
|
+
end
|
|
61
|
+
end
|
|
62
|
+
end
|
|
@@ -20,7 +20,7 @@ module Capybara
|
|
|
20
20
|
# Structure deliberately mirrors Network (same browser ref, same mutex +
|
|
21
21
|
# subscribe/unsubscribe lifecycle, same reset-after-disposeBrowserContext
|
|
22
22
|
# contract) so the two trackers behave identically under create_page/reset.
|
|
23
|
-
class Downloads
|
|
23
|
+
class Downloads # rubocop:disable Metrics/ClassLength
|
|
24
24
|
attr_reader :browser, :path
|
|
25
25
|
|
|
26
26
|
# How long #wait gives a download to BEGIN (the downloadWillBegin frame
|
|
@@ -33,8 +33,10 @@ module Capybara
|
|
|
33
33
|
@browser = browser
|
|
34
34
|
@path = nil
|
|
35
35
|
@enabled = false
|
|
36
|
+
@destination_unknown = false
|
|
36
37
|
@mutex = Mutex.new
|
|
37
|
-
@
|
|
38
|
+
@configuration_mutex = Mutex.new
|
|
39
|
+
@pending = {} # guid => absolute path when the download began
|
|
38
40
|
@files = [] # absolute paths of completed downloads, in order
|
|
39
41
|
@started = 0 # monotonic count of downloads begun this session
|
|
40
42
|
@will_handler = nil
|
|
@@ -48,36 +50,59 @@ module Capybara
|
|
|
48
50
|
# method is a no-op without one). Subscribe BEFORE the wire toggle so no
|
|
49
51
|
# event can slip past, and roll the handlers back if the command fails.
|
|
50
52
|
def enable(save_path)
|
|
51
|
-
|
|
53
|
+
@configuration_mutex.synchronize do
|
|
54
|
+
allow_downloads(save_path) unless @enabled || save_path.to_s.empty?
|
|
55
|
+
end
|
|
56
|
+
end
|
|
52
57
|
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
58
|
+
# Ferrum's `downloads.set_behavior(save_path:, behavior: :allow)`, which
|
|
59
|
+
# Cuprite suites call in setup to pick the download directory. `:allow`
|
|
60
|
+
# (re)points downloads at `save_path`; `:deny` and `:default` turn them
|
|
61
|
+
# off. Files already recorded stay listed. Lasts until the next
|
|
62
|
+
# `reset!`, which goes back to the configured `save_path`, so call it
|
|
63
|
+
# per test as those suites do.
|
|
64
|
+
BEHAVIORS = { allow: "allow", deny: "deny", default: "default" }.freeze
|
|
65
|
+
|
|
66
|
+
def set_behavior(save_path:, behavior: :allow)
|
|
67
|
+
wire = BEHAVIORS.fetch(behavior.to_sym) do
|
|
68
|
+
raise ArgumentError, "unknown download behavior #{behavior.inspect} (use :allow, :deny or :default)"
|
|
69
|
+
end
|
|
70
|
+
@configuration_mutex.synchronize do
|
|
71
|
+
return allow_downloads(save_path) if wire == "allow" && !save_path.to_s.empty?
|
|
72
|
+
|
|
73
|
+
disable_downloads(wire == "allow" ? "deny" : wire)
|
|
63
74
|
end
|
|
64
|
-
|
|
75
|
+
end
|
|
76
|
+
|
|
77
|
+
# Chromium accepts the directory when configuring the page. Lightpanda
|
|
78
|
+
# creates it on the browser host when the first attachment arrives.
|
|
79
|
+
def download_path=(destination)
|
|
80
|
+
@configuration_mutex.synchronize { allow_downloads(destination, prepare_directory: false) }
|
|
65
81
|
end
|
|
66
82
|
|
|
67
83
|
# Absolute paths of downloads completed this session (newest last).
|
|
68
84
|
def files
|
|
69
|
-
@mutex.synchronize
|
|
85
|
+
@mutex.synchronize do
|
|
86
|
+
check_destination!
|
|
87
|
+
@files.dup
|
|
88
|
+
end
|
|
70
89
|
end
|
|
71
90
|
|
|
72
91
|
# Monotonic count of downloads that have begun this session. Used by
|
|
73
92
|
# #wait to detect "a download started since I was called" even after the
|
|
74
93
|
# event queue has drained and @pending is empty again.
|
|
75
94
|
def started_count
|
|
76
|
-
@mutex.synchronize
|
|
95
|
+
@mutex.synchronize do
|
|
96
|
+
check_destination!
|
|
97
|
+
@started
|
|
98
|
+
end
|
|
77
99
|
end
|
|
78
100
|
|
|
79
101
|
def in_progress?
|
|
80
|
-
@mutex.synchronize
|
|
102
|
+
@mutex.synchronize do
|
|
103
|
+
check_destination!
|
|
104
|
+
@pending.any?
|
|
105
|
+
end
|
|
81
106
|
end
|
|
82
107
|
|
|
83
108
|
# Block until in-flight downloads finish, then return the completed-file
|
|
@@ -90,17 +115,21 @@ module Capybara
|
|
|
90
115
|
# give a download up to DOWNLOAD_BEGIN_GRACE to begin (tracked by the
|
|
91
116
|
# monotonic @started counter, immune to the queue already having drained),
|
|
92
117
|
# then wait up to `timeout` for every in-flight download to finish.
|
|
93
|
-
|
|
118
|
+
# With a trigger block the caller expects a new download: record the
|
|
119
|
+
# baseline before yielding and allow the full timeout for it to start.
|
|
120
|
+
def wait(seconds = 5, timeout: seconds)
|
|
94
121
|
baseline = started_count
|
|
95
|
-
|
|
96
|
-
hard_deadline = clock.call + timeout
|
|
97
|
-
begin_deadline = clock.call + [timeout, DOWNLOAD_BEGIN_GRACE].min
|
|
122
|
+
yield if block_given?
|
|
98
123
|
|
|
124
|
+
deadline = Utils::Wait.deadline(timeout)
|
|
125
|
+
begin_deadline = block_given? ? deadline : Utils::Wait.deadline([timeout, DOWNLOAD_BEGIN_GRACE].min)
|
|
99
126
|
loop do
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
127
|
+
now = Utils::Wait.monotonic_time
|
|
128
|
+
@mutex.synchronize do
|
|
129
|
+
check_destination!
|
|
130
|
+
began = @started > baseline
|
|
131
|
+
return @files.dup if now >= deadline || (@pending.empty? && (began || now >= begin_deadline))
|
|
132
|
+
end
|
|
104
133
|
sleep 0.02
|
|
105
134
|
end
|
|
106
135
|
end
|
|
@@ -117,13 +146,69 @@ module Capybara
|
|
|
117
146
|
# the subscriptions and the per-connection download config — leaving
|
|
118
147
|
# @enabled true would no-op the next #enable. Mirrors Network#reset.
|
|
119
148
|
def reset
|
|
149
|
+
@configuration_mutex.synchronize do
|
|
150
|
+
unsubscribe
|
|
151
|
+
clear
|
|
152
|
+
@enabled = false
|
|
153
|
+
@mutex.synchronize do
|
|
154
|
+
@path = nil
|
|
155
|
+
@destination_unknown = false
|
|
156
|
+
end
|
|
157
|
+
end
|
|
158
|
+
end
|
|
159
|
+
|
|
160
|
+
private
|
|
161
|
+
|
|
162
|
+
def disable_downloads(behavior)
|
|
163
|
+
result = browser.command("Browser.setDownloadBehavior", behavior: behavior, eventsEnabled: false)
|
|
120
164
|
unsubscribe
|
|
121
|
-
clear
|
|
122
165
|
@enabled = false
|
|
123
|
-
@
|
|
166
|
+
@mutex.synchronize do
|
|
167
|
+
@path = nil
|
|
168
|
+
@destination_unknown = false
|
|
169
|
+
@pending.clear
|
|
170
|
+
end
|
|
171
|
+
result
|
|
172
|
+
rescue TimeoutError
|
|
173
|
+
invalidate_destination
|
|
174
|
+
raise
|
|
124
175
|
end
|
|
125
176
|
|
|
126
|
-
|
|
177
|
+
def allow_downloads(save_path, prepare_directory: true)
|
|
178
|
+
destination = File.expand_path(save_path.to_s)
|
|
179
|
+
FileUtils.mkdir_p(destination) if prepare_directory
|
|
180
|
+
subscribe unless @enabled
|
|
181
|
+
begin
|
|
182
|
+
params = { behavior: "allow", downloadPath: destination, eventsEnabled: true }
|
|
183
|
+
browser.client.command("Browser.setDownloadBehavior", params,
|
|
184
|
+
on_success: -> { @mutex.synchronize { @path = destination } })
|
|
185
|
+
rescue TimeoutError
|
|
186
|
+
invalidate_destination
|
|
187
|
+
raise
|
|
188
|
+
rescue StandardError
|
|
189
|
+
unsubscribe unless @enabled
|
|
190
|
+
raise
|
|
191
|
+
end
|
|
192
|
+
@mutex.synchronize { @destination_unknown = false }
|
|
193
|
+
@enabled = true
|
|
194
|
+
end
|
|
195
|
+
|
|
196
|
+
def invalidate_destination
|
|
197
|
+
# The browser may already have applied a command whose reply timed out.
|
|
198
|
+
unsubscribe
|
|
199
|
+
@enabled = false
|
|
200
|
+
@mutex.synchronize do
|
|
201
|
+
@destination_unknown = true
|
|
202
|
+
@path = nil
|
|
203
|
+
@pending.clear
|
|
204
|
+
end
|
|
205
|
+
end
|
|
206
|
+
|
|
207
|
+
def check_destination!
|
|
208
|
+
return unless @destination_unknown
|
|
209
|
+
|
|
210
|
+
raise Error, "Download destination is unknown after a timeout. Set the destination again or reset the session."
|
|
211
|
+
end
|
|
127
212
|
|
|
128
213
|
def subscribe
|
|
129
214
|
@will_handler = build_will_handler
|
|
@@ -134,7 +219,7 @@ module Capybara
|
|
|
134
219
|
|
|
135
220
|
# downloadWillBegin carries {guid, url, suggestedFilename}. behavior
|
|
136
221
|
# "allow" writes the file under the (path-stripped) suggested name, so
|
|
137
|
-
#
|
|
222
|
+
# record the directory here in case the next download uses another one.
|
|
138
223
|
def build_will_handler
|
|
139
224
|
lambda do |params|
|
|
140
225
|
guid = params["guid"]
|
|
@@ -142,7 +227,9 @@ module Capybara
|
|
|
142
227
|
next if guid.nil? || name.empty?
|
|
143
228
|
|
|
144
229
|
@mutex.synchronize do
|
|
145
|
-
|
|
230
|
+
next unless @path
|
|
231
|
+
|
|
232
|
+
@pending[guid] = File.join(@path, name)
|
|
146
233
|
@started += 1
|
|
147
234
|
end
|
|
148
235
|
end
|
|
@@ -156,8 +243,8 @@ module Capybara
|
|
|
156
243
|
case params["state"]
|
|
157
244
|
when "completed"
|
|
158
245
|
@mutex.synchronize do
|
|
159
|
-
|
|
160
|
-
@files <<
|
|
246
|
+
path = @pending.delete(guid)
|
|
247
|
+
@files << path if path
|
|
161
248
|
end
|
|
162
249
|
when "canceled"
|
|
163
250
|
@mutex.synchronize { @pending.delete(guid) }
|
|
@@ -30,7 +30,7 @@ module Capybara
|
|
|
30
30
|
@browser = nil
|
|
31
31
|
dead.quit
|
|
32
32
|
end
|
|
33
|
-
@browser ||=
|
|
33
|
+
@browser ||= build_browser
|
|
34
34
|
end
|
|
35
35
|
|
|
36
36
|
def browser_alive?
|
|
@@ -109,6 +109,7 @@ module Capybara
|
|
|
109
109
|
|
|
110
110
|
def evaluate_async_script(script, *args)
|
|
111
111
|
unwrap_script_result(browser.evaluate_async(script.strip, *native_args(args),
|
|
112
|
+
wait: session_options.default_max_wait_time,
|
|
112
113
|
frame: browser.frame_stack.last))
|
|
113
114
|
end
|
|
114
115
|
|
|
@@ -123,9 +124,9 @@ module Capybara
|
|
|
123
124
|
# Block until in-flight HTTP traffic settles. Auto-enables the tracker
|
|
124
125
|
# on first call so callers don't have to remember to flip it on.
|
|
125
126
|
# Returns true on success, false on timeout.
|
|
126
|
-
def wait_for_network_idle(timeout: 5, connections: 0)
|
|
127
|
+
def wait_for_network_idle(timeout: 5, connections: 0, **)
|
|
127
128
|
network.enable
|
|
128
|
-
network.wait_for_idle(timeout: timeout, connections: connections)
|
|
129
|
+
network.wait_for_idle(timeout: timeout, connections: connections, **)
|
|
129
130
|
end
|
|
130
131
|
|
|
131
132
|
# -- Downloads --
|
|
@@ -186,10 +187,13 @@ module Capybara
|
|
|
186
187
|
end
|
|
187
188
|
|
|
188
189
|
# -- Window Support --
|
|
189
|
-
# Single-window driver
|
|
190
|
-
#
|
|
191
|
-
#
|
|
192
|
-
#
|
|
190
|
+
# Single-window driver: the gem drives one CDP target per session, and
|
|
191
|
+
# Browser's per-page state (target and session ids, frame stack,
|
|
192
|
+
# navigation sniff) is built around it. That is the gem's own model now:
|
|
193
|
+
# Lightpanda serves several targets per BrowserContext since upstream
|
|
194
|
+
# #3945 (11844, in the floor; below it a second Target.createTarget
|
|
195
|
+
# failed with TargetAlreadyLoaded, #1962). So there is exactly one
|
|
196
|
+
# window and its handle is the CDP target id.
|
|
193
197
|
#
|
|
194
198
|
# Resizing drives Emulation.setDeviceMetricsOverride, so it is real for
|
|
195
199
|
# window.innerWidth/innerHeight and for what `matchMedia` reports. Two
|
|
@@ -239,6 +243,12 @@ module Capybara
|
|
|
239
243
|
browser.set_viewport(width, height)
|
|
240
244
|
end
|
|
241
245
|
|
|
246
|
+
# Cuprite's `page.driver.resize(width, height)`. Same viewport, same
|
|
247
|
+
# resize-then-visit rule as `current_window.resize_to`.
|
|
248
|
+
def resize(width, height)
|
|
249
|
+
browser.set_viewport(width, height)
|
|
250
|
+
end
|
|
251
|
+
|
|
242
252
|
# No window manager and no layout, so "as large as the screen" has no
|
|
243
253
|
# meaning beyond the configured size. Both reset to it rather than
|
|
244
254
|
# raising, because suites call maximize defensively in setup and a
|
|
@@ -252,7 +262,7 @@ module Capybara
|
|
|
252
262
|
|
|
253
263
|
def open_new_window(_kind = :tab)
|
|
254
264
|
raise Capybara::NotSupportedByDriverError,
|
|
255
|
-
"
|
|
265
|
+
"capybara-lightpanda drives a single target per session, " \
|
|
256
266
|
"so a second window cannot be opened. Drive the second page in its own Capybara session instead."
|
|
257
267
|
end
|
|
258
268
|
|
|
@@ -315,10 +325,20 @@ module Capybara
|
|
|
315
325
|
browser.network.headers = headers
|
|
316
326
|
end
|
|
317
327
|
|
|
328
|
+
def add_header(name, value, permanent: true)
|
|
329
|
+
browser.headers.add({ name => value }, permanent: permanent)
|
|
330
|
+
end
|
|
331
|
+
|
|
318
332
|
def add_headers(headers)
|
|
319
333
|
browser.network.add_headers(headers)
|
|
320
334
|
end
|
|
321
335
|
|
|
336
|
+
# Request header + navigator.userAgent, until the next reset!. See
|
|
337
|
+
# Network#user_agent= for the "Mozilla" refusal.
|
|
338
|
+
def user_agent=(value)
|
|
339
|
+
browser.network.user_agent = value
|
|
340
|
+
end
|
|
341
|
+
|
|
322
342
|
# -- Lifecycle --
|
|
323
343
|
|
|
324
344
|
# Thin wrapper. The interesting work — disposing the BrowserContext
|
|
@@ -384,8 +404,19 @@ module Capybara
|
|
|
384
404
|
end
|
|
385
405
|
end
|
|
386
406
|
|
|
407
|
+
# Whether `text` keeps the tab innerText puts between table cells.
|
|
408
|
+
# Selenium's text has a space there, so the stock driver does too; the
|
|
409
|
+
# Cuprite registration overrides this (see Node#visible_text).
|
|
410
|
+
def keep_cell_tabs?
|
|
411
|
+
false
|
|
412
|
+
end
|
|
413
|
+
|
|
387
414
|
private
|
|
388
415
|
|
|
416
|
+
def build_browser
|
|
417
|
+
Browser.new(@options)
|
|
418
|
+
end
|
|
419
|
+
|
|
389
420
|
# A node found while a frame is switched to lives in that frame's
|
|
390
421
|
# document, which Node#call checks for a frame navigation.
|
|
391
422
|
def build_node(oid, initial_cache = {})
|
|
@@ -13,6 +13,8 @@ module Capybara
|
|
|
13
13
|
class UnsupportedPlatformError < Error; end
|
|
14
14
|
|
|
15
15
|
class TimeoutError < Error; end
|
|
16
|
+
class ScriptTimeoutError < TimeoutError; end
|
|
17
|
+
|
|
16
18
|
# A JS dialog opened with no accept_modal/dismiss_modal pre-arm in flight
|
|
17
19
|
# and the driver was configured with `raise_on_unhandled_modal: true`.
|
|
18
20
|
# Lightpanda has already applied its silent default by the time this is
|
|
@@ -46,6 +48,7 @@ module Capybara
|
|
|
46
48
|
end
|
|
47
49
|
end
|
|
48
50
|
|
|
51
|
+
class NoSuchCookieError < BrowserError; end
|
|
49
52
|
class DeadBrowserError < BrowserError; end
|
|
50
53
|
class NodeNotFoundError < BrowserError; end
|
|
51
54
|
class NoExecutionContextError < BrowserError; end
|
|
@@ -106,12 +109,8 @@ module Capybara
|
|
|
106
109
|
end
|
|
107
110
|
end
|
|
108
111
|
|
|
109
|
-
#
|
|
110
|
-
#
|
|
111
|
-
# mouse path, no multi-page API, no page-status tracking). They mirror
|
|
112
|
-
# the error taxonomy of other Capybara CDP drivers (MouseEventFailed, plus
|
|
113
|
-
# Ferrum's NoSuchPageError/StatusError) so suites migrating from them
|
|
114
|
-
# don't NameError on rescue lists that reference them.
|
|
112
|
+
# Error names shared with other Capybara CDP drivers. MouseEventFailed
|
|
113
|
+
# retains the coordinates and selector for callers parsing its message.
|
|
115
114
|
class MouseEventFailed < BrowserError
|
|
116
115
|
attr_reader :node, :selector, :position
|
|
117
116
|
|
|
@@ -31,6 +31,7 @@ module Capybara
|
|
|
31
31
|
delete: { key: "Delete", code: "Delete", keyCode: 46 },
|
|
32
32
|
semicolon: { key: ";", code: "Semicolon", keyCode: 186, text: ";" },
|
|
33
33
|
equals: { key: "=", code: "Equal", keyCode: 187, text: "=" },
|
|
34
|
+
numpad_enter: { key: "Enter", code: "NumpadEnter", keyCode: 13, text: "\r" },
|
|
34
35
|
numpad0: { key: "0", code: "Numpad0", keyCode: 96, text: "0" },
|
|
35
36
|
numpad1: { key: "1", code: "Numpad1", keyCode: 97, text: "1" },
|
|
36
37
|
numpad2: { key: "2", code: "Numpad2", keyCode: 98, text: "2" },
|
|
@@ -87,6 +88,33 @@ module Capybara
|
|
|
87
88
|
SHIFT_MAP[char] || char.upcase
|
|
88
89
|
end
|
|
89
90
|
|
|
91
|
+
# US-layout physical key (`code`, `keyCode`) behind each printable ASCII
|
|
92
|
+
# character, so a typed character reports what Chrome reports: "a" and
|
|
93
|
+
# "A" are KeyA/65, "1" and "!" are Digit1/49.
|
|
94
|
+
PUNCTUATION_KEYS = {
|
|
95
|
+
" " => ["Space", 32], "`" => ["Backquote", 192], "-" => ["Minus", 189],
|
|
96
|
+
"=" => ["Equal", 187], "[" => ["BracketLeft", 219], "]" => ["BracketRight", 221],
|
|
97
|
+
"\\" => ["Backslash", 220], ";" => ["Semicolon", 186], "'" => ["Quote", 222],
|
|
98
|
+
"," => ["Comma", 188], "." => ["Period", 190], "/" => ["Slash", 191],
|
|
99
|
+
}.freeze
|
|
100
|
+
UNSHIFT_MAP = SHIFT_MAP.invert.freeze
|
|
101
|
+
private_constant :PUNCTUATION_KEYS, :UNSHIFT_MAP
|
|
102
|
+
|
|
103
|
+
# Key definition for a character a US keyboard can type, nil for any
|
|
104
|
+
# other (accented letters, emoji, control characters).
|
|
105
|
+
def self.char_definition(char)
|
|
106
|
+
base = UNSHIFT_MAP[char] || char.downcase
|
|
107
|
+
code, key_code =
|
|
108
|
+
case base
|
|
109
|
+
when /\A[a-z]\z/ then ["Key#{base.upcase}", base.upcase.ord]
|
|
110
|
+
when /\A[0-9]\z/ then ["Digit#{base}", base.ord]
|
|
111
|
+
else PUNCTUATION_KEYS[base]
|
|
112
|
+
end
|
|
113
|
+
return unless code
|
|
114
|
+
|
|
115
|
+
{ key: char, code: code, keyCode: key_code, text: char }
|
|
116
|
+
end
|
|
117
|
+
|
|
90
118
|
def initialize(browser)
|
|
91
119
|
@browser = browser
|
|
92
120
|
end
|
|
@@ -173,7 +201,17 @@ module Capybara
|
|
|
173
201
|
raw_dispatch(key_definition(key))
|
|
174
202
|
end
|
|
175
203
|
|
|
204
|
+
# A character on the US layout is a key press: keydown, keypress,
|
|
205
|
+
# beforeinput, input, keyup, as when a user types it. `Input.insertText`
|
|
206
|
+
# alone fired `input` only, so a filter or autocomplete bound to `keyup`
|
|
207
|
+
# or `keydown` never ran. Lightpanda inserts the keyDown's `text` itself
|
|
208
|
+
# (upstream #3542), so nothing is typed twice. Characters no US key
|
|
209
|
+
# produces (accents, emoji, newlines) stay on `Input.insertText`, the
|
|
210
|
+
# IME/paste path, as in Puppeteer.
|
|
176
211
|
def dispatch_char(char)
|
|
212
|
+
definition = self.class.char_definition(char)
|
|
213
|
+
return raw_dispatch(definition) if definition
|
|
214
|
+
|
|
177
215
|
@browser.page_command("Input.insertText", text: char)
|
|
178
216
|
end
|
|
179
217
|
|
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Capybara
|
|
4
|
+
module Lightpanda
|
|
5
|
+
class Network
|
|
6
|
+
# Retain the hash keys used by existing traffic assertions and JSON logs.
|
|
7
|
+
class Exchange < Hash
|
|
8
|
+
Request = Data.define(:url, :method) # rubocop:disable Lint/DataDefineOverride
|
|
9
|
+
|
|
10
|
+
def initialize(params)
|
|
11
|
+
super()
|
|
12
|
+
merge!(request_id: params["requestId"], timestamp: params["timestamp"],
|
|
13
|
+
url: params.dig("request", "url"), method: params.dig("request", "method"), response: nil, error: nil)
|
|
14
|
+
@finished = false
|
|
15
|
+
end
|
|
16
|
+
|
|
17
|
+
def request = Request.new(self[:url], self[:method])
|
|
18
|
+
def url = self[:url]
|
|
19
|
+
def response = self[:response]
|
|
20
|
+
def error = self[:error]
|
|
21
|
+
def finished? = @finished
|
|
22
|
+
def pending? = !finished?
|
|
23
|
+
|
|
24
|
+
def finish!
|
|
25
|
+
@finished = true
|
|
26
|
+
end
|
|
27
|
+
end
|
|
28
|
+
|
|
29
|
+
class Response < Hash
|
|
30
|
+
def initialize(raw)
|
|
31
|
+
super()
|
|
32
|
+
raw ||= {}
|
|
33
|
+
merge!(status: raw["status"], headers: raw["headers"] || {}, mime_type: raw["mimeType"])
|
|
34
|
+
end
|
|
35
|
+
|
|
36
|
+
def status = self[:status]
|
|
37
|
+
def headers = self[:headers]
|
|
38
|
+
end
|
|
39
|
+
end
|
|
40
|
+
end
|
|
41
|
+
end
|