Module: Percy::Capybara::Client::Snapshots

Included in:
Percy::Capybara::Client
Defined in:
lib/percy/capybara/client/snapshots.rb

Instance Method Summary collapse

Instance Method Details

#snapshot(page, options = {}) ⇒ Object

Takes a snapshot of the given page HTML and its assets.

Parameters:

  • page (Capybara::Session)

    The Capybara page to snapshot.

  • options (Hash) (defaults to: {})

Options Hash (options):

  • :name (String)

    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.



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/percy/capybara/client/snapshots.rb', line 14

def snapshot(page, options = {})
  return unless enabled? # Silently skip if the client is disabled.

  if current_build.nil?
    raise 'Whoops! Percy is enabled, but Percy::Capybara.initialize_build was never ' \
      'called. Did you forget to setup Percy in your spec_helper.rb? ' \
      'See: https://percy.io/docs/clients/ruby/capybara'
  end

  loader = initialize_loader(page: page)

  Percy.logger.debug { "Snapshot started (name: #{options[:name].inspect})" }
  start = Time.now
  current_build_id = current_build['data']['id']
  resources = loader.snapshot_resources
  resource_map = {}
  resources.each do |r|
    resource_map[r.sha] = r
    Percy.logger.debug { "Snapshot resource: #{r.resource_url}" }
  end
  Percy.logger.debug { "All snapshot resources loaded (#{Time.now - start}s)" }

  # Create the snapshot and upload any missing snapshot resources.
  start = Time.now
  rescue_connection_failures do
    begin
      snapshot = client.create_snapshot(current_build_id, resources, options)
    rescue Percy::Client::BadRequestError => e
      Percy.logger.warn('Bad request error, skipping snapshot:')
      Percy.logger.warn(e)
      return
    end

    missing_resource_data = snapshot['data']['relationships']['missing-resources']['data']
    missing_resource_data.each do |missing_resource|
      sha = missing_resource['id']
      client.upload_resource(current_build_id, resource_map[sha].content)
    end
    Percy.logger.debug { "All snapshot resources uploaded (#{Time.now - start}s)" }

    # Finalize the snapshot.
    client.finalize_snapshot(snapshot['data']['id'])
  end
  if failed?
    Percy.logger.error { 'Percy build failed! Check log above for errors.' }
    return
  end
  true
end