@percy/core
The core component of Percy's CLI and SDKs that handles creating builds, discovering snapshot
assets, uploading snapshots, and finalizing builds. Uses @percy/client
for API communication, a
Puppeteer browser for asset discovery, and starts a local API server for posting snapshots from
other processes.
Usage
The Percy
class will manage a Percy build and perform asset discovery on snapshots before
uploading them to Percy. It also hosts a local API server for Percy SDKs to communicate with.
import Percy from '@percy/core'
const percy = new Percy({
token: PERCY_TOKEN,
loglevel: 'info',
server: true,
port: 5338,
concurrency: 5,
snapshot: {},
discovery: {
allowedHostnames: [],
networkIdleTimeout: 100,
disableCache: false,
concurrency: 5,
launchOptions: {}
},
...config
})
#start()
Starting a Percy
instance will start a local API server, start the asset discovery browser, and
create a new Percy build. If an asset discovery browser is not found, one will be downloaded.
await percy.start()
API Server
Starting a Percy
instance will start a local API server unless server
is false
. The server can
be found at http://localhost:5338/
or at the provided port
number.
- GET
/percy/healthcheck
– Responds with information about the running instance - GET
/percy/dom.js
– Responds with the @percy/dom
library - POST
/percy/snapshot
– Calls #snapshot()
with provided snapshot options - POST
/percy/stop
- Remotely stops the running Percy
instance
#snapshot(options)
Performs asset discovery for the provided DOM snapshot at widths specified here or in the instance's
provided snapshot
option. This is the primary method used by Percy SDKs to upload snapshots.
percy.snapshot({
name: 'My Snapshot',
url: 'http://localhost:3000',
domSnapshot: domSnapshot,
widths: [500, 1280],
minHeight: 1024,
percyCSS: '',
requestHeaders: {},
authorization: {},
clientInfo: '',
environmentInfo: ''
})
#capture(options)
Navigates to a URL and captures a snapshot or multiple snapshots of a page after optionally
interacting with the page. Any #snapshot()
options can also be provided, with
the exception of domSnapshot
.
percy.capture({
name: 'My Snapshot',
url: 'http://localhost:3000/',
waitForTimeout: 1000,
waitForSelector: '.selector',
execute: async () => {},
snapshots: [{
name: 'Second Snapshot',
execute: async () => {},
...options
}],
...options
})
#stop()
Stopping a Percy
instance will wait for any pending snapshots, close the asset discovery browser,
close the local API server, and finalize the current Percy build.
await percy.stop()
Advanced
Chromium Executable Path
Use with caution as asset discovery may not work with some versions of Chromium. To avoid
downloading the browser used for asset discovery, the local browser executable can be defined with
an executable
option provided within discovery.launchOptions
. This option should be a path to
Chromium's binary executable and falls back to downloading a compatible browser when not found.