Skip to content

Commit 5456c99

Browse files
committed
chore: refactor afterScreenshot options
- implemented for saveWebScreen
1 parent 6e1f9b8 commit 5456c99

5 files changed

Lines changed: 503 additions & 63 deletions

File tree

packages/image-comparison-core/src/commands/saveWebScreen.test.ts

Lines changed: 19 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -89,20 +89,25 @@ vi.mock('../helpers/utils.js', () => ({
8989
canUseBidiScreenshot: vi.fn().mockReturnValue(false),
9090
getMethodOrWicOption: vi.fn().mockImplementation((method, wic, option) => method[option] ?? wic[option])
9191
}))
92-
vi.mock('../helpers/options.js', () => ({
93-
createBeforeScreenshotOptions: vi.fn().mockReturnValue({
94-
instanceData: { test: 'data' },
95-
addressBarShadowPadding: 6,
96-
toolBarShadowPadding: 6,
97-
disableBlinkingCursor: false,
98-
disableCSSAnimation: false,
99-
enableLayoutTesting: false,
100-
hideElements: [],
101-
noScrollBars: true,
102-
removeElements: [],
103-
waitForFontsLoaded: false,
104-
})
105-
}))
92+
vi.mock('../helpers/options.js', async (importOriginal) => {
93+
const actual = await importOriginal() as any
94+
return {
95+
...actual,
96+
createBeforeScreenshotOptions: vi.fn().mockReturnValue({
97+
instanceData: { test: 'data' },
98+
addressBarShadowPadding: 6,
99+
toolBarShadowPadding: 6,
100+
disableBlinkingCursor: false,
101+
disableCSSAnimation: false,
102+
enableLayoutTesting: false,
103+
hideElements: [],
104+
noScrollBars: true,
105+
removeElements: [],
106+
waitForFontsLoaded: false,
107+
}),
108+
// Let buildAfterScreenshotOptions use the real implementation
109+
}
110+
})
106111

