testsurfer 0.5.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 +7 -0
- data/LICENSE +202 -0
- data/README.md +38 -0
- data/lib/testsurfer/api.rb +195 -0
- data/lib/testsurfer/browser.rb +167 -0
- data/lib/testsurfer/capybara/driver.rb +537 -0
- data/lib/testsurfer/capybara/node.rb +574 -0
- data/lib/testsurfer/capybara.rb +12 -0
- data/lib/testsurfer/clock.rb +114 -0
- data/lib/testsurfer/context.rb +299 -0
- data/lib/testsurfer/cookies.rb +136 -0
- data/lib/testsurfer/daemon.rb +176 -0
- data/lib/testsurfer/emulation.rb +87 -0
- data/lib/testsurfer/errors.rb +70 -0
- data/lib/testsurfer/events.rb +581 -0
- data/lib/testsurfer/expect.rb +684 -0
- data/lib/testsurfer/frame.rb +350 -0
- data/lib/testsurfer/hooks.rb +367 -0
- data/lib/testsurfer/input.rb +160 -0
- data/lib/testsurfer/locator.rb +503 -0
- data/lib/testsurfer/media.rb +453 -0
- data/lib/testsurfer/net.rb +376 -0
- data/lib/testsurfer/page.rb +655 -0
- data/lib/testsurfer/page_events.rb +217 -0
- data/lib/testsurfer/query.rb +342 -0
- data/lib/testsurfer/rspec.rb +98 -0
- data/lib/testsurfer/selector.rb +357 -0
- data/lib/testsurfer/serialize.rb +122 -0
- data/lib/testsurfer/snapshot.rb +300 -0
- data/lib/testsurfer/storage.rb +176 -0
- data/lib/testsurfer/sync.rb +75 -0
- data/lib/testsurfer/version.rb +8 -0
- data/lib/testsurfer/wire.rb +295 -0
- data/lib/testsurfer.rb +50 -0
- data/testsurfer.gemspec +25 -0
- metadata +97 -0
|
@@ -0,0 +1,160 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Testsurfer
|
|
4
|
+
# The keyboard of a page. Keys go to the focused element.
|
|
5
|
+
class Keyboard
|
|
6
|
+
def initialize(page)
|
|
7
|
+
@page = page
|
|
8
|
+
end
|
|
9
|
+
|
|
10
|
+
# Press a key or chord on the focused element: `Enter`, `Shift+Tab`, `ControlOrMeta+a`.
|
|
11
|
+
def press(key, delay: nil) # rubocop:disable Lint/UnusedMethodArgument
|
|
12
|
+
@page.engine_call("E.key(#{Serialize.js_string(key)})")
|
|
13
|
+
nil
|
|
14
|
+
end
|
|
15
|
+
|
|
16
|
+
def down(key)
|
|
17
|
+
@page.engine_call("E.key(#{Serialize.js_string(key)},'down')")
|
|
18
|
+
nil
|
|
19
|
+
end
|
|
20
|
+
|
|
21
|
+
def up(key)
|
|
22
|
+
@page.engine_call("E.key(#{Serialize.js_string(key)},'up')")
|
|
23
|
+
nil
|
|
24
|
+
end
|
|
25
|
+
|
|
26
|
+
# Type `text` with trusted per-character key events.
|
|
27
|
+
def type(text, delay: nil) # rubocop:disable Lint/UnusedMethodArgument
|
|
28
|
+
@page.type_trusted(text.to_s)
|
|
29
|
+
end
|
|
30
|
+
alias type_text type
|
|
31
|
+
|
|
32
|
+
# Insert `text` as an input event, without key events.
|
|
33
|
+
def insert_text(text)
|
|
34
|
+
@page.engine_call("E.insertText(#{Serialize.js_string(text)})")
|
|
35
|
+
nil
|
|
36
|
+
end
|
|
37
|
+
end
|
|
38
|
+
|
|
39
|
+
# The mouse of a page. Coordinates are CSS pixels in the viewport.
|
|
40
|
+
# Pointer input is native and trusted (vibesurfer's cursor primitives).
|
|
41
|
+
class Mouse
|
|
42
|
+
def initialize(page)
|
|
43
|
+
@page = page
|
|
44
|
+
end
|
|
45
|
+
|
|
46
|
+
# Move the pointer. While a button is down, the move is part of the
|
|
47
|
+
# drag `up` makes; `steps` has no effect (vibesurfer draws a path).
|
|
48
|
+
def move(x, y, steps: nil) # rubocop:disable Lint/UnusedMethodArgument
|
|
49
|
+
@page.update_pointer { |p| p[:at] = [x.to_f, y.to_f] }
|
|
50
|
+
return nil if @page.pointer[:pressed_at]
|
|
51
|
+
|
|
52
|
+
@page.mouse_move(x.to_f, y.to_f)
|
|
53
|
+
end
|
|
54
|
+
alias move_to move
|
|
55
|
+
|
|
56
|
+
# Click at (x, y). button: "left" | "right" | "middle", click_count:.
|
|
57
|
+
def click(x, y, button: "left", click_count: 1, delay: nil) # rubocop:disable Lint/UnusedMethodArgument
|
|
58
|
+
@page.update_pointer { |p| p[:at] = [x.to_f, y.to_f] }
|
|
59
|
+
@page.click_at(x.to_f, y.to_f, button.to_s, click_count)
|
|
60
|
+
end
|
|
61
|
+
|
|
62
|
+
def dblclick(x, y, button: "left")
|
|
63
|
+
click(x, y, button: button, click_count: 2)
|
|
64
|
+
end
|
|
65
|
+
|
|
66
|
+
# Press the button where the pointer is.
|
|
67
|
+
def down(button: "left", click_count: 1) # rubocop:disable Lint/UnusedMethodArgument
|
|
68
|
+
@page.update_pointer { |p| p[:pressed_at] = p[:at] }
|
|
69
|
+
nil
|
|
70
|
+
end
|
|
71
|
+
|
|
72
|
+
# Release it: a click where it was pressed, or a drag to where the pointer is now.
|
|
73
|
+
def up(button: "left", click_count: 1) # rubocop:disable Lint/UnusedMethodArgument
|
|
74
|
+
s = @page.pointer
|
|
75
|
+
@page.update_pointer { |p| p[:pressed_at] = nil }
|
|
76
|
+
from = s[:pressed_at]
|
|
77
|
+
return nil if from.nil?
|
|
78
|
+
|
|
79
|
+
if from == s[:at]
|
|
80
|
+
@page.click_at(s[:at][0], s[:at][1], "left", 1)
|
|
81
|
+
else
|
|
82
|
+
@page.mouse_drag(from, s[:at])
|
|
83
|
+
end
|
|
84
|
+
end
|
|
85
|
+
|
|
86
|
+
# Scroll by (delta_x, delta_y) where the pointer is, with native wheel input.
|
|
87
|
+
def wheel(delta_x, delta_y)
|
|
88
|
+
x, y = @page.pointer[:at]
|
|
89
|
+
id = @page.ensure_open
|
|
90
|
+
req =Wire::Request.new("vs_wheel").arg(id).arg(Testsurfer.fmt_num(x)).arg(Testsurfer.fmt_num(y))
|
|
91
|
+
.arg(Testsurfer.fmt_num(delta_x.to_f)).arg(Testsurfer.fmt_num(delta_y.to_f))
|
|
92
|
+
.flag("css").flag_value("session", @page.session_id).flag_value("mode", @page.input_mode)
|
|
93
|
+
@page.wire.call(req)
|
|
94
|
+
# Let the page see the scroll before the next call reads it.
|
|
95
|
+
begin
|
|
96
|
+
@page.evaluate("() => new Promise((done) => requestAnimationFrame(() => requestAnimationFrame(() => done(0))))")
|
|
97
|
+
rescue Error
|
|
98
|
+
nil
|
|
99
|
+
end
|
|
100
|
+
nil
|
|
101
|
+
end
|
|
102
|
+
end
|
|
103
|
+
|
|
104
|
+
class Page
|
|
105
|
+
# The page's keyboard.
|
|
106
|
+
def keyboard
|
|
107
|
+
Keyboard.new(self)
|
|
108
|
+
end
|
|
109
|
+
|
|
110
|
+
# The page's mouse.
|
|
111
|
+
def mouse
|
|
112
|
+
Mouse.new(self)
|
|
113
|
+
end
|
|
114
|
+
|
|
115
|
+
# Move the pointer to (x, y) in CSS pixels with trusted input.
|
|
116
|
+
def mouse_move(x, y)
|
|
117
|
+
id = ensure_open
|
|
118
|
+
req = Wire::Request.new("vs_move_to").arg(id).arg(Testsurfer.fmt_coord(x)).arg(Testsurfer.fmt_coord(y))
|
|
119
|
+
.flag("css").flag_value("session", session_id).flag_value("mode", input_mode)
|
|
120
|
+
wire.call(req)
|
|
121
|
+
nil
|
|
122
|
+
end
|
|
123
|
+
|
|
124
|
+
# Press at `from`, move along a path, release at `to`.
|
|
125
|
+
def mouse_drag(from, to)
|
|
126
|
+
id = ensure_open
|
|
127
|
+
t = token
|
|
128
|
+
req = Wire::Request.new("vs_drag").arg(id)
|
|
129
|
+
.arg(Testsurfer.fmt_coord(from[0])).arg(Testsurfer.fmt_coord(from[1]))
|
|
130
|
+
.arg(Testsurfer.fmt_coord(to[0])).arg(Testsurfer.fmt_coord(to[1]))
|
|
131
|
+
.flag("css").flag_value("session", session_id)
|
|
132
|
+
.flag_value("token", t.empty? ? "0" : t).flag_value("mode", input_mode)
|
|
133
|
+
resp = wire.call(req)
|
|
134
|
+
set_token(resp.token)
|
|
135
|
+
nil
|
|
136
|
+
end
|
|
137
|
+
|
|
138
|
+
# Type `text` into the focused element with trusted key events.
|
|
139
|
+
def type_trusted(text)
|
|
140
|
+
id = ensure_open
|
|
141
|
+
req = Wire::Request.new("vs_type").arg(id).arg(text).flag_value("session", session_id).flag_value("mode", input_mode)
|
|
142
|
+
wire.call(req)
|
|
143
|
+
nil
|
|
144
|
+
end
|
|
145
|
+
|
|
146
|
+
# A trusted native click at (x, y) in CSS pixels.
|
|
147
|
+
def click_at(x, y, button = "left", count = 1)
|
|
148
|
+
id = ensure_open
|
|
149
|
+
t = token
|
|
150
|
+
req = Wire::Request.new("vs_click_at").arg(id).arg(Testsurfer.fmt_coord(x)).arg(Testsurfer.fmt_coord(y))
|
|
151
|
+
.flag("css").flag_value("session", session_id)
|
|
152
|
+
.flag_value("token", t.empty? ? "0" : t).flag_value("mode", input_mode)
|
|
153
|
+
req.flag_value("button", button) if %w[right middle].include?(button.to_s)
|
|
154
|
+
req.flag_value("count", [count, 3].min) if count > 1
|
|
155
|
+
resp = wire.call(req)
|
|
156
|
+
set_token(resp.token)
|
|
157
|
+
nil
|
|
158
|
+
end
|
|
159
|
+
end
|
|
160
|
+
end
|
|
@@ -0,0 +1,503 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require "base64"
|
|
4
|
+
require "json"
|
|
5
|
+
require "tmpdir"
|
|
6
|
+
require "fileutils"
|
|
7
|
+
|
|
8
|
+
module Testsurfer
|
|
9
|
+
# Files for `set_input_files`, `drop` and `FileChooser#set_files`: paths
|
|
10
|
+
# (String or Pathname), or hashes { name:, mime_type:, buffer: }.
|
|
11
|
+
module InputFiles
|
|
12
|
+
MIME = {
|
|
13
|
+
"txt" => "text/plain", "html" => "text/html", "htm" => "text/html", "css" => "text/css",
|
|
14
|
+
"js" => "text/javascript", "json" => "application/json", "png" => "image/png",
|
|
15
|
+
"jpg" => "image/jpeg", "jpeg" => "image/jpeg", "gif" => "image/gif", "svg" => "image/svg+xml",
|
|
16
|
+
"pdf" => "application/pdf", "webp" => "image/webp", "csv" => "text/csv", "zip" => "application/zip"
|
|
17
|
+
}.freeze
|
|
18
|
+
|
|
19
|
+
module_function
|
|
20
|
+
|
|
21
|
+
# MIME type by file extension, as Playwright guesses it for a path.
|
|
22
|
+
def mime_of(path)
|
|
23
|
+
MIME.fetch(File.extname(path.to_s).delete_prefix(".").downcase, "application/octet-stream")
|
|
24
|
+
end
|
|
25
|
+
|
|
26
|
+
def list(files)
|
|
27
|
+
files.is_a?(Array) ? files : [files]
|
|
28
|
+
end
|
|
29
|
+
|
|
30
|
+
def buffer?(f)
|
|
31
|
+
f.is_a?(Hash)
|
|
32
|
+
end
|
|
33
|
+
|
|
34
|
+
# Each file as { name:, mime:, given_mime:, bytes:, b64:, path: }.
|
|
35
|
+
def read(files, api)
|
|
36
|
+
list(files).map do |f|
|
|
37
|
+
if buffer?(f)
|
|
38
|
+
h = f.transform_keys(&:to_sym)
|
|
39
|
+
mime = (h[:mime_type] || h[:mimeType]).to_s
|
|
40
|
+
bytes = h[:buffer].to_s.b
|
|
41
|
+
{ name: h[:name].to_s, mime: mime, given_mime: mime, bytes: bytes, b64: Base64.strict_encode64(bytes), path: nil }
|
|
42
|
+
else
|
|
43
|
+
p = f.to_s
|
|
44
|
+
bytes = begin
|
|
45
|
+
File.binread(p)
|
|
46
|
+
rescue SystemCallError => e
|
|
47
|
+
raise UsageError, "#{api}: #{p}: #{e.message}"
|
|
48
|
+
end
|
|
49
|
+
{ name: File.basename(p), mime: mime_of(p), given_mime: "", bytes: bytes, b64: Base64.strict_encode64(bytes), path: p }
|
|
50
|
+
end
|
|
51
|
+
end
|
|
52
|
+
end
|
|
53
|
+
end
|
|
54
|
+
|
|
55
|
+
# A set of elements a chain of queries finds. Lazy: each action and
|
|
56
|
+
# read resolves the chain again in the page, so locators survive
|
|
57
|
+
# re-renders and navigations. One-element operations are strict: more
|
|
58
|
+
# than one match fails unless `first`, `last` or `nth` picks one.
|
|
59
|
+
# Actions wait, in the daemon, until the element is attached, visible,
|
|
60
|
+
# enabled and not covered, then act with trusted native input.
|
|
61
|
+
class Locator
|
|
62
|
+
attr_reader :page, :chain
|
|
63
|
+
|
|
64
|
+
def initialize(page, chain, description = nil)
|
|
65
|
+
@page = page
|
|
66
|
+
@chain = chain
|
|
67
|
+
@description = description
|
|
68
|
+
end
|
|
69
|
+
|
|
70
|
+
# The locator in Playwright's notation, as error messages print it.
|
|
71
|
+
def to_s
|
|
72
|
+
@description || chain.describe
|
|
73
|
+
end
|
|
74
|
+
|
|
75
|
+
def inspect
|
|
76
|
+
"#<Testsurfer::Locator #{self}>"
|
|
77
|
+
end
|
|
78
|
+
|
|
79
|
+
# The same locator, printed as `description` in messages.
|
|
80
|
+
def describe(description)
|
|
81
|
+
Locator.new(page, chain, description)
|
|
82
|
+
end
|
|
83
|
+
|
|
84
|
+
# --- narrowing ------------------------------------------------------
|
|
85
|
+
|
|
86
|
+
def first
|
|
87
|
+
nth(0)
|
|
88
|
+
end
|
|
89
|
+
|
|
90
|
+
def last
|
|
91
|
+
nth(-1)
|
|
92
|
+
end
|
|
93
|
+
|
|
94
|
+
# The match at `index`; negative counts from the end.
|
|
95
|
+
def nth(index)
|
|
96
|
+
with(chain.with_last { |s| s.index = index })
|
|
97
|
+
end
|
|
98
|
+
|
|
99
|
+
# Only the matches that pass: has_text:, has_not_text:, has: (a
|
|
100
|
+
# locator), has_not:, visible:.
|
|
101
|
+
def filter(**opts)
|
|
102
|
+
f = Filter.new(**opts)
|
|
103
|
+
with(chain.with_last { |s| s.filters << f })
|
|
104
|
+
end
|
|
105
|
+
|
|
106
|
+
# Matches of both this and `other`.
|
|
107
|
+
def and(other)
|
|
108
|
+
with(Chain.of(Query.new(:and, l: chain, r: other.chain)))
|
|
109
|
+
end
|
|
110
|
+
|
|
111
|
+
# Matches of this or `other`.
|
|
112
|
+
def or(other)
|
|
113
|
+
with(Chain.of(Query.new(:or, l: chain, r: other.chain)))
|
|
114
|
+
end
|
|
115
|
+
|
|
116
|
+
# Elements inside this one's matches that `selector` (a string or a
|
|
117
|
+
# Locator) finds, optionally filtered.
|
|
118
|
+
def locator(selector, **filter)
|
|
119
|
+
l = selector.is_a?(Locator) ? with(chain.then_all(selector.chain)) : with(chain.then_all(Selector.steps(selector)))
|
|
120
|
+
filter.empty? ? l : l.filter(**filter)
|
|
121
|
+
end
|
|
122
|
+
|
|
123
|
+
# The document of the iframe this locator matches.
|
|
124
|
+
def content_frame
|
|
125
|
+
with(chain.then(Step.new(Query.new(:frame))))
|
|
126
|
+
end
|
|
127
|
+
|
|
128
|
+
# Locators inside the iframe this locator's `selector` matches.
|
|
129
|
+
def frame_locator(selector)
|
|
130
|
+
FrameLocator.new(locator(selector))
|
|
131
|
+
end
|
|
132
|
+
|
|
133
|
+
def get_by_role(role, **options)
|
|
134
|
+
step(Query.new(:role, role: role.to_s, options: ByRole.new(**options)))
|
|
135
|
+
end
|
|
136
|
+
|
|
137
|
+
def get_by_text(text, exact: false)
|
|
138
|
+
step(Query.new(:text, text: TextMatch.check!(text), exact: exact, light: false, legacy: false))
|
|
139
|
+
end
|
|
140
|
+
|
|
141
|
+
def get_by_label(text, exact: false)
|
|
142
|
+
step(Query.new(:label, text: TextMatch.check!(text), exact: exact))
|
|
143
|
+
end
|
|
144
|
+
|
|
145
|
+
def get_by_placeholder(text, exact: false)
|
|
146
|
+
step(Query.new(:attr, method: "getByPlaceholder", attr: "placeholder", value: TextMatch.check!(text), exact: exact))
|
|
147
|
+
end
|
|
148
|
+
|
|
149
|
+
def get_by_alt_text(text, exact: false)
|
|
150
|
+
step(Query.new(:attr, method: "getByAltText", attr: "alt", value: TextMatch.check!(text), exact: exact))
|
|
151
|
+
end
|
|
152
|
+
|
|
153
|
+
def get_by_title(text, exact: false)
|
|
154
|
+
step(Query.new(:attr, method: "getByTitle", attr: "title", value: TextMatch.check!(text), exact: exact))
|
|
155
|
+
end
|
|
156
|
+
|
|
157
|
+
def get_by_test_id(id)
|
|
158
|
+
step(Query.new(:attr, method: "getByTestId", attr: page.test_id_attribute, value: TextMatch.check!(id), exact: true))
|
|
159
|
+
end
|
|
160
|
+
|
|
161
|
+
# --- engine calls ---------------------------------------------------
|
|
162
|
+
|
|
163
|
+
def steps
|
|
164
|
+
chain.wire_checked
|
|
165
|
+
rescue Query::InvalidSelector => e
|
|
166
|
+
raise JavaScriptError, e.message
|
|
167
|
+
end
|
|
168
|
+
|
|
169
|
+
def request(op, args)
|
|
170
|
+
{ steps: steps, op: op, args: args, many: false, first: false }
|
|
171
|
+
end
|
|
172
|
+
|
|
173
|
+
def strict_error(api, r)
|
|
174
|
+
list = (r["strict"] || []).each_with_index.map { |p, i| " #{i + 1}) #{p}" }
|
|
175
|
+
StrictModeError.new("#{api}: Error: strict mode violation: #{self} resolved to #{r['n'] || 0} elements:\n#{list.join("\n")}\n")
|
|
176
|
+
end
|
|
177
|
+
|
|
178
|
+
# --- actions --------------------------------------------------------
|
|
179
|
+
|
|
180
|
+
# Click the element with trusted native input: vibesurfer waits for
|
|
181
|
+
# it, clicks, lets focus settle, and clicks again if a re-render
|
|
182
|
+
# swallowed the click. button: "left" | "right" | "middle",
|
|
183
|
+
# click_count:, modifiers: ["Shift", "ControlOrMeta", ...],
|
|
184
|
+
# position: { x:, y: }, force:, trial:, timeout: (ms).
|
|
185
|
+
def click(button: "left", click_count: 1, modifiers: [], position: nil, force: false, trial: false, timeout: nil, delay: nil, no_wait_after: nil) # rubocop:disable Lint/UnusedMethodArgument
|
|
186
|
+
pos = position && { "x" => (position[:x] || position["x"]).to_f, "y" => (position[:y] || position["y"]).to_f }
|
|
187
|
+
point = { "position" => pos, "force" => force }
|
|
188
|
+
budget = budget(timeout)
|
|
189
|
+
if trial
|
|
190
|
+
wait_one("locator.click", "clickPoint", point, timeout)
|
|
191
|
+
return nil
|
|
192
|
+
end
|
|
193
|
+
c = { "mode" => page.input_mode }
|
|
194
|
+
c["button"] = button.to_s if %w[right middle].include?(button.to_s)
|
|
195
|
+
c["count"] = [click_count, 3].min if click_count > 1
|
|
196
|
+
c["modifiers"] = modifiers.map { |m| Locator.modifier_wire(m) }.join(",") unless modifiers.empty?
|
|
197
|
+
req = request("clickPoint", point)
|
|
198
|
+
begin
|
|
199
|
+
r = page.locate_waiting(req, budget, click: c)
|
|
200
|
+
settled("locator.click", budget, r)
|
|
201
|
+
rescue Error => e
|
|
202
|
+
# A popup's own Close button: the click closed the page under it.
|
|
203
|
+
return nil if page.closed_by_itself?(e)
|
|
204
|
+
|
|
205
|
+
raise
|
|
206
|
+
end
|
|
207
|
+
nil
|
|
208
|
+
end
|
|
209
|
+
|
|
210
|
+
def dblclick(**opts)
|
|
211
|
+
click(**opts, click_count: 2)
|
|
212
|
+
end
|
|
213
|
+
|
|
214
|
+
# Move the pointer over the element with trusted input.
|
|
215
|
+
def hover(timeout: nil, force: false)
|
|
216
|
+
pt = wait_one("locator.hover", "clickPoint", { "enabled" => false, "force" => force }, timeout)
|
|
217
|
+
page.mouse_move(pt["x"].to_f, pt["y"].to_f)
|
|
218
|
+
end
|
|
219
|
+
|
|
220
|
+
# Replace the field's value (inputs, textareas, contenteditable).
|
|
221
|
+
def fill(value, timeout: nil, force: nil) # rubocop:disable Lint/UnusedMethodArgument
|
|
222
|
+
wait_one("locator.fill", "fill", { "value" => value.to_s }, timeout)
|
|
223
|
+
nil
|
|
224
|
+
end
|
|
225
|
+
|
|
226
|
+
def clear(timeout: nil)
|
|
227
|
+
fill("", timeout: timeout)
|
|
228
|
+
end
|
|
229
|
+
|
|
230
|
+
# Focus the element, then type with trusted per-character key
|
|
231
|
+
# events, for editors that ignore `fill`.
|
|
232
|
+
def press_sequentially(text, delay: nil, timeout: nil) # rubocop:disable Lint/UnusedMethodArgument
|
|
233
|
+
wait_one("locator.pressSequentially", "focus", {}, timeout)
|
|
234
|
+
page.type_trusted(text.to_s)
|
|
235
|
+
end
|
|
236
|
+
alias type press_sequentially
|
|
237
|
+
|
|
238
|
+
# Focus the element and press a key or chord: `Enter`, `Control+A`.
|
|
239
|
+
def press(key, timeout: nil)
|
|
240
|
+
wait_one("locator.press", "press", { "key" => key }, timeout)
|
|
241
|
+
nil
|
|
242
|
+
end
|
|
243
|
+
|
|
244
|
+
def focus(timeout: nil)
|
|
245
|
+
wait_one("locator.focus", "focus", {}, timeout)
|
|
246
|
+
nil
|
|
247
|
+
end
|
|
248
|
+
|
|
249
|
+
def blur(timeout: nil)
|
|
250
|
+
wait_one("locator.blur", "blur", {}, timeout)
|
|
251
|
+
nil
|
|
252
|
+
end
|
|
253
|
+
|
|
254
|
+
# Check or uncheck a checkbox or radio by clicking it.
|
|
255
|
+
def set_checked(checked, timeout: nil, force: false)
|
|
256
|
+
state = wait_one("locator.setChecked", "checked", {}, timeout)
|
|
257
|
+
return nil if state == checked
|
|
258
|
+
|
|
259
|
+
click(timeout: timeout, force: force)
|
|
260
|
+
# The click's events may still be on their way to the page
|
|
261
|
+
# (WebKitGTK sends each once the last is acknowledged), so give
|
|
262
|
+
# the state a moment to change before calling the click a failure.
|
|
263
|
+
deadline = Testsurfer.now + 1
|
|
264
|
+
loop do
|
|
265
|
+
found, v = peek("locator.setChecked", "checked", {})
|
|
266
|
+
return nil if !found || v == checked
|
|
267
|
+
raise JavaScriptError, "Clicking the checkbox did not change its state" if Testsurfer.now >= deadline
|
|
268
|
+
|
|
269
|
+
sleep 0.01
|
|
270
|
+
end
|
|
271
|
+
end
|
|
272
|
+
|
|
273
|
+
def check(**opts)
|
|
274
|
+
set_checked(true, **opts)
|
|
275
|
+
end
|
|
276
|
+
|
|
277
|
+
def uncheck(**opts)
|
|
278
|
+
set_checked(false, **opts)
|
|
279
|
+
end
|
|
280
|
+
|
|
281
|
+
# Select options: by value or label (a string or a list of them), or
|
|
282
|
+
# with value:, label:, index: (each one or a list). Returns the
|
|
283
|
+
# selected values. An empty list deselects all.
|
|
284
|
+
def select_option(values = nil, value: nil, label: nil, index: nil, element: nil, timeout: nil, force: nil) # rubocop:disable Lint/UnusedMethodArgument
|
|
285
|
+
wanted = []
|
|
286
|
+
Array(values).each do |v|
|
|
287
|
+
wanted << (v.is_a?(Hash) ? v.transform_keys(&:to_s) : { "valueOrLabel" => v.to_s })
|
|
288
|
+
end
|
|
289
|
+
Array(value).each { |v| wanted << { "value" => v.to_s } }
|
|
290
|
+
Array(label).each { |v| wanted << { "label" => v.to_s } }
|
|
291
|
+
Array(index).each { |v| wanted << { "index" => Integer(v) } }
|
|
292
|
+
r = wait_one("locator.selectOption", "selectOption", { "values" => wanted }, timeout)
|
|
293
|
+
(r.is_a?(Hash) ? r["values"] : nil) || []
|
|
294
|
+
end
|
|
295
|
+
|
|
296
|
+
# Set the files of an `<input type=file>`: paths, or hashes
|
|
297
|
+
# { name:, mime_type:, buffer: }. An empty list clears them.
|
|
298
|
+
def set_input_files(files, timeout: nil)
|
|
299
|
+
payload = InputFiles.read(files, "locator.setInputFiles").map { |f| { "name" => f[:name], "mimeType" => f[:mime], "b64" => f[:b64] } }
|
|
300
|
+
wait_one("locator.setInputFiles", "setFiles", { "files" => payload }, timeout)
|
|
301
|
+
nil
|
|
302
|
+
end
|
|
303
|
+
|
|
304
|
+
# Select the element's text (inputs and contenteditable).
|
|
305
|
+
def select_text(timeout: nil)
|
|
306
|
+
wait_one("locator.selectText", "selectText", {}, timeout)
|
|
307
|
+
nil
|
|
308
|
+
end
|
|
309
|
+
|
|
310
|
+
# Fire a DOM event on the element, e.g. `click` or `input`.
|
|
311
|
+
def dispatch_event(type, event_init = nil, timeout: nil)
|
|
312
|
+
wait_one("locator.dispatchEvent", "dispatch", { "type" => type, "init" => event_init || {} }, timeout)
|
|
313
|
+
nil
|
|
314
|
+
end
|
|
315
|
+
|
|
316
|
+
def scroll_into_view_if_needed(timeout: nil)
|
|
317
|
+
wait_one("locator.scrollIntoViewIfNeeded", "scroll", {}, timeout)
|
|
318
|
+
nil
|
|
319
|
+
end
|
|
320
|
+
|
|
321
|
+
# Drag this element onto `target` with trusted pointer input.
|
|
322
|
+
def drag_to(target, **)
|
|
323
|
+
from = bounding_box || raise(TimeoutError, "locator.dragTo: #{self} is not rendered")
|
|
324
|
+
to = target.bounding_box || raise(TimeoutError, "locator.dragTo: #{target} is not rendered")
|
|
325
|
+
page.mouse_drag([from["x"] + from["width"] / 2.0, from["y"] + from["height"] / 2.0],
|
|
326
|
+
[to["x"] + to["width"] / 2.0, to["y"] + to["height"] / 2.0])
|
|
327
|
+
end
|
|
328
|
+
|
|
329
|
+
# Wait until the matches are "attached", "detached", "visible" (default) or "hidden".
|
|
330
|
+
def wait_for(state: "visible", timeout: nil)
|
|
331
|
+
b = budget(timeout)
|
|
332
|
+
req = request("waitState", { "state" => state.to_s })
|
|
333
|
+
req[:many] = true
|
|
334
|
+
r = page.locate_waiting(req, b)
|
|
335
|
+
raise strict_error("locator.waitFor", r) if r["k"] == "strict"
|
|
336
|
+
return nil if r["k"] == "ok"
|
|
337
|
+
|
|
338
|
+
raise TimeoutError, "locator.waitFor: Timeout #{b}ms exceeded.\nCall log:\n - waiting for #{self} to be #{state}"
|
|
339
|
+
end
|
|
340
|
+
|
|
341
|
+
# --- reading --------------------------------------------------------
|
|
342
|
+
|
|
343
|
+
def text_content(timeout: nil)
|
|
344
|
+
wait_one("locator.textContent", "text", {}, timeout)
|
|
345
|
+
end
|
|
346
|
+
|
|
347
|
+
def inner_text(timeout: nil)
|
|
348
|
+
wait_one("locator.innerText", "innerText", {}, timeout)
|
|
349
|
+
end
|
|
350
|
+
|
|
351
|
+
def inner_html(timeout: nil)
|
|
352
|
+
wait_one("locator.innerHTML", "innerHTML", {}, timeout)
|
|
353
|
+
end
|
|
354
|
+
|
|
355
|
+
def get_attribute(name, timeout: nil)
|
|
356
|
+
wait_one("locator.getAttribute", "attr", { "name" => name }, timeout)
|
|
357
|
+
end
|
|
358
|
+
|
|
359
|
+
def input_value(timeout: nil)
|
|
360
|
+
wait_one("locator.inputValue", "value", {}, timeout)
|
|
361
|
+
end
|
|
362
|
+
|
|
363
|
+
def checked?(timeout: nil)
|
|
364
|
+
wait_one("locator.isChecked", "checked", {}, timeout)
|
|
365
|
+
end
|
|
366
|
+
alias is_checked checked?
|
|
367
|
+
|
|
368
|
+
def disabled?(timeout: nil)
|
|
369
|
+
wait_one("locator.isDisabled", "disabled", {}, timeout)
|
|
370
|
+
end
|
|
371
|
+
alias is_disabled disabled?
|
|
372
|
+
|
|
373
|
+
def enabled?(timeout: nil)
|
|
374
|
+
!disabled?(timeout: timeout)
|
|
375
|
+
end
|
|
376
|
+
alias is_enabled enabled?
|
|
377
|
+
|
|
378
|
+
def editable?(timeout: nil)
|
|
379
|
+
wait_one("locator.isEditable", "editable", {}, timeout)
|
|
380
|
+
end
|
|
381
|
+
alias is_editable editable?
|
|
382
|
+
|
|
383
|
+
def focused?(timeout: nil)
|
|
384
|
+
wait_one("locator.isFocused", "focused", {}, timeout)
|
|
385
|
+
end
|
|
386
|
+
alias is_focused focused?
|
|
387
|
+
|
|
388
|
+
# Whether the element is visible right now (no waiting).
|
|
389
|
+
def visible?
|
|
390
|
+
found, v = peek("locator.isVisible", "visible", {})
|
|
391
|
+
found && v == true
|
|
392
|
+
end
|
|
393
|
+
alias is_visible visible?
|
|
394
|
+
|
|
395
|
+
def hidden?
|
|
396
|
+
!visible?
|
|
397
|
+
end
|
|
398
|
+
alias is_hidden hidden?
|
|
399
|
+
|
|
400
|
+
# The number of current matches (no waiting).
|
|
401
|
+
def count
|
|
402
|
+
req = request("count", {})
|
|
403
|
+
req[:many] = true
|
|
404
|
+
r = page.locate(req)
|
|
405
|
+
(r["n"] || 0).to_i
|
|
406
|
+
rescue ContextDestroyedError
|
|
407
|
+
# A navigation under the query: nothing matched in that document.
|
|
408
|
+
0
|
|
409
|
+
end
|
|
410
|
+
|
|
411
|
+
# One locator per current match, in document order (no waiting).
|
|
412
|
+
def all
|
|
413
|
+
(0...count).map { |i| nth(i) }
|
|
414
|
+
end
|
|
415
|
+
|
|
416
|
+
def all_text_contents
|
|
417
|
+
many("texts", {}) || []
|
|
418
|
+
end
|
|
419
|
+
|
|
420
|
+
def all_inner_texts
|
|
421
|
+
many("innerTexts", {}) || []
|
|
422
|
+
end
|
|
423
|
+
|
|
424
|
+
# The element's box { "x", "y", "width", "height" }, or nil when it is not rendered.
|
|
425
|
+
def bounding_box(timeout: nil)
|
|
426
|
+
wait_one("locator.boundingBox", "box", {}, timeout)
|
|
427
|
+
end
|
|
428
|
+
|
|
429
|
+
# Call `function(element, arg)` in the page and return its result.
|
|
430
|
+
def evaluate(function, arg: nil, timeout: nil)
|
|
431
|
+
wait_one("locator.evaluate", "eval", { "arg" => Serialize.encode(arg) }, timeout, function)
|
|
432
|
+
end
|
|
433
|
+
|
|
434
|
+
# Call `function(elements, arg)` with every match and return its result.
|
|
435
|
+
def evaluate_all(function, arg: nil)
|
|
436
|
+
many("evalAll", { "arg" => Serialize.encode(arg) }, function)
|
|
437
|
+
end
|
|
438
|
+
|
|
439
|
+
# A handle to the element (Playwright's `elementHandle`), held in the page.
|
|
440
|
+
def element_handle(timeout: nil)
|
|
441
|
+
id = wait_one("locator.elementHandle", "adopt", {}, timeout)
|
|
442
|
+
JSHandle.new(page, id, "element")
|
|
443
|
+
end
|
|
444
|
+
|
|
445
|
+
# Handles to every current match.
|
|
446
|
+
def element_handles
|
|
447
|
+
(many("adoptAll", {}) || []).map { |id| JSHandle.new(page, id, "element") }
|
|
448
|
+
end
|
|
449
|
+
|
|
450
|
+
def self.modifier_wire(m)
|
|
451
|
+
case m.to_s
|
|
452
|
+
when "ControlOrMeta" then RbConfig::CONFIG["host_os"].match?(/darwin/) ? "meta" : "ctrl"
|
|
453
|
+
when "Control" then "ctrl"
|
|
454
|
+
else m.to_s.downcase
|
|
455
|
+
end
|
|
456
|
+
end
|
|
457
|
+
|
|
458
|
+
# Wait for one element and run `op` on it, as the daemon judges it ready.
|
|
459
|
+
def wait_one(api, op, args, timeout, func = nil)
|
|
460
|
+
b = budget(timeout)
|
|
461
|
+
r = page.locate_waiting(request(op, args), b, func: func)
|
|
462
|
+
settled(api, b, r)
|
|
463
|
+
end
|
|
464
|
+
|
|
465
|
+
# One-element op, no waiting: [found, value].
|
|
466
|
+
def peek(api, op, args)
|
|
467
|
+
r = page.locate(request(op, args))
|
|
468
|
+
raise strict_error(api, r) if r["k"] == "strict"
|
|
469
|
+
|
|
470
|
+
r["k"] == "none" ? [false, nil] : [true, r["v"]]
|
|
471
|
+
end
|
|
472
|
+
|
|
473
|
+
private
|
|
474
|
+
|
|
475
|
+
def with(c)
|
|
476
|
+
Locator.new(page, c)
|
|
477
|
+
end
|
|
478
|
+
|
|
479
|
+
def step(q)
|
|
480
|
+
with(chain.then(Step.new(q)))
|
|
481
|
+
end
|
|
482
|
+
|
|
483
|
+
def budget(t)
|
|
484
|
+
t || page.timeouts.action
|
|
485
|
+
end
|
|
486
|
+
|
|
487
|
+
# The value of a waited-on reply, or the error Playwright gives for it.
|
|
488
|
+
def settled(api, budget, r)
|
|
489
|
+
raise strict_error(api, r) if r["k"] == "strict"
|
|
490
|
+
return r["v"] if r["k"] == "ok"
|
|
491
|
+
|
|
492
|
+
why = r["why"] ? "\n - #{r['why']}" : ""
|
|
493
|
+
raise TimeoutError, "#{api}: Timeout #{budget}ms exceeded.\nCall log:\n - waiting for #{self}#{why}"
|
|
494
|
+
end
|
|
495
|
+
|
|
496
|
+
# An op over all current matches.
|
|
497
|
+
def many(op, args, func = nil)
|
|
498
|
+
req = request(op, args)
|
|
499
|
+
req[:many] = true
|
|
500
|
+
page.locate(req, func: func)["v"]
|
|
501
|
+
end
|
|
502
|
+
end
|
|
503
|
+
end
|