11import type { BrowserCommand , ResolvedConfig } from 'vitest/node'
22import type { ScreenshotOptions } from '../../../context'
3- import { mkdir } from 'node:fs/promises'
3+ import { mkdir , rm } from 'node:fs/promises'
44import { normalize } from 'node:path'
55import { basename , dirname , relative , resolve } from 'pathe'
66import { PlaywrightBrowserProvider } from '../providers/playwright'
@@ -15,6 +15,12 @@ export const screenshot: BrowserCommand<[string, ScreenshotOptions]> = async (
1515 throw new Error ( `Cannot take a screenshot without a test path` )
1616 }
1717
18+ options . save ??= true
19+
20+ if ( ! options . save ) {
21+ options . base64 = true
22+ }
23+
1824 const path = options . path
1925 ? resolve ( dirname ( context . testPath ) , options . path )
2026 : resolveScreenshotPath (
@@ -31,28 +37,28 @@ export const screenshot: BrowserCommand<[string, ScreenshotOptions]> = async (
3137 const element = context . iframe . locator ( `${ selector } ` )
3238 const buffer = await element . screenshot ( {
3339 ...config ,
34- path : savePath ,
40+ path : options . save ? savePath : undefined ,
3541 } )
3642 return returnResult ( options , path , buffer )
3743 }
3844
3945 const buffer = await context . iframe . locator ( 'body' ) . screenshot ( {
4046 ...options ,
41- path : savePath ,
47+ path : options . save ? savePath : undefined ,
4248 } )
4349 return returnResult ( options , path , buffer )
4450 }
4551
4652 if ( context . provider instanceof WebdriverBrowserProvider ) {
4753 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 } ` )
5357
54- const element = await page . $ ( `${ options . element } ` )
5558 const buffer = await element . saveScreenshot ( savePath )
59+ if ( ! options . save ) {
60+ await rm ( savePath , { force : true } )
61+ }
5662 return returnResult ( options , path , buffer )
5763 }
5864
@@ -84,6 +90,9 @@ function returnResult(
8490 path : string ,
8591 buffer : Buffer ,
8692) {
93+ if ( ! options . save ) {
94+ return buffer . toString ( 'base64' )
95+ }
8796 if ( options . base64 ) {
8897 return { path, base64 : buffer . toString ( 'base64' ) }
8998 }
0 commit comments