Playwright Tests #118
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
name: Playwright Tests | |
on: | |
deployment_status: | |
jobs: | |
test: | |
timeout-minutes: 60 | |
runs-on: ubuntu-latest | |
permissions: | |
contents: write | |
pull-requests: write | |
if: ${{ github.event_name == 'deployment_status' && startsWith(github.event.deployment_status.environment, 'preview') && github.event.deployment_status.state == 'success' }} | |
steps: | |
- uses: actions/checkout@v4 | |
with: | |
fetch-depth: 0 | |
- uses: actions/setup-node@v4 | |
with: | |
node-version: lts/* | |
- name: Install dependencies | |
run: npm install -g pnpm && pnpm install | |
- name: Install Playwright Browsers | |
run: npx playwright install --with-deps | |
- name: Run tests | |
id: run_playwright_tests | |
run: | | |
pnpm run test:e2e-ci | tee output.log | |
if grep -q -e "Error: A snapshot doesn't exist at" -e "Screenshot comparison failed" -e "should match visual snapshot" output.log; then | |
echo "Playwright tests failed due to a snapshot issue." | |
echo "SNAPSHOT_DIFFERENCES=true" >> $GITHUB_ENV | |
exit 1 | |
elif grep -q "failed" output.log && grep -q "e2e/global.setup.ts" output.log && ! grep -q "Warning: \[setup db\] › e2e/global.setup.ts took" output.log; then | |
echo "Playwright tests failed during global setup." | |
echo "SETUP_ERROR=true" >> $GITHUB_ENV | |
exit 1 | |
elif grep -q "failed" output.log; then | |
echo "Playwright tests failed due to an issue unrelated to snapshots or global setup." | |
exit 1 | |
fi | |
env: | |
E2E_UPLOADTHING_TOKEN: ${{ secrets.E2E_UPLOADTHING_TOKEN }} | |
BASE_URL: ${{ github.event.deployment_status.environment_url }} | |
- name: Get PR data | |
uses: actions/github-script@v7 | |
if: always() && steps.run_playwright_tests.outcome == 'failure' | |
id: get_pr_data | |
with: | |
script: | | |
return ( | |
await github.rest.repos.listPullRequestsAssociatedWithCommit({ | |
commit_sha: context.sha, | |
owner: context.repo.owner, | |
repo: context.repo.repo, | |
}) | |
).data[0]; | |
- name: Extract branch name | |
if: always() && steps.run_playwright_tests.outcome == 'failure' | |
id: extract_branch | |
run: | | |
COMMIT_SHA="${{ github.event.deployment.ref }}" | |
git fetch --all | |
BRANCH_NAME=$(git branch -r --contains ${COMMIT_SHA} | grep -v HEAD | head -n 1 | sed 's/origin\///' | xargs) | |
echo "branch=${BRANCH_NAME}" >> $GITHUB_OUTPUT | |
- name: Install Neon CLI | |
if: always() && env.SETUP_ERROR == 'true' | |
run: npm i -g neonctl@latest | |
- name: Delete Neon branch if tests fail | |
if: always() && env.SETUP_ERROR == 'true' | |
run: neonctl branches delete preview/${{ steps.extract_branch.outputs.branch }} --project-id ${{ secrets.NEON_PROJECT_ID }} | |
env: | |
NEON_API_KEY: ${{ secrets.NEON_API_KEY }} | |
- uses: actions/upload-artifact@v4 | |
id: artifact-upload | |
if: always() | |
with: | |
name: playwright-report | |
path: playwright-report/ | |
retention-days: 30 | |
- name: Comment on PR with report link | |
uses: thollander/actions-comment-pull-request@v3 | |
if: ${{ failure() && env.SNAPSHOT_DIFFERENCES == 'true' && fromJson(steps.get_pr_data.outputs.result).number != '' }} | |
with: | |
message: | | |
### Playwright visual snapshot differences were detected. | |
View the [Playwright report](${{ steps.artifact-upload.outputs.artifact-url }}) to review the visual differences. | |
**To approve the snapshot changes and update the snapshots, please comment:** /approve-snapshots <preview-url> | |
**Example:** /approve-snapshots https://fresco-sandbox.vercel.app/ | |
pr-number: ${{ fromJson(steps.get_pr_data.outputs.result).number }} |