To mock routes in playwright, we can do a page.route, and that page.route will apply for every method of that route. That means that within page.route
, you need to specify
test("Run a test", async ({ page }) => {
page.route('endpoint', route => {
switch (route.request().method()) {
case 'GET':
await route.fullfill(<get_response>)
case 'POST':
await route.fullfill(<post>)
defaut:
await route.continue()
}
})
})
If I need to then use some other response for another case, I can call page.route
again for this route. But let’s say I only need to change the POST response. If I were to do this:
test("Run a test", async ({ page }) => {
page.route('endpoint', route => {
switch (route.request().method()) {
case 'GET':
await route.fullfill(<get_response>)
case 'POST':
await route.fullfill(<post_response>)
defaut:
await route.continue()
}
})
// perform some actions
page.route('endpoint', () => {
switch (route.request().method()) {
case 'POST':
await route.fullfill(<different_post_response>)
defaut:
await route.continue()
}
})
})
In this case, all methods of the route
ed endpoint are overwritten, and a GET
call to that endpoint would fall through the route.continue()
.
Is there a more precise way to mock just a single route method in playwright, without overwriting mocks for other methods of the same endpoint?
(An example would be like in cypress, where a user can cy.intercept({ url: 'url', method: METHOD })
, which can be called multiple times with various arguments, but because the method is part of the call option params, there is no interference between methods.)