-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtakeScreenshotsOnErrorIfNeeded.ts
More file actions
64 lines (52 loc) · 1.93 KB
/
takeScreenshotsOnErrorIfNeeded.ts
File metadata and controls
64 lines (52 loc) · 1.93 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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
import {join} from 'node:path';
// eslint-disable-next-line import/no-internal-modules
import {takeScreenshot} from '../../actions/takeScreenshot';
import {getRetryIndex} from '../../context/retryIndex';
import {getFullPackConfig} from '../config';
import {addTimeoutToPromise} from '../promise';
import {getScreenshotFileNames} from './getScreenshotFileNames';
import type {TestStaticOptions} from '../../types/internal';
const errorScreenshotTimeoutInMs = 2_000;
/**
* Takes page screenshot and full page screenshot, if needed.
* @internal
*/
export const takeScreenshotsOnErrorIfNeeded = async (
testStaticOptions: TestStaticOptions,
): Promise<void> => {
const {
takeFullPageScreenshotOnError: takeFullPageScreenshotOnErrorFromConfig,
takeViewportScreenshotOnError: takeViewportScreenshotOnErrorFromConfig,
} = getFullPackConfig();
const {options} = testStaticOptions;
const takeFullPageScreenshotOnError =
options.takeFullPageScreenshotOnError ?? takeFullPageScreenshotOnErrorFromConfig;
const takeViewportScreenshotOnError =
options.takeViewportScreenshotOnError ?? takeViewportScreenshotOnErrorFromConfig;
if (!takeFullPageScreenshotOnError && !takeViewportScreenshotOnError) {
return;
}
const retryIndex = getRetryIndex();
const retryDirectoryName = `retry${retryIndex}`;
const {fullPage, viewport} = await getScreenshotFileNames(retryDirectoryName, testStaticOptions);
if (takeFullPageScreenshotOnError) {
await addTimeoutToPromise(
takeScreenshot({
fullPage: true,
path: join(retryDirectoryName, fullPage),
timeout: errorScreenshotTimeoutInMs,
}),
errorScreenshotTimeoutInMs,
);
}
if (takeViewportScreenshotOnError) {
await addTimeoutToPromise(
takeScreenshot({
fullPage: false,
path: join(retryDirectoryName, viewport),
timeout: errorScreenshotTimeoutInMs,
}),
errorScreenshotTimeoutInMs,
);
}
};