How to assert that an element's text is not empty in playwright
P粉675258598
P粉675258598 2023-08-17 23:11:33
0
1
359

I executed this line of code to verify that there is some text on the title.

await expect(page.locator('div.welcome-content > h2')).not.toBeEmpty;

But when I execute it, this line of code does nothing, it neither asserts nor returns an error.

P粉675258598
P粉675258598

reply all (1)
P粉018653751

The correct way to use the expect function with the .not.toBeEmpty matcher is to put parentheses after .not.toBeEmpty to call the assertion and compare it with the actual value.

await expect(page.locator('div.welcome-content > h2')).not.toBeEmpty()

If you are using playwright's own assertion library, you can do this:

await expect(page.locator('div.welcome-content > h2')).toHaveText(text => text.length > 0);
    Latest Downloads
    More>
    Web Effects
    Website Source Code
    Website Materials
    Front End Template
    About us Disclaimer Sitemap
    php.cn:Public welfare online PHP training,Help PHP learners grow quickly!