Donnerhorn
Donnerhorn

Reputation: 89

Playwright - Checking Class Name of an Element

I'm testing a site which has a list that needs to be opened or is open already. The class of the element changes according to the state it is currently in.

So the element is either

div class="list-open"

or

div class="list-closed"

My script has to check this to make sure to open the list if it is closed to continue the algorithm. How can I check the class name with Playwright for this?

Upvotes: 1

Views: 12828

Answers (3)

rekomat
rekomat

Reputation: 61

Playwright's LocatorAssertion toHaveClass seems to be what you are looking for:

await expect(page.locator('div')).toHaveClass(/list-open/);

Note that either the argument needs to be a full match or you have to use regex as in the example above.

Upvotes: 0

Alapan Das
Alapan Das

Reputation: 18650

You can also use the expect assertion like this. .list-open,.list-closed will be evaluated as either .list-open or .list-closed.

expect(page.locator("div")).to_have_class(".list-open,.list-closed")

Upvotes: 2

hardkoded
hardkoded

Reputation: 21607

You can ask for the class name of the element.

page.locator("div").evaluate("node => node.className")

Upvotes: 6

Related Questions