-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgetScreenshotFileNames.ts
More file actions
66 lines (52 loc) · 2.09 KB
/
Copy pathgetScreenshotFileNames.ts
File metadata and controls
66 lines (52 loc) · 2.09 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
65
66
import {readdir} from 'node:fs/promises';
import {join} from 'node:path';
import {SCREENSHOTS_DIRECTORY_PATH} from '../../constants/internal';
import {assertValueIsTrue} from '../asserts';
import type {TestStaticOptions} from '../../types/internal';
type Return = Readonly<{fullPage: string; viewport: string}>;
const fullPageSuffix = '.fullPage.png';
const viewportSuffix = '.viewport.png';
/**
* Get filenames with page screenshots taken on test error.
* @internal
*/
export const getScreenshotFileNames = async (
directoryName: string,
testStaticOptions: TestStaticOptions,
): Promise<Return> => {
const directoryPath = join(SCREENSHOTS_DIRECTORY_PATH, directoryName);
const alreadyExistingScreenshots = await readdir(directoryPath).catch((): string[] => []);
const fileNamePrefix = `${testStaticOptions.name}.`.replace(/[^a-zA-Z0-9\-._~]/g, '_');
const currentFullPageScreenshotsIndex = Math.max(
0,
...alreadyExistingScreenshots.map((fileName): number => {
if (!fileName.startsWith(fileNamePrefix) || !fileName.endsWith(fullPageSuffix)) {
return 0;
}
return Number(fileName.slice(fileNamePrefix.length, -fullPageSuffix.length));
}),
);
assertValueIsTrue(
Number.isInteger(currentFullPageScreenshotsIndex),
'currentFullPageScreenshotsIndex is integer',
{alreadyExistingScreenshots, directoryName, testStaticOptions},
);
const currentViewportScreenshotsIndex = Math.max(
0,
...alreadyExistingScreenshots.map((fileName): number => {
if (!fileName.startsWith(fileNamePrefix) || !fileName.endsWith(viewportSuffix)) {
return 0;
}
return Number(fileName.slice(fileNamePrefix.length, -viewportSuffix.length));
}),
);
assertValueIsTrue(
Number.isInteger(currentViewportScreenshotsIndex),
'currentViewportScreenshotsIndex is integer',
{alreadyExistingScreenshots, directoryName, testStaticOptions},
);
return {
fullPage: `${fileNamePrefix}${currentFullPageScreenshotsIndex + 1}${fullPageSuffix}`,
viewport: `${fileNamePrefix}${currentViewportScreenshotsIndex + 1}${viewportSuffix}`,
};
};