Skip to content

feat(test-runner-visual-regression): add testName to GetNameArgs #1657

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/gold-needles-jump.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@web/test-runner-visual-regression': patch
---

Supply testFile to extensibility points in visualDiffPlugin, making it easy to store snapshot images alongside test files
1 change: 1 addition & 0 deletions packages/test-runner-visual-regression/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@ type PixelMatchOptions = PixelMatchParams[5];
export interface GetNameArgs {
browser: string;
name: string;
testFile: string;
}

export interface ImageArgs {
Expand Down
1 change: 1 addition & 0 deletions packages/test-runner-visual-regression/src/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ type PixelMatchOptions = PixelMatchParams[5];
export interface GetNameArgs {
browser: string;
name: string;
testFile: string;
}

export interface ImageArgs {
Expand Down
13 changes: 9 additions & 4 deletions packages/test-runner-visual-regression/src/visualDiffCommand.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,19 @@ export interface VisualDiffCommandResult {
passed: boolean;
}

export interface VisualDiffCommandContext {
browser: string;
testFile: string;
}

export async function visualDiffCommand(
options: VisualRegressionPluginOptions,
image: Buffer,
browser: string,
name: string,
{ browser, testFile }: VisualDiffCommandContext,
): Promise<VisualDiffCommandResult> {
const baseDir = path.resolve(options.baseDir);
const baselineName = options.getBaselineName({ browser, name });
const baselineName = options.getBaselineName({ browser, name, testFile });

const baselineImage = await options.getBaseline({
filePath: resolveImagePath(baseDir, baselineName),
Expand All @@ -42,8 +47,8 @@ export async function visualDiffCommand(
return { diffPercentage: -1, passed: true };
}

const diffName = options.getDiffName({ browser, name });
const failedName = options.getFailedName({ browser, name });
const diffName = options.getDiffName({ browser, name, testFile });
const failedName = options.getFailedName({ browser, name, testFile });
const diffFilePath = resolveImagePath(baseDir, diffName);

const saveFailed = async () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,11 @@ import type { PlaywrightLauncher } from '@web/test-runner-playwright';
import type { WebdriverLauncher } from '@web/test-runner-webdriver';

import { defaultOptions, VisualRegressionPluginOptions } from './config';
import { visualDiffCommand, VisualDiffCommandResult } from './visualDiffCommand';
import {
visualDiffCommand,
VisualDiffCommandContext,
VisualDiffCommandResult,
} from './visualDiffCommand';
import { VisualRegressionError } from './VisualRegressionError';

interface Payload {
Expand Down Expand Up @@ -49,6 +53,11 @@ export function visualRegressionPlugin(
return;
}

const context: VisualDiffCommandContext = {
testFile: session.testFile,
browser: session.browser.name,
};

if (session.browser.type === 'puppeteer') {
const browser = session.browser as ChromeLauncher;
const page = browser.getPage(session.id);
Expand All @@ -67,7 +76,7 @@ export function visualRegressionPlugin(
}

const screenshot = (await element.screenshot({ encoding: 'binary' })) as Buffer;
return visualDiffCommand(mergedOptions, screenshot, session.browser.name, payload.name);
return visualDiffCommand(mergedOptions, screenshot, payload.name, context);
}

if (session.browser.type === 'playwright') {
Expand All @@ -88,7 +97,7 @@ export function visualRegressionPlugin(
}

const screenshot = await element.screenshot();
return visualDiffCommand(mergedOptions, screenshot, session.browser.name, payload.name);
return visualDiffCommand(mergedOptions, screenshot, payload.name, context);
}

if (session.browser.type === 'webdriver') {
Expand All @@ -106,7 +115,7 @@ export function visualRegressionPlugin(
`;

const screenshot = await browser.takeScreenshot(session.id, locator);
return visualDiffCommand(mergedOptions, screenshot, session.browser.name, payload.name);
return visualDiffCommand(mergedOptions, screenshot, payload.name, context);
}

throw new Error(
Expand Down