0

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?

Donnerhorn
  • 59
  • 3
  • 9

2 Answers2

3

You can ask for the class name of the element.

page.locator("div").evaluate("node => node.className")
hardkoded
  • 18,915
  • 3
  • 52
  • 64
3

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")
Alapan Das
  • 17,144
  • 3
  • 29
  • 52