diff --git a/packages/playwright-core/src/server/injected/injectedScript.ts b/packages/playwright-core/src/server/injected/injectedScript.ts index 10997c4297..5242981b1f 100644 --- a/packages/playwright-core/src/server/injected/injectedScript.ts +++ b/packages/playwright-core/src/server/injected/injectedScript.ts @@ -1122,18 +1122,14 @@ export class InjectedScript { return { received, matches: false }; // Each matcher should get a "received" that matches it, in order. - let i = 0; const matchers = options.expectedText.map(e => new ExpectedTextMatcher(e)); - let allMatchesFound = true; - for (const matcher of matchers) { - while (i < received.length && !matcher.matches(received[i])) - i++; - if (i >= received.length) { - allMatchesFound = false; - break; - } + let mIndex = 0, rIndex = 0; + while (mIndex < matchers.length && rIndex < received.length) { + if (matchers[mIndex].matches(received[rIndex])) + ++mIndex; + ++rIndex; } - return { received, matches: allMatchesFound }; + return { received, matches: mIndex === matchers.length }; } throw this.createStacklessError('Unknown expect matcher: ' + expression); } diff --git a/tests/playwright-test/playwright.expect.text.spec.ts b/tests/playwright-test/playwright.expect.text.spec.ts index 2396ea3641..03c1548ebe 100644 --- a/tests/playwright-test/playwright.expect.text.spec.ts +++ b/tests/playwright-test/playwright.expect.text.spec.ts @@ -222,6 +222,12 @@ test('should support toHaveText w/ array', async ({ runInlineTest }) => { const locator = page.locator('div'); await expect(locator).toHaveText(['Text 1', /Text \\d/, 'Extra'], { timeout: 1000 }); }); + + test('fail on repeating array matchers', async ({ page }) => { + await page.setContent('