@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,
disableAssetCache: 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. All POST requests accept a
JSON body with the application/json
content-type.
- 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],
minimumHeight: 1024,
percyCSS: '',
requestHeaders: {},
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/',
waitFor: selectorOrTimeout,
execute: async (page) => {},
snapshots: [{
name: 'Second Snapshot',
execute: async (page) => {},
...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
Puppeteer Executable Path
To avoid downloading the browser used for asset discovery, the local browser executable can be
defined with an executablePath
option provided within discovery.launchOptions
. This options
falls back to the PUPPETEER_EXECUTABLE_PATH
environment variable if defined.