Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 | import fs from 'fs' import path from 'path' import { PNG } from 'pngjs' import pixelmatch from 'pixelmatch' interface ComparisonResult { name: string status: 'pass' | 'fail' | 'new' | 'missing' diffPixels?: number totalPixels?: number diffPercentage?: number diffPath?: string } interface ComparisonReport { timestamp: string baselineDir: string currentDir: string diffDir: string threshold: number results: ComparisonResult[] summary: { total: number passed: number failed: number new: number missing: number } } function parseArgs(): { baseline: string; current: string; diff: string; threshold: number; output: string } { const args = process.argv.slice(2) const config = { baseline: './screenshots-production', current: './screenshots', diff: './screenshot-diffs', threshold: 0.001, // 0.1% default tolerance output: './vrt-report.json', } for (let i = 0; i < args.length; i++) { const arg = args[i] if (arg === '--baseline' && args[i + 1]) { config.baseline = args[++i] } else if (arg === '--current' && args[i + 1]) { config.current = args[++i] } else if (arg === '--diff' && args[i + 1]) { config.diff = args[++i] } else if (arg === '--threshold' && args[i + 1]) { config.threshold = parseFloat(args[++i]) } else if (arg === '--output' && args[i + 1]) { config.output = args[++i] } } return config } function getScreenshots(dir: string): string[] { if (!fs.existsSync(dir)) { return [] } return fs.readdirSync(dir).filter((file) => file.endsWith('.png')) } function loadPNG(filePath: string): PNG { const buffer = fs.readFileSync(filePath) return PNG.sync.read(buffer) } function compareImages( baselinePath: string, currentPath: string, diffPath: string, threshold: number ): { diffPixels: number; totalPixels: number; diffPercentage: number } { const baseline = loadPNG(baselinePath) const current = loadPNG(currentPath) // Handle size mismatch if (baseline.width !== current.width || baseline.height !== current.height) { console.warn( `Size mismatch for ${path.basename(baselinePath)}: ` + `baseline ${baseline.width}x${baseline.height} vs current ${current.width}x${current.height}` ) // Return 100% diff for size mismatch const totalPixels = Math.max(baseline.width * baseline.height, current.width * current.height) return { diffPixels: totalPixels, totalPixels, diffPercentage: 100 } } const { width, height } = baseline const diff = new PNG({ width, height }) const totalPixels = width * height const diffPixels = pixelmatch(baseline.data, current.data, diff.data, width, height, { threshold: 0.1, // Per-pixel threshold for color difference includeAA: false, // Ignore anti-aliasing differences }) const diffPercentage = (diffPixels / totalPixels) * 100 // Save diff image if there are differences if (diffPixels > 0) { fs.mkdirSync(path.dirname(diffPath), { recursive: true }) fs.writeFileSync(diffPath, PNG.sync.write(diff)) } return { diffPixels, totalPixels, diffPercentage } } async function main(): Promise<void> { const config = parseArgs() console.log('Visual Regression Comparison') console.log('============================') console.log(`Baseline directory: ${config.baseline}`) console.log(`Current directory: ${config.current}`) console.log(`Diff directory: ${config.diff}`) console.log(`Threshold: ${config.threshold * 100}%`) console.log('') const baselineScreenshots = new Set(getScreenshots(config.baseline)) const currentScreenshots = new Set(getScreenshots(config.current)) const allScreenshots = new Set([...baselineScreenshots, ...currentScreenshots]) const results: ComparisonResult[] = [] // Ensure diff directory exists fs.mkdirSync(config.diff, { recursive: true }) for (const screenshot of allScreenshots) { const baselinePath = path.join(config.baseline, screenshot) const currentPath = path.join(config.current, screenshot) const diffPath = path.join(config.diff, `diff-${screenshot}`) const inBaseline = baselineScreenshots.has(screenshot) const inCurrent = currentScreenshots.has(screenshot) if (!inBaseline && inCurrent) { // New screenshot results.push({ name: screenshot, status: 'new', }) console.log(`[NEW] ${screenshot}`) } else if (inBaseline && !inCurrent) { // Missing screenshot results.push({ name: screenshot, status: 'missing', }) console.log(`[MISSING] ${screenshot}`) } else { // Compare screenshots const { diffPixels, totalPixels, diffPercentage } = compareImages( baselinePath, currentPath, diffPath, config.threshold ) const passed = diffPercentage <= config.threshold * 100 results.push({ name: screenshot, status: passed ? 'pass' : 'fail', diffPixels, totalPixels, diffPercentage, diffPath: passed ? undefined : diffPath, }) const statusIcon = passed ? '✓' : '✗' console.log(`[${statusIcon}] ${screenshot}: ${diffPercentage.toFixed(3)}% diff`) } } // Generate summary const summary = { total: results.length, passed: results.filter((r) => r.status === 'pass').length, failed: results.filter((r) => r.status === 'fail').length, new: results.filter((r) => r.status === 'new').length, missing: results.filter((r) => r.status === 'missing').length, } const report: ComparisonReport = { timestamp: new Date().toISOString(), baselineDir: config.baseline, currentDir: config.current, diffDir: config.diff, threshold: config.threshold, results, summary, } // Write report fs.writeFileSync(config.output, JSON.stringify(report, null, 2)) console.log('') console.log('Summary') console.log('-------') console.log(`Total: ${summary.total}`) console.log(`Passed: ${summary.passed}`) console.log(`Failed: ${summary.failed}`) console.log(`New: ${summary.new}`) console.log(`Missing: ${summary.missing}`) console.log('') console.log(`Report written to: ${config.output}`) // Exit with error code if there are failures if (summary.failed > 0 || summary.missing > 0) { process.exit(1) } } main().catch((error) => { console.error('Error:', error) process.exit(1) }) |