Skip to content

Commit

Permalink
feat(playwright): allow to customize screenshots root folder (#153)
Browse files Browse the repository at this point in the history
  • Loading branch information
gregberge authored Oct 11, 2024
1 parent 0e6df53 commit 27dba65
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 6 deletions.
1 change: 1 addition & 0 deletions packages/playwright/docs/index.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -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.

Expand Down
25 changes: 19 additions & 6 deletions packages/playwright/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<Page["locator"]>[1];

Expand Down Expand Up @@ -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<LocatorScreenshotOptions> &
ScreenshotOptions<PageScreenshotOptions>;
Expand Down Expand Up @@ -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.");
}
Expand All @@ -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),
]);
Expand Down Expand Up @@ -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 });
}

Expand Down

0 comments on commit 27dba65

Please sign in to comment.