diff --git a/packages/playwright-core/src/client/fetch.ts b/packages/playwright-core/src/client/fetch.ts index e4b5135548..3f8009b981 100644 --- a/packages/playwright-core/src/client/fetch.ts +++ b/packages/playwright-core/src/client/fetch.ts @@ -220,7 +220,7 @@ export class APIRequestContext extends ChannelOwner implements api.Ro if (options.response) { statusOption ||= options.response.status(); headersOption ||= options.response.headers(); - if (options.body === undefined && options.path === undefined && options.response instanceof APIResponse) - fetchResponseUid = (options.response as APIResponse)._fetchUid(); + if (options.body === undefined && options.path === undefined && options.response instanceof APIResponse) { + if (options.response._request._connection === this._connection) + fetchResponseUid = (options.response as APIResponse)._fetchUid(); + else + body = await options.response.body(); + } } let isBase64 = false; diff --git a/tests/browsertype-connect.spec.ts b/tests/browsertype-connect.spec.ts index f2adcef51d..831b9eec95 100644 --- a/tests/browsertype-connect.spec.ts +++ b/tests/browsertype-connect.spec.ts @@ -555,3 +555,19 @@ test('should record trace with sources', async ({ browserType, startRemoteServer const thisFile = await fs.promises.readFile(__filename); expect(sourceFile).toEqual(thisFile); }); + +test('should fulfill with global fetch result', async ({ browserType, startRemoteServer, playwright, server }) => { + const remoteServer = await startRemoteServer(); + const browser = await browserType.connect(remoteServer.wsEndpoint()); + const context = await browser.newContext(); + const page = await context.newPage(); + + await page.route('**/*', async route => { + const request = await playwright.request.newContext(); + const response = await request.get(server.PREFIX + '/simple.json'); + route.fulfill({ response }); + }); + const response = await page.goto(server.EMPTY_PAGE); + expect(response.status()).toBe(200); + expect(await response.json()).toEqual({ 'foo': 'bar' }); +});