Visual Regression Testing with Tugboat

Tugboat builds a complete, working preview of your site for every pull request. Diffy checks that preview against production before it merges: once the preview is online, Diffy screenshots both sites page by page and at every breakpoint, compares them, and reports the result as a check on the GitHub pull request.

There is no test code to write and no screenshots to store in the repository. You add the Diffy CLI to your .tugboat/config.yml and run one command; the pages, breakpoints, masks and login steps live in your Diffy project.

How Chromatic tests a 20-site Drupal multisite with Tugboat

Chromatic, a distributed Drupal agency, maintains a multisite of more than 20 sites. Every change had to be checked on all of them, which took a long time and still left room for mistakes.

Now every pull request gets Tugboat previews, one per site, and Diffy compares each of them with that site’s production. A typical result: 15 sites come back with no changes and 5 have differences, so the team knows exactly where to look before anything merges.

“Diffy has a sort of multiplier effect that we can focus on the things that matter, where humans can do a better job of impacting and letting machines worry about this stuff that can be automated.”

— Alfonso Gómez-Arzola, Software Architect at Chromatic

Read the Chromatic case study →

How it works

  1. A pull request is opened or updated, and Tugboat builds a preview for it.
  2. When the preview is online, a command in the Tugboat config runs diffy project:compare with the preview URL and the commit SHA.
  3. A check named Diffy check “<your project>” appears on the pull request while Diffy screenshots production and the preview on its own servers.
  4. The check completes as success with “No visual changes found”, or as action required with the number of changes that need review. Details opens the comparison in Diffy.
  5. A reviewer goes through the changes in Diffy and marks each one as expected or as a bug. When every change is reviewed, Diffy updates the check to success: “All visual changes reviewed”, with the number of bugs marked, if any.

Why Diffy on top of Tugboat’s visual diffs

Tugboat has visual diffs of its own, and they’re a good start. Diffy adds what teams usually need next:

  • Pages behind a login. Tugboat’s visual diffs can’t reach them. Diffy logs in before taking screenshots, with a ready-made preset for Drupal’s /user login and for WordPress.
  • Production as the baseline. Tugboat compares a preview with its base preview. Diffy compares it with the live site, which catches differences between what’s in the repository and what’s actually deployed. You can still compare with the base preview too; see below.
  • Every breakpoint, with stable screenshots. Masks, hidden elements and mocked content keep carousels, dates and ads from showing up as changes.
  • Review on the pull request. Each change is approved or marked as a bug, and the check can be a required status check, so nothing merges until someone has looked.

What you need

  • A Diffy project with your production URL, the pages you want tested and your breakpoints. See what visual regression testing covers if you’re choosing pages for the first time.
  • The Diffy GitHub app installed on the repository (github.com/apps/diffy-testing).
  • A Diffy API key, from app.diffy.website/#/keys. API access is included in every plan, the free one too; comparing Tugboat previews needs a paid plan, because a preview is a custom environment.
  • PHP in the Tugboat service image. The Diffy CLI is a PHP archive. Most Drupal and WordPress images already have PHP; if yours doesn’t, install php-cli in the init step.

Step 1: Connect the GitHub repository in Diffy