107112
describe('saveWebScreen', () => {
108113
let takeBase64BiDiScreenshotSpy: ReturnType<typeof vi.fn>

packages/image-comparison-core/src/commands/saveWebScreen.ts

Lines changed: 10 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,11 @@ import beforeScreenshot from '../helpers/beforeScreenshot.js'
44
import afterScreenshot from '../helpers/afterScreenshot.js'
55
import { determineScreenRectangles } from '../methods/rectangles.js'
66
import type { BeforeScreenshotResult } from '../helpers/beforeScreenshot.interfaces.js'
7-
import type { AfterScreenshotOptions, ScreenshotOutput } from '../helpers/afterScreenshot.interfaces.js'
7+
import type { ScreenshotOutput } from '../helpers/afterScreenshot.interfaces.js'
88
import type { RectanglesOutput, ScreenRectanglesOptions } from '../methods/rectangles.interfaces.js'
99
import type { InternalSaveScreenMethodOptions } from './save.interfaces.js'
1010
import { canUseBidiScreenshot, getMethodOrWicOption } from '../helpers/utils.js'
11-
import { createBeforeScreenshotOptions } from '../helpers/options.js'
11+
import { createBeforeScreenshotOptions, buildAfterScreenshotOptions } from '../helpers/options.js'
1212

1313
/**
1414
* Saves an image of the viewport of the screen
@@ -33,8 +33,6 @@ export default async function saveWebScreen(
3333
const beforeOptions = createBeforeScreenshotOptions(instanceData, saveScreenOptions.method, saveScreenOptions.wic)
3434
const enrichedInstanceData: BeforeScreenshotResult = await beforeScreenshot(browserInstance, beforeOptions)
3535
const {
36-
browserName,
37-
browserVersion,
3836
deviceName,
3937
dimensions: {
4038
window: {
@@ -43,22 +41,13 @@ export default async function saveWebScreen(
4341
innerWidth,
4442
isEmulated,
4543
isLandscape,
46-
outerHeight,
47-
outerWidth,
48-
screenHeight,
49-
screenWidth,
5044
},
5145
},
5246
initialDevicePixelRatio,
5347
isAndroidChromeDriverScreenshot,
5448
isAndroidNativeWebScreenshot,
5549
isIOS,
5650
isMobile,
57-
isTestInBrowser,
58-
logName,
59-
name,
60-
platformName,
61-
platformVersion,
6251
} = enrichedInstanceData
6352

6453
// 3. Take the screenshot
@@ -98,43 +87,16 @@ export default async function saveWebScreen(
9887
}
9988

10089
// 5. The after the screenshot methods
101-
const afterOptions: AfterScreenshotOptions = {
102-
actualFolder: folders.actualFolder,
90+
const afterOptions = buildAfterScreenshotOptions({
10391
base64Image,
104-
disableBlinkingCursor: beforeOptions.disableBlinkingCursor,
105-
disableCSSAnimation: beforeOptions.disableCSSAnimation,
106-
enableLayoutTesting: beforeOptions.enableLayoutTesting,
107-
filePath: {
108-
browserName,
109-
deviceName,
110-
isMobile,
111-
savePerInstance,
112-
},
113-
fileName: {
114-
browserName,
115-
browserVersion,
116-
deviceName,
117-
devicePixelRatio: devicePixelRatio || NaN,
118-
formatImageName,
119-
isMobile,
120-
isTestInBrowser,
121-
logName,
122-
name,
123-
outerHeight: outerHeight || NaN,
124-
outerWidth: outerWidth || NaN,
125-
platformName,
126-
platformVersion,
127-
screenHeight: screenHeight || NaN,
128-
screenWidth: screenWidth || NaN,
129-
tag,
130-
},
131-
hideElements: beforeOptions.hideElements,
132-
hideScrollBars: beforeOptions.noScrollBars,
133-
isLandscape,
92+
folders,
93+
tag,
13494
isNativeContext,
135-
platformName: instanceData.platformName,
136-
removeElements: beforeOptions.removeElements,
137-
}
95+
instanceData,
96+
enrichedInstanceData,
97+
beforeOptions,
98+
wicOptions: { formatImageName, savePerInstance }
99+
})
138100

139101
// 6. Return the data
140102
return afterScreenshot(browserInstance, afterOptions)

packages/image-comparison-core/src/helpers/__snapshots__/options.test.ts.snap

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,79 @@
11
// Vitest Snapshot v1, https://vitest.dev/guide/snapshot.html
22

3+
exports[`options > buildAfterScreenshotOptions > should build options for native commands (no enriched data) 1`] = `
4+
{
5+
"actualFolder": "/test/actual",
6+
"base64Image": "test-screenshot-data",
7+
"fileName": {
8+
"browserName": "chrome",
9+
"browserVersion": "120.0.0",
10+
"deviceName": "desktop",
11+
"devicePixelRatio": 2,
12+
"formatImageName": "{tag}-{browserName}-{width}x{height}",
13+
"isMobile": false,
14+
"isTestInBrowser": false,
15+
"logName": "chrome",
16+
"name": "chrome",
17+
"outerHeight": NaN,
18+
"outerWidth": NaN,
19+
"platformName": "desktop",
20+
"platformVersion": "120.0.0",
21+
"screenHeight": 1080,
22+
"screenWidth": 1920,
23+
"tag": "test-element",
24+
},
25+
"filePath": {
26+
"browserName": "chrome",
27+
"deviceName": "desktop",
28+
"isMobile": false,
29+
"savePerInstance": false,
30+
},
31+
"isLandscape": false,
32+
"isNativeContext": true,
33+
"platformName": "desktop",
34+
}
35+
`;
36+
37+
exports[`options > buildAfterScreenshotOptions > should build options for web commands with enriched data 1`] = `
38+
{
39+
"actualFolder": "/test/actual",
40+
"base64Image": "test-screenshot-data",
41+
"disableBlinkingCursor": true,
42+
"disableCSSAnimation": false,
43+
"enableLayoutTesting": true,
44+
"fileName": {
45+
"browserName": "chrome",
46+
"browserVersion": "120.0.0",
47+
"deviceName": "desktop",
48+
"devicePixelRatio": 3,
49+
"formatImageName": "{tag}-{browserName}-{width}x{height}",
50+
"isMobile": false,
51+
"isTestInBrowser": true,
52+
"logName": "chrome",
53+
"name": "chrome",
54+
"outerHeight": 1000,
55+
"outerWidth": 1200,
56+
"platformName": "desktop",
57+
"platformVersion": "120.0.0",
58+
"screenHeight": 1440,
59+
"screenWidth": 2560,
60+
"tag": "test-element",
61+
},
62+
"filePath": {
63+
"browserName": "chrome",
64+
"deviceName": "desktop",
65+
"isMobile": false,
66+
"savePerInstance": false,
67+
},
68+
"hideElements": [],
69+
"hideScrollBars": true,
70+
"isLandscape": true,
71+
"isNativeContext": false,
72+
"platformName": "desktop",
73+
"removeElements": [],
74+
}
75+
`;
76+
377
exports[`options > defaultOptions > should return the default options when no options are provided 1`] = `
478
{
579
"addIOSBezelCorners": false,

0 commit comments

Comments
 (0)