0

I have a string that I get from an element while testing and I need to check if it matches one of two other strings. For example:

expect.soft(s).toMatch('Test String 1');

But what I need is to check if 's' matches either "Test String 1" or "Test String 2". If it matches either the test should pass and if neither then it should fail. I'm new to playwright so any help will be appreciated!

Tried passing in an array or trying to make a regex that matches only those two strings but with no luck.

  • 1
    Does this answer your question? [How to use playwright expect to do an exact match of one of two possible values?](https://stackoverflow.com/questions/75570005/how-to-use-playwright-expect-to-do-an-exact-match-of-one-of-two-possible-values). "Tried passing in an array or trying to make a regex that matches only those two strings but with no luck." -- please share your code. – ggorlen May 18 '23 at 14:26
  • The linked answer seems to be an overkill for simple general scenarios. – Vishal Aggarwal May 18 '23 at 14:44
  • @VishalAggarwal I offer three options, two of which are more or less the same complexity as yours. Also, if you're doing these tests dozens of times, writing a matcher and burying it in a utils file seems entirely appropriate. Either way, it seems like a pretty clear dupe. One can always add answers to the canonical thread rather than re-inventing the wheel here. – ggorlen May 18 '23 at 15:05

1 Answers1

0

This will precisely match (in non regex strings) either the string 1 or 2.

// Use toMatch to match either of two strings
  expect(value).toMatch(/^string1$|^string2$/)
Vishal Aggarwal
  • 1,929
  • 1
  • 13
  • 23