delta-qa-vision 0.0.1.beta2
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/NOTICE +4 -0
- data/README.md +259 -0
- data/lib/delta_qa/vision/delta-qa-capture.js +78 -0
- data/lib/delta_qa/vision/errors.rb +111 -0
- data/lib/delta_qa/vision/ferrum.rb +217 -0
- data/lib/delta_qa/vision/frameworks.rb +57 -0
- data/lib/delta_qa/vision/gate.rb +140 -0
- data/lib/delta_qa/vision/playwright.rb +166 -0
- data/lib/delta_qa/vision/relay.rb +668 -0
- data/lib/delta_qa/vision/resize_wait.rb +34 -0
- data/lib/delta_qa/vision/selenium.rb +237 -0
- data/lib/delta_qa/vision.rb +109 -0
- metadata +59 -0
|
@@ -0,0 +1,140 @@
|
|
|
1
|
+
require "json"
|
|
2
|
+
require "net/http"
|
|
3
|
+
require "uri"
|
|
4
|
+
require_relative "errors"
|
|
5
|
+
|
|
6
|
+
module DeltaQa
|
|
7
|
+
module Vision
|
|
8
|
+
# The Vision gate of one Delta-QA server: one method per route of openapi/v1.yaml the SDK calls — the gate of
|
|
9
|
+
# every Vision SDK. The key leaves only in the Authorization header, taken out of every text an error shows.
|
|
10
|
+
class Gate
|
|
11
|
+
API_PATH = "/api/vision/v1".freeze
|
|
12
|
+
API_KEY_VARIABLE = "DELTAQA_API_KEY".freeze
|
|
13
|
+
SERVER_URL_VARIABLE = "DELTAQA_SERVER_URL".freeze
|
|
14
|
+
SAAS_SERVER_URL = "https://api.delta-qa.com".freeze
|
|
15
|
+
KEY_SHOWN_AS = "[API key]".freeze
|
|
16
|
+
HEADER_SAFE = /\A[\x21-\x7e]+\z/
|
|
17
|
+
|
|
18
|
+
def self.api_key_of(env)
|
|
19
|
+
variable = API_KEY_VARIABLE
|
|
20
|
+
raise ConfigurationError.new("No Delta-QA API key: set #{variable} to an API key of your workspace — Settings → Workspace → API keys in Delta-QA", variable) unless env.key?(variable)
|
|
21
|
+
key = env[variable]
|
|
22
|
+
raise ConfigurationError.new("#{variable} is set but empty: give it an API key of your workspace", variable) if key.empty?
|
|
23
|
+
unless HEADER_SAFE.match?(key)
|
|
24
|
+
raise ConfigurationError.new("#{variable} holds a character an HTTP header cannot carry (a space, a line break…): give it the key alone",
|
|
25
|
+
variable)
|
|
26
|
+
end
|
|
27
|
+
key
|
|
28
|
+
end
|
|
29
|
+
|
|
30
|
+
def self.server_url_of(env)
|
|
31
|
+
value = env[SERVER_URL_VARIABLE]
|
|
32
|
+
return SAAS_SERVER_URL if value.nil?
|
|
33
|
+
parsed = begin
|
|
34
|
+
URI.parse(value)
|
|
35
|
+
rescue URI::InvalidURIError
|
|
36
|
+
nil # refused just below, named
|
|
37
|
+
end
|
|
38
|
+
unless parsed.is_a?(URI::HTTP) && parsed.host && !parsed.host.empty?
|
|
39
|
+
raise ConfigurationError.new("#{SERVER_URL_VARIABLE} is not an http(s) address: \"#{value}\" — give the root of the Delta-QA server, " \
|
|
40
|
+
"e.g. #{SAAS_SERVER_URL}", SERVER_URL_VARIABLE)
|
|
41
|
+
end
|
|
42
|
+
value.sub(%r{/+\z}, "")
|
|
43
|
+
end
|
|
44
|
+
|
|
45
|
+
def self.from_environment(env)
|
|
46
|
+
new(server_url_of(env), api_key_of(env))
|
|
47
|
+
end
|
|
48
|
+
|
|
49
|
+
attr_reader :server_url
|
|
50
|
+
|
|
51
|
+
def initialize(server_url, api_key)
|
|
52
|
+
@server_url = server_url
|
|
53
|
+
@api_key = api_key
|
|
54
|
+
end
|
|
55
|
+
|
|
56
|
+
def inspect
|
|
57
|
+
"#<DeltaQa::Vision::Gate #{@server_url}>"
|
|
58
|
+
end
|
|
59
|
+
alias to_s inspect
|
|
60
|
+
|
|
61
|
+
def open_launch(request)
|
|
62
|
+
json(*call("POST", "/launches", JSON.generate(request), "application/json"))
|
|
63
|
+
end
|
|
64
|
+
|
|
65
|
+
def declare_resources(sha256)
|
|
66
|
+
json(*call("POST", "/resources/missing", JSON.generate({ "sha256" => sha256 }), "application/json"))["sha256"]
|
|
67
|
+
end
|
|
68
|
+
|
|
69
|
+
def send_resource(sha256, content_type, data, url)
|
|
70
|
+
call("PUT", "/resources/#{escape(sha256)}", data, content_type, subject: "the resource #{url}")
|
|
71
|
+
nil
|
|
72
|
+
end
|
|
73
|
+
|
|
74
|
+
def add_check(launch_id, request)
|
|
75
|
+
json(*call("POST", "/launches/#{escape(launch_id)}/checks", JSON.generate(request), "application/json"))
|
|
76
|
+
end
|
|
77
|
+
|
|
78
|
+
def close_launch(launch_id)
|
|
79
|
+
call("POST", "/launches/#{escape(launch_id)}/close", nil, nil)
|
|
80
|
+
nil
|
|
81
|
+
end
|
|
82
|
+
|
|
83
|
+
def read_launch(launch_id, timeout_s)
|
|
84
|
+
json(*call("GET", "/launches/#{escape(launch_id)}", nil, nil, timeout_s: timeout_s))
|
|
85
|
+
end
|
|
86
|
+
|
|
87
|
+
def refusal(method, path, status, text, subject)
|
|
88
|
+
about = subject.nil? ? "" : " (#{subject})"
|
|
89
|
+
prefix = "Delta-QA refused #{method} #{path}#{about}: HTTP #{status}"
|
|
90
|
+
body = begin
|
|
91
|
+
JSON.parse(text)
|
|
92
|
+
rescue JSON::ParserError
|
|
93
|
+
nil
|
|
94
|
+
end
|
|
95
|
+
return ApiError.new(shown("#{prefix} — #{text}"), status) unless body.is_a?(Hash) && body["code"].is_a?(String)
|
|
96
|
+
params = body["params"].is_a?(Hash) ? body["params"] : {}
|
|
97
|
+
details = params.empty? ? "" : " #{JSON.generate(params)}"
|
|
98
|
+
meaning = body["message"] ? " — #{body["message"]}" : ""
|
|
99
|
+
ticket = body["ticket"] ? " (ticket #{body["ticket"]})" : ""
|
|
100
|
+
ApiError.new(shown("#{prefix} #{body["code"]}#{meaning}#{details}#{ticket}"), status, body["code"], body["ticket"], params)
|
|
101
|
+
end
|
|
102
|
+
|
|
103
|
+
private
|
|
104
|
+
|
|
105
|
+
def escape(segment)
|
|
106
|
+
URI.encode_www_form_component(segment).gsub("+", "%20")
|
|
107
|
+
end
|
|
108
|
+
|
|
109
|
+
def call(method, path, body, content_type, subject: nil, timeout_s: 300)
|
|
110
|
+
uri = URI.parse("#{@server_url}#{API_PATH}#{path}")
|
|
111
|
+
request = Net::HTTPGenericRequest.new(method, !body.nil?, true, uri.request_uri,
|
|
112
|
+
{ "Accept" => "application/json", "Authorization" => "Bearer #{@api_key}" })
|
|
113
|
+
request["Content-Type"] = content_type unless content_type.nil?
|
|
114
|
+
request.body = body unless body.nil?
|
|
115
|
+
begin
|
|
116
|
+
response = Net::HTTP.start(uri.host, uri.port, use_ssl: uri.scheme == "https", open_timeout: timeout_s, read_timeout: timeout_s) do |http|
|
|
117
|
+
http.request(request)
|
|
118
|
+
end
|
|
119
|
+
rescue SystemCallError, IOError, SocketError, Timeout::Error, OpenSSL::SSL::SSLError => e
|
|
120
|
+
raise UnreachableError, shown("Delta-QA unreachable: #{method} #{uri} — #{e.message}")
|
|
121
|
+
end
|
|
122
|
+
status = response.code.to_i
|
|
123
|
+
text = (response.body || "").dup.force_encoding("UTF-8")
|
|
124
|
+
raise refusal(method, uri.path, status, text, subject) if status < 200 || status >= 300
|
|
125
|
+
[method, uri.path, status, text]
|
|
126
|
+
end
|
|
127
|
+
|
|
128
|
+
def json(method, path, status, text)
|
|
129
|
+
JSON.parse(text)
|
|
130
|
+
rescue JSON::ParserError
|
|
131
|
+
# The parsing error quotes the body, which may echo the key: the message shows the body, the key taken out — never its cause.
|
|
132
|
+
raise ApiError.new(shown("Delta-QA answered #{method} #{path} with HTTP #{status} and a body that is not JSON — #{text}"), status), cause: nil
|
|
133
|
+
end
|
|
134
|
+
|
|
135
|
+
def shown(text)
|
|
136
|
+
text.gsub(@api_key, KEY_SHOWN_AS)
|
|
137
|
+
end
|
|
138
|
+
end
|
|
139
|
+
end
|
|
140
|
+
end
|
|
@@ -0,0 +1,166 @@
|
|
|
1
|
+
require_relative "relay"
|
|
2
|
+
require_relative "resize_wait"
|
|
3
|
+
|
|
4
|
+
module DeltaQa
|
|
5
|
+
module Vision
|
|
6
|
+
# The page of a Playwright test (playwright-ruby-client) as the relay takes it — the steps of the Playwright adapter
|
|
7
|
+
# of the JavaScript SDK: Playwright evaluates in each frame, a frame of another origin included, and opens a session
|
|
8
|
+
# of Chromium's protocol on the page (new_cdp_session), detached once the relay is done with it.
|
|
9
|
+
class PlaywrightPageDriver
|
|
10
|
+
# The Chromium family, as a user agent names it — for a persistent context, which has no browser object.
|
|
11
|
+
CHROMIUM_AGENT = %r{\b(Chrome|Chromium|HeadlessChrome|Edg)/}
|
|
12
|
+
# What Playwright says when the page changed under a call.
|
|
13
|
+
CHANGING = ["Execution context was destroyed", "Frame was detached", "Frame has been detached", "Object doesn't exist"].freeze
|
|
14
|
+
|
|
15
|
+
# A session of Chromium's protocol the adapter opened on the page: detached when the relay closes it.
|
|
16
|
+
class Session
|
|
17
|
+
def initialize(session)
|
|
18
|
+
@session = session
|
|
19
|
+
end
|
|
20
|
+
|
|
21
|
+
def send(method, params)
|
|
22
|
+
@session.send_message(method, params: params)
|
|
23
|
+
end
|
|
24
|
+
|
|
25
|
+
def close
|
|
26
|
+
@session.detach
|
|
27
|
+
end
|
|
28
|
+
end
|
|
29
|
+
|
|
30
|
+
def initialize(page)
|
|
31
|
+
@page = page
|
|
32
|
+
end
|
|
33
|
+
|
|
34
|
+
def framework
|
|
35
|
+
"Playwright"
|
|
36
|
+
end
|
|
37
|
+
|
|
38
|
+
def browser
|
|
39
|
+
browser = @page.context.browser
|
|
40
|
+
if browser.nil?
|
|
41
|
+
# A persistent context has no browser object: its pages tell their engine by their user agent.
|
|
42
|
+
agent = @page.evaluate("() => navigator.userAgent")
|
|
43
|
+
return [CHROMIUM_AGENT.match?(agent) && !agent.include?("Firefox/"), agent]
|
|
44
|
+
end
|
|
45
|
+
type = browser.browser_type.name
|
|
46
|
+
[type == "chromium", "#{type} #{browser.version}"]
|
|
47
|
+
end
|
|
48
|
+
|
|
49
|
+
def url
|
|
50
|
+
@page.url
|
|
51
|
+
end
|
|
52
|
+
|
|
53
|
+
def title
|
|
54
|
+
@page.title
|
|
55
|
+
end
|
|
56
|
+
|
|
57
|
+
def documents
|
|
58
|
+
top = @page.main_frame
|
|
59
|
+
documents = [Relay::PageDocument.new(top.url, 0, top)]
|
|
60
|
+
@page.frames.each do |frame|
|
|
61
|
+
# No document arrived in this frame: its reading would never answer, the page closed included.
|
|
62
|
+
next if frame.equal?(top) || frame.url.empty?
|
|
63
|
+
documents << Relay::PageDocument.new(frame.url, depth_of(frame), frame)
|
|
64
|
+
end
|
|
65
|
+
documents
|
|
66
|
+
end
|
|
67
|
+
|
|
68
|
+
def evaluate(document, function, args)
|
|
69
|
+
document.handle.evaluate(source(function), arg: args)
|
|
70
|
+
end
|
|
71
|
+
|
|
72
|
+
def host_of(document)
|
|
73
|
+
document.handle.frame_element
|
|
74
|
+
end
|
|
75
|
+
|
|
76
|
+
def evaluate_on(element, function, args)
|
|
77
|
+
element.evaluate(source(function), arg: args)
|
|
78
|
+
end
|
|
79
|
+
|
|
80
|
+
def release(element)
|
|
81
|
+
element.dispose
|
|
82
|
+
end
|
|
83
|
+
|
|
84
|
+
def document_received(document)
|
|
85
|
+
frame = document.handle
|
|
86
|
+
# Without a timeout of its own, the wait is bounded by the navigation timeout of the test.
|
|
87
|
+
frame.wait_for_load_state(state: "domcontentloaded")
|
|
88
|
+
rescue ::Playwright::TimeoutError => e
|
|
89
|
+
raise DocumentStillReceivingError.new(frame.url, e.message)
|
|
90
|
+
end
|
|
91
|
+
|
|
92
|
+
def open_cdp
|
|
93
|
+
Session.new(@page.context.new_cdp_session(@page))
|
|
94
|
+
end
|
|
95
|
+
|
|
96
|
+
def viewport
|
|
97
|
+
size = @page.viewport_size
|
|
98
|
+
return Relay::Size.new(size[:width].to_i, size[:height].to_i) unless size.nil?
|
|
99
|
+
# A context without a viewport: the page is as large as its window.
|
|
100
|
+
width, height = @page.evaluate("() => [window.innerWidth, window.innerHeight]")
|
|
101
|
+
Relay::Size.new(width.to_i, height.to_i)
|
|
102
|
+
end
|
|
103
|
+
|
|
104
|
+
def resize(size)
|
|
105
|
+
return if viewport == size
|
|
106
|
+
@page.evaluate("() => { #{ResizeWait::LISTEN} }")
|
|
107
|
+
begin
|
|
108
|
+
@page.set_viewport_size({ width: size.width, height: size.height })
|
|
109
|
+
begin
|
|
110
|
+
@page.wait_for_function("() => #{ResizeWait::FIRED}", timeout: ResizeWait::TIMEOUT_MS).dispose
|
|
111
|
+
rescue ::Playwright::TimeoutError
|
|
112
|
+
raise ResizeWait.timed_out(size)
|
|
113
|
+
end
|
|
114
|
+
ensure
|
|
115
|
+
@page.evaluate("() => { #{ResizeWait::FORGET} }")
|
|
116
|
+
end
|
|
117
|
+
end
|
|
118
|
+
|
|
119
|
+
def zone_selector_sets(zone, kind, function)
|
|
120
|
+
case zone
|
|
121
|
+
when String
|
|
122
|
+
of_locator(@page.locator(zone), "selector \"#{zone}\"", function)
|
|
123
|
+
when ::Playwright::Locator
|
|
124
|
+
raise ArgumentError, "The zone to #{kind} #{zone} is a locator of another page: designate the zones on the page of the test" unless zone.page.equal?(@page)
|
|
125
|
+
of_locator(zone, zone.to_s, function)
|
|
126
|
+
when ::Playwright::ElementHandle
|
|
127
|
+
described = "element #{zone}"
|
|
128
|
+
frame = zone.owner_frame
|
|
129
|
+
return [described, []] if frame.nil?
|
|
130
|
+
raise ArgumentError, "The zone to #{kind} #{described} is an element of another page: designate the zones on the page of the test" unless frame.page.equal?(@page)
|
|
131
|
+
connected = frame.evaluate(source(Relay.capture("connected")), arg: [[zone]])
|
|
132
|
+
return [described, []] unless connected[0] == true
|
|
133
|
+
[described, frame.evaluate(source(function), arg: [[zone]])]
|
|
134
|
+
else
|
|
135
|
+
raise TypeError, "A zone of a Playwright test is a CSS selector, a Locator or an ElementHandle, got #{zone.inspect}"
|
|
136
|
+
end
|
|
137
|
+
end
|
|
138
|
+
|
|
139
|
+
def page_changing?(error)
|
|
140
|
+
error.is_a?(::Playwright::Error) && CHANGING.any? { |marker| error.message.to_s.include?(marker) }
|
|
141
|
+
end
|
|
142
|
+
|
|
143
|
+
private
|
|
144
|
+
|
|
145
|
+
# evaluate_all calls the function with the elements first: they become the one argument of the function.
|
|
146
|
+
def of_locator(locator, described, function)
|
|
147
|
+
[described, locator.evaluate_all("function (elements) { return (#{source(function)})([elements]); }")]
|
|
148
|
+
end
|
|
149
|
+
|
|
150
|
+
# A function of the page as Playwright sends it: its source, which carries the capture script.
|
|
151
|
+
def source(function)
|
|
152
|
+
"function (#{function.params.join(", ")}) {\n#{function.body}\n}"
|
|
153
|
+
end
|
|
154
|
+
|
|
155
|
+
def depth_of(frame)
|
|
156
|
+
hops = 0
|
|
157
|
+
parent = frame.parent_frame
|
|
158
|
+
until parent.nil?
|
|
159
|
+
hops += 1
|
|
160
|
+
parent = parent.parent_frame
|
|
161
|
+
end
|
|
162
|
+
hops
|
|
163
|
+
end
|
|
164
|
+
end
|
|
165
|
+
end
|
|
166
|
+
end
|