Skip to content

Commit 565f39b

Browse files
fix(expect): preserve screenshot assertion errors without comparison details (#43749)
Co-authored-by: microsoft-playwright-automation[bot] <203992400+microsoft-playwright-automation[bot]@users.noreply.github.com>
1 parent ac65100 commit 565f39b

2 files changed

Lines changed: 22 additions & 0 deletions

File tree

‎packages/playwright-core/src/client/page.ts‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -686,6 +686,8 @@ export class Page extends ChannelOwner<channels.PageChannel> implements api.Page
686686
if (!(e instanceof PlaywrightError))
687687
throw e;
688688
const details = e.details as channels.PageExpectScreenshotErrorDetails;
689+
if (!details.customErrorMessage)
690+
throw e;
689691
return { ...details, errorMessage: details.customErrorMessage };
690692
}
691693
}, { title });

‎tests/playwright-test/to-have-screenshot.spec.ts‎

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -455,6 +455,26 @@ test('should support omitBackground option for locator', async ({ runInlineTest
455455
expect.soft(png.data[3], 'image A must be 0').toBe(0);
456456
});
457457

458+
test('should enforce strictness for locator screenshots', { annotation: { type: 'issue', description: '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/microsoft/playwright/issues/43745' } }, async ({ runInlineTest }) => {
459+
const result = await runInlineTest({
460+
...playwrightConfig({}),
461+
'a.spec.js': `
462+
const { test, expect } = require('@playwright/test');
463+
test('is a test', async ({ page }) => {
464+
await page.setContent('<button>First</button><button>Second</button>');
465+
const buttons = page.getByRole('button');
466+
await expect(buttons).toHaveCount(2);
467+
await expect(buttons.first()).toHaveScreenshot('button.png');
468+
await expect(buttons.screenshot()).rejects.toThrow('strict mode violation');
469+
await expect(expect(buttons).toHaveScreenshot('button.png')).rejects.toThrow('strict mode violation');
470+
});
471+
`
472+
}, { 'update-snapshots': true });
473+
expect(result.exitCode).toBe(0);
474+
const regularResult = await runInlineTest({});
475+
expect(regularResult.exitCode).toBe(0);
476+
});
477+
458478
test('should fail to screenshot an element with infinite animation', async ({ runInlineTest }, testInfo) => {
459479
const infiniteAnimationURL = pathToFileURL(path.join(__dirname, '../assets/rotate-z.html'));
460480
const result = await runInlineTest({

0 commit comments

Comments
 (0)