I have this Playwright piece of code that selects options for a multi-select element
I use the expect()
function with regular expressions in order to assert the presence of "xx-oranges"
in the selected options
But I need to provide a regex for each list element.
def test_list_regex(page: Page):
page.goto("https://example.cypress.io/commands/actions")
select2 = page.locator(".action-select-multiple")
select2.select_option(['fr-apples', 'fr-oranges', 'fr-bananas'])
expect(select2).to_have_values(['fr-apples', 'fr-oranges', 'fr-bananas'])
# Don't know how to verify 'fr-oranges' is part of selected options in an elegant way with expect()
expect(select2).to_have_values([re.compile(".*"), re.compile(".*oranges"), re.compile(".*")])
I would like to have an statement that can check for the regex ".*oranges"
independently of the size of the list and the position of the element within the list
I would like to have an expression that can work against lists like this:
['fr-oranges']
['fr-apples', 'fr-oranges']
['fr-apples', 'fr-oranges', 'fr-bananas', 'en-tomatos']
['en-oranges', 'fr-bananas', 'fr-apples', 'en-tomatos']
Any ideas on how to write such regex that can be passed to expect(select2).to_have_values()
?
Good question. Since the Playwright Python assertion API is pretty limited at the time of writing, one option is to do most of the work in CSS:
Another option, assuming you don't need to auto-wait (a reasonable assumption, since selecting options is always synchronous as far as I've seen), here is an immediate assertion you can use that is agnostic of ordering:
This website happens to have jQuery running, so you could use that:
If you need to retry this, you can toss in a loop and a timer and write your own function wrapper on that code for reusability, since Python Playwright doesn't have custom matchers or
poll
ortoPass
methods like Node Playwright does.If I find anything better, I'll update (and feel free to comment if you find a better answer--I'd love to see it).
See also Python playwright: wait for arbitrary DOM state.