-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscreenshot.test.ts
More file actions
66 lines (55 loc) · 2.26 KB
/
screenshot.test.ts
File metadata and controls
66 lines (55 loc) · 2.26 KB
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
import { expect, test } from "@playwright/test";
import fs from "node:fs";
import path from "node:path";
import { fileURLToPath, pathToFileURL } from "node:url";
const __dirname = path.dirname(fileURLToPath(import.meta.url));
const outputDir = path.resolve(__dirname, "../output");
test("generate HTML and PDF previews", async ({ page }) => {
test.setTimeout(120000);
const htmlFiles = fs
.readdirSync(outputDir)
.filter((file) => file.endsWith(".html") && file !== "viewer.html")
.sort();
const pdfFiles = fs
.readdirSync(outputDir)
.filter((file) => file.endsWith(".pdf"))
.sort();
expect(htmlFiles.length).toBeGreaterThan(0);
expect(pdfFiles.length).toBeGreaterThan(0);
for (const htmlFile of htmlFiles) {
const name = path.basename(htmlFile, ".html");
const htmlUrl = pathToFileURL(path.join(outputDir, htmlFile)).href;
await page.goto(htmlUrl, { waitUntil: "load" });
await page.evaluate(async () => {
if (document.fonts?.ready) {
await document.fonts.ready;
}
});
await page.waitForFunction(() => Array.from(document.images).every((img) => img.complete), { timeout: 10000 });
// Firefox treats file:// body as hidden; skip visibility check there
const isFirefox = page.context().browser()?.browserType().name() === "firefox";
if (!isFirefox) {
await expect(page.locator("body")).toBeVisible();
}
await page.screenshot({
path: path.join(outputDir, `${name}-html-preview.png`),
fullPage: true,
});
}
await import("../../scripts/build-viewer.mjs");
const viewerUrl = pathToFileURL(path.join(outputDir, "viewer.html")).href;
await page.goto(viewerUrl);
// Wait until every embedded PDF rendered into its canvas.
await page.waitForFunction(() => {
const canvases = document.querySelectorAll("canvas");
return canvases.length > 0 && Array.from(canvases).every((canvas) => canvas.width > 50);
}, { timeout: 15000 });
for (const pdfFile of pdfFiles) {
const name = path.basename(pdfFile, ".pdf");
const canvas = page.locator(`#c_${name}`);
await expect(canvas).toBeVisible();
await canvas.screenshot({
path: path.join(outputDir, `${name}-pdf-preview.png`),
});
}
});