( context: BrowserCommandContext, name: string, options: Omit<ScreenshotCommandOptions, 'base64'>, )
| 18 | * @throws {Error} If the function is not called within a test or if the browser provider does not support screenshots. |
| 19 | */ |
| 20 | export 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 | } |
nothing calls this directly
no test coverage detected