From f150f903fad1ff9a55a7d33813484ef222759f41 Mon Sep 17 00:00:00 2001 From: prklm10 Date: Thu, 25 Jan 2024 11:03:56 +0530 Subject: [PATCH 1/7] adding timeout of 600 sec for sync --- lib/percy.rb | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/lib/percy.rb b/lib/percy.rb index b5444af..339a2dc 100644 --- a/lib/percy.rb +++ b/lib/percy.rb @@ -31,6 +31,7 @@ def self.snapshot(driver, name, options = {}) unless response.body.to_json['success'] raise StandardError, data['error'] end + response.body.to_json['data'] rescue StandardError => e log("Could not take DOM snapshot '#{name}'") @@ -90,7 +91,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(url.path) + request.body = data.to_json + http.request(request) else Net::HTTP.get_response(uri) end From 54a08d2639c9ebf1d7c96cb094612847a01863b1 Mon Sep 17 00:00:00 2001 From: prklm10 Date: Thu, 25 Jan 2024 11:07:54 +0530 Subject: [PATCH 2/7] rubocop fix --- lib/percy.rb | 1 + 1 file changed, 1 insertion(+) diff --git a/lib/percy.rb b/lib/percy.rb index 339a2dc..bc93fcd 100644 --- a/lib/percy.rb +++ b/lib/percy.rb @@ -31,6 +31,7 @@ def self.snapshot(driver, name, options = {}) unless response.body.to_json['success'] raise StandardError, data['error'] end + response.body.to_json['data'] rescue StandardError => e log("Could not take DOM snapshot '#{name}'") From 5a7650ddc1cf1afa2f81a872538ee4e4328861ff Mon Sep 17 00:00:00 2001 From: prklm10 Date: Thu, 25 Jan 2024 18:05:39 +0530 Subject: [PATCH 3/7] addressing comment --- lib/percy.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/percy.rb b/lib/percy.rb index bc93fcd..413deb6 100644 --- a/lib/percy.rb +++ b/lib/percy.rb @@ -94,7 +94,7 @@ def self.fetch(url, data = nil) response = if data http = Net::HTTP.new(uri.host, uri.port) http.read_timeout = 600 # seconds - request = Net::HTTP::Post.new(url.path) + request = Net::HTTP::Post.new(uri.path) request.body = data.to_json http.request(request) else From 0f10681b7e0c97df0ca84d39096bc02cb17d40a3 Mon Sep 17 00:00:00 2001 From: prklm10 Date: Thu, 25 Jan 2024 18:12:49 +0530 Subject: [PATCH 4/7] adding tests --- spec/lib/percy/percy_spec.rb | 37 +++++++++++++++++++++++++++++++++++- 1 file changed, 36 insertions(+), 1 deletion(-) diff --git a/spec/lib/percy/percy_spec.rb b/spec/lib/percy/percy_spec.rb index df905b9..f5f3803 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', 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: + "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 From a78921c9d8b6ffdb25025ca8973c985de55af80c Mon Sep 17 00:00:00 2001 From: Chinmay Maheshwari Date: Thu, 1 Feb 2024 17:30:21 +0530 Subject: [PATCH 5/7] Fix issue for sync cli --- lib/percy.rb | 3 ++- spec/lib/percy/percy_spec.rb | 2 +- 2 files changed, 3 insertions(+), 2 deletions(-) diff --git a/lib/percy.rb b/lib/percy.rb index 413deb6..9cd14fe 100644 --- a/lib/percy.rb +++ b/lib/percy.rb @@ -32,7 +32,8 @@ def self.snapshot(driver, name, options = {}) raise StandardError, data['error'] end - response.body.to_json['data'] + body = JSON.parse(response.body) + body['data'] rescue StandardError => e log("Could not take DOM snapshot '#{name}'") diff --git a/spec/lib/percy/percy_spec.rb b/spec/lib/percy/percy_spec.rb index f5f3803..536b02e 100644 --- a/spec/lib/percy/percy_spec.rb +++ b/spec/lib/percy/percy_spec.rb @@ -123,7 +123,7 @@ .to_return(status: 200, body: '{"success": "true", "data": "sync_data" }', headers: {}) visit 'index.html' - data = Percy.snapshot(page, 'Name', widths: [944]) + data = Percy.snapshot(page, 'Name', { :sync => true }) expect(WebMock).to have_requested(:post, "#{Percy::PERCY_SERVER_ADDRESS}/percy/snapshot") .with( From 18ddc26d04b165aa85fe7c97af117424a0368c9a Mon Sep 17 00:00:00 2001 From: Chinmay Maheshwari Date: Thu, 1 Feb 2024 17:35:45 +0530 Subject: [PATCH 6/7] lint fix --- spec/lib/percy/percy_spec.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/spec/lib/percy/percy_spec.rb b/spec/lib/percy/percy_spec.rb index 536b02e..8324ed5 100644 --- a/spec/lib/percy/percy_spec.rb +++ b/spec/lib/percy/percy_spec.rb @@ -123,7 +123,7 @@ .to_return(status: 200, body: '{"success": "true", "data": "sync_data" }', headers: {}) visit 'index.html' - data = Percy.snapshot(page, 'Name', { :sync => true }) + data = Percy.snapshot(page, 'Name', {sync: true}) expect(WebMock).to have_requested(:post, "#{Percy::PERCY_SERVER_ADDRESS}/percy/snapshot") .with( From 096a88be2c06e7274993524ad3908aecd37c97c7 Mon Sep 17 00:00:00 2001 From: Chinmay Maheshwari Date: Thu, 1 Feb 2024 18:30:43 +0530 Subject: [PATCH 7/7] Update node to run test --- .github/workflows/test.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) 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)"