/** * Copyright 2018 Google Inc. All rights reserved. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ import expect from 'expect'; import { getTestState, setupTestBrowserHooks, setupTestPageAndContextHooks, itFailsFirefox, } from './mocha-utils'; describe('JSHandle', function () { setupTestBrowserHooks(); setupTestPageAndContextHooks(); describe('Page.evaluateHandle', function () { it('should work', async () => { const { page } = getTestState(); const windowHandle = await page.evaluateHandle(() => window); expect(windowHandle).toBeTruthy(); }); it('should accept object handle as an argument', async () => { const { page } = getTestState(); const navigatorHandle = await page.evaluateHandle(() => navigator); const text = await page.evaluate((e) => e.userAgent, navigatorHandle); expect(text).toContain('Mozilla'); }); it('should accept object handle to primitive types', async () => { const { page } = getTestState(); const aHandle = await page.evaluateHandle(() => 5); const isFive = await page.evaluate((e) => Object.is(e, 5), aHandle); expect(isFive).toBeTruthy(); }); it('should warn on nested object handles', async () => { const { page } = getTestState(); const aHandle = await page.evaluateHandle(() => document.body); let error = null; await page .evaluateHandle((opts) => opts.elem.querySelector('p'), { elem: aHandle, }) .catch((error_) => (error = error_)); expect(error.message).toContain('Are you passing a nested JSHandle?'); }); it('should accept object handle to unserializable value', async () => { const { page } = getTestState(); const aHandle = await page.evaluateHandle(() => Infinity); expect(await page.evaluate((e) => Object.is(e, Infinity), aHandle)).toBe( true ); }); it('should use the same JS wrappers', async () => { const { page } = getTestState(); const aHandle = await page.evaluateHandle(() => { globalThis.FOO = 123; return window; }); expect(await page.evaluate((e) => e.FOO, aHandle)).toBe(123); }); it('should work with primitives', async () => { const { page } = getTestState(); const aHandle = await page.evaluateHandle(() => { globalThis.FOO = 123; return window; }); expect(await page.evaluate((e) => e.FOO, aHandle)).toBe(123); }); }); describe('JSHandle.getProperty', function () { it('should work', async () => { const { page } = getTestState(); const aHandle = await page.evaluateHandle(() => ({ one: 1, two: 2, three: 3, })); const twoHandle = await aHandle.getProperty('two'); expect(await twoHandle.jsonValue()).toEqual(2); }); }); describe('JSHandle.jsonValue', function () { it('should work', async () => { const { page } = getTestState(); const aHandle = await page.evaluateHandle(() => ({ foo: 'bar' })); const json = await aHandle.jsonValue(); expect(json).toEqual({ foo: 'bar' }); }); itFailsFirefox('should not work with dates', async () => { const { page } = getTestState(); const dateHandle = await page.evaluateHandle( () => new Date('2017-09-26T00:00:00.000Z') ); const json = await dateHandle.jsonValue(); expect(json).toEqual({}); }); it('should throw for circular objects', async () => { const { page, isChrome } = getTestState(); const windowHandle = await page.evaluateHandle('window'); let error = null; await windowHandle.jsonValue().catch((error_) => (error = error_)); if (isChrome) expect(error.message).toContain('Object reference chain is too long'); else expect(error.message).toContain('Object is not serializable'); }); }); describe('JSHandle.getProperties', function () { it('should work', async () => { const { page } = getTestState(); const aHandle = await page.evaluateHandle(() => ({ foo: 'bar', })); const properties = await aHandle.getProperties(); const foo = properties.get('foo'); expect(foo).toBeTruthy(); expect(await foo.jsonValue()).toBe('bar'); }); it('should return even non-own properties', async () => { const { page } = getTestState(); const aHandle = await page.evaluateHandle(() => { class A { a: string; constructor() { this.a = '1'; } } class B extends A { b: string; constructor() { super(); this.b = '2'; } } return new B(); }); const properties = await aHandle.getProperties(); expect(await properties.get('a').jsonValue()).toBe('1'); expect(await properties.get('b').jsonValue()).toBe('2'); }); }); describe('JSHandle.asElement', function () { it('should work', async () => { const { page } = getTestState(); const aHandle = await page.evaluateHandle(() => document.body); const element = aHandle.asElement(); expect(element).toBeTruthy(); }); it('should return null for non-elements', async () => { const { page } = getTestState(); const aHandle = await page.evaluateHandle(() => 2); const element = aHandle.asElement(); expect(element).toBeFalsy(); }); it('should return ElementHandle for TextNodes', async () => { const { page } = getTestState(); await page.setContent('