Skip to content

Instantly share code, notes, and snippets.

@giannif
Created July 9, 2025 16:15
Show Gist options
  • Save giannif/0e74fda367ce381162357e85ee75ec96 to your computer and use it in GitHub Desktop.
Save giannif/0e74fda367ce381162357e85ee75ec96 to your computer and use it in GitHub Desktop.
How to use playwright with snapshots with actions/cache
name: Playwright Tests
on:
push:
# Allow updating snapshots during manual runs
workflow_call:
inputs:
update-snapshots:
description: 'Update snapshots?'
type: boolean
# Allow updating snapshots during automatic runs
workflow_dispatch:
inputs:
update-snapshots:
description: 'Update snapshots?'
type: boolean
jobs:
test:
name: Playwright Tests
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- name: Delete old snapshot cache
continue-on-error: true
# if we're updating snapshots because we made a change we want
# or we have a new tests, we'll delete the old cache
env:
GH_TOKEN: ${{ github.token }}
if: ${{inputs.update-snapshots == true || github.ref == 'refs/heads/master'}}
run: gh cache delete --repo ${{ github.repository }} ${{github.repository}}/${{github.ref}}-snapshots
- name: Set up snapshot cache
id: branch-cache
uses: actions/cache@v4
with:
key: ${{github.repository}}/${{github.ref}}-snapshots
path: packages/playwright-tests/.tests/**
- name: Restore master snapshot cache
if: ${{ (steps.branch-cache.outputs.cache-hit != 'true') }}
id: master-cache
uses: actions/cache/restore@v4
with:
key: ${{ github.repository }}/refs/heads/master-snapshots
path: packages/playwright-tests/.tests/**
- name: Setup Node.js
uses: actions/setup-node@v4
with:
node-version: 20
cache: 'yarn'
- name: Install dependencies
run: yarn install --immutable
- name: Install Playwright browsers
run: yarn workspace playwright-tests playwright install --with-deps chromium
- name: Initialize snapshots
# if there is no cache or we are manually updating the snapshots
# or we're on master
if: ${{ (steps.branch-cache.outputs.cache-hit != 'true' && steps.master-cache.outputs.cache-hit != 'true') || inputs.update-snapshots == true || github.ref == 'refs/heads/master' }}
run: yarn workspace playwright-tests playwright test --update-snapshots
- name: Run Playwright tests
if: ${{ (steps.branch-cache.outputs.cache-hit == 'true' || steps.master-cache.outputs.cache-hit == 'true') && inputs.update-snapshots != true }}
run: yarn workspace playwright-tests playwright test
- name: Upload test results
if: always() # Upload test results even if tests fail
uses: actions/upload-artifact@v4
with:
name: playwright-report
path: packages/playwright-tests/playwright-report/
retention-days: 7
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment