Skip to content

[Docs]: toHaveClass should have a proper "relaxed regex" example #34343

@rrauenza

Description

@rrauenza

Page(s)

https://playwright.dev/docs/api/class-locatorassertions#locator-assertions-to-have-class

Description

The docs mention a 'relaxed regular expression' but doesn't define what 'relaxed' means. The existing example of toHaveClass(/selected/) would also match class="selectedfoo row"

I think it could be helpful if the example used the zero length word boundary pattern, \b:

<div class='selected row' id='component></div>

const locator = page.locator('#component');
await expect(locator).toHaveClass(/\bselected\b/);
await expect(locator).toHaveClass(/\brow\b/);

Unfortunately, \b considers - to be a boundary, so there is still some possible ambiguity. Perhaps /(^|\s)selected(\s|$)/ would be better

<div class='selected row' id='component></div>

const locator = page.locator('#component');
await expect(locator).toHaveClass(/(^|\s)selected(\s|$)/);
await expect(locator).toHaveClass(/(^|\s)row(\s|$)/);

\s could be replaced with (space), but it may be clearer to have the \s

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions