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
fix: reset element not found error when element is present (#37860)
Yury Semikhatsky6e775c7c4066
diff --git a/packages/playwright-core/src/server/frames.ts b/packages/playwright-core/src/server/frames.tsindex c705cef563..16a3b17e54 100644--- a/packages/playwright-core/src/server/frames.ts+++ b/packages/playwright-core/src/server/frames.ts@@ -1444,10 +1444,12 @@ export class Frame extends SdkObject {progress.log(log);// Note: missingReceived avoids `unexpected value "undefined"` when element was not found.if (matches === options.isNot) {- if (missingReceived)+ if (missingReceived) {lastIntermediateResult.errorMessage = 'Error: element(s) not found';- else+ } else {+ lastIntermediateResult.errorMessage = undefined;lastIntermediateResult.received = received;+ }lastIntermediateResult.isSet = true;if (!missingReceived && !Array.isArray(received))progress.log(` unexpected value "${renderUnexpectedValue(options.expression, received)}"`);diff --git a/tests/page/expect-to-have-text.spec.ts b/tests/page/expect-to-have-text.spec.tsindex 6b80d522fa..818dc77ff4 100644--- a/tests/page/expect-to-have-text.spec.ts+++ b/tests/page/expect-to-have-text.spec.ts@@ -167,6 +167,24 @@ test.describe('toHaveText with text', () => {expect(stripAnsi(error.message)).toContain('Expected: "Text"');expect(stripAnsi(error.message)).toContain('Received: "Text content"');});++ test('do not show "element(s) not found" when the real failure is a string mismatch', async ({ page }) => {+ await page.setContent(`+ <div>Initial</div>+ <script>+ const field = document.querySelector('div');+ setTimeout(() => {+ field.id = 'field';+ field.textContent = 'Final value';+ }, 1000);+ </script>+ `);++ const cell = page.locator('#field');+ const error = await expect(cell).toHaveText('Something', { timeout: 2000 }).catch(e => e);+ expect(stripAnsi(error.message)).toContain('Expected: "Something"');+ expect(stripAnsi(error.message)).toContain('Received: "Final value"');+ });});test.describe('not.toHaveText', () => {