2018-03-20 03:00:12 +00:00
|
|
|
/**
|
|
|
|
* 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.
|
|
|
|
*/
|
|
|
|
|
2020-06-23 05:18:46 +00:00
|
|
|
import expect from 'expect';
|
|
|
|
import {
|
2020-05-07 10:54:55 +00:00
|
|
|
getTestState,
|
|
|
|
setupTestBrowserHooks,
|
|
|
|
setupTestPageAndContextHooks,
|
2020-06-23 05:18:46 +00:00
|
|
|
itFailsFirefox,
|
2020-07-13 09:22:26 +00:00
|
|
|
} from './mocha-utils'; // eslint-disable-line import/extensions
|
2020-04-09 05:56:25 +00:00
|
|
|
|
2020-05-07 10:54:55 +00:00
|
|
|
describe('JSHandle', function () {
|
2020-04-09 05:56:25 +00:00
|
|
|
setupTestBrowserHooks();
|
|
|
|
setupTestPageAndContextHooks();
|
2018-03-20 03:00:12 +00:00
|
|
|
|
2020-05-07 10:54:55 +00:00
|
|
|
describe('Page.evaluateHandle', function () {
|
|
|
|
it('should work', async () => {
|
|
|
|
const { page } = getTestState();
|
2020-04-09 05:56:25 +00:00
|
|
|
|
2018-11-21 04:18:57 +00:00
|
|
|
const windowHandle = await page.evaluateHandle(() => window);
|
|
|
|
expect(windowHandle).toBeTruthy();
|
|
|
|
});
|
2020-05-07 10:54:55 +00:00
|
|
|
it('should accept object handle as an argument', async () => {
|
|
|
|
const { page } = getTestState();
|
2020-04-09 05:56:25 +00:00
|
|
|
|
2018-11-21 04:18:57 +00:00
|
|
|
const navigatorHandle = await page.evaluateHandle(() => navigator);
|
2020-07-10 10:52:13 +00:00
|
|
|
const text = await page.evaluate(
|
|
|
|
(e: Navigator) => 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 () => {
|
|
|
|
const { page } = getTestState();
|
2020-04-09 05:56:25 +00:00
|
|
|
|
2018-11-21 04:18:57 +00:00
|
|
|
const aHandle = await page.evaluateHandle(() => 5);
|
2020-05-07 10:54:55 +00:00
|
|
|
const isFive = await page.evaluate((e) => Object.is(e, 5), aHandle);
|
2018-11-21 04:18:57 +00:00
|
|
|
expect(isFive).toBeTruthy();
|
|
|
|
});
|
2020-05-07 10:54:55 +00:00
|
|
|
it('should warn on nested object handles', async () => {
|
|
|
|
const { page } = getTestState();
|
2020-04-09 05:56:25 +00:00
|
|
|
|
2019-01-14 22:30:50 +00:00
|
|
|
const aHandle = await page.evaluateHandle(() => document.body);
|
|
|
|
let error = null;
|
2020-05-07 10:54:55 +00:00
|
|
|
await page
|
2020-07-01 11:44:08 +00:00
|
|
|
// @ts-expect-error we are deliberately passing a bad type here (nested object)
|
2020-05-07 10:54:55 +00:00
|
|
|
.evaluateHandle((opts) => opts.elem.querySelector('p'), {
|
|
|
|
elem: aHandle,
|
|
|
|
})
|
|
|
|
.catch((error_) => (error = error_));
|
2019-01-14 22:30:50 +00:00
|
|
|
expect(error.message).toContain('Are you passing a nested JSHandle?');
|
|
|
|
});
|
2020-05-07 10:54:55 +00:00
|
|
|
it('should accept object handle to unserializable value', async () => {
|
|
|
|
const { page } = getTestState();
|
2020-04-09 05:56:25 +00:00
|
|
|
|
2019-02-02 19:55:15 +00:00
|
|
|
const aHandle = await page.evaluateHandle(() => Infinity);
|
2020-05-07 10:54:55 +00:00
|
|
|
expect(await page.evaluate((e) => Object.is(e, Infinity), aHandle)).toBe(
|
|
|
|
true
|
|
|
|
);
|
2019-02-02 19:55:15 +00:00
|
|
|
});
|
2020-05-07 10:54:55 +00:00
|
|
|
it('should use the same JS wrappers', async () => {
|
|
|
|
const { page } = getTestState();
|
2020-04-09 05:56:25 +00:00
|
|
|
|
2019-02-02 19:55:15 +00:00
|
|
|
const aHandle = await page.evaluateHandle(() => {
|
2020-06-23 05:18:46 +00:00
|
|
|
globalThis.FOO = 123;
|
2019-02-02 19:55:15 +00:00
|
|
|
return window;
|
|
|
|
});
|
2020-07-10 10:52:13 +00:00
|
|
|
expect(await page.evaluate((e: { FOO: number }) => e.FOO, aHandle)).toBe(
|
|
|
|
123
|
|
|
|
);
|
2019-02-02 19:55:15 +00:00
|
|
|
});
|
2020-05-07 10:54:55 +00:00
|
|
|
it('should work with primitives', async () => {
|
|
|
|
const { page } = getTestState();
|
2020-04-09 05:56:25 +00:00
|
|
|
|
2019-02-02 19:55:15 +00:00
|
|
|
const aHandle = await page.evaluateHandle(() => {
|
2020-06-23 05:18:46 +00:00
|
|
|
globalThis.FOO = 123;
|
2019-02-02 19:55:15 +00:00
|
|
|
return window;
|
|
|
|
});
|
2020-07-10 10:52:13 +00:00
|
|
|
expect(await page.evaluate((e: { FOO: number }) => e.FOO, aHandle)).toBe(
|
|
|
|
123
|
|
|
|
);
|
2019-02-02 19:55:15 +00:00
|
|
|
});
|
2018-11-21 04:18:57 +00:00
|
|
|
});
|
|
|
|
|
2020-05-07 10:54:55 +00:00
|
|
|
describe('JSHandle.getProperty', function () {
|
|
|
|
it('should work', async () => {
|
|
|
|
const { page } = getTestState();
|
2020-04-09 05:56:25 +00:00
|
|
|
|
2018-03-20 03:00:12 +00:00
|
|
|
const aHandle = await page.evaluateHandle(() => ({
|
|
|
|
one: 1,
|
|
|
|
two: 2,
|
2020-05-07 10:54:55 +00:00
|
|
|
three: 3,
|
2018-03-20 03:00:12 +00:00
|
|
|
}));
|
|
|
|
const 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 () => {
|
|
|
|
const { page } = getTestState();
|
2020-04-09 05:56:25 +00:00
|
|
|
|
2020-05-07 10:54:55 +00:00
|
|
|
const aHandle = await page.evaluateHandle(() => ({ foo: 'bar' }));
|
2018-03-20 03:00:12 +00:00
|
|
|
const json = await aHandle.jsonValue();
|
2020-05-07 10:54:55 +00:00
|
|
|
expect(json).toEqual({ foo: 'bar' });
|
2018-03-20 03:00:12 +00:00
|
|
|
});
|
2020-05-07 10:54:55 +00:00
|
|
|
itFailsFirefox('should not work with dates', async () => {
|
|
|
|
const { page } = getTestState();
|
2020-04-09 05:56:25 +00:00
|
|
|
|
2020-05-07 10:54:55 +00:00
|
|
|
const dateHandle = await page.evaluateHandle(
|
|
|
|
() => new Date('2017-09-26T00:00:00.000Z')
|
|
|
|
);
|
2018-03-20 03:00:12 +00:00
|
|
|
const json = await dateHandle.jsonValue();
|
|
|
|
expect(json).toEqual({});
|
|
|
|
});
|
2020-05-07 10:54:55 +00:00
|
|
|
it('should throw for circular objects', async () => {
|
|
|
|
const { page, isChrome } = getTestState();
|
2020-04-09 05:56:25 +00:00
|
|
|
|
2018-03-20 03:00:12 +00:00
|
|
|
const windowHandle = await page.evaluateHandle('window');
|
|
|
|
let error = null;
|
2020-05-07 10:54:55 +00:00
|
|
|
await windowHandle.jsonValue().catch((error_) => (error = error_));
|
2020-04-09 05:56:25 +00:00
|
|
|
if (isChrome)
|
2019-02-14 07:59:38 +00:00
|
|
|
expect(error.message).toContain('Object reference chain is too long');
|
2020-05-07 10:54:55 +00:00
|
|
|
else expect(error.message).toContain('Object is not serializable');
|
2018-03-20 03:00:12 +00:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2020-05-07 10:54:55 +00:00
|
|
|
describe('JSHandle.getProperties', function () {
|
|
|
|
it('should work', async () => {
|
|
|
|
const { page } = getTestState();
|
2020-04-09 05:56:25 +00:00
|
|
|
|
2018-03-20 03:00:12 +00:00
|
|
|
const aHandle = await page.evaluateHandle(() => ({
|
2020-05-07 10:54:55 +00:00
|
|
|
foo: 'bar',
|
2018-03-20 03:00:12 +00:00
|
|
|
}));
|
|
|
|
const properties = await aHandle.getProperties();
|
|
|
|
const 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 () => {
|
|
|
|
const { page } = getTestState();
|
2020-04-09 05:56:25 +00:00
|
|
|
|
2018-03-20 03:00:12 +00:00
|
|
|
const aHandle = await page.evaluateHandle(() => {
|
|
|
|
class A {
|
2020-06-23 05:18:46 +00:00
|
|
|
a: string;
|
2018-03-20 03:00:12 +00:00
|
|
|
constructor() {
|
|
|
|
this.a = '1';
|
|
|
|
}
|
|
|
|
}
|
|
|
|
class B extends A {
|
2020-06-23 05:18:46 +00:00
|
|
|
b: string;
|
2018-03-20 03:00:12 +00:00
|
|
|
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 () => {
|
|
|
|
const { page } = getTestState();
|
2020-04-09 05:56:25 +00:00
|
|
|
|
2018-03-20 03:00:12 +00:00
|
|
|
const aHandle = await page.evaluateHandle(() => document.body);
|
|
|
|
const element = aHandle.asElement();
|
|
|
|
expect(element).toBeTruthy();
|
|
|
|
});
|
2020-05-07 10:54:55 +00:00
|
|
|
it('should return null for non-elements', async () => {
|
|
|
|
const { page } = getTestState();
|
2020-04-09 05:56:25 +00:00
|
|
|
|
2018-03-20 03:00:12 +00:00
|
|
|
const aHandle = await page.evaluateHandle(() => 2);
|
|
|
|
const element = aHandle.asElement();
|
|
|
|
expect(element).toBeFalsy();
|
|
|
|
});
|
2020-06-12 13:55:51 +00:00
|
|
|
it('should return ElementHandle for TextNodes', async () => {
|
2020-05-07 10:54:55 +00:00
|
|
|
const { page } = getTestState();
|
2020-04-09 05:56:25 +00:00
|
|
|
|
2018-03-20 03:00:12 +00:00
|
|
|
await page.setContent('<div>ee!</div>');
|
2020-05-07 10:54:55 +00:00
|
|
|
const aHandle = await page.evaluateHandle(
|
|
|
|
() => document.querySelector('div').firstChild
|
|
|
|
);
|
2018-03-20 03:00:12 +00:00
|
|
|
const element = aHandle.asElement();
|
|
|
|
expect(element).toBeTruthy();
|
2020-05-07 10:54:55 +00:00
|
|
|
expect(
|
2020-07-10 10:52:13 +00:00
|
|
|
await page.evaluate(
|
|
|
|
(e: HTMLElement) => e.nodeType === Node.TEXT_NODE,
|
|
|
|
element
|
|
|
|
)
|
2020-05-07 10:54:55 +00:00
|
|
|
);
|
2018-03-20 03:00:12 +00:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2020-05-07 10:54:55 +00:00
|
|
|
describe('JSHandle.toString', function () {
|
|
|
|
it('should work for primitives', async () => {
|
|
|
|
const { page } = getTestState();
|
2020-04-09 05:56:25 +00:00
|
|
|
|
2018-03-20 03:00:12 +00:00
|
|
|
const numberHandle = await page.evaluateHandle(() => 2);
|
|
|
|
expect(numberHandle.toString()).toBe('JSHandle:2');
|
|
|
|
const stringHandle = await page.evaluateHandle(() => 'a');
|
|
|
|
expect(stringHandle.toString()).toBe('JSHandle:a');
|
|
|
|
});
|
2020-05-07 10:54:55 +00:00
|
|
|
it('should work for complicated objects', async () => {
|
|
|
|
const { page } = getTestState();
|
2020-04-09 05:56:25 +00:00
|
|
|
|
2018-03-20 03:00:12 +00:00
|
|
|
const aHandle = await page.evaluateHandle(() => window);
|
|
|
|
expect(aHandle.toString()).toBe('JSHandle@object');
|
|
|
|
});
|
2020-05-07 10:54:55 +00:00
|
|
|
it('should work with different subtypes', async () => {
|
|
|
|
const { page } = getTestState();
|
2020-04-09 05:56:25 +00:00
|
|
|
|
2020-05-07 10:54:55 +00:00
|
|
|
expect((await page.evaluateHandle('(function(){})')).toString()).toBe(
|
|
|
|
'JSHandle@function'
|
|
|
|
);
|
2019-02-02 19:55:15 +00:00
|
|
|
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'
|
|
|
|
);
|
2019-02-02 19:55:15 +00:00
|
|
|
});
|
2018-03-20 03:00:12 +00:00
|
|
|
});
|
2020-04-09 05:56:25 +00:00
|
|
|
});
|