We are using routeFromHAR(har, {update: true})
to record network traffic while testing an application that makes a series of REST calls (GETs and POSTs). I am doing the recording for the purposes of later running the tests offline using mocked data (i.e. {update: false}
).
Question: is there an API in Playwright that STOPS the HAR file recording, so that I can start recording to a different HAR file?
More detail: let’s say I start recording, and then do a GET and a POST to modify something in the database. Like this pseudo-code:
1. page.routeFromHAR(har, {update: true});
2. oldData = GET('/foo'); // get initial data
3. POST('/foo', newData); // update with new data
4. assert( GET('/foo') === newData) ); // get again; compare with new data
The problem happens when running later against the mocked HAR file (i.e. {update: false}
). The GET call in step (4) will return oldData
and the test will fail.
I think what I want to do is spin up a new HAR file recording after step (3). Like this:
1. page.routeFromHAR(har1, {update: true});
2. oldData = GET('/foo'); // get initial data
3. POST('/foo', newData); // update with new data
4. page.routeFromHAR(har2, {update: true});
5. assert( GET('/foo') === newData) ); // get again; compare with new data
This way, I get two HAR files, and step (5) succeeds even when running offline against mocked data.
But … when recording, even after starting har2
at line 4, the har1
file continues to record. How can I STOP recording har1
before starting har2
? Calling page.unroute(har1)
does not work.