capybara-lightpanda 0.9.0 → 0.10.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 +37 -0
- data/README.md +14 -1
- data/lib/capybara/lightpanda/auto_scripts.rb +3 -1
- data/lib/capybara/lightpanda/binary.rb +70 -8
- data/lib/capybara/lightpanda/browser/console.rb +89 -7
- data/lib/capybara/lightpanda/browser/modals.rb +54 -3
- data/lib/capybara/lightpanda/browser/navigation.rb +1 -0
- data/lib/capybara/lightpanda/browser/selenium_compat.rb +124 -0
- data/lib/capybara/lightpanda/browser.rb +104 -4
- data/lib/capybara/lightpanda/downloads.rb +176 -0
- data/lib/capybara/lightpanda/driver.rb +109 -2
- data/lib/capybara/lightpanda/errors.rb +5 -0
- data/lib/capybara/lightpanda/javascripts/errors.js +74 -0
- data/lib/capybara/lightpanda/javascripts/predicates.js +19 -55
- data/lib/capybara/lightpanda/network.rb +36 -10
- data/lib/capybara/lightpanda/node.rb +177 -65
- data/lib/capybara/lightpanda/options.rb +44 -7
- data/lib/capybara/lightpanda/process.rb +151 -11
- data/lib/capybara/lightpanda/version.rb +1 -1
- data/lib/capybara-lightpanda.rb +1 -0
- metadata +5 -2
|
@@ -92,61 +92,25 @@ function isContentEditable(el) {
|
|
|
92
92
|
return false;
|
|
93
93
|
}
|
|
94
94
|
|
|
95
|
-
//
|
|
96
|
-
//
|
|
97
|
-
//
|
|
98
|
-
//
|
|
99
|
-
//
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
DIV:1, DL:1, DT:1, DD:1, FIELDSET:1, FIGCAPTION:1, FIGURE:1,
|
|
104
|
-
FOOTER:1, FORM:1, H1:1, H2:1, H3:1, H4:1, H5:1, H6:1, HEADER:1,
|
|
105
|
-
HGROUP:1, HR:1, LI:1, MAIN:1, NAV:1, OL:1, P:1, PRE:1, SECTION:1,
|
|
106
|
-
TABLE:1, TR:1, UL:1 };
|
|
107
|
-
|
|
108
|
-
// Walk descendants and accumulate text from visible nodes only. Inserts
|
|
109
|
-
// newlines around block-display containers so paragraphs/lists render with
|
|
110
|
-
// natural breaks (Capybara expects this from Chrome's innerText).
|
|
111
|
-
// Lightpanda's innerText returns textContent verbatim (no rendering, so no
|
|
112
|
-
// hidden-descendant filtering), so we DIY the visibility filtering here.
|
|
95
|
+
// Capybara's "visible text" = WebDriver getText semantics, which native
|
|
96
|
+
// innerText alone doesn't provide at two edges: a non-rendered element must
|
|
97
|
+
// yield "" (innerText returns textContent per the HTML getter's step 1), and a
|
|
98
|
+
// ShadowRoot/DocumentFragment has no innerText at all. So we gate on visibility
|
|
99
|
+
// and otherwise delegate the rendered-text collection — block line breaks +
|
|
100
|
+
// display:none descendant skipping + whitespace — to native innerText
|
|
101
|
+
// (Lightpanda #2785/#2795, guaranteed by the build floor). node.rb#visible_text
|
|
102
|
+
// normalizes the whitespace on top.
|
|
113
103
|
function visibleText(el) {
|
|
114
|
-
|
|
115
|
-
//
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
if (node.nodeType === 11) {
|
|
125
|
-
var fout = '';
|
|
126
|
-
for (var k = 0; k < node.childNodes.length; k++) fout += walk(node.childNodes[k]);
|
|
127
|
-
return fout;
|
|
128
|
-
}
|
|
129
|
-
if (node.nodeType !== 1) return '';
|
|
130
|
-
if (!isVisible(node)) return '';
|
|
131
|
-
var tag = (node.tagName || '').toUpperCase();
|
|
132
|
-
if (tag === 'TEXTAREA') return node.value || '';
|
|
133
|
-
if (tag === 'BR') return '\n';
|
|
134
|
-
var win = node.ownerDocument.defaultView || window;
|
|
135
|
-
var style = win.getComputedStyle(node);
|
|
136
|
-
var disp = (style.display || '').toUpperCase();
|
|
137
|
-
var isBlock = BLOCK_DISP[disp] || BLOCK_TAG[tag];
|
|
138
|
-
var out = '';
|
|
139
|
-
for (var i = 0; i < node.childNodes.length; i++) {
|
|
140
|
-
out += walk(node.childNodes[i]);
|
|
141
|
-
}
|
|
142
|
-
// Block-level elements get wrapped in \n…\n only when they actually
|
|
143
|
-
// contribute visible text. An empty <div> between two inline siblings
|
|
144
|
-
// would otherwise introduce a phantom line break that Chrome's
|
|
145
|
-
// innerText algorithm collapses out (required line breaks around
|
|
146
|
-
// empty blocks coalesce in the line-collapse pass).
|
|
147
|
-
if (isBlock && /\S/.test(out)) out = '\n' + out + '\n';
|
|
148
|
-
return out;
|
|
104
|
+
if (el.nodeType === 1) return isVisible(el) ? el.innerText : '';
|
|
105
|
+
// ShadowRoot / DocumentFragment: no innerText of its own. Concatenate the
|
|
106
|
+
// native innerText of each visible element child with the text nodes between
|
|
107
|
+
// them — collapsing the latter's whitespace to a single space so template
|
|
108
|
+
// newlines between inline children render as a separating space, not a break.
|
|
109
|
+
var out = '';
|
|
110
|
+
for (var i = 0; i < el.childNodes.length; i++) {
|
|
111
|
+
var c = el.childNodes[i];
|
|
112
|
+
if (c.nodeType === 3) out += c.nodeValue.replace(/[\t\n\r\f\v ]+/g, ' ');
|
|
113
|
+
else if (c.nodeType === 1 && isVisible(c)) out += c.innerText;
|
|
149
114
|
}
|
|
150
|
-
|
|
151
|
-
return walk(el);
|
|
115
|
+
return out;
|
|
152
116
|
}
|
|
@@ -150,10 +150,11 @@ module Capybara
|
|
|
150
150
|
# The handlers also capture the last top-level navigation response
|
|
151
151
|
# (mirrors capybara-playwright-driver's navigation_request? hook). CDP
|
|
152
152
|
# normally marks the main-document response via
|
|
153
|
-
# `Network.responseReceived.type
|
|
154
|
-
# responses
|
|
155
|
-
# long way: remember the document requestId, store
|
|
156
|
-
# requestId equals it.
|
|
153
|
+
# `Network.responseReceived.type`. Lightpanda only started emitting that
|
|
154
|
+
# field on responses in build 8318 (#3037) — far above the gem's floor —
|
|
155
|
+
# so match the long way instead: remember the document requestId, store
|
|
156
|
+
# the response whose requestId equals it. Works on every supported build;
|
|
157
|
+
# don't "simplify" it to read response.type until the floor clears 8318.
|
|
157
158
|
def subscribe
|
|
158
159
|
@request_handler = build_request_handler
|
|
159
160
|
@response_handler = build_response_handler
|
|
@@ -161,6 +162,16 @@ module Capybara
|
|
|
161
162
|
browser.on("Network.responseReceived", &@response_handler)
|
|
162
163
|
end
|
|
163
164
|
|
|
165
|
+
# Redirects follow Chrome's shape (Lightpanda since #3175, build ≥8602):
|
|
166
|
+
# every followed hop re-emits requestWillBeSent with the SAME requestId
|
|
167
|
+
# and the 3xx riding along as `redirectResponse`; the 3xx never gets a
|
|
168
|
+
# responseReceived of its own. So the hop's event is what closes the
|
|
169
|
+
# previous entry — without it that entry stays `response: nil` forever,
|
|
170
|
+
# pending_connections wedges at ≥1 and every wait_for_idle burns its
|
|
171
|
+
# full timeout after the first redirect (Ferrum's
|
|
172
|
+
# subscribe_request_will_be_sent does the same close). Deliberately NOT
|
|
173
|
+
# fed into @last_navigation_response: status_code must report the final
|
|
174
|
+
# hop, and the redirected requestWillBeSent resets it below anyway.
|
|
164
175
|
def build_request_handler
|
|
165
176
|
lambda do |params|
|
|
166
177
|
if params["type"] == "Document"
|
|
@@ -175,6 +186,9 @@ module Capybara
|
|
|
175
186
|
response: nil,
|
|
176
187
|
}
|
|
177
188
|
@traffic_mutex.synchronize do
|
|
189
|
+
if (redirect = params["redirectResponse"]) && (previous = last_open_entry(params["requestId"]))
|
|
190
|
+
previous[:response] = response_summary(redirect)
|
|
191
|
+
end
|
|
178
192
|
@traffic << entry
|
|
179
193
|
@traffic.shift(@traffic.size - TRAFFIC_LIMIT) if @traffic.size > TRAFFIC_LIMIT
|
|
180
194
|
end
|
|
@@ -190,18 +204,30 @@ module Capybara
|
|
|
190
204
|
}
|
|
191
205
|
end
|
|
192
206
|
@traffic_mutex.synchronize do
|
|
193
|
-
|
|
207
|
+
# Last open entry, not `find`: after a redirect chain several
|
|
208
|
+
# entries share the requestId and only the newest is still open.
|
|
209
|
+
request = last_open_entry(params["requestId"])
|
|
194
210
|
next unless request
|
|
195
211
|
|
|
196
|
-
request[:response] =
|
|
197
|
-
status: params.dig("response", "status"),
|
|
198
|
-
headers: params.dig("response", "headers"),
|
|
199
|
-
mime_type: params.dig("response", "mimeType"),
|
|
200
|
-
}
|
|
212
|
+
request[:response] = response_summary(params["response"])
|
|
201
213
|
end
|
|
202
214
|
end
|
|
203
215
|
end
|
|
204
216
|
|
|
217
|
+
# Caller holds @traffic_mutex.
|
|
218
|
+
def last_open_entry(request_id)
|
|
219
|
+
@traffic.reverse_each.find { |t| t[:request_id] == request_id && t[:response].nil? }
|
|
220
|
+
end
|
|
221
|
+
|
|
222
|
+
def response_summary(response)
|
|
223
|
+
response ||= {}
|
|
224
|
+
{
|
|
225
|
+
status: response["status"],
|
|
226
|
+
headers: response["headers"],
|
|
227
|
+
mime_type: response["mimeType"],
|
|
228
|
+
}
|
|
229
|
+
end
|
|
230
|
+
|
|
205
231
|
def unsubscribe
|
|
206
232
|
browser.off("Network.requestWillBeSent", @request_handler) if @request_handler
|
|
207
233
|
browser.off("Network.responseReceived", @response_handler) if @response_handler
|
|
@@ -13,6 +13,22 @@ module Capybara
|
|
|
13
13
|
@remote_object_id = remote_object_id
|
|
14
14
|
end
|
|
15
15
|
|
|
16
|
+
# Capybara::Driver::Node#native returns the constructor's second argument,
|
|
17
|
+
# which here is the raw CDP objectId — a String. So the Selenium/Cuprite
|
|
18
|
+
# idiom `element.native.send_keys(...)` (solidus's
|
|
19
|
+
# return_authorizations_spec.rb does exactly that) died with
|
|
20
|
+
# "undefined method 'send_keys' for an instance of String". This driver
|
|
21
|
+
# has no lower-level node object behind the Capybara one — the CDP handle
|
|
22
|
+
# IS this Node (see #remote_object_id) — so `native` is self.
|
|
23
|
+
#
|
|
24
|
+
# Safe against Capybara::Driver::Node#==, which compares `native ==
|
|
25
|
+
# other.native` and would recurse forever on a self-returning `native`:
|
|
26
|
+
# #== and #eql? below are full overrides that never call super and never
|
|
27
|
+
# read #native.
|
|
28
|
+
def native
|
|
29
|
+
self
|
|
30
|
+
end
|
|
31
|
+
|
|
16
32
|
def text
|
|
17
33
|
call("function() { return this.textContent }")
|
|
18
34
|
end
|
|
@@ -21,10 +37,11 @@ module Capybara
|
|
|
21
37
|
filter_text(call("function() { return this.textContent }"))
|
|
22
38
|
end
|
|
23
39
|
|
|
24
|
-
#
|
|
25
|
-
#
|
|
26
|
-
#
|
|
27
|
-
#
|
|
40
|
+
# Delegates to _lightpanda.visibleText, which gates on visibility (a
|
|
41
|
+
# not-visible element reads as "" — WebDriver semantics) and otherwise
|
|
42
|
+
# hands the rendered-text collection (block line breaks + display:none
|
|
43
|
+
# descendant skipping) to native innerText (#2785/#2795). We normalize the
|
|
44
|
+
# whitespace here to match Capybara's expected Chrome semantics.
|
|
28
45
|
def visible_text
|
|
29
46
|
call(VISIBLE_TEXT_JS).to_s
|
|
30
47
|
.gsub(/\A[[:space:]&&[^\u00A0]]+/, "")
|
|
@@ -69,6 +86,17 @@ module Capybara
|
|
|
69
86
|
previous
|
|
70
87
|
end
|
|
71
88
|
|
|
89
|
+
# Quiet form of the `isConnected` guard every other operation carries:
|
|
90
|
+
# true while the node is still attached to a live document, false once
|
|
91
|
+
# it has been detached or its document navigated away (mirrors Ferrum's
|
|
92
|
+
# `Node#exists?`, whose probe is `DOM.resolveNode`). Anything else that
|
|
93
|
+
# goes wrong still raises — only "gone" is turned into false.
|
|
94
|
+
def exists?
|
|
95
|
+
call("function() { return true; }")
|
|
96
|
+
rescue ObsoleteNode, NodeNotFoundError, NoExecutionContextError
|
|
97
|
+
false
|
|
98
|
+
end
|
|
99
|
+
|
|
72
100
|
# Routed through #call (not a bare call_function_on) so a detached
|
|
73
101
|
# host raises ObsoleteNode like every other node operation — Capybara's
|
|
74
102
|
# automatic_reload then re-finds the host instead of silently reading
|
|
@@ -107,8 +135,15 @@ module Capybara
|
|
|
107
135
|
call("function() { this.dispatchEvent(new MouseEvent('dblclick', {bubbles: true, cancelable: true})) }")
|
|
108
136
|
end
|
|
109
137
|
|
|
138
|
+
# A real pointer entering an element fires `mouseover` (bubbling) AND
|
|
139
|
+
# `mouseenter` (non-bubbling), in that order. Dispatching only `mouseover`
|
|
140
|
+
# silently no-ops the `mouseenter->menu#open` Stimulus idiom and the
|
|
141
|
+
# Floating UI / tippy-style menus built on it — the dominant hover-menu
|
|
142
|
+
# pattern in Rails apps — so fire both. CSS `:hover` still reveals nothing
|
|
143
|
+
# (upstream tracks no pointer state); test/features/hover_test.rb pins
|
|
144
|
+
# both halves.
|
|
110
145
|
def hover
|
|
111
|
-
call(
|
|
146
|
+
call(HOVER_JS)
|
|
112
147
|
end
|
|
113
148
|
|
|
114
149
|
# Kept as a deliberate no-op despite upstream now tracking scroll position
|
|
@@ -150,17 +185,29 @@ module Capybara
|
|
|
150
185
|
end
|
|
151
186
|
|
|
152
187
|
# Capybara's drag-and-drop API (`Element#drop`). String/Pathname arguments
|
|
153
|
-
# are file paths
|
|
154
|
-
#
|
|
155
|
-
#
|
|
156
|
-
#
|
|
188
|
+
# are file paths; Hash arguments are `{ mime_type => data }` string drops.
|
|
189
|
+
# We assemble a `DataTransfer` and fire `dragenter` -> `dragover` -> `drop`
|
|
190
|
+
# on this element, so HTML5 dropzones see the payload via
|
|
191
|
+
# `event.dataTransfer`.
|
|
192
|
+
#
|
|
193
|
+
# Files reach the page the way Cuprite's #316 does it: a hidden
|
|
194
|
+
# `<input type=file>` is attached to this element's document,
|
|
195
|
+
# `DOM.setFileInputFiles` points it at the paths (the browser reads the
|
|
196
|
+
# bytes off disk itself), and the drop JS moves `input.files` into the
|
|
197
|
+
# DataTransfer and removes the input. Previously the bytes were base64'd
|
|
198
|
+
# into the `Runtime.callFunctionOn` message, which capped a drop at
|
|
199
|
+
# ~70 MB under `--cdp-max-message-size` and pinned every byte in Ruby;
|
|
200
|
+
# now the size ceiling is Lightpanda's own file handling. Paths are read
|
|
201
|
+
# on the machine running Lightpanda (local for the spawned process),
|
|
202
|
+
# exactly like `attach_file`.
|
|
157
203
|
#
|
|
158
204
|
# DataTransfer/DataTransferItem/DragEvent landed upstream in PR #2671
|
|
159
205
|
# (build ≥6699) and are guaranteed by the MINIMUM_NIGHTLY_BUILD floor;
|
|
160
206
|
# without them the drop JS raises "DataTransfer is not defined".
|
|
161
207
|
def drop(*args)
|
|
162
|
-
|
|
163
|
-
|
|
208
|
+
paths, strings = partition_drop_args(args)
|
|
209
|
+
input = paths.empty? ? nil : attach_drop_input(paths)
|
|
210
|
+
call(DROP_JS, input, strings.to_json)
|
|
164
211
|
nil
|
|
165
212
|
end
|
|
166
213
|
|
|
@@ -371,37 +418,26 @@ module Capybara
|
|
|
371
418
|
# here and base64-encoded so binary content survives the JSON hop; Hashes
|
|
372
419
|
# are `{ type => data }` string drops. Returns `[files, strings]`.
|
|
373
420
|
def partition_drop_args(args)
|
|
374
|
-
|
|
421
|
+
paths = []
|
|
375
422
|
strings = []
|
|
376
423
|
args.each do |arg|
|
|
377
424
|
if arg.is_a?(Hash)
|
|
378
425
|
arg.each { |type, data| strings << { type: type.to_s, data: data.to_s } }
|
|
379
426
|
else
|
|
380
|
-
|
|
381
|
-
files << {
|
|
382
|
-
name: File.basename(path),
|
|
383
|
-
type: drop_mime_for(path),
|
|
384
|
-
b64: [File.binread(path)].pack("m0"),
|
|
385
|
-
}
|
|
427
|
+
paths << File.expand_path(arg.to_s)
|
|
386
428
|
end
|
|
387
429
|
end
|
|
388
|
-
[
|
|
430
|
+
[paths, strings]
|
|
389
431
|
end
|
|
390
432
|
|
|
391
|
-
#
|
|
392
|
-
#
|
|
393
|
-
#
|
|
394
|
-
|
|
395
|
-
|
|
396
|
-
|
|
397
|
-
|
|
398
|
-
|
|
399
|
-
".zip" => "application/zip",
|
|
400
|
-
}.freeze
|
|
401
|
-
private_constant :DROP_MIME_TYPES
|
|
402
|
-
|
|
403
|
-
def drop_mime_for(path)
|
|
404
|
-
DROP_MIME_TYPES.fetch(File.extname(path).downcase, "application/octet-stream")
|
|
433
|
+
# Hidden `<input type=file multiple>` in this element's own document (so
|
|
434
|
+
# drops inside an iframe stay in that frame's DOM), pre-loaded via
|
|
435
|
+
# DOM.setFileInputFiles. Returned as a Node so it can be bound as a
|
|
436
|
+
# callFunctionOn argument; DROP_JS removes it once the files are moved.
|
|
437
|
+
def attach_drop_input(paths)
|
|
438
|
+
oid = call(CREATE_DROP_INPUT_JS, return_by_value: false)["objectId"]
|
|
439
|
+
driver.browser.set_file_input_files(oid, paths)
|
|
440
|
+
self.class.new(driver, oid)
|
|
405
441
|
end
|
|
406
442
|
|
|
407
443
|
# Whitespace-normalized text (Cuprite pattern). Capybara's text matchers compare
|
|
@@ -455,6 +491,18 @@ module Capybara
|
|
|
455
491
|
JS
|
|
456
492
|
end
|
|
457
493
|
|
|
494
|
+
# `mouseenter` carries `bubbles: false` per spec — it is dispatched on the
|
|
495
|
+
# target only, not walked up the ancestor chain the way a real pointer
|
|
496
|
+
# would. That covers the handler-on-the-hovered-element case (every
|
|
497
|
+
# Stimulus `mouseenter->` action) and stops short of emulating pointer
|
|
498
|
+
# geometry we don't have.
|
|
499
|
+
HOVER_JS = <<~JS
|
|
500
|
+
function() {
|
|
501
|
+
this.dispatchEvent(new MouseEvent('mouseover', {bubbles: true, cancelable: true}));
|
|
502
|
+
this.dispatchEvent(new MouseEvent('mouseenter', {bubbles: false, cancelable: true}));
|
|
503
|
+
}
|
|
504
|
+
JS
|
|
505
|
+
|
|
458
506
|
# We dispatch a `MouseEvent` (not a generic `Event`) because Turbo's link
|
|
459
507
|
# and form interceptors guard with `event instanceof MouseEvent` before
|
|
460
508
|
# they consider intercepting — a synthetic `Event('click')` is silently
|
|
@@ -473,25 +521,57 @@ module Capybara
|
|
|
473
521
|
CLICK_JS = <<~JS
|
|
474
522
|
function() {
|
|
475
523
|
var EventCtor = (typeof MouseEvent !== 'undefined') ? MouseEvent : Event;
|
|
524
|
+
// Emulate coordinate hit-testing without a layout engine. In a real
|
|
525
|
+
// browser a pointer click on a container lands on its frontmost
|
|
526
|
+
// descendant: THAT element is the event target, and the sequence
|
|
527
|
+
// bubbles UP from there (so the container's own handlers still fire).
|
|
528
|
+
// Lightpanda has no geometry — every rect is a hardcoded ~5x5, so
|
|
529
|
+
// document.elementFromPoint(center) just returns the container — so
|
|
530
|
+
// when `this` is a plain non-interactive wrapper we walk DOWN to the
|
|
531
|
+
// first visible child, repeatedly, stopping at the first element that
|
|
532
|
+
// is itself interactive or carries a click handler (onclick /
|
|
533
|
+
// role=button). That lands on the element a centered pointer would
|
|
534
|
+
// hit, so widgets that bind their handlers on an inner node fire:
|
|
535
|
+
// select2 v3 binds its open handler on the inner .select2-choice
|
|
536
|
+
// (single, mousedown) / .select2-choices (multi, click), the FIRST
|
|
537
|
+
// child of the .select2-container that Capybara helpers click. We
|
|
538
|
+
// can't use "single visible child" as a guard — select2 keeps an
|
|
539
|
+
// offscreen .select2-focusser <input> sibling that has no geometry to
|
|
540
|
+
// distinguish, so it reads as a second visible child. The descent
|
|
541
|
+
// never starts from an interactive element, so a normal
|
|
542
|
+
// button/link/input click still lands exactly on `this`.
|
|
543
|
+
var INTERACTIVE = { A: 1, BUTTON: 1, INPUT: 1, SELECT: 1, TEXTAREA: 1,
|
|
544
|
+
OPTION: 1, LABEL: 1, SUMMARY: 1 };
|
|
545
|
+
var hit = this;
|
|
546
|
+
while (!INTERACTIVE[hit.tagName] &&
|
|
547
|
+
!hit.hasAttribute('onclick') &&
|
|
548
|
+
hit.getAttribute('role') !== 'button') {
|
|
549
|
+
var next = null, ch = hit.children;
|
|
550
|
+
for (var i = 0; i < ch.length; i++) {
|
|
551
|
+
if (!window._lightpanda || _lightpanda.isVisible(ch[i])) { next = ch[i]; break; }
|
|
552
|
+
}
|
|
553
|
+
if (!next) break;
|
|
554
|
+
hit = next;
|
|
555
|
+
}
|
|
476
556
|
// Real pointer clicks are a mousedown -> mouseup -> click sequence,
|
|
477
557
|
// and widgets like select2 open on `mousedown`, not `click`.
|
|
478
558
|
// Cancelling mousedown suppresses focus/text-selection in a real
|
|
479
559
|
// browser but never the click, so the click below is dispatched
|
|
480
560
|
// unconditionally.
|
|
481
|
-
|
|
482
|
-
|
|
561
|
+
hit.dispatchEvent(new EventCtor('mousedown', { bubbles: true, cancelable: true }));
|
|
562
|
+
hit.dispatchEvent(new EventCtor('mouseup', { bubbles: true, cancelable: true }));
|
|
483
563
|
var clickEvt = new EventCtor('click', { bubbles: true, cancelable: true });
|
|
484
|
-
var notCancelled =
|
|
564
|
+
var notCancelled = hit.dispatchEvent(clickEvt);
|
|
485
565
|
if (!notCancelled || clickEvt.defaultPrevented) return;
|
|
486
|
-
var tag =
|
|
487
|
-
if (tag === 'A' &&
|
|
566
|
+
var tag = hit.tagName;
|
|
567
|
+
if (tag === 'A' && hit.href && hit.target !== '_blank') {
|
|
488
568
|
// Same-document fragment-only navigation: just update hash (or do
|
|
489
569
|
// nothing if identical). Mirrors Chrome — assigning location.href
|
|
490
570
|
// to a same-document URL on Lightpanda triggers a real navigation
|
|
491
571
|
// tick that cancels pending setTimeout callbacks and clears form
|
|
492
572
|
// values, which breaks any test driving DOM updates from a click
|
|
493
573
|
// handler on `<a href="#...">`.
|
|
494
|
-
var dest = new URL(
|
|
574
|
+
var dest = new URL(hit.href, document.baseURI);
|
|
495
575
|
var here = new URL(window.location.href);
|
|
496
576
|
if (dest.origin === here.origin && dest.pathname === here.pathname &&
|
|
497
577
|
dest.search === here.search) {
|
|
@@ -499,7 +579,7 @@ module Capybara
|
|
|
499
579
|
window.location.hash = dest.hash;
|
|
500
580
|
}
|
|
501
581
|
} else {
|
|
502
|
-
window.location.href =
|
|
582
|
+
window.location.href = hit.href;
|
|
503
583
|
}
|
|
504
584
|
}
|
|
505
585
|
}
|
|
@@ -532,17 +612,30 @@ module Capybara
|
|
|
532
612
|
}
|
|
533
613
|
JS
|
|
534
614
|
|
|
535
|
-
|
|
536
|
-
|
|
537
|
-
|
|
538
|
-
|
|
615
|
+
CREATE_DROP_INPUT_JS = <<~JS
|
|
616
|
+
function() {
|
|
617
|
+
var doc = this.ownerDocument || document;
|
|
618
|
+
var input = doc.createElement('input');
|
|
619
|
+
input.type = 'file';
|
|
620
|
+
input.multiple = true;
|
|
621
|
+
input.style.display = 'none';
|
|
622
|
+
input.setAttribute('data-lightpanda-drop-input', '');
|
|
623
|
+
(doc.body || doc.documentElement).appendChild(input);
|
|
624
|
+
return input;
|
|
625
|
+
}
|
|
626
|
+
JS
|
|
627
|
+
|
|
628
|
+
# Build a DataTransfer from the pre-loaded hidden input (files) and the
|
|
629
|
+
# JSON string payloads (typed items), then replay the HTML5 drop sequence
|
|
630
|
+
# on this element. The input is removed once its files are moved.
|
|
539
631
|
DROP_JS = <<~JS
|
|
540
|
-
function(
|
|
632
|
+
function(input, stringsJson) {
|
|
541
633
|
var el = this;
|
|
542
634
|
var dt = new DataTransfer();
|
|
543
|
-
|
|
544
|
-
|
|
545
|
-
|
|
635
|
+
if (input) {
|
|
636
|
+
for (var i = 0; i < input.files.length; i++) dt.items.add(input.files[i]);
|
|
637
|
+
input.remove();
|
|
638
|
+
}
|
|
546
639
|
JSON.parse(stringsJson).forEach(function(s) {
|
|
547
640
|
dt.items.add(s.data, s.type);
|
|
548
641
|
});
|
|
@@ -699,34 +792,53 @@ module Capybara
|
|
|
699
792
|
|
|
700
793
|
OBSCURED_JS = "function() { return _lightpanda.isObscured(this); }"
|
|
701
794
|
|
|
795
|
+
# Capybara's contract for Element#path is an XPath that re-finds the very
|
|
796
|
+
# same node (`node #path returns xpath which points to itself`), so this
|
|
797
|
+
# emits `/HTML/BODY/DIV[2]/P[1]` rather than the CSS-ish
|
|
798
|
+
# `html > body > div:nth-of-type(2) > p` it used to. Chrome exposes no
|
|
799
|
+
# native equivalent either; Cuprite hand-rolls the same walk.
|
|
800
|
+
#
|
|
801
|
+
# Every step is indexed, including single children: `P[1]` and `P` select
|
|
802
|
+
# the same node, but the explicit index keeps the output stable if a
|
|
803
|
+
# sibling appears later, and it is what Chrome DevTools' "Copy XPath"
|
|
804
|
+
# produces. Positions count same-tag preceding siblings, which is exactly
|
|
805
|
+
# XPath's own positional semantics.
|
|
806
|
+
#
|
|
807
|
+
# No `id`-based shortcut: an `//*[@id="x"]` prefix is shorter but stops
|
|
808
|
+
# pointing at *this* node the moment the document has a duplicate id,
|
|
809
|
+
# which malformed real-world pages routinely do.
|
|
810
|
+
#
|
|
811
|
+
# An element inside a shadow root has no document-level XPath at all
|
|
812
|
+
# (XPath doesn't cross shadow boundaries, and a `/SPAN[1]` computed from
|
|
813
|
+
# the shadow tree would re-find some unrelated light-DOM node). Selenium's
|
|
814
|
+
# driver returns the sentinel string below in that case and Capybara's
|
|
815
|
+
# shared `node #path` spec pins it, so mirror it rather than emit a path
|
|
816
|
+
# that lies.
|
|
702
817
|
GET_PATH_JS = <<~JS
|
|
703
818
|
function() {
|
|
704
819
|
var el = this;
|
|
705
|
-
|
|
820
|
+
if (el.getRootNode && typeof ShadowRoot !== 'undefined' && el.getRootNode() instanceof ShadowRoot) {
|
|
821
|
+
return '(: Shadow DOM element - no XPath :)';
|
|
822
|
+
}
|
|
823
|
+
var steps = [];
|
|
706
824
|
while (el && el.nodeType === Node.ELEMENT_NODE) {
|
|
707
|
-
var
|
|
708
|
-
|
|
709
|
-
|
|
710
|
-
|
|
711
|
-
|
|
712
|
-
} else {
|
|
713
|
-
var sibling = el;
|
|
714
|
-
var nth = 1;
|
|
715
|
-
while (sibling = sibling.previousElementSibling) {
|
|
716
|
-
if (sibling.nodeName.toLowerCase() === el.nodeName.toLowerCase()) nth++;
|
|
717
|
-
}
|
|
718
|
-
if (nth > 1) selector += ':nth-of-type(' + nth + ')';
|
|
825
|
+
var name = el.nodeName.toUpperCase();
|
|
826
|
+
var index = 1;
|
|
827
|
+
var sibling = el;
|
|
828
|
+
while (sibling = sibling.previousElementSibling) {
|
|
829
|
+
if (sibling.nodeName.toUpperCase() === name) index++;
|
|
719
830
|
}
|
|
720
|
-
|
|
721
|
-
el = el.
|
|
831
|
+
steps.unshift(name + '[' + index + ']');
|
|
832
|
+
el = el.parentElement;
|
|
722
833
|
}
|
|
723
|
-
return
|
|
834
|
+
return '/' + steps.join('/');
|
|
724
835
|
}
|
|
725
836
|
JS
|
|
726
837
|
|
|
727
838
|
# Internal wire format, not API — aligned with browser.rb's convention
|
|
728
839
|
# of private_constant for its JS snippets.
|
|
729
|
-
private_constant :CLICK_JS, :TRIGGER_JS, :DROP_JS, :
|
|
840
|
+
private_constant :CLICK_JS, :TRIGGER_JS, :DROP_JS, :CREATE_DROP_INPUT_JS, :SHADOW_ROOT_JS, :VISIBLE_JS,
|
|
841
|
+
:VISIBLE_TEXT_JS,
|
|
730
842
|
:PROPERTY_OR_ATTRIBUTE_JS, :GET_VALUE_JS, :SET_VALUE_JS,
|
|
731
843
|
:IMPLICIT_SUBMIT_JS, :SELECT_OPTION_JS, :UNSELECT_OPTION_JS,
|
|
732
844
|
:SET_CHECKBOX_JS, :EDITABLE_HOST_JS, :DISABLED_JS, :GET_STYLE_JS,
|
|
@@ -20,14 +20,36 @@ module Capybara
|
|
|
20
20
|
# `Capybara::Lightpanda.configure { |c| c.port = 9222 }` when external
|
|
21
21
|
# tooling needs a known address.
|
|
22
22
|
DEFAULT_PORT = 0
|
|
23
|
-
|
|
23
|
+
# Mirrors Lightpanda's own `Viewport.default` (1920x1080) rather than
|
|
24
|
+
# Cuprite's 1024x768. window_size is applied for real now (see below), so
|
|
25
|
+
# a 1024x768 default would silently shrink the viewport of every existing
|
|
26
|
+
# suite and flip `@media` branches under them. Matching the browser's
|
|
27
|
+
# native default keeps `window_size` truthful AND leaves default
|
|
28
|
+
# behavior byte-identical to before it was wired up.
|
|
29
|
+
DEFAULT_WINDOW_SIZE = [1920, 1080].freeze
|
|
24
30
|
|
|
25
|
-
# window_size
|
|
26
|
-
#
|
|
27
|
-
#
|
|
28
|
-
#
|
|
31
|
+
# window_size drives Emulation.setDeviceMetricsOverride via
|
|
32
|
+
# Browser#set_viewport on every create_page. This is a JS-visible
|
|
33
|
+
# viewport only — it sets window.innerWidth/innerHeight and the viewport
|
|
34
|
+
# that `matchMedia` / `@media` evaluate against, so responsive branches
|
|
35
|
+
# resolve at the size you ask for. It is NOT real layout: Lightpanda has
|
|
36
|
+
# no rendering engine, so getBoundingClientRect stays synthetic and
|
|
37
|
+
# nothing reflows. Sizing down will not make an off-viewport element
|
|
38
|
+
# report as obscured.
|
|
39
|
+
# headless is accepted for Cuprite drop-in compatibility but inert —
|
|
40
|
+
# headless is the only mode Lightpanda runs in.
|
|
41
|
+
# save_path: directory for downloaded files (Cuprite parity). nil falls
|
|
42
|
+
# back to Capybara.save_path at create_page time; downloads stay off when
|
|
43
|
+
# both are nil (Browser#create_page only opts in when a path exists).
|
|
44
|
+
# raise_on_unhandled_modal: a JS dialog that opens with no
|
|
45
|
+
# accept_modal/dismiss_modal pre-arm in flight is resolved by Lightpanda's
|
|
46
|
+
# silent default (confirm → cancel, prompt → null, alert → dismissed), so
|
|
47
|
+
# a runaway confirm cancels the action and the spec can still pass. false
|
|
48
|
+
# (default, Cuprite parity) warns on stderr; true raises
|
|
49
|
+
# UnhandledModalError from the action that opened it.
|
|
29
50
|
attr_accessor :host, :port, :timeout, :handshake_timeout, :process_timeout,
|
|
30
|
-
:window_size, :browser_path, :headless, :logger
|
|
51
|
+
:window_size, :browser_path, :headless, :logger, :save_path,
|
|
52
|
+
:raise_on_unhandled_modal
|
|
31
53
|
attr_writer :ws_url
|
|
32
54
|
|
|
33
55
|
def initialize(options = {})
|
|
@@ -36,9 +58,11 @@ module Capybara
|
|
|
36
58
|
@timeout = options.fetch(:timeout, DEFAULT_TIMEOUT)
|
|
37
59
|
@handshake_timeout = options.fetch(:handshake_timeout, DEFAULT_HANDSHAKE_TIMEOUT)
|
|
38
60
|
@process_timeout = options.fetch(:process_timeout, DEFAULT_PROCESS_TIMEOUT)
|
|
39
|
-
@window_size = options.fetch(:window_size, DEFAULT_WINDOW_SIZE)
|
|
61
|
+
@window_size = validate_window_size(options.fetch(:window_size, DEFAULT_WINDOW_SIZE))
|
|
40
62
|
@browser_path = options[:browser_path]
|
|
41
63
|
@headless = options.fetch(:headless, true)
|
|
64
|
+
@save_path = options[:save_path]
|
|
65
|
+
@raise_on_unhandled_modal = options.fetch(:raise_on_unhandled_modal, false)
|
|
42
66
|
@ws_url = options[:ws_url]
|
|
43
67
|
@logger = parse_logger(options[:logger])
|
|
44
68
|
end
|
|
@@ -62,6 +86,8 @@ module Capybara
|
|
|
62
86
|
browser_path: browser_path,
|
|
63
87
|
headless: headless,
|
|
64
88
|
logger: logger,
|
|
89
|
+
save_path: save_path,
|
|
90
|
+
raise_on_unhandled_modal: raise_on_unhandled_modal,
|
|
65
91
|
}
|
|
66
92
|
h[:ws_url] = @ws_url if @ws_url
|
|
67
93
|
h
|
|
@@ -69,6 +95,17 @@ module Capybara
|
|
|
69
95
|
|
|
70
96
|
private
|
|
71
97
|
|
|
98
|
+
# Validated here rather than at apply time so a bad value fails before
|
|
99
|
+
# Browser#initialize spawns a Lightpanda process — raising later would
|
|
100
|
+
# orphan it. Upstream reads a 0 dimension as "keep the current one", so
|
|
101
|
+
# forwarding junk would half-apply a viewport instead of failing.
|
|
102
|
+
def validate_window_size(size)
|
|
103
|
+
width, height = size
|
|
104
|
+
return size if width.is_a?(Integer) && height.is_a?(Integer) && width.positive? && height.positive?
|
|
105
|
+
|
|
106
|
+
raise ArgumentError, "window_size must be [width, height] of positive Integers, got #{size.inspect}"
|
|
107
|
+
end
|
|
108
|
+
|
|
72
109
|
def parse_logger(logger)
|
|
73
110
|
return logger if logger.is_a?(Capybara::Lightpanda::Logger)
|
|
74
111
|
return Capybara::Lightpanda::Logger.new(logger) if logger
|