MCPcopy
hub / github.com/vitest-dev/vitest / takeScreenshot

Function takeScreenshot

packages/browser-webdriverio/src/commands/screenshot.ts:22–70  ·  view source on GitHub ↗
(
  context: BrowserCommandContext,
  name: string,
  options: Omit<ScreenshotCommandOptions, 'base64'>,
)

Source from the content-addressed store, hash-verified

20 * @throws {Error} If the function is not called within a test or if the browser provider does not support screenshots.
21 */
22export async function takeScreenshot(
23 context: BrowserCommandContext,
24 name: string,
25 options: Omit<ScreenshotCommandOptions, 'base64'>,
26): Promise<{ buffer: Buffer<ArrayBufferLike>; path: string }> {
27 if (!context.testPath) {
28 throw new Error(`Cannot take a screenshot without a test path`)
29 }
30
31 const path = resolveScreenshotPath(
32 context.testPath,
33 name,
34 context.project.config,
35 options.path,
36 )
37
38 // playwright does not need a screenshot path if we don't intend to save it
39 let savePath: string | undefined
40
41 if (options.save) {
42 savePath = normalize(path)
43
44 await mkdir(dirname(savePath), { recursive: true })
45 }
46
47 // webdriverio needs a path, so if one is not already set we create a temporary one
48 if (savePath === undefined) {
49 savePath = resolve(context.project.tmpDir, crypto.randomUUID())
50
51 await mkdir(context.project.tmpDir, { recursive: true })
52 }
53
54 const page = context.browser
55 const element = !options.element
56 ? await page.$('body')
57 : await page.$(`${options.element}`)
58
59 // webdriverio expects the path to contain the extension and only works with PNG files
60 const savePathWithExtension = savePath.endsWith('.png') ? savePath : `${savePath}.png`
61
62 // there seems to be a bug in webdriverio, `X:/` gets appended to cwd, so we convert to `X:\`
63 const buffer = await element.saveScreenshot(
64 platformNormalize(savePathWithExtension),
65 )
66 if (!options.save) {
67 await rm(savePathWithExtension, { force: true })
68 }
69 return { buffer, path }
70}

Callers

nothing calls this directly

Calls 5

resolveScreenshotPathFunction · 0.90
normalizeFunction · 0.85
$Method · 0.80
resolveFunction · 0.50
rmFunction · 0.50

Tested by

no test coverage detected