Smart Systems for Home

Plain how-to guides for smart home, security and home Wi-Fi

Catch Visual Regressions in WordPress Pages with Screenshots

· 5 min read · Home Tech

In this article
  1. What You’ll Need
  2. Step-by-Step
  3. Common Mistakes
  4. Tips
  5. Troubleshooting
  6. Frequently Asked Questions

Visual regressions are subtle changes in a page’s appearance that break its design—misaligned buttons, shifted text, or wrong colors—often caused by a CSS update, a plugin change, or a new theme. Catching them early keeps your site looking polished and prevents frustrated visitors. This guide shows you a practical, screenshot-based workflow using free tools like BackstopJS and Playwright, which you can run locally or in a CI pipeline. You’ll learn how to set up baseline screenshots, compare new ones against them, and review the differences before they go live.

What You’ll Need

  • A WordPress site (local or staging) with a stable URL you can access.
  • Node.js (version 16 or later) installed on your computer.
  • A code editor (like VS Code) and basic familiarity with the command line.
  • Git (optional but recommended) to track your test configuration.
  • Access to your WordPress admin to log in if you need to capture authenticated pages.

Step-by-Step

  1. Set up a new project folder. Create a directory, e.g., wp-visual-tests, and open it in your terminal. Initialize a Node project with npm init -y.
  2. Install BackstopJS. Run npm install --save-dev backstopjs. BackstopJS is a popular visual regression tool that uses Puppeteer or Playwright under the hood.
  3. Create a basic BackstopJS configuration. Run npx backstop init. This generates a backstop.json file and a backstop_data folder. Open backstop.json in your editor.
  4. Configure your scenarios. In backstop.json, set the viewports to common screen sizes (e.g., 375x812 for mobile, 1366x768 for desktop). Under scenarios, add entries for each page you want to test. For example:
    { "label": "homepage", "url": "https://your-site.com/", "hideSelectors": [], "removeSelectors": [], "selectors": ["document"] }
    Add more scenarios for key pages like “about”, “contact”, or a product page.
  5. Set a baseline. Run npx backstop reference. This captures the current state of your pages as the “golden” screenshots. Store these in backstop_data/bitmaps_reference.
  6. Make changes to your WordPress site. Update a theme, add a plugin, or tweak CSS. This is what you want to test for regressions.
  7. Run the test. Execute npx backstop test. BackstopJS will take new screenshots and compare them to the baseline. It will report any differences with a visual diff image.
  8. Review the results. After the test, open the HTML report by running npx backstop openReport. It shows a side-by-side comparison: baseline, new, and diff. Click on any failing scenario to see the exact pixel differences highlighted in red.
  9. Approve or update the baseline. If the changes are intentional (e.g., you redesigned a button), run npx backstop approve to update the baseline to the new screenshots. If they are unintended regressions, fix your code and rerun the test.
  10. Automate (optional). Add a script to your package.json to run tests easily: "test:visual": "backstop test". For CI, you can run backstop test --config=backstop.json in a GitHub Action or similar, and fail the build if there are mismatches.

Common Mistakes

  • Ignoring dynamic content. Pages with rotating banners, live timers, or user-specific elements will cause false positives. Use hideSelectors to hide those elements, or use removeSelectors to remove them from the screenshot.
  • Testing only the homepage. Visual regressions often appear on inner pages. Include your most important templates: single posts, archives, product pages, and checkout.
  • Not using a stable environment. If you test on a live site that changes frequently, baselines become outdated. Use a local or staging site with a fixed URL.
  • Forgetting to update baselines after intentional changes. If you don’t approve new baselines, every future test will fail, leading to “cry wolf” and ignored failures.
  • Overlooking viewport differences. A design might look fine on desktop but break on mobile. Always test multiple viewports.

Tips

  • Start with a small set of critical pages (5-10) and expand later. This keeps the test run fast and manageable.
  • Use selectors to test specific elements (e.g., a header or footer) instead of the whole page. This reduces noise and makes diffs easier to read.
  • Set a misMatchThreshold (e.g., 0.1%) to allow tiny pixel differences that are often due to font rendering or anti-aliasing.
  • Integrate with your deployment workflow. Run tests after every code change, especially before merging to production.
  • Keep your baseline screenshots in version control so you can track when and why they changed.

Troubleshooting

  • Tests fail with “Reference not found”. You haven’t run backstop reference yet. Do that first.
  • Browser doesn’t launch. Ensure you have the required system dependencies. For Puppeteer, run npx puppeteer browsers install chrome or check your OS libraries.
  • Login required pages show a login form instead of the content. Use the cookiePath option in BackstopJS to pass a session cookie. Log in to your site, extract the cookie, and save it to a file.
  • Tests are too slow. Reduce the number of scenarios or use asyncCaptureLimit and asyncCompareLimit to run captures in parallel.
  • False positives due to animations. Disable CSS animations in your test by adding a script that injects * { animation: none !important; transition: none !important; } via the onBeforeScript option.

Frequently Asked Questions

Do I need to know how to code to use this?

Basic familiarity with the command line and editing JSON files is enough. You don’t need to write complex code—just configure the scenarios.

Can I test pages that require a login?

Yes, use the cookiePath option to provide a session cookie. Log in to your site in a browser, copy the cookie value, and save it to a file that BackstopJS can read.

How often should I run visual regression tests?

Run them whenever you make changes to your theme, plugins, or custom CSS. For active sites, consider running them in your CI pipeline on every pull request.

What if I have a page with a lot of dynamic content?

Use hideSelectors to hide elements like sliders or live counters. Alternatively, use removeSelectors to remove them from the DOM before capturing.

Can I use this on a WordPress.com site?

Yes, as long as you can access the site over HTTP and the pages are public (or you can supply cookies). The tool doesn’t care about the hosting platform.

Is BackstopJS the only option?

No, there are other tools like Percy, Applitools, and Playwright’s own visual testing. BackstopJS is free and open-source, which is why it’s used here.