|
| 1 | +#!/usr/bin/env ts-node |
| 2 | +/* ============================================================================ |
| 3 | + * Copyright (c) Palo Alto Networks |
| 4 | + * |
| 5 | + * This source code is licensed under the MIT license found in the |
| 6 | + * LICENSE file in the root directory of this source tree. |
| 7 | + * ========================================================================== */ |
| 8 | + |
| 9 | +import fs from "fs"; |
| 10 | +import path from "path"; |
| 11 | + |
| 12 | +import { XMLParser } from "fast-xml-parser"; |
| 13 | +import pixelmatch from "pixelmatch"; |
| 14 | +import { chromium } from "playwright"; |
| 15 | +import { PNG } from "pngjs"; |
| 16 | + |
| 17 | +interface Options { |
| 18 | + previewUrl: string; |
| 19 | + outputDir: string; |
| 20 | + tolerance: number; |
| 21 | + width: number; |
| 22 | + viewHeight: number; |
| 23 | + summaryFile: string; |
| 24 | +} |
| 25 | + |
| 26 | +function parseArgs(): Options { |
| 27 | + const args = process.argv.slice(2); |
| 28 | + const opts: Options = { |
| 29 | + previewUrl: "", |
| 30 | + outputDir: "visual_diffs", |
| 31 | + tolerance: 0, |
| 32 | + width: 1280, |
| 33 | + viewHeight: 1024, |
| 34 | + summaryFile: "visual_diffs/results.json", |
| 35 | + }; |
| 36 | + for (let i = 0; i < args.length; i++) { |
| 37 | + const arg = args[i]; |
| 38 | + switch (arg) { |
| 39 | + case "-p": |
| 40 | + case "--preview-url": |
| 41 | + opts.previewUrl = args[++i]; |
| 42 | + break; |
| 43 | + case "-o": |
| 44 | + case "--output-dir": |
| 45 | + opts.outputDir = args[++i]; |
| 46 | + break; |
| 47 | + case "-t": |
| 48 | + case "--tolerance": |
| 49 | + opts.tolerance = Number(args[++i]); |
| 50 | + break; |
| 51 | + case "-w": |
| 52 | + case "--width": |
| 53 | + opts.width = Number(args[++i]); |
| 54 | + break; |
| 55 | + case "-v": |
| 56 | + case "--view-height": |
| 57 | + opts.viewHeight = Number(args[++i]); |
| 58 | + break; |
| 59 | + case "-s": |
| 60 | + case "--summary-file": |
| 61 | + opts.summaryFile = args[++i]; |
| 62 | + break; |
| 63 | + } |
| 64 | + } |
| 65 | + return opts; |
| 66 | +} |
| 67 | + |
| 68 | +async function fetchSitemap(url: string): Promise<string> { |
| 69 | + const resp = await fetch(url); |
| 70 | + if (!resp.ok) throw new Error(`Failed to fetch ${url}: ${resp.status}`); |
| 71 | + return resp.text(); |
| 72 | +} |
| 73 | + |
| 74 | +function parseUrlsFromSitemap(xml: string): string[] { |
| 75 | + const parser = new XMLParser(); |
| 76 | + const result = parser.parse(xml); |
| 77 | + const urls = result.urlset?.url || []; |
| 78 | + const arr = Array.isArray(urls) ? urls : [urls]; |
| 79 | + return arr.map((u: any) => String(u.loc).trim()).filter(Boolean); |
| 80 | +} |
| 81 | + |
| 82 | +async function screenshotFullPage(page: any, url: string, outputPath: string) { |
| 83 | + await page.goto(url, { waitUntil: "networkidle" }); |
| 84 | + await page.evaluate(() => { |
| 85 | + document.querySelectorAll("details").forEach((d) => { |
| 86 | + const summary = d.querySelector("summary"); |
| 87 | + if (!d.open && summary) (summary as HTMLElement).click(); |
| 88 | + (d as HTMLDetailsElement).open = true; |
| 89 | + d.setAttribute("data-collapsed", "false"); |
| 90 | + }); |
| 91 | + }); |
| 92 | + await page.waitForTimeout(500); |
| 93 | + await fs.promises.mkdir(path.dirname(outputPath), { recursive: true }); |
| 94 | + await page.screenshot({ path: outputPath, fullPage: true }); |
| 95 | +} |
| 96 | + |
| 97 | +function compareImages( |
| 98 | + prodPath: string, |
| 99 | + prevPath: string, |
| 100 | + diffPath: string, |
| 101 | + tolerance: number |
| 102 | +): boolean { |
| 103 | + const prod = PNG.sync.read(fs.readFileSync(prodPath)); |
| 104 | + const prev = PNG.sync.read(fs.readFileSync(prevPath)); |
| 105 | + if (prod.width !== prev.width || prod.height !== prev.height) { |
| 106 | + console.warn(`Size mismatch for ${prevPath}`); |
| 107 | + return false; |
| 108 | + } |
| 109 | + const diff = new PNG({ width: prod.width, height: prod.height }); |
| 110 | + const numDiff = pixelmatch( |
| 111 | + prod.data, |
| 112 | + prev.data, |
| 113 | + diff.data, |
| 114 | + prod.width, |
| 115 | + prod.height, |
| 116 | + { |
| 117 | + threshold: tolerance, |
| 118 | + } |
| 119 | + ); |
| 120 | + if (numDiff > 0) { |
| 121 | + fs.mkdirSync(path.dirname(diffPath), { recursive: true }); |
| 122 | + fs.writeFileSync(diffPath, PNG.sync.write(diff)); |
| 123 | + return false; |
| 124 | + } |
| 125 | + return true; |
| 126 | +} |
| 127 | + |
| 128 | +async function run() { |
| 129 | + const opts = parseArgs(); |
| 130 | + if (!opts.previewUrl) { |
| 131 | + throw new Error("Missing preview URL"); |
| 132 | + } |
| 133 | + if (!opts.previewUrl.endsWith("/")) opts.previewUrl += "/"; |
| 134 | + |
| 135 | + const sitemapXml = await fetchSitemap("https://pan.dev/sitemap.xml"); |
| 136 | + const paths = parseUrlsFromSitemap(await sitemapXml); |
| 137 | + console.log(`Found ${paths.length} paths.`); |
| 138 | + |
| 139 | + const browser = await chromium.launch(); |
| 140 | + const context = await browser.newContext({ |
| 141 | + viewport: { width: opts.width, height: opts.viewHeight }, |
| 142 | + }); |
| 143 | + const page = await context.newPage(); |
| 144 | + |
| 145 | + let total = 0; |
| 146 | + let matches = 0; |
| 147 | + let mismatches = 0; |
| 148 | + let skipped = 0; |
| 149 | + const pages: { path: string; status: string }[] = []; |
| 150 | + |
| 151 | + for (const url of paths) { |
| 152 | + total += 1; |
| 153 | + const cleanPath = |
| 154 | + new URL(url).pathname.replace(/^\//, "").replace(/\/$/, "") || "root"; |
| 155 | + const prodSnap = path.join(opts.outputDir, "prod", `${cleanPath}.png`); |
| 156 | + const prevSnap = path.join(opts.outputDir, "preview", `${cleanPath}.png`); |
| 157 | + const diffImg = path.join(opts.outputDir, "diff", `${cleanPath}.png`); |
| 158 | + try { |
| 159 | + await screenshotFullPage(page, url, prodSnap); |
| 160 | + await screenshotFullPage( |
| 161 | + page, |
| 162 | + new URL(cleanPath, opts.previewUrl).toString(), |
| 163 | + prevSnap |
| 164 | + ); |
| 165 | + if (compareImages(prodSnap, prevSnap, diffImg, opts.tolerance)) { |
| 166 | + console.log(`MATCH: /${cleanPath}`); |
| 167 | + matches += 1; |
| 168 | + pages.push({ path: `/${cleanPath}`, status: "match" }); |
| 169 | + } else { |
| 170 | + console.warn(`DIFF: /${cleanPath}`); |
| 171 | + mismatches += 1; |
| 172 | + pages.push({ path: `/${cleanPath}`, status: "diff" }); |
| 173 | + } |
| 174 | + } catch (e) { |
| 175 | + console.warn(`SKIP: /${cleanPath} - ${e}`); |
| 176 | + skipped += 1; |
| 177 | + pages.push({ path: `/${cleanPath}`, status: "skip" }); |
| 178 | + } |
| 179 | + } |
| 180 | + |
| 181 | + await browser.close(); |
| 182 | + console.log( |
| 183 | + `Total: ${total}, Matches: ${matches}, Diffs: ${mismatches}, Skipped: ${skipped}` |
| 184 | + ); |
| 185 | + |
| 186 | + await fs.promises.mkdir(path.dirname(opts.summaryFile), { recursive: true }); |
| 187 | + await fs.promises.writeFile( |
| 188 | + opts.summaryFile, |
| 189 | + JSON.stringify( |
| 190 | + { summary: { total, matches, mismatches, skipped }, pages }, |
| 191 | + null, |
| 192 | + 2 |
| 193 | + ) |
| 194 | + ); |
| 195 | +} |
| 196 | + |
| 197 | +run().catch((e) => { |
| 198 | + console.error(e); |
| 199 | + process.exit(1); |
| 200 | +}); |
0 commit comments