diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index a2fa0ed..ad0ce34 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -38,7 +38,7 @@ jobs: restore-keys: v1/${{ runner.os }}/ruby-${{ matrix.ruby }}/ - uses: actions/setup-node@v3 with: - node-version: 14 + node-version: 16 - name: Get yarn cache directory path id: yarn-cache-dir-path run: echo "::set-output name=dir::$(yarn cache dir)" diff --git a/lib/percy.rb b/lib/percy.rb index b5444af..9cd14fe 100644 --- a/lib/percy.rb +++ b/lib/percy.rb @@ -31,6 +31,9 @@ def self.snapshot(driver, name, options = {}) unless response.body.to_json['success'] raise StandardError, data['error'] end + + body = JSON.parse(response.body) + body['data'] rescue StandardError => e log("Could not take DOM snapshot '#{name}'") @@ -90,7 +93,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 diff --git a/spec/lib/percy/percy_spec.rb b/spec/lib/percy/percy_spec.rb index df905b9..8324ed5 100644 --- a/spec/lib/percy/percy_spec.rb +++ b/spec/lib/percy/percy_spec.rb @@ -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( @@ -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', {sync: true}) + + 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: + "I am a pageSnapshot me\n", + 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