Skip to content

Commit fc285cf

Browse files
added screenshots file
1 parent 1b2c93d commit fc285cf

1 file changed

Lines changed: 45 additions & 0 deletions

File tree

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
import fs from 'fs';
2+
import path from 'path';
3+
import { Page } from '@playwright/test';
4+
5+
export class ScreenshotManager {
6+
private readonly outputDir: string;
7+
private counter = 0;
8+
9+
constructor(
10+
private readonly page: Page,
11+
outputDir = path.resolve(process.cwd(), 'screenshots'),
12+
) {
13+
this.outputDir = outputDir;
14+
fs.mkdirSync(this.outputDir, { recursive: true });
15+
}
16+
17+
async takeAfterAction(actionName: string): Promise<string> {
18+
this.counter += 1;
19+
const safeName = actionName.replace(/[^a-zA-Z0-9_-]/g, '_');
20+
const fileName = `${String(this.counter).padStart(3, '0')}_after_${safeName}.png`;
21+
const filePath = path.join(this.outputDir, fileName);
22+
23+
await this.page.screenshot({ path: filePath, fullPage: false });
24+
return filePath;
25+
}
26+
27+
async takeFullPage(name: string): Promise<string> {
28+
this.counter += 1;
29+
const safeName = name.replace(/[^a-zA-Z0-9_-]/g, '_');
30+
const fileName = `${String(this.counter).padStart(3, '0')}_${safeName}.png`;
31+
const filePath = path.join(this.outputDir, fileName);
32+
33+
await this.page.screenshot({ path: filePath, fullPage: true });
34+
return filePath;
35+
}
36+
37+
async attachToWorld(attach: (data: Buffer, mediaType: string) => Promise<void>): Promise<void> {
38+
const buffer = await this.page.screenshot({ fullPage: true });
39+
await attach(buffer, 'image/png');
40+
}
41+
42+
getOutputDir(): string {
43+
return this.outputDir;
44+
}
45+
}

0 commit comments

Comments
 (0)