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

Function takeScreenshot

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

Source from the content-addressed store, hash-verified

18 * @throws {Error} If the function is not called within a test or if the browser provider does not support screenshots.
19 */
20export async function takeScreenshot(
21 context: BrowserCommandContext,
22 name: string,
23 options: Omit<ScreenshotCommandOptions, 'base64'>,
24): Promise<{ buffer: Buffer<ArrayBufferLike>; path: string }> {
25 if (!context.testPath) {
26 throw new Error(`Cannot take a screenshot without a test path`)
27 }
28
29 const path = resolveScreenshotPath(
30 context.testPath,
31 name,
32 context.project.config,
33 options.path,
34 )
35
36 // playwright does not need a screenshot path if we don't intend to save it
37 let savePath: string | undefined
38
39 if (options.save) {
40 savePath = normalize(path)
41
42 await mkdir(dirname(savePath), { recursive: true })
43 }
44
45 const mask = options.mask?.map(selector => getDescribedLocator(context, selector))
46
47 if (options.element) {
48 const { element: selector, ...config } = options
49 const element = getDescribedLocator(context, selector)
50 const buffer = await element.screenshot({
51 ...config,
52 mask,
53 path: savePath,
54 })
55 return { buffer, path }
56 }
57
58 const buffer = await getDescribedLocator(context, 'body').screenshot({
59 ...options,
60 mask,
61 path: savePath,
62 })
63 return { buffer, path }
64}

Callers

nothing calls this directly

Calls 4

resolveScreenshotPathFunction · 0.90
getDescribedLocatorFunction · 0.90
normalizeFunction · 0.85
screenshotMethod · 0.65

Tested by

no test coverage detected