Skip to content

Playwright is a framework for Web Testing and Automation. It allows testing Chromium, Firefox and WebKit with a single API.

test: add test for sendBeacon and asserting request body (#12274)

Max Schmittbe2e4866b072
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' });
+});