diff --git a/packages/playwright-core/src/server/page.ts b/packages/playwright-core/src/server/page.ts index 25394b1b5c..f2e59aa56f 100644 --- a/packages/playwright-core/src/server/page.ts +++ b/packages/playwright-core/src/server/page.ts @@ -846,6 +846,8 @@ export class PageBinding { const handle = await context.evaluateHandle(takeHandle, { name, seq }).catch(e => null); result = await binding.playwrightFunction({ frame: context.frame, page, context: page._browserContext }, handle); } else { + if (!Array.isArray(serializedArgs)) + throw new Error(`serializedArgs is not an array. This can happen when Array.prototype.toJSON is defined incorrectly`); const args = serializedArgs!.map(a => parseEvaluationResultValue(a)); result = await binding.playwrightFunction({ frame: context.frame, page, context: page._browserContext }, ...args); } diff --git a/tests/page/page-expose-function.spec.ts b/tests/page/page-expose-function.spec.ts index ece4a602df..fc18ada02b 100644 --- a/tests/page/page-expose-function.spec.ts +++ b/tests/page/page-expose-function.spec.ts @@ -300,3 +300,12 @@ it('should work with busted Array.prototype.map/push', async ({ page, server }) await page.exposeFunction('add', (a, b) => a + b); expect(await page.evaluate('add(5, 6)')).toBe(11); }); + +it('should fail with busted Array.prototype.toJSON', async ({ page }) => { + await page.evaluateHandle(() => (Array.prototype as any).toJSON = () => '"[]"'); + + await page.exposeFunction('add', (a, b) => a + b); + await expect(() => page.evaluate(`add(5, 6)`)).rejects.toThrowError('serializedArgs is not an array. This can happen when Array.prototype.toJSON is defined incorrectly'); + + expect.soft(await page.evaluate(() => ([] as any).toJSON())).toBe('"[]"'); +});