Sha256: f39525c83073588b7b0d6c78b01bc25d6f5821fe58c466f8f04b6e3ac30abfcd

Contents?: true

Size: 1.81 KB

Versions: 13

Compression:

Stored size: 1.81 KB

Contents

require 'set'
require 'faraday'
require 'httpclient'
require 'digest'
require 'uri'
require 'pathname'

module Percy
  module Capybara
    class Client
      module Snapshots
        # Takes a snapshot of the given page HTML and its assets.
        #
        # @param [Capybara::Session] page The Capybara page to snapshot.
        # @param [Hash] options
        # @option options [String] :name A unique name for the current page that identifies it across
        #   builds. By default this is the URL of the page, but can be customized if the URL does not
        #   entirely identify the current state.
        def snapshot(page, options = {})
          return if !enabled?  # Silently skip if the client is disabled.
          name = options[:name]
          loader = initialize_loader(page: page)

          # If this is the first snapshot, create the build and upload build resources.
          if !build_initialized?
            build_resources = loader.build_resources
            initialize_build(resources: build_resources)
            upload_missing_build_resources(build_resources)
          end

          current_build_id = current_build['data']['id']
          resources = loader.snapshot_resources
          resource_map = {}
          resources.each { |r| resource_map[r.sha] = r }

          # Create the snapshot and upload any missing snapshot resources.
          snapshot = client.create_snapshot(current_build_id, resources, name: name)
          snapshot['data']['relationships']['missing-resources']['data'].each do |missing_resource|
            sha = missing_resource['id']
            client.upload_resource(current_build_id, resource_map[sha].content)
          end

          # Finalize the snapshot.
          client.finalize_snapshot(snapshot['data']['id'])

          true
        end
      end
    end
  end
end

Version data entries

13 entries across 13 versions & 1 rubygems

Version Path
percy-capybara-0.4.12 lib/percy/capybara/client/snapshots.rb
percy-capybara-0.4.11 lib/percy/capybara/client/snapshots.rb
percy-capybara-0.4.10 lib/percy/capybara/client/snapshots.rb
percy-capybara-0.4.9 lib/percy/capybara/client/snapshots.rb
percy-capybara-0.4.8 lib/percy/capybara/client/snapshots.rb
percy-capybara-0.4.7 lib/percy/capybara/client/snapshots.rb
percy-capybara-0.4.6 lib/percy/capybara/client/snapshots.rb
percy-capybara-0.4.5 lib/percy/capybara/client/snapshots.rb
percy-capybara-0.4.4 lib/percy/capybara/client/snapshots.rb
percy-capybara-0.4.3 lib/percy/capybara/client/snapshots.rb
percy-capybara-0.4.2 lib/percy/capybara/client/snapshots.rb
percy-capybara-0.4.1 lib/percy/capybara/client/snapshots.rb
percy-capybara-0.4.0 lib/percy/capybara/client/snapshots.rb