Skip to content

Commit

Permalink
visual diffing
Browse files Browse the repository at this point in the history
  • Loading branch information
wibjorn committed Nov 9, 2024
1 parent a286e19 commit df5fd3e
Show file tree
Hide file tree
Showing 5 changed files with 198 additions and 28 deletions.
49 changes: 41 additions & 8 deletions .github/workflows/visual-diff.yml
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,9 @@ jobs:
visual-diff:
# The type of runner that the job will run on
runs-on: windows-latest
permissions:
pages: write # to deploy to Pages
id-token: write # to verify the deployment originates from an appropriate source

# Steps represent a sequence of tasks that will be executed as part of the job
steps:
Expand All @@ -51,20 +54,20 @@ jobs:
run: npx playwright install

# Take images
- name: Take screenshots for comparison [push|main]
- name: Take screenshots for baseline [push|main]
if: ${{ github.event_name == 'push' && github.ref_name == 'main' }}
id: 'visual_diff_all'
run: npm run screenshots:all

# On pushes to main, upload images for comparison against pull_request runs
- name: Upload comparison screenshots [push|main]
# On pushes to main, upload images for baseline against pull_request runs
- name: Upload baseline screenshots [push|main]
if: ${{ github.event_name == 'push' && github.ref_name == 'main' }}
uses: actions/upload-artifact@v4
with:
name: visual_diff_comparison_shapshots
name: visual_diff_baseline_shapshots
path: ./integration/visual-diff/screenshots/updated

# On pull requests into main, download images for comparison
# On pull requests into main, download images for baseline
- name: Take screenshots [pull_request]
if: ${{ github.event_name == 'pull_request' && github.base_ref == 'main' }}
id: 'visual_diff_light'
Expand All @@ -78,9 +81,39 @@ jobs:
name: visual_diff_pull_request_shapshots-${{ github.sha }}
path: ./integration/visual-diff/screenshots/updated

- name: Download comparison screenshots [pull_request]
- name: Download baseline screenshots [pull_request]
if: ${{ github.event_name == 'pull_request' && github.base_ref == 'main' }}
uses: actions/upload-artifact@v4
with:
name: visual_diff_comparison_shapshots
path: ./integration/visual-diff/screenshots/comparison
name: visual_diff_baseline_shapshots
path: ./integration/visual-diff/screenshots/baseline

- name: Download baseline screenshots [pull_request]
if: ${{ github.event_name == 'pull_request' && github.base_ref == 'main' }}
uses: actions/upload-artifact@v4
with:
name: visual_diff_baseline_shapshots
path: ./integration/visual-diff/screenshots/baseline

# populate ./integration/visual-diff/screenshots/ with the contents of the diff site
- name: Compare Screenshots and Generate Site
run: npm run compare -w @microsoft/atlas-integration

# Ready for Parcel to build the thing
- name: Build diff report site
run: npm run build-report -w @microsoft/atlas-integration

# # Pages setup and upload
# - name: Setup Pages
# uses: actions/configure-pages@v5

# - name: Upload artifact
# uses: actions/upload-pages-artifact@v3
# with:
# name: github-pages
# # Upload entire repository
# path: './integration/visual-diff/screenshots/'

# - name: Deploy to GitHub Pages
# id: deployment
# uses: actions/deploy-pages@v4
21 changes: 17 additions & 4 deletions integration/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@
"description": "",
"scripts": {
"codegen": "playwright codegen localhost:1111",
"compare": "node \"./tasks/dist/compare.js\" ",
"compile-tasks": "tsc -p \"./tasks/tsconfig.json\"",
"compare": "wireit",
"compile-tasks": "wireit",
"test": "playwright test --config=integration-tests.config.ts",
"test:debug": "playwright test --debug --config=integration-tests.config.ts",
"screenshots:all": "cross-env FULL_DIFF=true playwright test --config=visual-diff.config.ts",
Expand All @@ -18,21 +18,34 @@
"serve-report": "parcel \"./visual-diff/screenshots/*.html\" --port 7777 --open Chrome --no-cache",
"build-report": "parcel build \"./visual-diff/screenshots/*.html\""
},
"wireit": {
"compare": {
"command": "node \"./tasks/dist/compare.js\" ",
"dependencies": [
"compile-tasks"
]
},
"compile-tasks": {
"command": "tsc -p \"./tasks/tsconfig.json\""
}
},
"author": "Microsoft Corporation",
"license": "ISC",
"devDependencies": {
"@axe-core/playwright": "^4.7.3",
"@playwright/test": "^1.35.1",
"@types/normalize-path": "^3.0.0",
"@types/pixelmatch": "^5.2.4",
"@types/fs-extra": "^11.0.4",
"@types/pngjs": "6.0.1",
"cross-env": "^7.0.3",
"execa": "^7.1.1",
"fs-extra": "^11.1.1",
"glob": "^10.3.1",
"glob": "^11.0.0",
"normalize-path": "^3.0.0",
"parcel": "^2.12.0",
"pixelmatch": "^5.3.0",
"pngjs": "^7.0.0"
"pngjs": "^7.0.0",
"wireit": "0.14.9"
}
}
20 changes: 10 additions & 10 deletions integration/tasks/compare.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,17 @@
// @ts-check

import { emptyDir, ensureDir, readFile, writeFile, writeJSON } from 'fs-extra';
import { emptyDir, ensureDir, PathLike, readFile, writeFile, writeJSON } from 'fs-extra';
import { basename, dirname, join, resolve, sep } from 'path';
import * as pixelmatch from 'pixelmatch';
import { PNG } from 'pngjs';
import { promisify } from 'util';
import {
baselineDirectory,
diffDirectory,
screenshotsOutputDir,
screenshotsRoot
} from './locations';
import { generateHtmlReport } from './report';
export const glob = promisify(require('glob'));
import { glob } from 'glob';

export const diffExt = '.diff.png';
/**
Expand Down Expand Up @@ -84,12 +83,13 @@ async function compare(
emptyDir(outDir)
]);

const sourceMap = sourceKeys.reduce(
(m: { set: (arg0: any, arg1: string) => any }, f: any) => m.set(f, join(sourceDir, f)),
const sourceMap: Map<string, string> = sourceKeys.reduce(
(m: Map<string, string>, f: any) => m.set(f, join(sourceDir, f)),
new Map()
);
const targetMap = targetKeys.reduce(
(m: { set: (arg0: any, arg1: string) => any }, f: any) => m.set(f, join(targetDir, f)),

const targetMap: Map<string, string> = targetKeys.reduce(
(m: Map<string, string>, f: any) => m.set(f, join(targetDir, f)),
new Map()
);
const manifest: VisualDiffReportManifest = [];
Expand All @@ -100,12 +100,12 @@ async function compare(
continue;
}

const targetFilename = targetMap.get(key);
const targetFilename = targetMap.get(key) || '';
const [source, target] = await Promise.all([
readFile(sourceFilename),
readFile(targetFilename)
readFile(targetFilename as PathLike)
]);
const diff = await compareBuffers(source, target);
const diff = await compareBuffers(source, target as Buffer);

if (diff.pixelCount === 0) {
continue;
Expand Down
1 change: 1 addition & 0 deletions integration/tasks/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
"$schema": "https://json.schemastore.org/tsconfig",
"compilerOptions": {
/*NOTE: This file is used only for linting. */
"skipLibCheck": true,
/* Basic Options */
"target": "es2021",
"module": "commonjs" /* Specify module code generation: 'none', 'commonjs', 'amd', 'system', 'umd', 'es2015', or 'ESNext'. */,
Expand Down
Loading

0 comments on commit df5fd3e

Please sign in to comment.