robotframework-browser icon indicating copy to clipboard operation
robotframework-browser copied to clipboard

Add support for HAR file replay for network mocking (routeFromHAR)

Open eslamelmishtawy opened this issue 1 year ago • 8 comments

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

eslamelmishtawy avatar Nov 04 '25 18:11 eslamelmishtawy

@eslamelmishtawy If it is possible then assign it to me

Siddhartha-singh01 avatar Nov 04 '25 18:11 Siddhartha-singh01

@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.

aaltat avatar Nov 05 '25 05:11 aaltat

@aaltat

I've put up a pull request to add @eslamelmishtawy! :tada:

allcontributors[bot] avatar Nov 05 '25 05:11 allcontributors[bot]

If possible adding both on page and context level would be great @aaltat

eslamelmishtawy avatar Nov 05 '25 08:11 eslamelmishtawy

We can have a single keyword which can handle both cases. Have you given any thought about the keyword name and arguments?

aaltat avatar Nov 05 '25 10:11 aaltat

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

eslamelmishtawy avatar Nov 05 '25 12:11 eslamelmishtawy

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

aaltat avatar Nov 05 '25 13:11 aaltat

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".

rasjani avatar Dec 29 '25 12:12 rasjani

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.

Snooz82 avatar Aug 05 '26 12:08 Snooz82

Update after the Playwright 1.63–1.65 review (main at 5e350998)

  • Not implemented. There is no routeFromHAR call anywhere in the library.
  • No Playwright API change in 1.63–1.65. page.routeFromHAR and browserContext.routeFromHAR take notFound (abort|fallback), update, updateContent, updateMode and url.
  • Order of work:
    1. #5113 first. Today recordHar keys other than path and omitContent are dropped without an error, so mode=minimal and urlFilter can't be used to record a small, committable HAR.
    2. 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 a Disposable, so rules can be removed when a Robot Framework scope ends.
  • 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=Context too.

aaltat avatar Nov 07 '26 16:11 aaltat