1
1
import type { BrowserCommand , ResolvedConfig } from 'vitest/node'
2
2
import type { ScreenshotOptions } from '../../../context'
3
- import { mkdir } from 'node:fs/promises'
3
+ import { mkdir , rm } from 'node:fs/promises'
4
4
import { normalize } from 'node:path'
5
5
import { basename , dirname , relative , resolve } from 'pathe'
6
6
import { PlaywrightBrowserProvider } from '../providers/playwright'
@@ -15,6 +15,12 @@ export const screenshot: BrowserCommand<[string, ScreenshotOptions]> = async (
15
15
throw new Error ( `Cannot take a screenshot without a test path` )
16
16
}
17
17
18
+ options . save ??= true
19
+
20
+ if ( ! options . save ) {
21
+ options . base64 = true
22
+ }
23
+
18
24
const path = options . path
19
25
? resolve ( dirname ( context . testPath ) , options . path )
20
26
: resolveScreenshotPath (
@@ -31,28 +37,28 @@ export const screenshot: BrowserCommand<[string, ScreenshotOptions]> = async (
31
37
const element = context . iframe . locator ( `${ selector } ` )
32
38
const buffer = await element . screenshot ( {
33
39
...config ,
34
- path : savePath ,
40
+ path : options . save ? savePath : undefined ,
35
41
} )
36
42
return returnResult ( options , path , buffer )
37
43
}
38
44
39
45
const buffer = await context . iframe . locator ( 'body' ) . screenshot ( {
40
46
...options ,
41
- path : savePath ,
47
+ path : options . save ? savePath : undefined ,
42
48
} )
43
49
return returnResult ( options , path , buffer )
44
50
}
45
51
46
52
if ( context . provider instanceof WebdriverBrowserProvider ) {
47
53
const page = context . provider . browser !
48
- if ( ! options . element ) {
49
- const body = await page . $ ( 'body' )
50
- const buffer = await body . saveScreenshot ( savePath )
51
- return returnResult ( options , path , buffer )
52
- }
54
+ const element = ! options . element
55
+ ? await page . $ ( 'body' )
56
+ : await page . $ ( `${ options . element } ` )
53
57
54
- const element = await page . $ ( `${ options . element } ` )
55
58
const buffer = await element . saveScreenshot ( savePath )
59
+ if ( ! options . save ) {
60
+ await rm ( savePath , { force : true } )
61
+ }
56
62
return returnResult ( options , path , buffer )
57
63
}
58
64
@@ -84,6 +90,9 @@ function returnResult(
84
90
path : string ,
85
91
buffer : Buffer ,
86
92
) {
93
+ if ( ! options . save ) {
94
+ return buffer . toString ( 'base64' )
95
+ }
87
96
if ( options . base64 ) {
88
97
return { path, base64 : buffer . toString ( 'base64' ) }
89
98
}
0 commit comments