Skip to content

Commit 51a0289

Browse files
pavelfeldmanaslushnikov
authored andcommitted
cherry-pick(#9860): another edge case fix
PR #9860 SHA 75ac579 Fixes #9849
1 parent 5e0f327 commit 51a0289

File tree

2 files changed

+22
-3
lines changed

2 files changed

+22
-3
lines changed

packages/playwright-core/src/server/frames.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1183,8 +1183,9 @@ export class Frame extends SdkObject {
11831183

11841184
// expect(listLocator).toHaveCount(0) passes when there are no elements matching.
11851185
// expect(listLocator).not.toHaveCount(1) passes when there are no elements matching.
1186-
if (options.expression === 'to.have.count')
1187-
return { matches: options.expectedNumber === 0, received: options.expectedNumber };
1186+
const expectsEmptyCount = options.expectedNumber === 0;
1187+
if (options.expression === 'to.have.count' && expectsEmptyCount !== options.isNot)
1188+
return { matches: expectsEmptyCount, received: 0 };
11881189

11891190
// When none of the above applies, keep waiting for the element.
11901191
return continuePolling;

tests/playwright-test/playwright.expect.misc.spec.ts

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,24 @@ test('should support toHaveCount', async ({ runInlineTest }) => {
4949
await expect(locator).not.toHaveCount(1);
5050
});
5151
52+
test('eventually pass non-zero', async ({ page }) => {
53+
await page.setContent('<ul></ul>');
54+
setTimeout(async () => {
55+
await page.setContent("<ul><li>one</li><li>two</li></ul>");
56+
}, 500);
57+
const locator = page.locator('li');
58+
await expect(locator).toHaveCount(2);
59+
});
60+
61+
test('eventually pass not non-zero', async ({ page }) => {
62+
await page.setContent('<ul><li>one</li><li>two</li></ul>');
63+
setTimeout(async () => {
64+
await page.setContent("<ul></ul>");
65+
}, 500);
66+
const locator = page.locator('li');
67+
await expect(locator).not.toHaveCount(2);
68+
});
69+
5270
test('fail zero', async ({ page }) => {
5371
await page.setContent('<div><span></span></div>');
5472
const locator = page.locator('span');
@@ -63,7 +81,7 @@ test('should support toHaveCount', async ({ runInlineTest }) => {
6381
`,
6482
}, { workers: 1 });
6583
const output = stripAscii(result.output);
66-
expect(result.passed).toBe(3);
84+
expect(result.passed).toBe(5);
6785
expect(result.failed).toBe(2);
6886
expect(result.exitCode).toBe(1);
6987
expect(output).toContain('Expected: 0');

0 commit comments

Comments
 (0)