puppeteer/test/src/jshandle.spec.ts

374 lines
11 KiB
TypeScript
Raw Normal View History

/**
2024-01-03 10:11:33 +00:00
* @license
* Copyright 2018 Google Inc.
* SPDX-License-Identifier: Apache-2.0
*/
import expect from 'expect';
2023-08-29 20:48:37 +00:00
import {JSHandle} from 'puppeteer-core/internal/api/JSHandle.js';
import {
asyncDisposeSymbol,
disposeSymbol,
} from 'puppeteer-core/internal/util/disposable.js';
2023-08-29 20:48:37 +00:00
import sinon from 'sinon';
import {getTestState, setupTestBrowserHooks} from './mocha-utils.js';
2020-05-07 10:54:55 +00:00
describe('JSHandle', function () {
setupTestBrowserHooks();
2020-05-07 10:54:55 +00:00
describe('Page.evaluateHandle', function () {
it('should work', async () => {
2023-06-21 19:41:09 +00:00
const {page} = await getTestState();
using windowHandle = await page.evaluateHandle(() => {
return window;
});
2018-11-21 04:18:57 +00:00
expect(windowHandle).toBeTruthy();
});
it('should return the RemoteObject', async () => {
2023-06-21 19:41:09 +00:00
const {page} = await getTestState();
using windowHandle = await page.evaluateHandle(() => {
return window;
});
expect(windowHandle.remoteObject()).toBeTruthy();
});
2020-05-07 10:54:55 +00:00
it('should accept object handle as an argument', async () => {
2023-06-21 19:41:09 +00:00
const {page} = await getTestState();
using navigatorHandle = await page.evaluateHandle(() => {
return navigator;
});
feat!: type inference for evaluation types (#8547) This PR greatly improves the types within Puppeteer: - **Almost everything** is auto-deduced. - Parameters don't need to be specified in the function. They are deduced from the spread. - Return types don't need to be specified. They are deduced from the function. (More on this below) - Selections based on tag names correctly deduce element type, similar to TypeScript's mechanism for `getElementByTagName`. - [**BREAKING CHANGE**] We've removed the ability to declare return types in type arguments for the following reasons: 1. Setting them will indubitably break auto-deduction. 2. You can just use `as ...` in TypeScript to coerce the correct type (given it makes sense). - [**BREAKING CHANGE**] `waitFor` is officially gone. To migrate to these changes, there are only four things you may need to change: - If you set a return type using the `ReturnType` type parameter, remove it and use `as ...` and `HandleFor` (if necessary). ⛔ `evaluate<ReturnType>(a: number, b: number) => {...}, a, b)` ✅ `(await evaluate(a, b) => {...}, a, b)) as ReturnType` ⛔ `evaluateHandle<ReturnType>(a: number, b: number) => {...}, a, b)` ✅ `(await evaluateHandle(a, b) => {...}, a, b)) as HandleFor<ReturnType>` - If you set any type parameters in the *parameters* of an evaluation function, remove them. ⛔ `evaluate(a: number, b: number) => {...}, a, b)` ✅ `evaluate(a, b) => {...}, a, b)` - If you set any type parameters in the method's declaration, remove them. ⛔ `evaluate<(a: number, b: number) => void>((a, b) => {...}, a, b)` ✅ `evaluate(a, b) => {...}, a, b)`
2022-06-23 09:29:46 +00:00
const text = await page.evaluate(e => {
return e.userAgent;
}, navigatorHandle);
2018-11-21 04:18:57 +00:00
expect(text).toContain('Mozilla');
});
2020-05-07 10:54:55 +00:00
it('should accept object handle to primitive types', async () => {
2023-06-21 19:41:09 +00:00
const {page} = await getTestState();
using aHandle = await page.evaluateHandle(() => {
return 5;
});
const isFive = await page.evaluate(e => {
return Object.is(e, 5);
}, aHandle);
2018-11-21 04:18:57 +00:00
expect(isFive).toBeTruthy();
});
2022-06-13 09:16:25 +00:00
it('should warn about recursive objects', async () => {
2023-06-21 19:41:09 +00:00
const {page} = await getTestState();
const test: {obj?: unknown} = {};
2022-06-13 09:16:25 +00:00
test.obj = test;
let error!: Error;
2020-05-07 10:54:55 +00:00
await page
.evaluateHandle(opts => {
return opts;
}, test)
.catch(error_ => {
return (error = error_);
});
2022-06-13 09:16:25 +00:00
expect(error.message).toContain('Recursive objects are not allowed.');
});
2020-05-07 10:54:55 +00:00
it('should accept object handle to unserializable value', async () => {
2023-06-21 19:41:09 +00:00
const {page} = await getTestState();
using aHandle = await page.evaluateHandle(() => {
return Infinity;
});
expect(
await page.evaluate(e => {
return Object.is(e, Infinity);
}, aHandle)
).toBe(true);
});
2020-05-07 10:54:55 +00:00
it('should use the same JS wrappers', async () => {
2023-06-21 19:41:09 +00:00
const {page} = await getTestState();
using aHandle = await page.evaluateHandle(() => {
(globalThis as any).FOO = 123;
return window;
});
expect(
feat!: type inference for evaluation types (#8547) This PR greatly improves the types within Puppeteer: - **Almost everything** is auto-deduced. - Parameters don't need to be specified in the function. They are deduced from the spread. - Return types don't need to be specified. They are deduced from the function. (More on this below) - Selections based on tag names correctly deduce element type, similar to TypeScript's mechanism for `getElementByTagName`. - [**BREAKING CHANGE**] We've removed the ability to declare return types in type arguments for the following reasons: 1. Setting them will indubitably break auto-deduction. 2. You can just use `as ...` in TypeScript to coerce the correct type (given it makes sense). - [**BREAKING CHANGE**] `waitFor` is officially gone. To migrate to these changes, there are only four things you may need to change: - If you set a return type using the `ReturnType` type parameter, remove it and use `as ...` and `HandleFor` (if necessary). ⛔ `evaluate<ReturnType>(a: number, b: number) => {...}, a, b)` ✅ `(await evaluate(a, b) => {...}, a, b)) as ReturnType` ⛔ `evaluateHandle<ReturnType>(a: number, b: number) => {...}, a, b)` ✅ `(await evaluateHandle(a, b) => {...}, a, b)) as HandleFor<ReturnType>` - If you set any type parameters in the *parameters* of an evaluation function, remove them. ⛔ `evaluate(a: number, b: number) => {...}, a, b)` ✅ `evaluate(a, b) => {...}, a, b)` - If you set any type parameters in the method's declaration, remove them. ⛔ `evaluate<(a: number, b: number) => void>((a, b) => {...}, a, b)` ✅ `evaluate(a, b) => {...}, a, b)`
2022-06-23 09:29:46 +00:00
await page.evaluate(e => {
return (e as any).FOO;
}, aHandle)
).toBe(123);
});
2018-11-21 04:18:57 +00:00
});
2020-05-07 10:54:55 +00:00
describe('JSHandle.getProperty', function () {
it('should work', async () => {
2023-06-21 19:41:09 +00:00
const {page} = await getTestState();
using aHandle = await page.evaluateHandle(() => {
return {
one: 1,
two: 2,
three: 3,
};
});
using twoHandle = await aHandle.getProperty('two');
expect(await twoHandle.jsonValue()).toEqual(2);
});
});
2020-05-07 10:54:55 +00:00
describe('JSHandle.jsonValue', function () {
it('should work', async () => {
2023-06-21 19:41:09 +00:00
const {page} = await getTestState();
using aHandle = await page.evaluateHandle(() => {
return {foo: 'bar'};
});
const json = await aHandle.jsonValue();
expect(json).toEqual({foo: 'bar'});
});
it('works with jsonValues that are not objects', async () => {
2023-06-21 19:41:09 +00:00
const {page} = await getTestState();
using aHandle = await page.evaluateHandle(() => {
return ['a', 'b'];
});
const json = await aHandle.jsonValue();
expect(json).toEqual(['a', 'b']);
});
it('works with jsonValues that are primitives', async () => {
2023-06-21 19:41:09 +00:00
const {page} = await getTestState();
using aHandle = await page.evaluateHandle(() => {
return 'foo';
});
expect(await aHandle.jsonValue()).toEqual('foo');
using bHandle = await page.evaluateHandle(() => {
return undefined;
});
expect(await bHandle.jsonValue()).toEqual(undefined);
});
it('should work with dates', async () => {
2023-06-21 19:41:09 +00:00
const {page} = await getTestState();
using dateHandle = await page.evaluateHandle(() => {
return new Date('2017-09-26T00:00:00.000Z');
});
const date = await dateHandle.jsonValue();
expect(date).toBeInstanceOf(Date);
expect(date.toISOString()).toEqual('2017-09-26T00:00:00.000Z');
});
it('should not throw for circular objects', async () => {
2023-06-21 19:41:09 +00:00
const {page} = await getTestState();
using handle = await page.evaluateHandle(() => {
const t: {t?: unknown; g: number} = {g: 1};
t.t = t;
return t;
});
await handle.jsonValue();
});
});
2020-05-07 10:54:55 +00:00
describe('JSHandle.getProperties', function () {
it('should work', async () => {
2023-06-21 19:41:09 +00:00
const {page} = await getTestState();
using aHandle = await page.evaluateHandle(() => {
return {
foo: 'bar',
};
});
const properties = await aHandle.getProperties();
using foo = properties.get('foo')!;
expect(foo).toBeTruthy();
expect(await foo.jsonValue()).toBe('bar');
});
2020-05-07 10:54:55 +00:00
it('should return even non-own properties', async () => {
2023-06-21 19:41:09 +00:00
const {page} = await getTestState();
using 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');
});
});
2020-05-07 10:54:55 +00:00
describe('JSHandle.asElement', function () {
it('should work', async () => {
2023-06-21 19:41:09 +00:00
const {page} = await getTestState();
using aHandle = await page.evaluateHandle(() => {
return document.body;
});
using element = aHandle.asElement();
expect(element).toBeTruthy();
});
2020-05-07 10:54:55 +00:00
it('should return null for non-elements', async () => {
2023-06-21 19:41:09 +00:00
const {page} = await getTestState();
using aHandle = await page.evaluateHandle(() => {
return 2;
});
using element = aHandle.asElement();
expect(element).toBeFalsy();
});
it('should return ElementHandle for TextNodes', async () => {
2023-06-21 19:41:09 +00:00
const {page} = await getTestState();
await page.setContent('<div>ee!</div>');
using aHandle = await page.evaluateHandle(() => {
return document.querySelector('div')!.firstChild;
});
using element = aHandle.asElement();
expect(element).toBeTruthy();
2020-05-07 10:54:55 +00:00
expect(
2022-06-24 06:40:08 +00:00
await page.evaluate(e => {
return e?.nodeType === Node.TEXT_NODE;
}, element)
2020-05-07 10:54:55 +00:00
);
});
});
2020-05-07 10:54:55 +00:00
describe('JSHandle.toString', function () {
it('should work for primitives', async () => {
2023-06-21 19:41:09 +00:00
const {page} = await getTestState();
using numberHandle = await page.evaluateHandle(() => {
return 2;
});
expect(numberHandle.toString()).toBe('JSHandle:2');
using stringHandle = await page.evaluateHandle(() => {
return 'a';
});
expect(stringHandle.toString()).toBe('JSHandle:a');
});
2020-05-07 10:54:55 +00:00
it('should work for complicated objects', async () => {
2023-06-21 19:41:09 +00:00
const {page} = await getTestState();
using aHandle = await page.evaluateHandle(() => {
return window;
});
expect(aHandle.toString()).atLeastOneToContain([
'JSHandle@object',
'JSHandle@window',
]);
});
2020-05-07 10:54:55 +00:00
it('should work with different subtypes', async () => {
2023-06-21 19:41:09 +00:00
const {page} = await getTestState();
2020-05-07 10:54:55 +00:00
expect((await page.evaluateHandle('(function(){})')).toString()).toBe(
'JSHandle@function'
);
expect((await page.evaluateHandle('12')).toString()).toBe('JSHandle:12');
2020-05-07 10:54:55 +00:00
expect((await page.evaluateHandle('true')).toString()).toBe(
'JSHandle:true'
);
expect((await page.evaluateHandle('undefined')).toString()).toBe(
'JSHandle:undefined'
);
expect((await page.evaluateHandle('"foo"')).toString()).toBe(
'JSHandle:foo'
);
expect((await page.evaluateHandle('Symbol()')).toString()).toBe(
'JSHandle@symbol'
);
expect((await page.evaluateHandle('new Map()')).toString()).toBe(
'JSHandle@map'
);
expect((await page.evaluateHandle('new Set()')).toString()).toBe(
'JSHandle@set'
);
expect((await page.evaluateHandle('[]')).toString()).toBe(
'JSHandle@array'
);
expect((await page.evaluateHandle('null')).toString()).toBe(
'JSHandle:null'
);
expect((await page.evaluateHandle('/foo/')).toString()).toBe(
'JSHandle@regexp'
);
expect((await page.evaluateHandle('document.body')).toString()).toBe(
'JSHandle@node'
);
expect((await page.evaluateHandle('new Date()')).toString()).toBe(
'JSHandle@date'
);
expect((await page.evaluateHandle('new WeakMap()')).toString()).toBe(
'JSHandle@weakmap'
);
expect((await page.evaluateHandle('new WeakSet()')).toString()).toBe(
'JSHandle@weakset'
);
expect((await page.evaluateHandle('new Error()')).toString()).toBe(
'JSHandle@error'
);
expect((await page.evaluateHandle('new Int32Array()')).toString()).toBe(
'JSHandle@typedarray'
);
expect((await page.evaluateHandle('new Proxy({}, {})')).toString()).toBe(
'JSHandle@proxy'
);
});
});
2023-08-29 20:48:37 +00:00
describe('JSHandle[Symbol.dispose]', () => {
it('should work', async () => {
const {page} = await getTestState();
using handle = await page.evaluateHandle('new Set()');
const spy = sinon.spy(handle, disposeSymbol);
2023-08-29 20:48:37 +00:00
{
using _ = handle;
}
expect(handle).toBeInstanceOf(JSHandle);
expect(spy.calledOnce).toBeTruthy();
expect(handle.disposed).toBeTruthy();
});
});
describe('JSHandle[Symbol.asyncDispose]', () => {
it('should work', async () => {
const {page} = await getTestState();
using handle = await page.evaluateHandle('new Set()');
const spy = sinon.spy(handle, asyncDisposeSymbol);
2023-08-29 20:48:37 +00:00
{
await using _ = handle;
}
expect(handle).toBeInstanceOf(JSHandle);
expect(spy.calledOnce).toBeTruthy();
expect(handle.disposed).toBeTruthy();
});
});
describe('JSHandle.move', () => {
it('should work', async () => {
const {page} = await getTestState();
using handle = await page.evaluateHandle('new Set()');
const spy = sinon.spy(handle, disposeSymbol);
2023-08-29 20:48:37 +00:00
{
using _ = handle;
handle.move();
}
expect(handle).toBeInstanceOf(JSHandle);
expect(spy.calledOnce).toBeTruthy();
expect(handle.disposed).toBeFalsy();
});
});
});