-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathplaywright-fixtures.js
More file actions
39 lines (36 loc) · 1.64 KB
/
playwright-fixtures.js
File metadata and controls
39 lines (36 loc) · 1.64 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
// playwright-fixtures.js
const { test } = require('@playwright/test');
const fs = require('fs');
const path = require('path');
// Custom fixture: capture screenshot for tests with 'screenshot' in title
// Usage: Add 'screenshot' to your test title to trigger screenshot capture
test.afterEach(async ({ page }, testInfo) => {
// Organize screenshots by suite and add timestamp, save to root screenshots/
const suiteName = testInfo.file ? path.basename(testInfo.file, path.extname(testInfo.file)) : 'suite';
const timestamp = new Date().toISOString().replace(/[:.]/g, '-');
const screenshotDir = path.join(process.cwd(), 'screenshots', suiteName);
if (!fs.existsSync(screenshotDir)) {
fs.mkdirSync(screenshotDir, { recursive: true });
}
// Filename: title + status + retry + timestamp
const fileName = `${testInfo.title.replace(/[^a-zA-Z0-9]/g, '_')}_${testInfo.status}${testInfo.retry ? `_retry${testInfo.retry}` : ''}_${timestamp}.png`;
const screenshotPath = path.join(screenshotDir, fileName);
try {
await page.screenshot({ path: screenshotPath, fullPage: true });
console.log(`[Fixture] Screenshot saved: ${screenshotPath}`);
} catch (err) {
console.error(`[Fixture] Screenshot failed: ${err}`);
}
// Video for tests with 'video' in title
if (testInfo.title.toLowerCase().includes('video')) {
const videoDir = path.join(process.cwd(), 'videos');
if (!fs.existsSync(videoDir)) {
fs.mkdirSync(videoDir);
}
const video = await page.video();
if (video) {
const fileName = testInfo.title.replace(/[^a-zA-Z0-9]/g, '_') + '.webm';
await video.saveAs(path.join(videoDir, fileName));
}
}
});