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,376 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require "base64"
|
|
4
|
+
require "json"
|
|
5
|
+
|
|
6
|
+
module Testsurfer
|
|
7
|
+
# Network: the requests and responses a context's pages make, and
|
|
8
|
+
# routes that answer them. Every request passes vibesurfer's proxy (an
|
|
9
|
+
# intercepting session); while a route is set, the proxy holds each
|
|
10
|
+
# request until a handler (or, with none matching, testsurfer) decides.
|
|
11
|
+
module Net
|
|
12
|
+
module_function
|
|
13
|
+
|
|
14
|
+
def b64(bytes)
|
|
15
|
+
Base64.strict_encode64(bytes.to_s.b)
|
|
16
|
+
end
|
|
17
|
+
|
|
18
|
+
def unb64(s)
|
|
19
|
+
Base64.decode64(s.to_s.strip)
|
|
20
|
+
end
|
|
21
|
+
|
|
22
|
+
def header_lines(h)
|
|
23
|
+
h.map { |k, v| "#{k}: #{v}" }.join("\n")
|
|
24
|
+
end
|
|
25
|
+
|
|
26
|
+
def add_header(headers, line)
|
|
27
|
+
k, v = line.split(": ", 2)
|
|
28
|
+
v ||= ""
|
|
29
|
+
k = k.downcase
|
|
30
|
+
headers[k] = headers.key?(k) ? "#{headers[k]}, #{v}" : v
|
|
31
|
+
end
|
|
32
|
+
|
|
33
|
+
# A response block: `res <id> <status> <url> <reason>` and `h` lines.
|
|
34
|
+
def parse_response_block(lines)
|
|
35
|
+
out = { id: 0, status: 0, url: "", status_text: "", headers: {} }
|
|
36
|
+
lines.each do |line|
|
|
37
|
+
if line.start_with?("res ")
|
|
38
|
+
parts = line[4..].split(/ /, 4)
|
|
39
|
+
out[:id] = parts[0].to_i
|
|
40
|
+
out[:status] = parts[1].to_i
|
|
41
|
+
out[:url] = parts[2].to_s
|
|
42
|
+
out[:status_text] = parts[3].to_s
|
|
43
|
+
elsif line.start_with?("h ")
|
|
44
|
+
add_header(out[:headers], line[2..])
|
|
45
|
+
end
|
|
46
|
+
end
|
|
47
|
+
out
|
|
48
|
+
end
|
|
49
|
+
|
|
50
|
+
def body_line(lines)
|
|
51
|
+
l = lines.find { |x| x.start_with?("body ") }
|
|
52
|
+
l && unb64(l[5..])
|
|
53
|
+
end
|
|
54
|
+
|
|
55
|
+
def field(parts, key)
|
|
56
|
+
parts.each { |p| return p[(key.length + 1)..] if p.start_with?("#{key}=") }
|
|
57
|
+
nil
|
|
58
|
+
end
|
|
59
|
+
|
|
60
|
+
# Split a `vs_net_poll` body into events: hashes with a :type.
|
|
61
|
+
def parse_stream(lines)
|
|
62
|
+
out = []
|
|
63
|
+
lines.each do |line|
|
|
64
|
+
if line.start_with?("h ")
|
|
65
|
+
last = out.last
|
|
66
|
+
add_header(last[:headers], line[2..]) if last && %i[req res].include?(last[:type])
|
|
67
|
+
next
|
|
68
|
+
end
|
|
69
|
+
if line.start_with?("body ")
|
|
70
|
+
last = out.last
|
|
71
|
+
last[:body] = unb64(line[5..]) if last && last[:type] == :req
|
|
72
|
+
next
|
|
73
|
+
end
|
|
74
|
+
parts = line.split(/ /, -1)
|
|
75
|
+
num = ->(i) { parts[i].to_s.match?(/\A\d+\z/) ? parts[i].to_i : 0 }
|
|
76
|
+
text = ->(i) { parts[i].to_s }
|
|
77
|
+
utf = ->(s) { unb64(s).force_encoding(Encoding::UTF_8).scrub }
|
|
78
|
+
ev = case parts.first
|
|
79
|
+
when "req"
|
|
80
|
+
{ type: :req, id: num[1], method: text[2], url: text[3], kind: field(parts, "type") || "other",
|
|
81
|
+
nav: field(parts, "nav") == "1", main_frame: field(parts, "frame") != "sub", headers: {}, body: nil }
|
|
82
|
+
when "res"
|
|
83
|
+
{ type: :res, id: num[1], status: num[2], url: text[3], reason: (parts[4..] || []).join(" "), headers: {} }
|
|
84
|
+
when "fin" then { type: :fin, id: num[1] }
|
|
85
|
+
when "fail" then { type: :fail, id: num[1], error: (parts[2..] || []).join(" ") }
|
|
86
|
+
when "page" then { type: :page, opener: text[1], page: text[2], url: text[3] }
|
|
87
|
+
when "close" then { type: :close, page: text[1] }
|
|
88
|
+
when "crash" then { type: :crash, page: text[1] }
|
|
89
|
+
when "dialog"
|
|
90
|
+
{ type: :dialog, page: text[1], id: num[2], kind: text[3], message: utf[text[4]], default: utf[text[5]] }
|
|
91
|
+
when "ev"
|
|
92
|
+
evs = begin
|
|
93
|
+
JSON.parse(unb64(text[2]))
|
|
94
|
+
rescue JSON::ParserError
|
|
95
|
+
[]
|
|
96
|
+
end
|
|
97
|
+
{ type: :ev, page: text[1], events: evs.is_a?(Array) ? evs : [] }
|
|
98
|
+
when "sw"
|
|
99
|
+
info = begin
|
|
100
|
+
JSON.parse(unb64(text[1]))
|
|
101
|
+
rescue JSON::ParserError
|
|
102
|
+
{}
|
|
103
|
+
end
|
|
104
|
+
{ type: :sw, info: info }
|
|
105
|
+
when "download"
|
|
106
|
+
err = field(parts, "err")
|
|
107
|
+
{ type: :download, page: text[1], id: num[2], filename: utf[text[3]], size: num[4], mime: utf[text[5]],
|
|
108
|
+
url: utf[text[6]], error: err && utf[err] }
|
|
109
|
+
end
|
|
110
|
+
out << ev if ev
|
|
111
|
+
end
|
|
112
|
+
out
|
|
113
|
+
end
|
|
114
|
+
end
|
|
115
|
+
|
|
116
|
+
# A URL matcher for routes and waits: an exact URL, a `**` glob, or a pattern.
|
|
117
|
+
class UrlMatcher
|
|
118
|
+
# `m` against the base URL (via the block) when it is a relative glob or URL.
|
|
119
|
+
attr_reader :source
|
|
120
|
+
|
|
121
|
+
def initialize(m, &resolve)
|
|
122
|
+
@source = m
|
|
123
|
+
m = TextMatch.check!(m)
|
|
124
|
+
if TextMatch.regex?(m)
|
|
125
|
+
@re = TextMatch.to_regexp(m)
|
|
126
|
+
else
|
|
127
|
+
pattern = m.start_with?("*") || m.include?("://") ? m : resolve.call(m)
|
|
128
|
+
if pattern.match?(/[*{]/)
|
|
129
|
+
@re = Regexp.new(Navigation.glob_to_regex(pattern))
|
|
130
|
+
else
|
|
131
|
+
@exact = pattern
|
|
132
|
+
end
|
|
133
|
+
end
|
|
134
|
+
rescue RegexpError => e
|
|
135
|
+
raise UsageError, "bad URL pattern #{m.inspect}: #{e.message}"
|
|
136
|
+
end
|
|
137
|
+
|
|
138
|
+
def match?(url)
|
|
139
|
+
return @exact == url if @exact
|
|
140
|
+
|
|
141
|
+
@re.match?(url)
|
|
142
|
+
end
|
|
143
|
+
end
|
|
144
|
+
|
|
145
|
+
# A request a page made (any kind: documents, scripts, images, fetch, XHR).
|
|
146
|
+
class Request
|
|
147
|
+
attr_reader :id, :url, :method, :headers, :resource_type, :wire, :session_id
|
|
148
|
+
attr_accessor :response, :failure, :redirected_from, :redirected_to
|
|
149
|
+
|
|
150
|
+
def initialize(id:, url:, method:, headers:, post_data:, resource_type:, navigation:, main_frame:, wire:, session_id:)
|
|
151
|
+
@id = id
|
|
152
|
+
@url = url
|
|
153
|
+
@method = method
|
|
154
|
+
@headers = headers
|
|
155
|
+
@post_data = post_data
|
|
156
|
+
@resource_type = resource_type
|
|
157
|
+
@navigation = navigation
|
|
158
|
+
@main_frame = main_frame
|
|
159
|
+
@wire = wire
|
|
160
|
+
@session_id = session_id
|
|
161
|
+
@response = nil
|
|
162
|
+
@failure = nil
|
|
163
|
+
@redirected_from = nil
|
|
164
|
+
@redirected_to = nil
|
|
165
|
+
end
|
|
166
|
+
|
|
167
|
+
def header(name)
|
|
168
|
+
headers[name.to_s.downcase]
|
|
169
|
+
end
|
|
170
|
+
|
|
171
|
+
alias header_value header
|
|
172
|
+
|
|
173
|
+
# The request body, as text.
|
|
174
|
+
def post_data
|
|
175
|
+
@post_data&.dup&.force_encoding(Encoding::UTF_8)
|
|
176
|
+
end
|
|
177
|
+
|
|
178
|
+
def post_data_buffer
|
|
179
|
+
@post_data
|
|
180
|
+
end
|
|
181
|
+
|
|
182
|
+
def post_data_json
|
|
183
|
+
d = post_data
|
|
184
|
+
d && JSON.parse(d)
|
|
185
|
+
end
|
|
186
|
+
|
|
187
|
+
def navigation_request?
|
|
188
|
+
@navigation
|
|
189
|
+
end
|
|
190
|
+
alias is_navigation_request navigation_request?
|
|
191
|
+
|
|
192
|
+
def main_frame?
|
|
193
|
+
@main_frame
|
|
194
|
+
end
|
|
195
|
+
|
|
196
|
+
def inspect
|
|
197
|
+
"#<Testsurfer::Request #{method} #{url}>"
|
|
198
|
+
end
|
|
199
|
+
end
|
|
200
|
+
|
|
201
|
+
# A response to a request.
|
|
202
|
+
class Response
|
|
203
|
+
attr_reader :id, :url, :status, :status_text, :headers, :request
|
|
204
|
+
|
|
205
|
+
def initialize(id:, url:, status:, status_text:, headers:, request:, from_service_worker: false, body: nil)
|
|
206
|
+
@id = id
|
|
207
|
+
@url = url
|
|
208
|
+
@status = status
|
|
209
|
+
@status_text = status_text
|
|
210
|
+
@headers = headers
|
|
211
|
+
@request = request
|
|
212
|
+
@from_service_worker = from_service_worker
|
|
213
|
+
@body = body
|
|
214
|
+
end
|
|
215
|
+
|
|
216
|
+
# Status in the 2xx range.
|
|
217
|
+
def ok
|
|
218
|
+
(200..299).cover?(status)
|
|
219
|
+
end
|
|
220
|
+
alias ok? ok
|
|
221
|
+
|
|
222
|
+
def header(name)
|
|
223
|
+
headers[name.to_s.downcase]
|
|
224
|
+
end
|
|
225
|
+
alias header_value header
|
|
226
|
+
|
|
227
|
+
# A service worker built or took the response.
|
|
228
|
+
def from_service_worker?
|
|
229
|
+
@from_service_worker
|
|
230
|
+
end
|
|
231
|
+
alias from_service_worker from_service_worker?
|
|
232
|
+
|
|
233
|
+
# The body as the proxy delivered it (binary String).
|
|
234
|
+
def body
|
|
235
|
+
return @body if @body
|
|
236
|
+
|
|
237
|
+
req = Wire::Request.new("vs_net_body").arg(id).flag_value("session", request.session_id)
|
|
238
|
+
Net.body_line(request.wire.call(req).body) || "".b
|
|
239
|
+
end
|
|
240
|
+
|
|
241
|
+
def text
|
|
242
|
+
body.dup.force_encoding(Encoding::UTF_8)
|
|
243
|
+
end
|
|
244
|
+
|
|
245
|
+
def json
|
|
246
|
+
JSON.parse(text)
|
|
247
|
+
rescue JSON::ParserError => e
|
|
248
|
+
raise UsageError, "response.json: #{e.message}"
|
|
249
|
+
end
|
|
250
|
+
|
|
251
|
+
def inspect
|
|
252
|
+
"#<Testsurfer::Response #{status} #{url}>"
|
|
253
|
+
end
|
|
254
|
+
end
|
|
255
|
+
|
|
256
|
+
# A held request a route handler decides: fulfill, continue, abort, or fall back.
|
|
257
|
+
class Route
|
|
258
|
+
attr_reader :request
|
|
259
|
+
|
|
260
|
+
def initialize(request, fallthrough = {})
|
|
261
|
+
@request = request
|
|
262
|
+
@state = :pending
|
|
263
|
+
@fallthrough = fallthrough
|
|
264
|
+
@lock = Mutex.new
|
|
265
|
+
end
|
|
266
|
+
|
|
267
|
+
def settled
|
|
268
|
+
@lock.synchronize { @state }
|
|
269
|
+
end
|
|
270
|
+
|
|
271
|
+
def fallthrough
|
|
272
|
+
@lock.synchronize { @fallthrough }
|
|
273
|
+
end
|
|
274
|
+
|
|
275
|
+
# Answer with a response; the request never reaches the network.
|
|
276
|
+
# status: (200), headers: {}, content_type:, body: (String), json:
|
|
277
|
+
# (any value, as JSON), path: (a file), response: (from `fetch`, to
|
|
278
|
+
# pass on or change).
|
|
279
|
+
def fulfill(status: nil, headers: nil, content_type: nil, body: nil, json: nil, path: nil, response: nil)
|
|
280
|
+
take!
|
|
281
|
+
h = {}
|
|
282
|
+
b = "".b
|
|
283
|
+
if response
|
|
284
|
+
h = response.headers.reject { |k, _| %w[content-length content-encoding transfer-encoding].include?(k) }
|
|
285
|
+
status ||= response.status
|
|
286
|
+
b = response.body
|
|
287
|
+
end
|
|
288
|
+
(headers || {}).each { |k, v| h[k.to_s.downcase] = v.to_s }
|
|
289
|
+
if !json.nil?
|
|
290
|
+
b = JSON.generate(json)
|
|
291
|
+
content_type ||= "application/json"
|
|
292
|
+
elsif path
|
|
293
|
+
b = File.binread(path)
|
|
294
|
+
elsif body
|
|
295
|
+
b = body
|
|
296
|
+
end
|
|
297
|
+
h["content-type"] = content_type if content_type
|
|
298
|
+
req = call("vs_net_fulfill").flag_value("status", status || 200)
|
|
299
|
+
req.flag_value("headers", Net.header_lines(h)) unless h.empty?
|
|
300
|
+
req.flag_value("body-b64", Net.b64(b)) unless b.empty?
|
|
301
|
+
request.wire.call(req)
|
|
302
|
+
nil
|
|
303
|
+
end
|
|
304
|
+
|
|
305
|
+
# Send the request on, with overrides applied: url:, method:, headers:, post_data:.
|
|
306
|
+
def continue(url: nil, method: nil, headers: nil, post_data: nil)
|
|
307
|
+
take!
|
|
308
|
+
o = merged(url: url, method: method, headers: headers, post_data: post_data)
|
|
309
|
+
request.wire.call(with_overrides(call("vs_net_continue"), o))
|
|
310
|
+
nil
|
|
311
|
+
end
|
|
312
|
+
alias continue_ continue
|
|
313
|
+
|
|
314
|
+
# Fail the request (`failed` by default, or e.g. `connectionrefused`).
|
|
315
|
+
def abort(error_code = nil)
|
|
316
|
+
take!
|
|
317
|
+
request.failure = error_code || "failed"
|
|
318
|
+
request.wire.call(call("vs_net_abort").flag_value("error", error_code || "failed"))
|
|
319
|
+
nil
|
|
320
|
+
end
|
|
321
|
+
|
|
322
|
+
# Let the next matching handler decide, or the network; overrides carry over.
|
|
323
|
+
def fallback(url: nil, method: nil, headers: nil, post_data: nil)
|
|
324
|
+
@lock.synchronize do
|
|
325
|
+
raise UsageError, "Route is already handled!" unless @state == :pending
|
|
326
|
+
|
|
327
|
+
@state = :fell_back
|
|
328
|
+
end
|
|
329
|
+
m = merged(url: url, method: method, headers: headers, post_data: post_data)
|
|
330
|
+
@lock.synchronize { @fallthrough = m }
|
|
331
|
+
nil
|
|
332
|
+
end
|
|
333
|
+
|
|
334
|
+
# Perform the request (with overrides) and return its response,
|
|
335
|
+
# without delivering it: the handler then fulfills with it, or changes it.
|
|
336
|
+
def fetch(url: nil, method: nil, headers: nil, post_data: nil)
|
|
337
|
+
o = merged(url: url, method: method, headers: headers, post_data: post_data)
|
|
338
|
+
resp = request.wire.call(with_overrides(call("vs_net_fetch"), o))
|
|
339
|
+
p = Net.parse_response_block(resp.body)
|
|
340
|
+
Response.new(id: request.id, url: p[:url], status: p[:status], status_text: p[:status_text], headers: p[:headers],
|
|
341
|
+
request: request, body: Net.body_line(resp.body) || "".b)
|
|
342
|
+
end
|
|
343
|
+
|
|
344
|
+
private
|
|
345
|
+
|
|
346
|
+
def take!
|
|
347
|
+
@lock.synchronize do
|
|
348
|
+
raise UsageError, "Route is already handled!" unless @state == :pending
|
|
349
|
+
|
|
350
|
+
@state = :decided
|
|
351
|
+
end
|
|
352
|
+
end
|
|
353
|
+
|
|
354
|
+
def call(primitive)
|
|
355
|
+
Wire::Request.new(primitive).arg(request.id).flag_value("session", request.session_id)
|
|
356
|
+
end
|
|
357
|
+
|
|
358
|
+
def merged(url:, method:, headers:, post_data:)
|
|
359
|
+
base = fallthrough
|
|
360
|
+
{
|
|
361
|
+
url: url || base[:url],
|
|
362
|
+
method: method || base[:method],
|
|
363
|
+
headers: headers ? headers.to_h { |k, v| [k.to_s.downcase, v.to_s] } : base[:headers],
|
|
364
|
+
post_data: post_data || base[:post_data]
|
|
365
|
+
}
|
|
366
|
+
end
|
|
367
|
+
|
|
368
|
+
def with_overrides(req, o)
|
|
369
|
+
req.flag_value("url", o[:url]) if o[:url]
|
|
370
|
+
req.flag_value("method", o[:method]) if o[:method]
|
|
371
|
+
req.flag_value("headers", Net.header_lines(o[:headers])) if o[:headers]
|
|
372
|
+
req.flag_value("body-b64", Net.b64(o[:post_data])) if o[:post_data]
|
|
373
|
+
req
|
|
374
|
+
end
|
|
375
|
+
end
|
|
376
|
+
end
|