playwright icon indicating copy to clipboard operation
playwright copied to clipboard

[Bug]: toHaveText() fails with irrelevant error if applied to non-existing/non-visible elements

Open sergtimosh opened this issue 1 year ago • 0 comments

Version

1.41.2

Steps to reproduce

Create assertion expecting non-existing field to to have text. Ex:

test('field should be empty', async ({ page }) => {
            //case1
            await expect(page.locator('.i-do-not-exist'), 'field should be empty').toHaveText('');
            //case2
            await expect(page.locator('i-do-not-exist') , 'field should have 'abc' text').toHaveText('abc');
});     

Expected behavior

the assertion should fail but signal the element do not exist instead of saying that element has '' text

Actual behavior

case1:

Locator: locator('.non-exising-element')
Expected string: ""
Received string: serializes to the same string

case2:

Error: field should have 'abc' text

    Timed out 15000ms waiting for expect(locator).toHaveText(expected)

    Locator: locator('.non-exising-element')
    Expected string: "abc"
    Received string: ""

Additional context

No response

Environment

System:
    OS: macOS 14.3
    CPU: (10) arm64 Apple M1 Pro
    Memory: 109.13 MB / 16.00 GB
  Binaries:
    Node: 18.18.2 - ~/.nvm/versions/node/v18.18.2/bin/node
  Languages:
    Bash: 3.2.57 - /bin/bash
  npmPackages:
    @playwright/test: ^1.41.2 => 1.41.2

sergtimosh avatar Mar 11 '24 09:03 sergtimosh