| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -105,6 +105,11 @@ export const page: { | |||
| 105 | 105 | The `getBy*` API is explained at [Locators API](/guide/browser/locators). | |
| 106 | 106 | ::: | |
| 107 | 107 | ||
| 108 | + ::: warning WARNING <Version>3.1.2</Version> | ||
| 109 | + Note that `screenshot` will always return a base64 string if `save` is set to `false`. | ||
| 110 | + The `path` is also ignored in that case. | ||
| 111 | + ::: | ||
| 112 | + | ||
| 108 | 113 | ## `cdp` | |
| 109 | 114 | ||
| 110 | 115 | The `cdp` export returns the current Chrome DevTools Protocol session. It is mostly useful to library authors to build tools on top of it. | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -769,6 +769,7 @@ await languages.selectOptions([ | |||
| 769 | 769 | ### screenshot | |
| 770 | 770 | ||
| 771 | 771 | ```ts | |
| 772 | + function screenshot(options: LocatorScreenshotOptions & { save: false }): Promise<string> | ||
| 772 | 773 | function screenshot(options: LocatorScreenshotOptions & { base64: true }): Promise<{ | |
| 773 | 774 | path: string | |
| 774 | 775 | base64: string | |
@@ -797,6 +798,11 @@ const { path, base64 } = await button.screenshot({ | |||
| 797 | 798 | // bas64 - base64 encoded string of the screenshot | |
| 798 | 799 | ``` | |
| 799 | 800 | ||
| 801 | + ::: warning WARNING <Version>3.1.2</Version> | ||
| 802 | + Note that `screenshot` will always return a base64 string if `save` is set to `false`. | ||
| 803 | + The `path` is also ignored in that case. | ||
| 804 | + ::: | ||
| 805 | + | ||
| 800 | 806 | ### query | |
| 801 | 807 | ||
| 802 | 808 | ```ts | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -29,12 +29,19 @@ export interface ScreenshotOptions { | |||
| 29 | 29 | element?: Element | Locator | |
| 30 | 30 | /** | |
| 31 | 31 | * Path relative to the current test file. | |
| 32 | + * @default `__screenshots__/${testFileName}/${testName}.png` | ||
| 32 | 33 | */ | |
| 33 | 34 | path?: string | |
| 34 | 35 | /** | |
| 35 | 36 | * Will also return the base64 encoded screenshot alongside the path. | |
| 36 | 37 | */ | |
| 37 | 38 | base64?: boolean | |
| 39 | + /** | ||
| 40 | + * Keep the screenshot on the file system. If file is not saved, | ||
| 41 | + * `page.screenshot` always returns `base64` screenshot. | ||
| 42 | + * @default true | ||
| 43 | + */ | ||
| 44 | + save?: boolean | ||
| 38 | 45 | } | |
| 39 | 46 | ||
| 40 | 47 | export interface BrowserCommands { | |
@@ -552,11 +559,16 @@ export interface BrowserPage extends LocatorSelectors { | |||
| 552 | 559 | * Make a screenshot of the test iframe or a specific element. | |
| 553 | 560 | * @returns Path to the screenshot file or path and base64. | |
| 554 | 561 | */ | |
| 562 | + screenshot(options: Omit<ScreenshotOptions, 'save'> & { save: false }): Promise<string> | ||
| 555 | 563 | screenshot(options: Omit<ScreenshotOptions, 'base64'> & { base64: true }): Promise<{ | |
| 556 | 564 | path: string | |
| 557 | 565 | base64: string | |
| 558 | 566 | }> | |
| 559 | - screenshot(options?: ScreenshotOptions): Promise<string> | ||
| 567 | + screenshot(options?: Omit<ScreenshotOptions, 'base64'>): Promise<string> | ||
| 568 | + screenshot(options?: ScreenshotOptions): Promise<string | { | ||
| 569 | + path: string | ||
| 570 | + base64: string | ||
| 571 | + }> | ||
| 560 | 572 | /** | |
| 561 | 573 | * Extend default `page` object with custom methods. | |
| 562 | 574 | */ | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -1,6 +1,6 @@ | |||
| 1 | 1 | import type { BrowserCommand, ResolvedConfig } from 'vitest/node' | |
| 2 | 2 | import type { ScreenshotOptions } from '../../../context' | |
| 3 | - import { mkdir } from 'node:fs/promises' | ||
| 3 | + import { mkdir, rm } from 'node:fs/promises' | ||
| 4 | 4 | import { normalize } from 'node:path' | |
| 5 | 5 | import { basename, dirname, relative, resolve } from 'pathe' | |
| 6 | 6 | import { PlaywrightBrowserProvider } from '../providers/playwright' | |
@@ -15,6 +15,12 @@ export const screenshot: BrowserCommand<[string, ScreenshotOptions]> = async ( | |||
| 15 | 15 | throw new Error(`Cannot take a screenshot without a test path`) | |
| 16 | 16 | } | |
| 17 | 17 | ||
| 18 | + options.save ??= true | ||
| 19 | + | ||
| 20 | + if (!options.save) { | ||
| 21 | + options.base64 = true | ||
| 22 | + } | ||
| 23 | + | ||
| 18 | 24 | const path = options.path | |
| 19 | 25 | ? resolve(dirname(context.testPath), options.path) | |
| 20 | 26 | : resolveScreenshotPath( | |
@@ -31,28 +37,28 @@ export const screenshot: BrowserCommand<[string, ScreenshotOptions]> = async ( | |||
| 31 | 37 | const element = context.iframe.locator(`${selector}`) | |
| 32 | 38 | const buffer = await element.screenshot({ | |
| 33 | 39 | ...config, | |
| 34 | - path: savePath, | ||
| 40 | + path: options.save ? savePath : undefined, | ||
| 35 | 41 | }) | |
| 36 | 42 | return returnResult(options, path, buffer) | |
| 37 | 43 | } | |
| 38 | 44 | ||
| 39 | 45 | const buffer = await context.iframe.locator('body').screenshot({ | |
| 40 | 46 | ...options, | |
| 41 | - path: savePath, | ||
| 47 | + path: options.save ? savePath : undefined, | ||
| 42 | 48 | }) | |
| 43 | 49 | return returnResult(options, path, buffer) | |
| 44 | 50 | } | |
| 45 | 51 | ||
| 46 | 52 | if (context.provider instanceof WebdriverBrowserProvider) { | |
| 47 | 53 | 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}`) | ||
| 53 | 57 | ||
| 54 | - const element = await page.$(`${options.element}`) | ||
| 55 | 58 | const buffer = await element.saveScreenshot(savePath) | |
| 59 | + if (!options.save) { | ||
| 60 | + await rm(savePath, { force: true }) | ||
| 61 | + } | ||
| 56 | 62 | return returnResult(options, path, buffer) | |
| 57 | 63 | } | |
| 58 | 64 | ||
@@ -84,6 +90,9 @@ function returnResult( | |||
| 84 | 90 | path: string, | |
| 85 | 91 | buffer: Buffer, | |
| 86 | 92 | ) { | |
| 93 | + if (!options.save) { | ||
| 94 | + return buffer.toString('base64') | ||
| 95 | + } | ||
| 87 | 96 | if (options.base64) { | |
| 88 | 97 | return { path, base64: buffer.toString('base64') } | |
| 89 | 98 | } | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -65,6 +65,20 @@ describe('dom related activity', () => { | |||
| 65 | 65 | expect(base64).toBeTypeOf('string') | |
| 66 | 66 | }) | |
| 67 | 67 | ||
| 68 | + test('doesn\'t save base64', async () => { | ||
| 69 | + const wrapper = createWrapper() | ||
| 70 | + const div = createNode() | ||
| 71 | + wrapper.appendChild(div) | ||
| 72 | + | ||
| 73 | + const base64 = await page.screenshot({ | ||
| 74 | + element: wrapper, | ||
| 75 | + save: false, | ||
| 76 | + }) | ||
| 77 | + expect(base64).toBeTypeOf('string') | ||
| 78 | + expect(base64).not.toContain('__screenshots__') | ||
| 79 | + expect(base64).not.toContain('dom.test.ts') | ||
| 80 | + }) | ||
| 81 | + | ||
| 68 | 82 | test('shadow dom screenshot', async () => { | |
| 69 | 83 | const wrapper = createWrapper() | |
| 70 | 84 | const div = createNode() | |
| Back | FazBrowse Home | New Git URL |
0 commit comments