From be2e4866b072dbbcc1688f09bb1d816883b9ab08 Mon Sep 17 00:00:00 2001 From: Max Schmitt Date: Tue, 22 Feb 2022 20:15:24 +0100 Subject: [PATCH] test: add test for sendBeacon and asserting request body (#12274) --- tests/page/network-post-data.spec.ts | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/tests/page/network-post-data.spec.ts b/tests/page/network-post-data.spec.ts index 3b5dd660b6..5b0665dda2 100644 --- a/tests/page/network-post-data.spec.ts +++ b/tests/page/network-post-data.spec.ts @@ -107,3 +107,17 @@ it('should get post data for file/blob', async ({ page, server, browserName }) = ]); expect(request.postData()).toBe('file-contents'); }); + +it('should get post data for navigator.sendBeacon api calls', async ({ page, server, browserName }) => { + it.info().annotations.push({ type: 'issue', description: 'https://github.com/microsoft/playwright/issues/12231' }); + it.fail(browserName === 'chromium', 'postData is empty'); + it.fail(browserName === 'webkit', 'postData is empty'); + await page.goto(server.EMPTY_PAGE); + const [request] = await Promise.all([ + page.waitForRequest('**/*'), + page.evaluate(() => navigator.sendBeacon(window.location.origin + '/api/foo', new Blob([JSON.stringify({ foo: 'bar' })]))) + ]); + expect(request.method()).toBe('POST'); + expect(request.url()).toBe(server.PREFIX + '/api/foo'); + expect(request.postDataJSON()).toStrictEqual({ foo: 'bar' }); +});