In your Diffy project, open Settings → Notifications → GitHub and enter the repository in GitHub project URL (for example https://github.com/acme/website). If you want a pull request comment as well as the check, tick “In addition to the check post a comment to the pull request with diff progress and results”.

Step 2: Add the Tugboat environment variables

In your Tugboat project or repository settings, add two custom environment variables:

  • DIFFY_API_KEY — your Diffy API key.
  • DIFFY_PROJECT_ID — the ID of the Diffy project, shown in the project settings on the General tab and in the project’s URL, as in app.diffy.website/#/projects/12345.

Step 3: Add Diffy to .tugboat/config.yml

Install the CLI in the init step of the service that serves your site, and run the comparison in the online step, which Tugboat runs once the preview is up:

services:
  apache:
    commands:
      init:
        # The Diffy CLI needs PHP. Uncomment if the image doesn't have it.
        # - apt-get update
        # - apt-get install -y php-cli
        - curl -L https://github.com/DiffyWebsite/diffy-cli/releases/latest/download/diffy.phar -o /usr/local/bin/diffy
        - chmod +x /usr/local/bin/diffy
        - diffy auth:login ${DIFFY_API_KEY}

      online:
        - diffy project:compare ${DIFFY_PROJECT_ID} prod custom --env2Url="${TUGBOAT_SERVICE_URL}" --commit-sha=${TUGBOAT_PREVIEW_SHA}

Replace apache with the name of your service. If your config already has init and online steps, add these lines to them.

What each part does

  • diffy auth:login — authenticates with your API key.
  • diffy project:compare <project> prod custom — compares two environments: prod is the production URL from your project settings, custom is the URL in --env2Url. Use stage instead of prod to compare against staging.
  • TUGBOAT_SERVICE_URL — the public URL Tugboat gives this service in the preview.
  • --commit-sha — the commit Diffy attaches the check to. Tugboat provides it as TUGBOAT_PREVIEW_SHA.
  • For previews behind HTTP basic auth, add --env2User and --env2Pass.

Put the comparison in online rather than build: by the time online runs, Tugboat has finished building the preview, so caches are rebuilt and configuration is imported before Diffy takes screenshots.

The command returns as soon as Diffy has created the comparison, so it doesn’t slow the preview down. Finding visual changes doesn’t fail the Tugboat build either; the result is reported through the check on the pull request.

Compare with the base preview

If your Tugboat project uses base previews, you can compare a pull request’s preview with the base preview instead of, or as well as, production. Add this to the online step:

online:
  - |
    if [[ -n "$TUGBOAT_BASE_PREVIEW_URL" ]]; then
      diffy project:compare $DIFFY_PROJECT_ID custom custom \
        --env1Url=$TUGBOAT_BASE_PREVIEW_URL \
        --env2Url=$TUGBOAT_SERVICE_URL \
        --commit-sha=${TUGBOAT_PREVIEW_SHA}
    fi

The if skips the comparison when there’s no base preview, for example when the base preview itself is being built.

Multisite

For a multisite, like Chromatic’s, create a Diffy project for each site, with that site’s production URL, pages and breakpoints. Then run one diffy project:compare per site in the online step, each with that site’s project ID and its preview URL. Each comparison gets its own check on the pull request, named after its Diffy project, so the pull request shows which sites changed and which didn’t.

Reviewing the result

Details on the check opens the comparison in Diffy. Reviewers go through the changes page by page and breakpoint by breakpoint, with production and the preview side by side, and mark each change as expected or as a bug. When the same change shows up on several pages, it’s grouped so it can be approved once.

To stop pull requests from merging before someone has looked at the visual changes, add the Diffy check as a required status check in your branch protection rules. A comparison with unreviewed changes completes as action required, which keeps the pull request blocked until the changes are reviewed. Marking a change as a bug still counts as reviewed; the idea is that the author fixes it and pushes, which rebuilds the preview and runs a new comparison. The GitHub Actions guide has the details.

FAQ

Tugboat already has visual diffs. Why use Diffy? Tugboat’s own visual diffs compare a preview with its base preview and can’t reach pages behind a login. Diffy compares the preview with production, or with the base preview if you prefer, logs in before it takes screenshots, and turns the result into a check on the pull request that a reviewer approves change by change.

Do I need to write test code? No. The pages, breakpoints, masks and login steps live in the Diffy project settings. The Tugboat config only installs the Diffy CLI and runs one command with the preview URL and the commit.

Does it work for a Drupal multisite? Yes. Give each site its own Diffy project and run one comparison per site in the Tugboat config. Each site gets its own check on the pull request, so you can see at a glance which sites changed.

Why does the Tugboat build finish before the Diffy check? The build only starts the comparison. Screenshots and the comparison run on Diffy’s servers, and the check on the pull request stays in progress until they finish. The preview is ready to use in the meantime.

Which Diffy plan do I need? The Tugboat config uses the Diffy CLI, which authenticates with an API key, and API access is included in every plan, the free one too. Comparing a Tugboat preview with production uses a custom environment, which comes with paid plans.

Get started

Create a Diffy account, add your production URL and key pages, and run a first comparison from the UI. When you’re ready to automate it, add the lines above to your Tugboat config. See also the Diffy docs for GitHub and Tugboat, visual regression testing for Drupal, or all Diffy integrations.