From 27dba6548cb1ebb3302212c7312b68cc6c181a75 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Greg=20Berg=C3=A9?= Date: Fri, 11 Oct 2024 21:08:59 +0200 Subject: [PATCH] feat(playwright): allow to customize screenshots root folder (#153) --- packages/playwright/docs/index.mdx | 1 + packages/playwright/src/index.ts | 25 +++++++++++++++++++------ 2 files changed, 20 insertions(+), 6 deletions(-) diff --git a/packages/playwright/docs/index.mdx b/packages/playwright/docs/index.mdx index ffae054..339f08c 100644 --- a/packages/playwright/docs/index.mdx +++ b/packages/playwright/docs/index.mdx @@ -210,6 +210,7 @@ export default defineConfig({ - `options.argosCSS`: Specific CSS applied during the screenshot process. More on [injecting CSS](/injecting-css) - `options.disableHover`: Disable hover effects by moving the mouse to the top-left corner of the page. Default to `true`. - `options.threshold`: Sensitivity threshold between 0 and 1. The higher the threshold, the less sensitive the diff will be. Default to `0.5`. +- `options.root`: Folder where the screenshots will be saved if not using the Argos reporter. Default to `./screenshots`. Unlike [Playwright's `screenshot` method](https://playwright.dev/docs/api/class-page#page-screenshot), set `fullPage` option to `true` by default. Feel free to override this option if you prefer partial screenshots of your pages. diff --git a/packages/playwright/src/index.ts b/packages/playwright/src/index.ts index 05ee049..99bc534 100644 --- a/packages/playwright/src/index.ts +++ b/packages/playwright/src/index.ts @@ -28,7 +28,7 @@ import { getAttachmentName } from "./attachment"; import { getLibraryMetadata, getTestMetadataFromTestInfo } from "./metadata"; import { checkIsUsingArgosReporter } from "./util"; -const screenshotFolder = "./screenshots"; +const DEFAULT_SCREENSHOT_ROOT = "./screenshots"; type LocatorOptions = Parameters[1]; @@ -65,6 +65,12 @@ export type ArgosScreenshotOptions = { * @default 0.5 */ threshold?: number; + + /** + * Folder where the screenshots will be saved if not using the Argos reporter. + * @default "./screenshots" + */ + root?: string; } & LocatorOptions & ScreenshotOptions & ScreenshotOptions; @@ -179,8 +185,15 @@ export async function argosScreenshot( */ options: ArgosScreenshotOptions = {}, ) { - const { element, has, hasText, viewports, argosCSS, ...playwrightOptions } = - options; + const { + element, + has, + hasText, + viewports, + argosCSS, + root = DEFAULT_SCREENSHOT_ROOT, + ...playwrightOptions + } = options; if (!page) { throw new Error("A Playwright `page` object is required."); } @@ -201,7 +214,7 @@ export async function argosScreenshot( await Promise.all([ // Create the screenshot folder if it doesn't exist - useArgosReporter ? null : mkdir(screenshotFolder, { recursive: true }), + useArgosReporter ? null : mkdir(root, { recursive: true }), // Inject Argos script into the page injectArgos(page), ]); @@ -275,10 +288,10 @@ export async function argosScreenshot( const screenshotPath = useArgosReporter && testInfo ? testInfo.outputPath("argos", `${names.name}.png`) - : resolve(screenshotFolder, `${names.name}.png`); + : resolve(root, `${names.name}.png`); const dir = dirname(screenshotPath); - if (dir !== screenshotFolder) { + if (dir !== root) { await mkdir(dirname(screenshotPath), { recursive: true }); }