|
| 1 | +import fs from "node:fs/promises"; |
| 2 | +import path from "node:path"; |
| 3 | +import type { Page } from "@playwright/test"; |
| 4 | +import type { UiCaptureScene, UiCaptureVariant } from "../scenes"; |
| 5 | +import { resolveCaptureTarget, waitForStableScene } from "./capture"; |
| 6 | +import { openPreviewScene } from "./prefs"; |
| 7 | + |
| 8 | +const OUTPUT_ROOT = path.resolve(process.cwd(), "output"); |
| 9 | +const SCREENSHOT_ROOT = path.join(OUTPUT_ROOT, "screenshots"); |
| 10 | + |
| 11 | +export interface CaptureSceneVariantArgs { |
| 12 | + scene: UiCaptureScene; |
| 13 | + variant: UiCaptureVariant; |
| 14 | +} |
| 15 | + |
| 16 | +export function buildScreenshotPath(scene: UiCaptureScene, variant: UiCaptureVariant) { |
| 17 | + return path.join( |
| 18 | + SCREENSHOT_ROOT, |
| 19 | + scene.category, |
| 20 | + scene.id, |
| 21 | + `${variant.device}__${variant.theme}__${variant.locale}.png` |
| 22 | + ); |
| 23 | +} |
| 24 | + |
| 25 | +export async function ensureParentDir(filePath: string) { |
| 26 | + await fs.mkdir(path.dirname(filePath), { recursive: true }); |
| 27 | +} |
| 28 | + |
| 29 | +async function openSettingsSection( |
| 30 | + page: Page, |
| 31 | + section: NonNullable<UiCaptureScene["settingsSection"]>, |
| 32 | + device: UiCaptureVariant["device"] |
| 33 | +) { |
| 34 | + const sectionOrder = { |
| 35 | + general: 0, |
| 36 | + providers: 1, |
| 37 | + appearance: 2, |
| 38 | + shortcuts: 3, |
| 39 | + } as const; |
| 40 | + |
| 41 | + const index = sectionOrder[section]; |
| 42 | + |
| 43 | + if (device === "mobile") { |
| 44 | + await page.locator(".settings-mobile-item").nth(index).click(); |
| 45 | + await page.locator(".settings-content--mobile, .settings-content").first().waitFor(); |
| 46 | + return; |
| 47 | + } |
| 48 | + |
| 49 | + if (section !== "general") { |
| 50 | + await page.locator(".settings-nav-item").nth(index).click(); |
| 51 | + } |
| 52 | +} |
| 53 | + |
| 54 | +export async function captureSceneVariant(page: Page, args: CaptureSceneVariantArgs) { |
| 55 | + await openPreviewScene(page, { |
| 56 | + sceneId: args.scene.id, |
| 57 | + device: args.variant.device, |
| 58 | + theme: args.variant.theme, |
| 59 | + locale: args.variant.locale, |
| 60 | + }); |
| 61 | + |
| 62 | + await waitForStableScene(page); |
| 63 | + |
| 64 | + if (args.scene.settingsSection) { |
| 65 | + await openSettingsSection(page, args.scene.settingsSection, args.variant.device); |
| 66 | + await waitForStableScene(page); |
| 67 | + } |
| 68 | + |
| 69 | + const filePath = buildScreenshotPath(args.scene, args.variant); |
| 70 | + await ensureParentDir(filePath); |
| 71 | + |
| 72 | + if (args.scene.fullPage) { |
| 73 | + await page.screenshot({ |
| 74 | + path: filePath, |
| 75 | + animations: "disabled", |
| 76 | + fullPage: true, |
| 77 | + scale: "device", |
| 78 | + }); |
| 79 | + return; |
| 80 | + } |
| 81 | + |
| 82 | + const target = await resolveCaptureTarget(page, args.scene.targetSelector); |
| 83 | + await target.screenshot({ |
| 84 | + path: filePath, |
| 85 | + animations: "disabled", |
| 86 | + scale: "device", |
| 87 | + }); |
| 88 | +} |
0 commit comments