Percy Client (Deprecated)

3.9.0 · deprecated · verified Wed Apr 22

The `percy-client` package was the original JavaScript API client library for Percy, a visual testing and review platform. It provided low-level programmatic access to Percy's API for managing visual builds and snapshots, crucial for integrating automated visual regression testing into CI/CD pipelines. This library allowed developers to create builds, capture DOM snapshots, upload associated resources (like HTML, CSS, and images), and finalize builds directly through code. The package is currently at version 3.9.0, with its last significant update approximately three years ago. It has since entered a maintenance state, receiving only minor dependency updates before being officially deprecated. Its primary differentiation was offering direct API control, which is now handled by the newer `@percy/client` package and the `@percy/cli` ecosystem, which offer a more comprehensive and extensible toolchain for visual testing.

Common errors

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to programmatically create a Percy build, create a snapshot, upload HTML content as a resource, and finalize the snapshot and build using the `percy-client` library. It illustrates the low-level API interaction for visual testing.

import PercyClient from 'percy-client';

// Ensure PERCY_TOKEN is set in your environment variables for authentication
// For example: export PERCY_TOKEN='YOUR_PERCY_PROJECT_TOKEN'
const percyToken = process.env.PERCY_TOKEN || '';

const client = new PercyClient({
  token: percyToken,
  clientInfo: 'my-custom-integration',
  environmentInfo: 'my-node-environment'
});

async function runPercyBuildAndSnapshot() {
  if (!percyToken) {
    console.warn('PERCY_TOKEN is not set. Snapshots will be skipped.');
    return;
  }

  console.log('Creating a new Percy build...');
  const build = await client.createBuild();
  console.log(`Build created: ${build.attributes['web-url']}`);

  console.log('Creating a snapshot...');
  const htmlContent = `
    <!DOCTYPE html>
    <html>
    <head><title>My Test Page</title></head>
    <body>
      <h1>Hello from Percy!</h1>
      <p>This is a test snapshot.</p>
    </body>
    </html>
  `;
  const snapshot = await client.createSnapshot(build.id, {
    name: 'Homepage with H1',
    widths: [768, 1280]
  });

  // Uploading the HTML content as a resource for the snapshot
  await client.uploadResource(build.id, snapshot.id, {
    resourceUrl: '/', // The URL this resource represents
    mimetype: 'text/html',
    content: Buffer.from(htmlContent)
  });

  console.log('Finalizing snapshot...');
  await client.finalizeSnapshot(snapshot.id);

  console.log('Finalizing build...');
  await client.finalizeBuild(build.id);
  console.log('Percy build process completed!');
}

runPercyBuildAndSnapshot().catch(error => {
  console.error('An error occurred during Percy process:', error);
  process.exit(1);
});

view raw JSON →