11import type { BrowserCommand , BrowserCommandContext , ResolvedConfig } from 'vitest/node'
22import type { ScreenshotOptions } from '../../../context'
33import { mkdir , rm } from 'node:fs/promises'
4- import { normalize } from 'node:path'
5- import { basename , dirname , relative , resolve } from 'pathe'
4+ import { normalize as platformNormalize } from 'node:path'
5+ import { nanoid } from '@vitest/utils/helpers'
6+ import { basename , dirname , normalize , relative , resolve } from 'pathe'
67import { PlaywrightBrowserProvider } from '../providers/playwright'
78import { WebdriverBrowserProvider } from '../providers/webdriverio'
89
@@ -50,8 +51,15 @@ export async function takeScreenshot(
5051 context . project . config ,
5152 options . path ,
5253 )
53- const savePath = normalize ( path )
54- await mkdir ( dirname ( path ) , { recursive : true } )
54+
55+ // playwright does not need a screenshot path if we don't intend to save it
56+ let savePath : string | undefined
57+
58+ if ( options . save ) {
59+ savePath = normalize ( path )
60+
61+ await mkdir ( dirname ( savePath ) , { recursive : true } )
62+ }
5563
5664 if ( context . provider instanceof PlaywrightBrowserProvider ) {
5765 const mask = options . mask ?. map ( selector => context . iframe . locator ( selector ) )
@@ -62,20 +70,27 @@ export async function takeScreenshot(
6270 const buffer = await element . screenshot ( {
6371 ...config ,
6472 mask,
65- path : options . save ? savePath : undefined ,
73+ path : savePath ,
6674 } )
6775 return { buffer, path }
6876 }
6977
7078 const buffer = await context . iframe . locator ( 'body' ) . screenshot ( {
7179 ...options ,
7280 mask,
73- path : options . save ? savePath : undefined ,
81+ path : savePath ,
7482 } )
7583 return { buffer, path }
7684 }
7785
7886 if ( context . provider instanceof WebdriverBrowserProvider ) {
87+ // webdriverio needs a path, so if one is not already set we create a temporary one
88+ if ( savePath === undefined ) {
89+ savePath = resolve ( context . project . tmpDir , nanoid ( ) )
90+
91+ await mkdir ( context . project . tmpDir , { recursive : true } )
92+ }
93+
7994 const page = context . provider . browser !
8095 const element = ! options . element
8196 ? await page . $ ( 'body' )
@@ -84,8 +99,9 @@ export async function takeScreenshot(
8499 // webdriverio expects the path to contain the extension and only works with PNG files
85100 const savePathWithExtension = savePath . endsWith ( '.png' ) ? savePath : `${ savePath } .png`
86101
102+ // there seems to be a bug in webdriverio, `X:/` gets appended to cwd, so we convert to `X:\`
87103 const buffer = await element . saveScreenshot (
88- savePathWithExtension ,
104+ platformNormalize ( savePathWithExtension ) ,
89105 )
90106 if ( ! options . save ) {
91107 await rm ( savePathWithExtension , { force : true } )
0 commit comments