TechBuzzPro

Playwright Visual Regression Tests in Bitbucket Pipelines

· 5 min read · Apps & Browsers

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

Visual regression testing with Playwright catches unintended UI changes by comparing screenshots against a baseline. Running these tests in Bitbucket Pipelines gives you automated checks on every push or pull request, without needing a local machine. This guide walks you through setting up a pipeline that installs Playwright, runs your visual tests, and reports failures—all within Bitbucket's CI/CD environment.

What You'll Need

  • A Bitbucket repository with your Playwright test suite already written (including visual tests using toHaveScreenshot() or page.screenshot()).
  • Node.js and npm installed locally (for initial setup and committing the package-lock.json file).
  • Playwright installed as a dev dependency in your project.
  • A Bitbucket Pipelines configuration file (bitbucket-pipelines.yml) at the repository root.
  • Access to your repository's Pipelines settings to enable Pipelines (usually on by default).

Step-by-Step

  1. Install Playwright and the test runner
    Run npm init -y and then npm install -D @playwright/test. This adds the test runner and the Playwright library to your project.
  2. Install the browser binaries
    Run npx playwright install locally to download the Chromium, Firefox, and WebKit browsers. This is needed to generate the initial screenshots and to verify tests pass before pushing.
  3. Create your visual test
    Write a test that uses toHaveScreenshot(). For example, in tests/visual.spec.js:
    const { test, expect } = require('@playwright/test');
    
    test('homepage matches baseline', async ({ page }) => {
      await page.goto('https://example.com');
      await expect(page).toHaveScreenshot('home.png');
    });
    Run it locally with npx playwright test. The first run creates the baseline screenshot. Commit the generated __screenshots__ folder to your repository.
  4. Set up the Bitbucket Pipelines configuration
    Create or edit bitbucket-pipelines.yml at the root of your repository. Start with a basic Node.js image and define a step that installs dependencies and runs the tests.
  5. Define the pipeline step
    Add the following to your YAML file:
    image: node:20
    
    pipelines:
      default:
        - step:
            name: Visual Regression Tests
            caches:
              - node
            script:
              - npm install
              - npx playwright install --with-deps
              - npx playwright test
            artifacts:
              - test-results/**
    The --with-deps flag installs system dependencies needed by the browser. The artifacts section preserves test results and screenshots so you can view them from the pipeline run.
  6. Commit and push
    Commit your bitbucket-pipelines.yml, the test file, and the baseline screenshots. Push to the branch. Bitbucket Pipelines will automatically trigger the pipeline.
  7. Check the pipeline results
    Go to the repository's Pipelines tab in Bitbucket. You'll see the run status. If tests fail, click the failed step and download the artifacts to view the diff images.
  8. Update baselines when changes are intentional
    If the UI changed deliberately, update the baseline screenshots. Run npx playwright test --update-snapshots locally, commit the updated images, and push again.

Common Mistakes

  • Forgetting to install system dependencies – Without --with-deps, the browser may fail to launch in the container.
  • Not committing the baseline screenshots – If the __screenshots__ folder is in .gitignore, the pipeline has no reference and will fail.
  • Using a different Node version than local – Version mismatch can cause subtle differences in rendering. Use the same Node version in the pipeline as your local environment.
  • Ignoring flaky tests due to animations – Animations or dynamic content can cause false failures. Disable animations in the test or use expect().toHaveScreenshot({ animations: 'disabled' }).
  • Not setting a timeout for the pipeline step – Default timeout is 2 hours, but if your tests take longer, you'll need to increase it. Add timeout: 30 (minutes) to the step.

Tips

  • Run tests in parallel – Playwright runs tests in parallel by default. Use multiple workers in the pipeline to speed up execution.
  • Use a dedicated branch for baseline updates – Create a branch that only updates screenshots, so you don't mix UI changes with test updates.
  • Set a custom screenshot path – If you have many tests, organize screenshots in folders per feature to keep the repo clean.
  • Leverage the Playwright HTML report – Add --reporter=html to the test command and include the playwright-report folder in artifacts to get a visual report.
  • Use environment variables for URLs – If your tests hit different environments (staging, prod), pass the base URL via an environment variable in the pipeline.

Troubleshooting

Pipeline fails with "browserType.launch: Executable doesn't exist"
This means the browser binaries are not installed. Ensure you ran npx playwright install --with-deps in the pipeline script. Also verify that the node cache is not interfering—clear the cache if needed.

Screenshots differ even though nothing changed
This can happen due to font rendering differences between local and CI. Use a consistent base image (e.g., mcr.microsoft.com/playwright instead of node:20) which contains pre-installed browsers and fonts. Also disable animations and set a fixed viewport size.

Tests pass locally but fail in pipeline
Check if the pipeline runs on a different operating system (Linux vs Windows/macOS). Use the same OS or use the official Playwright Docker image to ensure parity.

Artifacts not downloadable
Make sure the artifact path is correct. The default Playwright output directory is test-results. If you changed it, update the artifacts path accordingly.

Pipeline times out
Increase the step timeout using timeout: 60 (in minutes) in the step definition. Also reduce the number of workers if the CI machine is low on memory.

Frequently Asked Questions

Q: Do I need to commit the baseline screenshots?
Yes, the baseline images must be in the repository so the pipeline can compare against them. Never ignore them in .gitignore.

Q: Can I use Playwright's visual testing with a different test runner?
Playwright's toHaveScreenshot() is tied to its test runner. If you use Jest or Mocha, you'll need to manually capture screenshots and compare them using libraries like pixelmatch.

Q: How do I update the baseline screenshots after an intentional UI change?
Run npx playwright test --update-snapshots locally. This overwrites the existing baseline images. Commit the updated files and push.

Q: Can I run visual tests only on pull requests?
Yes, define a pull-request section in your bitbucket-pipelines.yml instead of (or in addition to) the default section.

Q: What if my app requires authentication?
Store credentials as repository variables in Bitbucket Pipelines settings and access them via process.env in your tests. Never hardcode secrets.

Q: How long does the pipeline typically take?
For a small suite, it usually takes 1-3 minutes. Larger suites with many screenshots can take longer. Use parallel workers and caching to speed it up.