Newsletter
TechAnV Blog
Get updates on security engineering, Rust, eBPF, and DevSecOps. No spam, unsubscribe anytime.
Check your inbox and click the confirmation link to complete your subscription.
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:
1from playwright.sync_api import expect2
3def test_markers_are_displayed(ds_server, page):4 page.goto(ds_server + "/data/latitude_longitude")5 # There should be two leaflet-marker-icons6 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.