-
Notifications
You must be signed in to change notification settings - Fork 0
/
plugin.mjs
39 lines (33 loc) · 1.35 KB
/
plugin.mjs
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
import fs from 'fs';
import path from 'path';
import mkdirp from 'mkdirp';
import { promisify } from 'util';
const writeFile = promisify(fs.writeFile);
const saveImage = async ({ filePath, content }) => {
await mkdirp(path.dirname(filePath));
await writeFile(filePath, content);
}
export function takeScreenshotPlugin() {
return {
name: 'take-screen-command',
async executeCommand({ command, payload, session }) {
if (command === 'take-screenshot') {
// handle specific behavior for playwright
if (session.browser.type === 'playwright') {
const page = session.browser.getPage(session.id);
const destinationFolder = payload.folder || 'evidences';
const screenshot = await page.screenshot();
const sanitizedName = payload.name = payload.name.replaceAll('/', '-').replaceAll('"', `'`);
const sanitizedFileName = `${sanitizedName}_${session.browser.product}`;
await saveImage({ filePath: `./${destinationFolder}/${sanitizedFileName}.png`, content: screenshot})
return true;
}
// you might not be able to support all browser launchers
throw new Error(
`Taking screenshots is not supported for browser type ${session.browser.type}.`,
);
}
return undefined;
},
};
}