Add .screenshot() method to mock tests

This commit is contained in:
Fedor Indutny
2025-08-28 09:44:44 -07:00
committed by GitHub
parent a832bafc04
commit f1faca416e

View File

@@ -221,6 +221,7 @@ export class Bootstrap {
#storagePath?: string;
#timestamp: number = Date.now() - WEEK;
#lastApp?: App;
#screenshotId = 0;
readonly #randomId = crypto.randomBytes(8).toString('hex');
constructor(options: BootstrapOptions = {}) {
@@ -545,6 +546,28 @@ export class Bootstrap {
}
}
public async screenshot(
app: App | undefined = this.#lastApp,
testName?: string
): Promise<void> {
if (!app) {
return;
}
const outDir = await this.#getArtifactsDir(testName);
if (outDir == null) {
return;
}
const window = await app.getWindow();
const screenshot = await window.screenshot();
const id = this.#screenshotId;
this.#screenshotId += 1;
await fs.writeFile(path.join(outDir, `screenshot-${id}.png`), screenshot);
}
public async saveLogs(
app: App | undefined = this.#lastApp,
testName?: string
@@ -566,11 +589,7 @@ export class Bootstrap {
?.context()
.tracing.stop({ path: path.join(outDir, 'trace.zip') });
}
if (app) {
const window = await app.getWindow();
const screenshot = await window.screenshot();
await fs.writeFile(path.join(outDir, 'screenshot.png'), screenshot);
}
await this.screenshot(app, testName);
}
public async createScreenshotComparator(