147 words
1 minute
Using expect() to wait for a selector to match multiple items

Using expect() to wait for a selector to match multiple items#

In the Playwright tests for datasette-cluster-map I wanted to assert that two markers had been displayed on a Leaflet map.

Since the map can take a little while to load, I wanted to run an assertion that would keep trying for up to a deadline to see if those elements had become available.

Here’s how to do that in a Playwright Python test:

from playwright.sync_api import expect
def test_markers_are_displayed(ds_server, page):
page.goto(ds_server + "/data/latitude_longitude")
# There should be two leaflet-marker-icons
expect(page.locator(".leaflet-marker-icon")).to_have_count(2)

The page and ds_server fixtures are explained in this TIL.

page.locator() returns a Locator, described by Playwright’s documentation as “the central piece of Playwright’s auto-waiting and retry-ability”.

Here’s the documentation for to_have_count() - it takes an optional second timeout floating point argument which defaults to 5.0 seconds.

Initially I tried using the page.locator(...).all() method, but this doesn’t wait for matching elements to become available.

Using expect() to wait for a selector to match multiple items
https://mranv.pages.dev/posts/using-expect-to-wait-for-a-selector-to-match-multiple-items/
Author
Anubhav Gain
Published at
2024-08-02
License
CC BY-NC-SA 4.0