Module: PercyCapybara
- Defined in:
- lib/percy/version.rb,
lib/percy/capybara.rb
Constant Summary collapse
- VERSION =
'5.0.1.pre.1'.freeze
- PERCY_DEBUG =
ENV['PERCY_LOGLEVEL'] == 'debug'
- PERCY_SERVER_ADDRESS =
ENV['PERCY_SERVER_ADDRESS'] || 'http://localhost:5338'
- PERCY_LABEL =
"[\u001b[35m" + (PERCY_DEBUG ? 'percy:capybara' : 'percy') + "\u001b[39m]"
Instance Method Summary collapse
-
#percy_snapshot(name, options = {}) ⇒ Object
Take a DOM snapshot and post it to the snapshot endpoint.
Instance Method Details
#percy_snapshot(name, options = {}) ⇒ Object
Take a DOM snapshot and post it to the snapshot endpoint
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 |
# File 'lib/percy/capybara.rb', line 18 def percy_snapshot(name, = {}) return unless percy_enabled? page = Capybara.current_session begin page.evaluate_script(fetch_percy_dom) # Readiness gate -- runs before serialize when CLI supports it. # Uses evaluate_async_script with a callback signal so the SDK can block # on PercyDOM.waitForReady. In-browser typeof guard makes this a no-op on # older CLIs that lack waitForReady. readiness_diagnostics = wait_for_ready(page, ) dom_snapshot = page .evaluate_script("(function() { return PercyDOM.serialize(#{.to_json}) })()") if readiness_diagnostics && dom_snapshot.is_a?(Hash) dom_snapshot['readiness_diagnostics'] = readiness_diagnostics end response = fetch('percy/snapshot', name: name, url: page.current_url, dom_snapshot: dom_snapshot, client_info: CLIENT_INFO, environment_info: ENV_INFO, **,) unless response.body.to_json['success'] raise StandardError, data['error'] end rescue StandardError => e log("Could not take DOM snapshot '#{name}'") if PERCY_DEBUG then log(e) end end end |