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

feat: add pwt toMatchScreenshot diff bubbles support #514

Merged
merged 1 commit into from
Oct 17, 2023
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
16 changes: 15 additions & 1 deletion lib/test-adapter/playwright.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,12 @@ import {ErrorName} from '../errors';
import {ImagesInfoFormatter} from '../image-handler';
import {AssertViewResult, ErrorDetails, ImageData, ImageInfoFull, ImageSize, TestError} from '../types';
import * as utils from '../server-utils';
import type {CoordBounds} from 'looks-same';

export type PlaywrightAttachment = PlaywrightTestResult['attachments'][number];

export type PwtImageDiffError = TestError & {meta?: {type: string, snapshotName: string, diffClusters: CoordBounds[]}};

export enum PwtTestStatus {
PASSED = 'passed',
FAILED = 'failed',
Expand Down Expand Up @@ -118,7 +121,8 @@ export class PlaywrightTestAdapter implements ReporterTestResult {
stateName: state,
refImg,
diffImg,
currImg
currImg,
diffClusters: this.getDiffClusters(state)
};
} else if (this.error?.name === ErrorName.NO_REF_IMAGE && currImg) {
return {
Expand Down Expand Up @@ -252,6 +256,16 @@ export class PlaywrightTestAdapter implements ReporterTestResult {
return '';
}

private getDiffClusters(state: string): CoordBounds[] {
const snapshotName = state + '.png';
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

у нас в state здесь точно не может быть названия с .png? чтобы не получилось plain.png.png

Copy link
Member Author

@KuznetsovRoman KuznetsovRoman Oct 17, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Этот стейт - от html-reporter.

playwright использует snapshotName, который всегда кончается на .png. Затем добавляет к имени файла суффиксы -expected, -actual, -diff так, что получаются -expected.png, -actual.png, -diff.png.

html-reporter создает свой state путем отбрасывания таких суффиксов вместе с .png

UPD: но в pwt-utils я в name у attachment прокидывал чистый snapshotName, без .png. Сейчас пофикшу

const errors = this._testResult.errors as PwtImageDiffError[];
const snapshotImageDiffError = errors.find(err => {
return err.meta?.type === 'ImageDiffError' && err.meta.snapshotName === snapshotName;
});

return snapshotImageDiffError?.meta?.diffClusters || [];
}

private get _attachmentsByState(): Record<string, PlaywrightAttachment[]> {
// Filtering out only images. Page screenshots on reject are named "screenshot", we don't want them in state either.
const imageAttachments = this._testResult.attachments.filter(
Expand Down
19 changes: 18 additions & 1 deletion test/unit/lib/test-adapter/playwright.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import sinon from 'sinon';
import _ from 'lodash';
import proxyquire from 'proxyquire';
import {TestCase, TestResult} from '@playwright/test/reporter';
import {ImageTitleEnding, PlaywrightAttachment, PlaywrightTestAdapterOptions, PwtTestStatus} from 'lib/test-adapter/playwright';
import {ImageTitleEnding, PlaywrightAttachment, PlaywrightTestAdapterOptions, PwtImageDiffError, PwtTestStatus} from 'lib/test-adapter/playwright';
import {ErrorName, ImageDiffError, NoRefImageError} from 'lib/errors';
import {TestStatus} from 'lib/constants';

Expand Down Expand Up @@ -88,6 +88,23 @@ describe('PlaywrightTestAdapter', () => {
assert.strictEqual(results[0].stateName, 'state1');
assert.strictEqual(results[0].currImg?.path, 'state1' + ImageTitleEnding.Actual);
});

it('should have diff clusters', () => {
const testCaseStub = mkTestCase();
const testResultStub = mkTestResult({
errors: [{message: 'Screenshot comparison failed', meta: {
type: 'ImageDiffError',
snapshotName: 'state1.png',
diffClusters: [{left: 0, top: 0, right: 1, bottom: 1}]
}} as PwtImageDiffError]
});

const adapter = new PlaywrightTestAdapter(testCaseStub, testResultStub, mkAdapterOptions());

const results = adapter.assertViewResults as ImageDiffError[];

assert.deepEqual(results[0].diffClusters, [{left: 0, top: 0, right: 1, bottom: 1}]);
});
});

describe('attempt', () => {
Expand Down
Loading