Skip to content
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

chore: unify toHaveScreenshot error formatting #33300

Merged
merged 4 commits into from
Oct 25, 2024
Merged
Show file tree
Hide file tree
Changes from 2 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
3 changes: 2 additions & 1 deletion packages/html-reporter/src/testResultView.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,8 @@ export const TestResultView: React.FC<{

function classifyErrors(testErrors: string[], diffs: ImageDiff[]) {
return testErrors.map(error => {
if (error.includes('Screenshot comparison failed:')) {
const firstLine = error.split('\n')[0];
if (firstLine.includes('toHaveScreenshot') || firstLine.includes('toMatchSnapshot')) {
const matchingDiff = diffs.find(diff => {
const attachmentName = diff.actual?.attachment.name;
return attachmentName && error.includes(attachmentName);
Expand Down
2 changes: 1 addition & 1 deletion packages/playwright-core/src/client/page.ts
Original file line number Diff line number Diff line change
Expand Up @@ -589,7 +589,7 @@ export class Page extends ChannelOwner<channels.PageChannel> implements api.Page
return result.binary;
}

async _expectScreenshot(options: ExpectScreenshotOptions): Promise<{ actual?: Buffer, previous?: Buffer, diff?: Buffer, errorMessage?: string, log?: string[]}> {
async _expectScreenshot(options: ExpectScreenshotOptions): Promise<{ actual?: Buffer, previous?: Buffer, diff?: Buffer, errorMessage?: string, log?: string[], timeout?: number}> {
const mask = options?.mask ? options?.mask.map(locator => ({
frame: (locator as Locator)._frame._channel,
selector: (locator as Locator)._selector,
Expand Down
1 change: 1 addition & 0 deletions packages/playwright-core/src/protocol/validator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1193,6 +1193,7 @@ scheme.PageExpectScreenshotResult = tObject({
errorMessage: tOptional(tString),
actual: tOptional(tBinary),
previous: tOptional(tBinary),
timeout: tOptional(tNumber),
log: tOptional(tArray(tString)),
});
scheme.PageScreenshotParams = tObject({
Expand Down
3 changes: 2 additions & 1 deletion packages/playwright-core/src/server/page.ts
Original file line number Diff line number Diff line change
Expand Up @@ -674,11 +674,12 @@ export class Page extends SdkObject {
throw e;
let errorMessage = e.message;
if (e instanceof TimeoutError && intermediateResult?.previous)
errorMessage = `Failed to take two consecutive stable screenshots. ${e.message}`;
errorMessage = `Failed to take two consecutive stable screenshots.`;
return {
log: e.message ? [...metadata.log, e.message] : metadata.log,
...intermediateResult,
errorMessage,
...((e instanceof TimeoutError) ? { timeout: callTimeout } : {}),
yury-s marked this conversation as resolved.
Show resolved Hide resolved
};
});
}
Expand Down
36 changes: 18 additions & 18 deletions packages/playwright/src/matchers/toMatchSnapshot.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ import type { Locator, Page } from 'playwright-core';
import type { ExpectScreenshotOptions, Page as PageEx } from 'playwright-core/lib/client/page';
import { currentTestInfo } from '../common/globals';
import type { ImageComparatorOptions, Comparator } from 'playwright-core/lib/utils';
import { getComparator, sanitizeForFilePath } from 'playwright-core/lib/utils';
import { getComparator, isString, sanitizeForFilePath } from 'playwright-core/lib/utils';
import {
addSuffixToFilePath,
trimLongString, callLogText,
Expand All @@ -31,7 +31,7 @@ import path from 'path';
import { mime } from 'playwright-core/lib/utilsBundle';
import type { TestInfoImpl } from '../worker/testInfo';
import type { ExpectMatcherState } from '../../types/test';
import type { MatcherResult } from './matcherHint';
import { matcherHint, type MatcherResult } from './matcherHint';
import type { FullProjectInternal } from '../common/config';

type NameOrSegments = string | string[];
Expand Down Expand Up @@ -250,16 +250,10 @@ class SnapshotHelper {
expected: Buffer | string | undefined,
previous: Buffer | string | undefined,
diff: Buffer | string | undefined,
diffError: string | undefined,
log: string[] | undefined,
title = `${this.kind} comparison failed:`): ImageMatcherResult {
const output = [
colors.red(title),
'',
];
if (diffError)
output.push(indent(diffError, ' '));

header: string,
diffError: string,
log: string[] | undefined): ImageMatcherResult {
const output = [`${header}${indent(diffError, ' ')}`];
if (expected !== undefined) {
// Copy the expectation inside the `test-results/` folder for backwards compatibility,
// so that one can upload `test-results/` directory and have all the data inside.
Expand Down Expand Up @@ -338,7 +332,9 @@ export function toMatchSnapshot(
return helper.createMatcherResult(helper.expectedPath + ' running with --update-snapshots, writing actual.', true);
}

return helper.handleDifferent(received, expected, undefined, result.diff, result.errorMessage, undefined);
const receiver = isString(received) ? 'string' : 'Buffer';
const header = matcherHint(this, undefined, 'toMatchSnapshot', receiver, undefined, undefined);
return helper.handleDifferent(received, expected, undefined, result.diff, header, result.errorMessage, undefined);
}

export function toHaveScreenshotStepTitle(
Expand Down Expand Up @@ -410,13 +406,16 @@ export async function toHaveScreenshot(
if (helper.updateSnapshots === 'none' && !hasSnapshot)
return helper.createMatcherResult(`A snapshot doesn't exist at ${helper.expectedPath}.`, false);

const receiver = locator ? 'locator' : 'page';
if (!hasSnapshot) {
// Regenerate a new screenshot by waiting until two screenshots are the same.
const { actual, previous, diff, errorMessage, log } = await page._expectScreenshot(expectScreenshotOptions);
const { actual, previous, diff, errorMessage, log, timeout } = await page._expectScreenshot(expectScreenshotOptions);
// We tried re-generating new snapshot but failed.
// This can be due to e.g. spinning animation, so we want to show it as a diff.
if (errorMessage)
return helper.handleDifferent(actual, undefined, previous, diff, undefined, log, errorMessage);
if (errorMessage) {
const header = matcherHint(this, locator, 'toHaveScreenshot', receiver, undefined, undefined, timeout);
yury-s marked this conversation as resolved.
Show resolved Hide resolved
return helper.handleDifferent(actual, undefined, previous, diff, header, errorMessage, log);
}

// We successfully generated new screenshot.
return helper.handleMissing(actual!);
Expand All @@ -427,7 +426,7 @@ export async function toHaveScreenshot(
// - regular matcher (i.e. not a `.not`)
// - perhaps an 'all' flag to update non-matching screenshots
expectScreenshotOptions.expected = await fs.promises.readFile(helper.expectedPath);
const { actual, previous, diff, errorMessage, log } = await page._expectScreenshot(expectScreenshotOptions);
const { actual, previous, diff, errorMessage, log, timeout } = await page._expectScreenshot(expectScreenshotOptions);

if (!errorMessage)
return helper.handleMatching();
Expand All @@ -440,7 +439,8 @@ export async function toHaveScreenshot(
return helper.createMatcherResult(helper.expectedPath + ' running with --update-snapshots, writing actual.', true);
}

return helper.handleDifferent(actual, expectScreenshotOptions.expected, previous, diff, errorMessage, log);
const header = matcherHint(this, undefined, 'toHaveScreenshot', receiver, undefined, undefined, timeout);
return helper.handleDifferent(actual, expectScreenshotOptions.expected, previous, diff, header, errorMessage, log);
}

function writeFileSync(aPath: string, content: Buffer | string) {
Expand Down
1 change: 1 addition & 0 deletions packages/protocol/src/channels.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2193,6 +2193,7 @@ export type PageExpectScreenshotResult = {
errorMessage?: string,
actual?: Binary,
previous?: Binary,
timeout?: number,
log?: string[],
};
export type PageScreenshotParams = {
Expand Down
1 change: 1 addition & 0 deletions packages/protocol/src/protocol.yml
Original file line number Diff line number Diff line change
Expand Up @@ -1501,6 +1501,7 @@ Page:
errorMessage: string?
actual: binary?
previous: binary?
timeout: number?
log:
type: array?
items: string
Expand Down
4 changes: 2 additions & 2 deletions tests/page/expect-matcher-result.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -295,15 +295,15 @@ test('toHaveScreenshot should populate matcherResult', async ({ page, server, is
actual: expect.stringContaining('screenshot-sanity-actual'),
expected: expect.stringContaining('screenshot-sanity-'),
diff: expect.stringContaining('screenshot-sanity-diff'),
message: expect.stringContaining(`Screenshot comparison failed`),
message: expect.stringContaining(`expect(page).toHaveScreenshot(expected)`),
name: 'toHaveScreenshot',
pass: false,
log: expect.any(Array),
printedExpected: expect.stringContaining('screenshot-sanity-'),
printedReceived: expect.stringContaining('screenshot-sanity-actual'),
});

expect.soft(stripAnsi(e.toString())).toContain(`Error: Screenshot comparison failed:
expect.soft(stripAnsi(e.toString())).toContain(`Error: expect(page).toHaveScreenshot(expected)

23362 pixels (ratio 0.10 of all image pixels) are different.

Expand Down
4 changes: 3 additions & 1 deletion tests/playwright-test/reporter-html.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -330,7 +330,9 @@ for (const useIntermediateMergeReport of [true, false] as const) {
await expect(page.locator('text=Image mismatch')).toHaveCount(1);
await expect(page.locator('text=Snapshot mismatch')).toHaveCount(0);
await expect(page.locator('.chip-header', { hasText: 'Screenshots' })).toHaveCount(0);
await expect(page.getByTestId('test-result-image-mismatch-tabs').locator('div')).toHaveText([
const errorChip = page.getByTestId('test-screenshot-error-view');
await expect(errorChip).toContainText('Failed to take two consecutive stable screenshots.');
await expect(errorChip.getByTestId('test-result-image-mismatch-tabs').locator('div')).toHaveText([
'Diff',
'Actual',
'Previous',
Expand Down
Loading