-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtakeElementScreenshot.ts
More file actions
46 lines (38 loc) · 1.31 KB
/
takeElementScreenshot.ts
File metadata and controls
46 lines (38 loc) · 1.31 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
40
41
42
43
44
45
46
import {join} from 'node:path';
import {
ADDITIONAL_STEP_TIMEOUT,
LogEventType,
SCREENSHOTS_DIRECTORY_PATH,
} from '../constants/internal';
import {step} from '../step';
import {getDimensionsString, getPngDimensions} from '../utils/screenshot';
import type {Locator} from '@playwright/test';
import type {Selector} from '../types/internal';
type Options = Parameters<Locator['screenshot']>[0];
/**
* Takes a screenshot of the specified element.
*/
export const takeElementScreenshot = async (
selector: Selector,
options: Options = {},
): Promise<void> => {
const {path: pathToScreenshot, ...optionsWithoutPath} = options;
const {timeout} = options;
await step(
'Take a screenshot of the element',
async () => {
if (pathToScreenshot !== undefined) {
// eslint-disable-next-line no-param-reassign
options.path = join(SCREENSHOTS_DIRECTORY_PATH, pathToScreenshot);
}
const screenshot = await selector.getPlaywrightLocator().screenshot(options);
const dimensions = getDimensionsString(getPngDimensions(screenshot));
return {dimensions};
},
{
payload: {pathToScreenshot, ...optionsWithoutPath, selector},
...(timeout !== undefined ? {timeout: timeout + ADDITIONAL_STEP_TIMEOUT} : undefined),
type: LogEventType.InternalAction,
},
);
};