What is @percy/cli-snapshot?
@percy/cli-snapshot is a command-line tool for capturing visual snapshots of web pages and components. It integrates with Percy, a visual testing and review platform, to help developers catch visual regressions in their web applications.
What are @percy/cli-snapshot's main functionalities?
Capture a snapshot of a single URL
This command captures a visual snapshot of the specified URL and uploads it to Percy for visual comparison.
npx @percy/cli-snapshot http://example.com
Capture snapshots from a list of URLs
This command reads a list of URLs from a file (urls.txt) and captures visual snapshots for each URL, uploading them to Percy.
npx @percy/cli-snapshot urls.txt
Capture snapshots with custom widths
This command captures visual snapshots of the specified URL at different viewport widths (375px, 768px, and 1280px) and uploads them to Percy.
npx @percy/cli-snapshot http://example.com --widths=375,768,1280
Capture snapshots with custom CSS
This command captures a visual snapshot of the specified URL with custom CSS applied, and uploads it to Percy.
npx @percy/cli-snapshot http://example.com --css='body { background-color: red; }'
Capture snapshots with authentication
This command captures a visual snapshot of a URL that requires basic authentication, using the provided username and password, and uploads it to Percy.
npx @percy/cli-snapshot http://example.com --auth=username:password
Other packages similar to @percy/cli-snapshot
puppeteer
Puppeteer is a Node library that provides a high-level API to control Chrome or Chromium over the DevTools Protocol. It can be used for taking screenshots, generating PDFs, and automating web interactions. Unlike @percy/cli-snapshot, Puppeteer does not integrate directly with a visual testing platform like Percy.
cypress
Cypress is a JavaScript end-to-end testing framework that allows developers to write tests for web applications. It includes features for taking screenshots and recording videos of tests. While Cypress focuses on functional testing, it can be extended with plugins to perform visual testing, but it does not natively integrate with Percy like @percy/cli-snapshot.
webdriverio
WebdriverIO is a test automation framework that allows you to run tests based on the WebDriver protocol and Appium. It supports taking screenshots and can be integrated with visual regression testing tools. However, it does not provide the same out-of-the-box integration with Percy as @percy/cli-snapshot.
@percy/cli-snapshot
Snapshot a list or static directory of web pages.
Commands
percy snapshot PATHNAME
Snapshot a list of pages from a file or directory
USAGE
$ percy snapshot PATHNAME
ARGUMENTS
PATHNAME path to a directory or file containing a list of pages
OPTIONS
-b, --base-url=base-url [default: /] the url path to serve the static directory from
-c, --config=config configuration file path
-d, --dry-run prints a list of pages to snapshot without snapshotting
-f, --files=files [default: **/*.{html,htm}] one or more globs matching static file
paths to snapshot
-h, --allowed-hostname=allowed-hostname allowed hostnames
-i, --ignore=ignore one or more globs matching static file paths to ignore
-q, --quiet log errors only
-t, --network-idle-timeout=network-idle-timeout asset discovery idle timeout
-v, --verbose log everything
--disable-cache disable asset discovery caches
--silent log nothing
EXAMPLES
$ percy snapshot ./public
$ percy snapshot pages.yml
Usage
Static Directory
When snapshotting a static directory, the directory will be served locally and each matching page
will be navigated to and snapshotted.
$ percy snapshot ./public
[percy] Percy has started!
[percy] Created build #1: https://percy.io/org/project/123
[percy] Snapshot taken: /index.html
[percy] Snapshot taken: /about.html
[percy] Snapshot taken: /contact.html
[percy] Stopping percy...
[percy] Finalized build #1: https://percy.io/org/project/123
[percy] Done!
Page Listing
When snapshotting a file containing a list of pages to snapshot, the page URLs must all be
accessible by a browser. The file must be YAML, JSON, or a JS file exporting a list of pages. Each
page must contain a snapshot name
and url
. The options waitForTimeout
and waitForSelector
can also be provided option to wait for a timeout or selector respectively before snapshotting.
YAML
pages.yml
:
- name: Snapshot one
url: http://localhost:8080
- name: Snapshot two
url: http://localhost:8080/two
waitForTimeout: 1000
waitForSelector: .some-element
Snapshotting pages.yml
:
$ percy snapshot pages.yml
[percy] Percy has started!
[percy] Created build #1: https://percy.io/org/project/123
[percy] Snapshot taken: Snapshot one
[percy] Snapshot taken: Snapshot two
[percy] Stopping percy...
[percy] Finalized build #1: https://percy.io/org/project/123
[percy] Done!
JSON
pages.json
:
[{
"name": "Snapshot one",
"url": "http://localhost:8080"
}, {
"name": "Snapshot two",
"url": "http://localhost:8080/two",
"waitForSelector": ".some-element"
}]
Snapshotting pages.json
:
$ percy snapshot pages.json
[percy] Percy has started!
[percy] Created build #1: https://percy.io/org/project/123
[percy] Snapshot taken: Snapshot one
[percy] Snapshot taken: Snapshot two
[percy] Stopping percy...
[percy] Finalized build #1: https://percy.io/org/project/123
[percy] Done!
JavaScript
For JavaScript exports, an execute
function and additional snapshots
may be specified for each
page to execute JavaScript within the execution context before snapshots are taken.
pages.js
:
module.exports = [{
name: 'Snapshot one',
url: 'http://localhost:8080',
async execute(page) {
await page.click('.button')
}
}, {
name: 'Snapshot two',
url: 'http://localhost:8080/two',
waitForSelector: '.some-element',
snapshots: [{
name: 'Snapshot two - after click',
execute() {
document.querySelector('.button').click();
}
}, {
name: 'Snapshot two - after double click',
execute() {
document.querySelector('.button').click();
document.querySelector('.button').click();
}
}]
}]
JavaScript files may also export sync or async functions that return a list of pages to snapshot.
module.exports = async () => {
let urls = await getSnapshotUrls()
return urls.map(url => ({ name: url, url }))
}
Snapshotting pages.js
:
$ percy snapshot pages.js
[percy] Percy has started!
[percy] Created build #1: https://percy.io/org/project/123
[percy] Snapshot taken: Snapshot one
[percy] Snapshot taken: Snapshot two
[percy] Snapshot taken: Snapshot two - after click
[percy] Snapshot taken: Snapshot two - after double click
[percy] Stopping percy...
[percy] Finalized build #1: https://percy.io/org/project/123
[percy] Done!