lib/percy.rb in percy-capybara-4.2.0 vs lib/percy.rb in percy-capybara-4.3.0

- old
+ new

@@ -18,17 +18,17 @@ # @option options [Array(Number)] :widths Widths, in pixels, that you'd like to capture for # this snapshot. def self.snapshot(page, options = {}) return unless self._is_agent_running? - domSnapshot = self._make_dom_snapshot(page) - return unless domSnapshot - if !options.has_key?(:name) options[:name] = page.current_url end + domSnapshot = self._make_dom_snapshot(page, self._keys_to_json(options)) + return unless domSnapshot + body = { url: page.current_url, domSnapshot: domSnapshot, clientInfo: Percy.client_info, environmentInfo: Percy.environment_info, @@ -69,56 +69,50 @@ self._logger.error { "Could not load #{AGENT_JS_PATH}. Error: #{e}" } return nil end end - def self._make_dom_snapshot(page) + def self._make_dom_snapshot(page, options) agent_js = self._get_agent_js - - if self._is_debug? - self._logger.info { "agent_js file: #{agent_js}" } - end - return unless agent_js begin page.execute_script(agent_js) - dom_snapshot_js = <<-JS - (function() { - var percyAgentClient = new PercyAgent({ handleAgentCommunication: false }); - return percyAgentClient.snapshot('unused'); - })() - JS - return page.evaluate_script(dom_snapshot_js) + dom_snapshot_js = "new window.PercyAgent({ handleAgentCommunication: false }).domSnapshot(document, #{options.to_json})" + + if self._is_capybara? + dom_snapshot = page.evaluate_script(dom_snapshot_js) + else + dom_snapshot = page.execute_script(dom_snapshot_js) + end + + return dom_snapshot rescue => e - self._logger.error { "Snapshotting failed. Note that Poltergeist and Rake::Test are no longer supported by percy-capybara. Error: #{e}" } + self._logger.error { "DOM snapshotting failed. Error: #{e}" } return nil end end def self._post_snapshot_to_agent(body) http = Net::HTTP.new(AGENT_HOST, AGENT_PORT) request = Net::HTTP::Post.new('/percy/snapshot', { 'Content-Type': 'application/json' }) request.body = body.to_json + begin response = http.request(request) - - if self._is_debug? - self._logger.info { "Response from agent: #{response}" } - end rescue => e - self._logger.error { "Agent rejected snapshot request. Error: #{e}" } + self._logger.error { "Percy rejected snapshot request. Error: #{e}" } end end def self._is_agent_running? begin Net::HTTP.get(AGENT_HOST, '/percy/healthcheck', AGENT_PORT) return true rescue => e if self._is_debug? - self._logger.error { "Healthcheck failed, agent is not running: #{e}" } + self._logger.error { "Healthcheck failed, Percy is not running: #{e}" } end return false end end @@ -139,7 +133,11 @@ return options end def self._is_debug? ENV['LOG_LEVEL'] == 'debug' + end + + def self._is_capybara? + Percy.capybara_version.length > 0 end end