diff --git a/packages/playwright-core/src/server/isomorphic/utilityScriptSerializers.ts b/packages/playwright-core/src/server/isomorphic/utilityScriptSerializers.ts index 1f8799d269..5371d0aa79 100644 --- a/packages/playwright-core/src/server/isomorphic/utilityScriptSerializers.ts +++ b/packages/playwright-core/src/server/isomorphic/utilityScriptSerializers.ts @@ -107,11 +107,11 @@ export function source() { function serialize(value: any, handleSerializer: (value: any) => HandleOrValue, visitorInfo: VisitorInfo): SerializedValue { if (value && typeof value === 'object') { - if (globalThis.Window && value instanceof globalThis.Window) + if (typeof globalThis.Window === 'function' && value instanceof globalThis.Window) return 'ref: '; - if (globalThis.Document && value instanceof globalThis.Document) + if (typeof globalThis.Document === 'function' && value instanceof globalThis.Document) return 'ref: '; - if (globalThis.Node && value instanceof globalThis.Node) + if (typeof globalThis.Node === 'function' && value instanceof globalThis.Node) return 'ref: '; } return innerSerialize(value, handleSerializer, visitorInfo); diff --git a/tests/page/page-evaluate.spec.ts b/tests/page/page-evaluate.spec.ts index 26fdfb3b64..783f44ccbe 100644 --- a/tests/page/page-evaluate.spec.ts +++ b/tests/page/page-evaluate.spec.ts @@ -680,3 +680,25 @@ it('should work with overridden Object.defineProperty', async ({ page, server }) await page.goto(server.PREFIX + '/test'); expect(await page.evaluate('1+2')).toBe(3); }); + +it('should work with overridden globalThis.Window/Document/Node', async ({ page, server }) => { + const testCases = [ + // @ts-ignore + () => globalThis.Window = {}, + // @ts-ignore + () => globalThis.Document = {}, + // @ts-ignore + () => globalThis.Node = {}, + () => globalThis.Window = null, + () => globalThis.Document = null, + () => globalThis.Node = null, + ]; + for (const testCase of testCases) { + await it.step(testCase.toString(), async () => { + await page.goto(server.EMPTY_PAGE); + await page.evaluate(testCase); + expect(await page.evaluate('1+2')).toBe(3); + expect(await page.evaluate(() => ['foo'])).toEqual(['foo']); + }); + } +});