Add support for HAR file replay for network mocking (routeFromHAR)
Is your feature request related to a problem?
I need to mock API responses for testing but Robot Framework Browser only supports recording HAR files, not replaying them. Currently, I can record network traffic with recordHar in New Context, but there's no way to use that HAR file to mock responses in subsequent test runs.
This means I cannot:
- Test applications offline with previously recorded traffic
- Create reproducible tests with consistent API responses
- Mock backend APIs while testing frontend independently
Describe the solution you'd like
Expose Playwright's routeFromHAR() functionality through new Robot Framework keywords. Playwright already supports this - it just needs to be exposed.
Proposed Keywords
2. Route Context From HAR (context-level)
Route Context From HAR
[Arguments] path url=${NONE} update=${False} notFound=abort
Example Usage
*** Test Cases ***
# Step 1: Record HAR (first run)
Record API Traffic
${har} = Create Dictionary path=./api.har
New Context recordHar=${har}
New Page https://example.com
Click text=Load Data
# Network traffic recorded
# Step 2: Replay HAR (subsequent runs)
Test With Mocked Data
New Context
Route Context From HAR path=./api.har url=**/api/**
New Page https://example.com
Click text=Load Data
# Requests to /api/** served from HAR file (offline, fast, consistent)
Additional context
- Playwright Documentation: https://playwright.dev/docs/mock#replaying-from-har
- Playwright API:
@eslamelmishtawy If it is possible then assign it to me
@allcontributors please add @eslamelmishtawy for ideas. @eslamelmishtawy there are two ways to implement this one, on context or page level, which way you would like to see this one to work? Or perhaps both ways are good ideas?
@Siddhartha-singh01 if you want create a PR to implement this one, please go ahead, the issue is yours. Do you need help on something or is it clear how to do this enhancement.
At least we should discuss about the keyword argument syntax and decide about the level where har is attached, page or context.
If possible adding both on page and context level would be great @aaltat
We can have a single keyword which can handle both cases. Have you given any thought about the keyword name and arguments?
Will the example provided above work?
I was thinking of two keywords Route Context From HAR Route Page From HAR
If we want to unify it we can Route From HAR {har_file_path} {scope} {url_filter} => urls to filter {update} => whether to update file with missing requests or not
Underlying Playwright API has plenty of options https://playwright.dev/docs/api/class-page#page-route-from-har and it would be nice to support them all
https://github.com/MarketSquare/robotframework-browser-extensions/blob/main/mockUrl/mockUrl.js#L82 - has mocking from har and i've used it when testing "big ecom site" during 2025 with my previous customer so "it should work".
Copilot Comment:
Implement context- and page-level HAR replay, exposing url, update, updateContent, and notFound, with acceptance tests. Context routing should be installed before navigation.
Update after the Playwright 1.63–1.65 review (main at 5e350998)
- Not implemented. There is no
routeFromHARcall anywhere in the library. - No Playwright API change in 1.63–1.65.
page.routeFromHARandbrowserContext.routeFromHARtakenotFound(abort|fallback),update,updateContent,updateModeandurl. - Order of work:
- #5113 first. Today
recordHarkeys other thanpathandomitContentare dropped without an error, somode=minimalandurlFiltercan't be used to record a small, committable HAR. - Then design this together with #5120 (network mocking). Both are built on Playwright routes, so they should share one Node-side rule store and the same
scope=and removal model.route()returns aDisposable, so rules can be removed when a Robot Framework scope ends.
- #5113 first. Today
- Page or context level (the open question above): context level is the natural default. It also covers popups and pages opened later. The other mocking keywords proposed in #5120 default to
scope=Contexttoo.