Highlights
Network Replay
Now you can record network traffic into a HAR file and re-use this traffic in your tests.
To record network into HAR file:
bash
npx playwright open --save-har=github.har.zip https://github.com/microsoft
Alternatively, you can record HAR programmatically:
python sync
context = browser.new_context(record_har_path="github.har.zip")
... do stuff ...
context.close()
Use the new methods [`method: Page.route_from_har`](https://playwright.dev/python/docs/api/class-page#page-route-from-har) or [`method: BrowserContext.route_from_har`](https://playwright.dev/python/docs/api/class-browsercontext#browser-context-route-from-har) to serve matching responses from the [HAR](http://www.softwareishard.com/blog/har-12-spec/) file:
python sync
context.route_from_har("github.har.zip")
Read more in [our documentation](https://playwright.dev/python/docs/network#record-and-replay-requests).
Advanced Routing
You can now use [`method: Route.fallback`](https://playwright.dev/python/docs/api/class-route#route-fallback) to defer routing to other handlers.
Consider the following example:
python sync
Remove a header from all requests
def remove_header_handler(route: Route) -> None:
headers = route.request.all_headers()
if "if-none-match" in headers:
del headers["if-none-match"]
route.fallback(headers=headers)
page.route("**/*", remove_header_handler)
Abort all images
def abort_images_handler(route: Route) -> None:
if route.request.resource_type == "image":
route.abort()
else:
route.fallback()
page.route("**/*", abort_images_handler)
Note that the new methods [`method: Page.route_from_har`](https://playwright.dev/python/docs/api/class-page#page-route-from-har) or [`method: BrowserContext.route_from_har`](https://playwright.dev/python/docs/api/class-browsercontext#browser-context-route-from-har) also participate in routing and could be deferred to.
Web-First Assertions Update
* New method [`method: LocatorAssertions.to_have_values`](https://playwright.dev/python/docs/test-assertions#locator-assertions-to-have-values) that asserts all selected values of `<select multiple>` element.
* Methods [`method: LocatorAssertions.to_contain_text`](https://playwright.dev/python/docs/test-assertions#locator-assertions-to-contain-text) and [`method: LocatorAssertions.to_have_text`](https://playwright.dev/python/docs/test-assertions#locator-assertions-to-have-text) now accept `ignore_case` option.
Miscellaneous
* If there's a service worker that's in your way, you can now easily disable it with a new context option `service_workers`:
python sync
context = browser.new_context(service_workers="block")
page = context.new_page()
* Using `.zip` path for `recordHar` context option automatically zips the resulting HAR:
python sync
context = browser.new_context(record_har_path="github.har.zip")
* If you intend to edit HAR by hand, consider using the `"minimal"` HAR recording mode
that only records information that is essential for replaying:
python sync
context = browser.new_context(record_har_mode="minimal", record_har_path="har.har")
* Playwright now runs on Ubuntu 22 amd64 and Ubuntu 22 arm64.