Skip to content

Commit

Permalink
feat(vrt): baseブランチの差分と比較できるように修正
Browse files Browse the repository at this point in the history
  • Loading branch information
mii288 committed Dec 5, 2024
1 parent e5b5a41 commit ee16e58
Show file tree
Hide file tree
Showing 4 changed files with 108 additions and 20 deletions.
50 changes: 50 additions & 0 deletions .github/actions/screenshots/action.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
name: Take screenshots

description: Take screenshots

inputs:
ref:
description: target branch
path:
description: the path to a directory containing saved screenshots
required: true

outputs:
cache-key:
description: the cache key name
value: vrt-${{ steps.latest-commit.outputs.sha }}

runs:
using: composite
steps:
- uses: actions/checkout@v4
- uses: actions/checkout@v4
with:
ref: ${{ inputs.ref }}
clean: false
- name: Check latest commit hash
id: latest-commit
run: |
git config --global --add safe.directory /__w/${{ github.event.repository.name }}/${{ github.event.repository.name }}
echo "sha=$(git rev-parse HEAD)" >> "$GITHUB_OUTPUT"
shell: bash
- name: Restore screenshots
id: vrt-screenshots
uses: actions/cache@v4
with:
path: ${{ inputs.path }}
key: vrt-${{ steps.latest-commit.outputs.sha }}
- if: ${{ steps.vrt-screenshots.outputs.cache-hit != 'true' }}
name: Install Japanese fonts
run: |
apt-get update
apt-get purge -y fonts-wqy-zenhei fonts-ipafont-gothic
apt-get install -y fonts-noto-cjk-extra
fc-cache -fv
shell: bash
- if: ${{ steps.vrt-screenshots.outputs.cache-hit != 'true' }}
uses: ./.github/actions/setup
- if: ${{ steps.vrt-screenshots.outputs.cache-hit != 'true' }}
name: Take screenshots
run: npm run screenshot
shell: bash
56 changes: 49 additions & 7 deletions .github/workflows/vrt.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,21 +4,63 @@ on:
pull_request:
types: [opened, synchronize]

env:
screenshots-dir: './.reg/screenshots'

jobs:
test:
prepare-screenshots-actual:
name: Take actual screenshots
runs-on: ubuntu-latest
container:
image: mcr.microsoft.com/playwright:v1.46.1-jammy
timeout-minutes: 60
outputs:
cache-key: ${{ steps.screenshots.outputs.cache-key }}
steps:
- uses: actions/checkout@v4
- uses: './.github/actions/screenshots'
id: screenshots
with:
ref: ${{ github.event.pull_request.head.sha }}
path: ${{ env.screenshots-dir }}

prepare-screenshots-expected:
name: Take expected screenshots
runs-on: ubuntu-latest
container:
image: mcr.microsoft.com/playwright:v1.46.1-jammy
timeout-minutes: 60
outputs:
cache-key: ${{ steps.screenshots.outputs.cache-key }}
steps:
- uses: actions/checkout@v4
- uses: './.github/actions/screenshots'
id: screenshots
with:
ref: ${{ github.base_ref }}
path: ${{ env.screenshots-dir }}

test:
name: Verify screenshots
needs: [prepare-screenshots-actual, prepare-screenshots-expected]
runs-on: ubuntu-latest
container:
image: mcr.microsoft.com/playwright:v1.46.1-jammy
steps:
- uses: actions/checkout@v4
- name: Install Japanese fonts
run: |
apt-get update
apt-get purge -y fonts-wqy-zenhei fonts-ipafont-gothic
apt-get install -y fonts-noto-cjk-extra
fc-cache -fv
- uses: ./.github/actions/setup
- name: Restore actual screenshots
uses: actions/cache@v4
with:
path: ${{ env.screenshots-dir }}
key: ${{ needs.prepare-screenshots-actual.outputs.cache-key }}
- run: mv ${{ env.screenshots-dir }} .reg/actual
- name: Restore expected screenshots
uses: actions/cache@v4
with:
path: ${{ env.screenshots-dir }}
key: ${{ needs.prepare-screenshots-expected.outputs.cache-key }}
- run: mv ${{ env.screenshots-dir }} .reg/expected
- name: Run visual tests
continue-on-error: true
id: vrt
Expand Down
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,8 @@
"prepare": "husky",
"preview": "astro preview",
"start": "astro dev",
"test:vrt": "playwright test -u && reg-cli .reg/actual .reg/expected .reg/diff -R .reg/index.html",
"screenshot": "playwright test -u",
"test:vrt": "reg-cli .reg/actual .reg/expected .reg/diff -R .reg/index.html",
"type-check": "astro check"
},
"dependencies": {
Expand Down
19 changes: 7 additions & 12 deletions tests/vrt.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,14 @@ import { test } from '@playwright/test'

test.describe('Capture screenshots', () => {
const workDir = '.reg'
const actualDir = `${workDir}/actual`
const expectedDir = `${workDir}/expected`
const saveDir = `${workDir}/screenshots`

test('Home', async ({ page }, info) => {
for (const dir of [expectedDir, actualDir]) {
const isExpectedDir = expectedDir === dir

await page.goto(isExpectedDir ? 'https://oysters.dev' : '/')
await page.screenshot({
path: `${dir}/${info.title}/${info.project.name}.png`,
fullPage: true,
animations: 'disabled'
})
}
await page.goto('/')
await page.screenshot({
path: `${saveDir}/${info.title}/${info.project.name}.png`,
fullPage: true,
animations: 'disabled'
})
})
})

0 comments on commit ee16e58

Please sign in to comment.