Nati Kamusher
Nati Kamusher

Reputation: 771

Playwright Test: Locator Behavior Inconsistent with Expectations

I'm encountering an issue with Playwright while testing my application. I have a test scenario, and here's the test case:

test('create a basic scenario', async ({ page }) => {
    await createScenario(page);
    expect(await page.getByRole('heading', { name: 'Scenario 2' })).toBeEmpty();
});

In this test case, I expect a locator to be empty under certain conditions, but it's returning "notEmpty" when using expect.toBeEmpty(), and vice versa.

Here are the two scenarios:

When I expect the locator to be empty:

Error: expect(locator).toBeEmpty() Locator: getByRole('heading', { name: 'Scenario 2' }) Expected: empty Received: notEmpty

When I expect it not to be empty:

Error: expect(locator).not.toBeEmpty() Locator: getByRole('heading', { name: 'Scenario 2' }) Expected: not empty Received: empty

I'm looking for insights into why the locator behavior is inconsistent with my expectations and how to resolve this issue. Any help or suggestions would be greatly appreciated.

Upvotes: 0

Views: 367

Answers (1)

unickq
unickq

Reputation: 3418

You should await expect and not the locator:

await expect(page.getByRole('heading', { name: 'Scenario 2' })).toBeEmpty();

Upvotes: 2

Related Questions