Playwright is a framework for Web Testing and Automation. It allows testing Chromium, Firefox and WebKit with a single API.
TypeScript automationchromechromiume2e-testingelectronend-to-end-testingfirefoxjavascriptplaywrighttesttest-automationtestingtesting-toolswebwebkit
feat(test runner): allow comparing binary files to strings (#9315)
Joel Einbinderd34b53a0d7b3
diff --git a/packages/playwright-test/src/matchers/golden.ts b/packages/playwright-test/src/matchers/golden.tsindex ad1af02912..1605bb55fe 100644--- a/packages/playwright-test/src/matchers/golden.ts+++ b/packages/playwright-test/src/matchers/golden.ts@@ -37,15 +37,17 @@ const extensionToMimeType: { [key: string]: string } = {};const GoldenComparators: { [key: string]: any } = {- 'application/octet-string': compareBuffers,+ 'application/octet-string': compareBuffersOrStrings,'image/png': compareImages,'image/jpeg': compareImages,'text/plain': compareText,};-function compareBuffers(actualBuffer: Buffer | string, expectedBuffer: Buffer, mimeType: string): { diff?: object; errorMessage?: string; } | null {+function compareBuffersOrStrings(actualBuffer: Buffer | string, expectedBuffer: Buffer, mimeType: string): { diff?: object; errorMessage?: string; } | null {+ if (typeof actualBuffer === 'string')+ return compareText(actualBuffer, expectedBuffer);if (!actualBuffer || !(actualBuffer instanceof Buffer))- return { errorMessage: 'Actual result should be Buffer.' };+ return { errorMessage: 'Actual result should be a Buffer or a string.' };if (Buffer.compare(actualBuffer, expectedBuffer))return { errorMessage: 'Buffers differ' };return null;@@ -53,7 +55,7 @@ function compareBuffers(actualBuffer: Buffer | string, expectedBuffer: Buffer, mfunction compareImages(actualBuffer: Buffer | string, expectedBuffer: Buffer, mimeType: string, options = {}): { diff?: object; errorMessage?: string; } | null {if (!actualBuffer || !(actualBuffer instanceof Buffer))- return { errorMessage: 'Actual result should be Buffer.' };+ return { errorMessage: 'Actual result should be a Buffer.' };const actual = mimeType === 'image/png' ? PNG.sync.read(actualBuffer) : jpeg.decode(actualBuffer);const expected = mimeType === 'image/png' ? PNG.sync.read(expectedBuffer) : jpeg.decode(expectedBuffer);@@ -69,7 +71,7 @@ function compareImages(actualBuffer: Buffer | string, expectedBuffer: Buffer, mifunction compareText(actual: Buffer | string, expectedBuffer: Buffer): { diff?: object; errorMessage?: string; diffExtension?: string; } | null {if (typeof actual !== 'string')- return { errorMessage: 'Actual result should be string' };+ return { errorMessage: 'Actual result should be a string' };const expected = expectedBuffer.toString('utf-8');if (expected === actual)return null;diff --git a/tests/playwright-test/golden.spec.ts b/tests/playwright-test/golden.spec.tsindex 5e77f2d066..47b41684b1 100644--- a/tests/playwright-test/golden.spec.ts+++ b/tests/playwright-test/golden.spec.ts@@ -654,3 +654,17 @@ test('should fail with missing expectations and retries', async ({ runInlineTestconst data = fs.readFileSync(snapshotOutputPath);expect(data.toString()).toBe('Hello world');});++test('should allow comparing text with text without file extension', async ({ runInlineTest }) => {+ const result = await runInlineTest({+ ...files,+ 'a.spec.js-snapshots/snapshot-no-extension': `Hello world`,+ 'a.spec.js': `+ const { test } = require('./helper');+ test('is a test', ({}) => {+ expect('Hello world').toMatchSnapshot('snapshot-no-extension');+ });+ `+ });+ expect(result.exitCode).toBe(0);+});\ No newline at end of file