Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added support for sync CLI #14

Merged
merged 7 commits into from
Feb 1, 2024
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion lib/percy.rb
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@ def self.snapshot(driver, name, options = {})
unless response.body.to_json['success']
raise StandardError, data['error']
end

response.body.to_json['data']
chinmay-browserstack marked this conversation as resolved.
Show resolved Hide resolved
rescue StandardError => e
log("Could not take DOM snapshot '#{name}'")

Expand Down Expand Up @@ -90,7 +92,11 @@ def self.fetch(url, data = nil)
uri = URI("#{PERCY_SERVER_ADDRESS}/#{url}")

response = if data
Net::HTTP.post(uri, data.to_json)
http = Net::HTTP.new(uri.host, uri.port)
http.read_timeout = 600 # seconds
request = Net::HTTP::Post.new(uri.path)
request.body = data.to_json
http.request(request)
else
Net::HTTP.get_response(uri)
end
Expand Down
37 changes: 36 additions & 1 deletion spec/lib/percy/percy_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@
.to_return(status: 200, body: '{"success": "true" }', headers: {})

visit 'index.html'
Percy.snapshot(page, 'Name', widths: [944])
data = Percy.snapshot(page, 'Name', widths: [944])

expect(WebMock).to have_requested(:post, "#{Percy::PERCY_SERVER_ADDRESS}/percy/snapshot")
.with(
Expand All @@ -104,6 +104,41 @@
widths: [944],
}.to_json,
).once

expect(data).to eq(nil)
end

it 'sends snapshots for sync' do
stub_request(:get, "#{Percy::PERCY_SERVER_ADDRESS}/percy/healthcheck")
.to_return(status: 200, body: '', headers: {'x-percy-core-version': '1.0.0'})

stub_request(:get, "#{Percy::PERCY_SERVER_ADDRESS}/percy/dom.js")
.to_return(
status: 200,
body: 'window.PercyDOM = { serialize: () => document.documentElement.outerHTML };',
headers: {},
)

stub_request(:post, 'http://localhost:5338/percy/snapshot')
.to_return(status: 200, body: '{"success": "true", "data": "sync_data" }', headers: {})

visit 'index.html'
data = Percy.snapshot(page, 'Name', widths: [944])

expect(WebMock).to have_requested(:post, "#{Percy::PERCY_SERVER_ADDRESS}/percy/snapshot")
.with(
body: {
name: 'Name',
url: 'http://127.0.0.1:3003/index.html',
dom_snapshot:
"<html><head><title>I am a page</title></head><body>Snapshot me\n</body></html>",
client_info: "percy-selenium-ruby/#{Percy::VERSION}",
environment_info: "selenium/#{Selenium::WebDriver::VERSION} ruby/#{RUBY_VERSION}",
widths: [944],
}.to_json,
).once

expect(data).to eq('sync_data')
end
end
end
Expand Down
Loading