2018-11-21 03:59:59 +00:00
|
|
|
/**
|
2024-01-03 10:11:33 +00:00
|
|
|
* @license
|
|
|
|
* Copyright 2018 Google Inc.
|
|
|
|
* SPDX-License-Identifier: Apache-2.0
|
2018-11-21 03:59:59 +00:00
|
|
|
*/
|
|
|
|
|
2020-06-23 05:18:46 +00:00
|
|
|
import expect from 'expect';
|
2023-02-15 23:09:31 +00:00
|
|
|
|
2023-07-03 12:01:29 +00:00
|
|
|
import {getTestState, setupTestBrowserHooks} from './mocha-utils.js';
|
2023-04-25 13:02:25 +00:00
|
|
|
import {attachFrame} from './utils.js';
|
2018-11-21 03:59:59 +00:00
|
|
|
|
2020-05-07 10:54:55 +00:00
|
|
|
describe('Evaluation specs', function () {
|
2023-07-03 12:01:29 +00:00
|
|
|
setupTestBrowserHooks();
|
|
|
|
|
2020-05-07 10:54:55 +00:00
|
|
|
describe('Page.evaluate', function () {
|
|
|
|
it('should work', async () => {
|
2023-06-21 19:41:09 +00:00
|
|
|
const {page} = await getTestState();
|
2020-04-09 05:56:25 +00:00
|
|
|
|
2022-06-15 10:09:22 +00:00
|
|
|
const result = await page.evaluate(() => {
|
|
|
|
return 7 * 3;
|
|
|
|
});
|
2018-11-21 03:59:59 +00:00
|
|
|
expect(result).toBe(21);
|
|
|
|
});
|
2023-02-24 18:26:29 +00:00
|
|
|
it('should transfer BigInt', async () => {
|
2023-06-21 19:41:09 +00:00
|
|
|
const {page} = await getTestState();
|
2020-04-09 05:56:25 +00:00
|
|
|
|
2022-06-15 10:09:22 +00:00
|
|
|
const result = await page.evaluate((a: bigint) => {
|
|
|
|
return a;
|
|
|
|
}, BigInt(42));
|
2019-03-15 17:20:48 +00:00
|
|
|
expect(result).toBe(BigInt(42));
|
|
|
|
});
|
2020-06-12 13:55:51 +00:00
|
|
|
it('should transfer NaN', async () => {
|
2023-06-21 19:41:09 +00:00
|
|
|
const {page} = await getTestState();
|
2020-04-09 05:56:25 +00:00
|
|
|
|
2022-06-22 13:25:44 +00:00
|
|
|
const result = await page.evaluate(a => {
|
2022-06-15 10:09:22 +00:00
|
|
|
return a;
|
|
|
|
}, NaN);
|
2019-02-02 02:40:40 +00:00
|
|
|
expect(Object.is(result, NaN)).toBe(true);
|
|
|
|
});
|
2020-06-12 13:55:51 +00:00
|
|
|
it('should transfer -0', async () => {
|
2023-06-21 19:41:09 +00:00
|
|
|
const {page} = await getTestState();
|
2020-04-09 05:56:25 +00:00
|
|
|
|
2022-06-22 13:25:44 +00:00
|
|
|
const result = await page.evaluate(a => {
|
2022-06-15 10:09:22 +00:00
|
|
|
return a;
|
|
|
|
}, -0);
|
2019-02-02 02:40:40 +00:00
|
|
|
expect(Object.is(result, -0)).toBe(true);
|
|
|
|
});
|
2020-06-12 13:55:51 +00:00
|
|
|
it('should transfer Infinity', async () => {
|
2023-06-21 19:41:09 +00:00
|
|
|
const {page} = await getTestState();
|
2020-04-09 05:56:25 +00:00
|
|
|
|
2022-06-22 13:25:44 +00:00
|
|
|
const result = await page.evaluate(a => {
|
2022-06-15 10:09:22 +00:00
|
|
|
return a;
|
|
|
|
}, Infinity);
|
2019-02-02 02:40:40 +00:00
|
|
|
expect(Object.is(result, Infinity)).toBe(true);
|
|
|
|
});
|
2020-06-12 13:55:51 +00:00
|
|
|
it('should transfer -Infinity', async () => {
|
2023-06-21 19:41:09 +00:00
|
|
|
const {page} = await getTestState();
|
2020-04-09 05:56:25 +00:00
|
|
|
|
2022-06-22 13:25:44 +00:00
|
|
|
const result = await page.evaluate(a => {
|
2022-06-15 10:09:22 +00:00
|
|
|
return a;
|
|
|
|
}, -Infinity);
|
2019-02-02 02:40:40 +00:00
|
|
|
expect(Object.is(result, -Infinity)).toBe(true);
|
|
|
|
});
|
2020-05-07 10:54:55 +00:00
|
|
|
it('should transfer arrays', async () => {
|
2023-06-21 19:41:09 +00:00
|
|
|
const {page} = await getTestState();
|
2020-04-09 05:56:25 +00:00
|
|
|
|
2022-06-15 10:09:22 +00:00
|
|
|
const result = await page.evaluate(
|
2022-06-22 13:25:44 +00:00
|
|
|
a => {
|
2022-06-15 10:09:22 +00:00
|
|
|
return a;
|
|
|
|
},
|
|
|
|
[1, 2, 3]
|
|
|
|
);
|
2020-05-07 10:54:55 +00:00
|
|
|
expect(result).toEqual([1, 2, 3]);
|
2019-02-02 02:40:40 +00:00
|
|
|
});
|
2020-05-07 10:54:55 +00:00
|
|
|
it('should transfer arrays as arrays, not objects', async () => {
|
2023-06-21 19:41:09 +00:00
|
|
|
const {page} = await getTestState();
|
2020-04-09 05:56:25 +00:00
|
|
|
|
2022-06-15 10:09:22 +00:00
|
|
|
const result = await page.evaluate(
|
2022-06-22 13:25:44 +00:00
|
|
|
a => {
|
2022-06-15 10:09:22 +00:00
|
|
|
return Array.isArray(a);
|
|
|
|
},
|
|
|
|
[1, 2, 3]
|
|
|
|
);
|
2019-02-02 02:40:40 +00:00
|
|
|
expect(result).toBe(true);
|
|
|
|
});
|
2020-05-07 10:54:55 +00:00
|
|
|
it('should modify global environment', async () => {
|
2023-06-21 19:41:09 +00:00
|
|
|
const {page} = await getTestState();
|
2020-04-09 05:56:25 +00:00
|
|
|
|
2022-06-15 10:09:22 +00:00
|
|
|
await page.evaluate(() => {
|
|
|
|
return ((globalThis as any).globalVar = 123);
|
|
|
|
});
|
2019-02-02 02:40:40 +00:00
|
|
|
expect(await page.evaluate('globalVar')).toBe(123);
|
|
|
|
});
|
2020-05-07 10:54:55 +00:00
|
|
|
it('should evaluate in the page context', async () => {
|
2023-06-21 19:41:09 +00:00
|
|
|
const {page, server} = await getTestState();
|
2020-04-09 05:56:25 +00:00
|
|
|
|
2019-02-02 02:40:40 +00:00
|
|
|
await page.goto(server.PREFIX + '/global-var.html');
|
|
|
|
expect(await page.evaluate('globalVar')).toBe(123);
|
|
|
|
});
|
2023-10-30 12:02:04 +00:00
|
|
|
it('should replace symbols with undefined', async () => {
|
2023-06-21 19:41:09 +00:00
|
|
|
const {page} = await getTestState();
|
2022-09-08 10:32:39 +00:00
|
|
|
|
|
|
|
expect(
|
|
|
|
await page.evaluate(() => {
|
2023-10-30 12:02:04 +00:00
|
|
|
return [Symbol('foo4'), 'foo'];
|
2022-09-08 10:32:39 +00:00
|
|
|
})
|
2023-10-30 12:02:04 +00:00
|
|
|
).toEqual([undefined, 'foo']);
|
2022-09-08 10:32:39 +00:00
|
|
|
});
|
2020-05-07 10:54:55 +00:00
|
|
|
it('should work with function shorthands', async () => {
|
2023-06-21 19:41:09 +00:00
|
|
|
const {page} = await getTestState();
|
2020-04-09 05:56:25 +00:00
|
|
|
|
2019-10-16 15:00:20 +00:00
|
|
|
const a = {
|
2022-06-15 10:09:22 +00:00
|
|
|
sum(a: number, b: number) {
|
2020-05-07 10:54:55 +00:00
|
|
|
return a + b;
|
|
|
|
},
|
2018-11-21 03:59:59 +00:00
|
|
|
|
2022-06-15 10:09:22 +00:00
|
|
|
async mult(a: number, b: number) {
|
2020-05-07 10:54:55 +00:00
|
|
|
return a * b;
|
|
|
|
},
|
2019-10-16 15:00:20 +00:00
|
|
|
};
|
2018-11-21 03:59:59 +00:00
|
|
|
expect(await page.evaluate(a.sum, 1, 2)).toBe(3);
|
|
|
|
expect(await page.evaluate(a.mult, 2, 4)).toBe(8);
|
|
|
|
});
|
2020-05-07 10:54:55 +00:00
|
|
|
it('should work with unicode chars', async () => {
|
2023-06-21 19:41:09 +00:00
|
|
|
const {page} = await getTestState();
|
2020-04-09 05:56:25 +00:00
|
|
|
|
2022-06-15 10:09:22 +00:00
|
|
|
const result = await page.evaluate(
|
2022-06-22 13:25:44 +00:00
|
|
|
a => {
|
2022-06-15 10:09:22 +00:00
|
|
|
return a['中文字符'];
|
|
|
|
},
|
|
|
|
{
|
|
|
|
中文字符: 42,
|
|
|
|
}
|
|
|
|
);
|
2019-05-18 11:05:28 +00:00
|
|
|
expect(result).toBe(42);
|
|
|
|
});
|
2022-09-08 10:32:39 +00:00
|
|
|
it('should throw when evaluation triggers reload', async () => {
|
2023-06-21 19:41:09 +00:00
|
|
|
const {page} = await getTestState();
|
2020-04-09 05:56:25 +00:00
|
|
|
|
2022-06-15 10:09:22 +00:00
|
|
|
let error!: Error;
|
2020-05-07 10:54:55 +00:00
|
|
|
await page
|
|
|
|
.evaluate(() => {
|
|
|
|
location.reload();
|
|
|
|
return new Promise(() => {});
|
|
|
|
})
|
2022-06-22 13:25:44 +00:00
|
|
|
.catch(error_ => {
|
2022-06-15 10:09:22 +00:00
|
|
|
return (error = error_);
|
|
|
|
});
|
2018-11-21 03:59:59 +00:00
|
|
|
expect(error.message).toContain('Protocol error');
|
|
|
|
});
|
2020-05-07 10:54:55 +00:00
|
|
|
it('should await promise', async () => {
|
2023-06-21 19:41:09 +00:00
|
|
|
const {page} = await getTestState();
|
2020-04-09 05:56:25 +00:00
|
|
|
|
2022-06-15 10:09:22 +00:00
|
|
|
const result = await page.evaluate(() => {
|
|
|
|
return Promise.resolve(8 * 7);
|
|
|
|
});
|
2018-11-21 03:59:59 +00:00
|
|
|
expect(result).toBe(56);
|
|
|
|
});
|
2020-05-07 10:54:55 +00:00
|
|
|
it('should work right after framenavigated', async () => {
|
2023-06-21 19:41:09 +00:00
|
|
|
const {page, server} = await getTestState();
|
2020-04-09 05:56:25 +00:00
|
|
|
|
2018-11-21 03:59:59 +00:00
|
|
|
let frameEvaluation = null;
|
2022-06-22 13:25:44 +00:00
|
|
|
page.on('framenavigated', async frame => {
|
2022-06-15 10:09:22 +00:00
|
|
|
frameEvaluation = frame.evaluate(() => {
|
|
|
|
return 6 * 7;
|
|
|
|
});
|
2018-11-21 03:59:59 +00:00
|
|
|
});
|
|
|
|
await page.goto(server.EMPTY_PAGE);
|
|
|
|
expect(await frameEvaluation).toBe(42);
|
|
|
|
});
|
2022-09-08 10:32:39 +00:00
|
|
|
it('should work from-inside an exposed function', async () => {
|
2023-06-21 19:41:09 +00:00
|
|
|
const {page} = await getTestState();
|
2020-04-09 05:56:25 +00:00
|
|
|
|
2018-11-21 03:59:59 +00:00
|
|
|
// Setup inpage callback, which calls Page.evaluate
|
2022-06-15 10:09:22 +00:00
|
|
|
await page.exposeFunction(
|
|
|
|
'callController',
|
|
|
|
async function (a: number, b: number) {
|
|
|
|
return await page.evaluate(
|
|
|
|
(a: number, b: number): number => {
|
|
|
|
return a * b;
|
|
|
|
},
|
|
|
|
a,
|
|
|
|
b
|
|
|
|
);
|
|
|
|
}
|
|
|
|
);
|
2020-05-07 10:54:55 +00:00
|
|
|
const result = await page.evaluate(async function () {
|
2023-09-01 07:49:33 +00:00
|
|
|
return (globalThis as any).callController(9, 3);
|
2018-11-21 03:59:59 +00:00
|
|
|
});
|
|
|
|
expect(result).toBe(27);
|
|
|
|
});
|
2020-05-07 10:54:55 +00:00
|
|
|
it('should reject promise with exception', async () => {
|
2023-06-21 19:41:09 +00:00
|
|
|
const {page} = await getTestState();
|
2020-04-09 05:56:25 +00:00
|
|
|
|
2022-06-15 10:09:22 +00:00
|
|
|
let error!: Error;
|
2020-05-07 10:54:55 +00:00
|
|
|
await page
|
2022-06-15 10:09:22 +00:00
|
|
|
.evaluate(() => {
|
|
|
|
// @ts-expect-error we know the object doesn't exist
|
|
|
|
return notExistingObject.property;
|
|
|
|
})
|
2022-06-22 13:25:44 +00:00
|
|
|
.catch(error_ => {
|
2022-06-15 10:09:22 +00:00
|
|
|
return (error = error_);
|
|
|
|
});
|
2018-11-21 03:59:59 +00:00
|
|
|
expect(error).toBeTruthy();
|
2020-06-23 05:18:46 +00:00
|
|
|
expect(error.message).toContain('notExistingObject');
|
2018-11-21 03:59:59 +00:00
|
|
|
});
|
2020-05-07 10:54:55 +00:00
|
|
|
it('should support thrown strings as error messages', async () => {
|
2023-06-21 19:41:09 +00:00
|
|
|
const {page} = await getTestState();
|
2020-04-09 05:56:25 +00:00
|
|
|
|
2022-06-15 10:09:22 +00:00
|
|
|
let error!: Error;
|
2020-05-07 10:54:55 +00:00
|
|
|
await page
|
|
|
|
.evaluate(() => {
|
|
|
|
throw 'qwerty';
|
|
|
|
})
|
2022-06-22 13:25:44 +00:00
|
|
|
.catch(error_ => {
|
2022-06-15 10:09:22 +00:00
|
|
|
return (error = error_);
|
|
|
|
});
|
2023-05-10 08:23:29 +00:00
|
|
|
expect(error).toEqual('qwerty');
|
2018-11-21 03:59:59 +00:00
|
|
|
});
|
2020-05-07 10:54:55 +00:00
|
|
|
it('should support thrown numbers as error messages', async () => {
|
2023-06-21 19:41:09 +00:00
|
|
|
const {page} = await getTestState();
|
2020-04-09 05:56:25 +00:00
|
|
|
|
2022-06-15 10:09:22 +00:00
|
|
|
let error!: Error;
|
2020-05-07 10:54:55 +00:00
|
|
|
await page
|
|
|
|
.evaluate(() => {
|
|
|
|
throw 100500;
|
|
|
|
})
|
2022-06-22 13:25:44 +00:00
|
|
|
.catch(error_ => {
|
2022-06-15 10:09:22 +00:00
|
|
|
return (error = error_);
|
|
|
|
});
|
2023-05-10 08:23:29 +00:00
|
|
|
expect(error).toEqual(100500);
|
2018-11-21 03:59:59 +00:00
|
|
|
});
|
2020-05-07 10:54:55 +00:00
|
|
|
it('should return complex objects', async () => {
|
2023-06-21 19:41:09 +00:00
|
|
|
const {page} = await getTestState();
|
2020-04-09 05:56:25 +00:00
|
|
|
|
2022-06-22 13:25:44 +00:00
|
|
|
const object = {foo: 'bar!'};
|
|
|
|
const result = await page.evaluate(a => {
|
2022-06-15 10:09:22 +00:00
|
|
|
return a;
|
|
|
|
}, object);
|
2018-11-21 03:59:59 +00:00
|
|
|
expect(result).not.toBe(object);
|
|
|
|
expect(result).toEqual(object);
|
|
|
|
});
|
2023-02-24 18:26:29 +00:00
|
|
|
it('should return BigInt', async () => {
|
2023-06-21 19:41:09 +00:00
|
|
|
const {page} = await getTestState();
|
2020-04-09 05:56:25 +00:00
|
|
|
|
2022-06-15 10:09:22 +00:00
|
|
|
const result = await page.evaluate(() => {
|
|
|
|
return BigInt(42);
|
|
|
|
});
|
2019-03-15 17:20:48 +00:00
|
|
|
expect(result).toBe(BigInt(42));
|
|
|
|
});
|
2020-06-12 13:55:51 +00:00
|
|
|
it('should return NaN', async () => {
|
2023-06-21 19:41:09 +00:00
|
|
|
const {page} = await getTestState();
|
2020-04-09 05:56:25 +00:00
|
|
|
|
2022-06-15 10:09:22 +00:00
|
|
|
const result = await page.evaluate(() => {
|
|
|
|
return NaN;
|
|
|
|
});
|
2018-11-21 03:59:59 +00:00
|
|
|
expect(Object.is(result, NaN)).toBe(true);
|
|
|
|
});
|
2020-06-12 13:55:51 +00:00
|
|
|
it('should return -0', async () => {
|
2023-06-21 19:41:09 +00:00
|
|
|
const {page} = await getTestState();
|
2020-04-09 05:56:25 +00:00
|
|
|
|
2022-06-15 10:09:22 +00:00
|
|
|
const result = await page.evaluate(() => {
|
|
|
|
return -0;
|
|
|
|
});
|
2018-11-21 03:59:59 +00:00
|
|
|
expect(Object.is(result, -0)).toBe(true);
|
|
|
|
});
|
2020-06-12 13:55:51 +00:00
|
|
|
it('should return Infinity', async () => {
|
2023-06-21 19:41:09 +00:00
|
|
|
const {page} = await getTestState();
|
2020-04-09 05:56:25 +00:00
|
|
|
|
2022-06-15 10:09:22 +00:00
|
|
|
const result = await page.evaluate(() => {
|
|
|
|
return Infinity;
|
|
|
|
});
|
2018-11-21 03:59:59 +00:00
|
|
|
expect(Object.is(result, Infinity)).toBe(true);
|
|
|
|
});
|
2020-06-12 13:55:51 +00:00
|
|
|
it('should return -Infinity', async () => {
|
2023-06-21 19:41:09 +00:00
|
|
|
const {page} = await getTestState();
|
2020-04-09 05:56:25 +00:00
|
|
|
|
2022-06-15 10:09:22 +00:00
|
|
|
const result = await page.evaluate(() => {
|
|
|
|
return -Infinity;
|
|
|
|
});
|
2018-11-21 03:59:59 +00:00
|
|
|
expect(Object.is(result, -Infinity)).toBe(true);
|
|
|
|
});
|
2022-06-15 10:09:22 +00:00
|
|
|
it('should accept "null" as one of multiple parameters', async () => {
|
2023-06-21 19:41:09 +00:00
|
|
|
const {page} = await getTestState();
|
2020-04-09 05:56:25 +00:00
|
|
|
|
2020-05-07 10:54:55 +00:00
|
|
|
const result = await page.evaluate(
|
2022-06-15 10:09:22 +00:00
|
|
|
(a, b) => {
|
|
|
|
return Object.is(a, null) && Object.is(b, 'foo');
|
|
|
|
},
|
|
|
|
null,
|
2020-05-07 10:54:55 +00:00
|
|
|
'foo'
|
|
|
|
);
|
2018-11-21 03:59:59 +00:00
|
|
|
expect(result).toBe(true);
|
|
|
|
});
|
2020-05-07 10:54:55 +00:00
|
|
|
it('should properly serialize null fields', async () => {
|
2023-06-21 19:41:09 +00:00
|
|
|
const {page} = await getTestState();
|
2020-04-09 05:56:25 +00:00
|
|
|
|
2022-06-15 10:09:22 +00:00
|
|
|
expect(
|
|
|
|
await page.evaluate(() => {
|
2022-06-22 13:25:44 +00:00
|
|
|
return {a: undefined};
|
2022-06-15 10:09:22 +00:00
|
|
|
})
|
|
|
|
).toEqual({});
|
2018-11-21 03:59:59 +00:00
|
|
|
});
|
2022-09-08 10:32:39 +00:00
|
|
|
it('should return undefined for non-serializable objects', async () => {
|
2023-06-21 19:41:09 +00:00
|
|
|
const {page} = await getTestState();
|
2020-04-09 05:56:25 +00:00
|
|
|
|
2022-09-08 10:32:39 +00:00
|
|
|
expect(
|
|
|
|
await page.evaluate(() => {
|
|
|
|
return window;
|
|
|
|
})
|
|
|
|
).toBe(undefined);
|
|
|
|
});
|
2023-02-02 14:14:28 +00:00
|
|
|
it('should return promise as empty object', async () => {
|
2023-06-21 19:41:09 +00:00
|
|
|
const {page} = await getTestState();
|
2023-02-02 14:14:28 +00:00
|
|
|
|
|
|
|
const result = await page.evaluate(() => {
|
|
|
|
return {
|
|
|
|
promise: new Promise(resolve => {
|
|
|
|
setTimeout(resolve, 1000);
|
|
|
|
}),
|
|
|
|
};
|
|
|
|
});
|
|
|
|
expect(result).toEqual({
|
|
|
|
promise: {},
|
|
|
|
});
|
|
|
|
});
|
2024-01-22 09:13:53 +00:00
|
|
|
it('should work for circular object', async () => {
|
2023-06-21 19:41:09 +00:00
|
|
|
const {page} = await getTestState();
|
2020-04-09 05:56:25 +00:00
|
|
|
|
2018-11-21 03:59:59 +00:00
|
|
|
const result = await page.evaluate(() => {
|
2024-01-22 09:13:53 +00:00
|
|
|
const a: Record<string, unknown> = {
|
|
|
|
c: 5,
|
|
|
|
d: {
|
|
|
|
foo: 'bar',
|
|
|
|
},
|
|
|
|
};
|
2022-06-22 13:25:44 +00:00
|
|
|
const b = {a};
|
2022-06-15 10:09:22 +00:00
|
|
|
a['b'] = b;
|
2018-11-21 03:59:59 +00:00
|
|
|
return a;
|
|
|
|
});
|
2024-01-22 09:13:53 +00:00
|
|
|
expect(result).toMatchObject({
|
|
|
|
c: 5,
|
|
|
|
d: {
|
|
|
|
foo: 'bar',
|
|
|
|
},
|
|
|
|
b: {
|
|
|
|
a: undefined,
|
|
|
|
},
|
|
|
|
});
|
2018-11-21 03:59:59 +00:00
|
|
|
});
|
2020-05-07 10:54:55 +00:00
|
|
|
it('should accept a string', async () => {
|
2023-06-21 19:41:09 +00:00
|
|
|
const {page} = await getTestState();
|
2020-04-09 05:56:25 +00:00
|
|
|
|
2018-11-21 03:59:59 +00:00
|
|
|
const result = await page.evaluate('1 + 2');
|
|
|
|
expect(result).toBe(3);
|
|
|
|
});
|
2020-05-07 10:54:55 +00:00
|
|
|
it('should accept a string with semi colons', async () => {
|
2023-06-21 19:41:09 +00:00
|
|
|
const {page} = await getTestState();
|
2020-04-09 05:56:25 +00:00
|
|
|
|
2018-11-21 03:59:59 +00:00
|
|
|
const result = await page.evaluate('1 + 5;');
|
|
|
|
expect(result).toBe(6);
|
|
|
|
});
|
2020-05-07 10:54:55 +00:00
|
|
|
it('should accept a string with comments', async () => {
|
2023-06-21 19:41:09 +00:00
|
|
|
const {page} = await getTestState();
|
2020-04-09 05:56:25 +00:00
|
|
|
|
2018-11-21 03:59:59 +00:00
|
|
|
const result = await page.evaluate('2 + 5;\n// do some math!');
|
|
|
|
expect(result).toBe(7);
|
|
|
|
});
|
2020-06-12 13:55:51 +00:00
|
|
|
it('should accept element handle as an argument', async () => {
|
2023-06-21 19:41:09 +00:00
|
|
|
const {page} = await getTestState();
|
2020-04-09 05:56:25 +00:00
|
|
|
|
2018-11-21 03:59:59 +00:00
|
|
|
await page.setContent('<section>42</section>');
|
2023-08-30 10:02:59 +00:00
|
|
|
using element = (await page.$('section'))!;
|
2022-06-22 13:25:44 +00:00
|
|
|
const text = await page.evaluate(e => {
|
2022-06-15 10:09:22 +00:00
|
|
|
return e.textContent;
|
|
|
|
}, element);
|
2018-11-21 03:59:59 +00:00
|
|
|
expect(text).toBe('42');
|
|
|
|
});
|
2020-06-12 13:55:51 +00:00
|
|
|
it('should throw if underlying element was disposed', async () => {
|
2023-06-21 19:41:09 +00:00
|
|
|
const {page} = await getTestState();
|
2020-05-07 10:54:55 +00:00
|
|
|
|
2020-06-12 13:55:51 +00:00
|
|
|
await page.setContent('<section>39</section>');
|
2023-08-30 10:02:59 +00:00
|
|
|
using element = (await page.$('section'))!;
|
2020-06-12 13:55:51 +00:00
|
|
|
expect(element).toBeTruthy();
|
2023-08-30 10:02:59 +00:00
|
|
|
// We want to dispose early.
|
2020-06-12 13:55:51 +00:00
|
|
|
await element.dispose();
|
2022-06-15 10:09:22 +00:00
|
|
|
let error!: Error;
|
2020-06-12 13:55:51 +00:00
|
|
|
await page
|
2024-04-24 12:26:01 +00:00
|
|
|
.evaluate(e => {
|
2022-06-15 10:09:22 +00:00
|
|
|
return e.textContent;
|
|
|
|
}, element)
|
2022-06-22 13:25:44 +00:00
|
|
|
.catch(error_ => {
|
2022-06-15 10:09:22 +00:00
|
|
|
return (error = error_);
|
|
|
|
});
|
2020-06-12 13:55:51 +00:00
|
|
|
expect(error.message).toContain('JSHandle is disposed');
|
|
|
|
});
|
2022-09-08 10:32:39 +00:00
|
|
|
it('should throw if elementHandles are from other frames', async () => {
|
2023-06-21 19:41:09 +00:00
|
|
|
const {page, server} = await getTestState();
|
2022-09-08 10:32:39 +00:00
|
|
|
|
2023-04-25 13:02:25 +00:00
|
|
|
await attachFrame(page, 'frame1', server.EMPTY_PAGE);
|
2023-08-30 10:02:59 +00:00
|
|
|
using bodyHandle = await page.frames()[1]!.$('body');
|
2022-09-08 10:32:39 +00:00
|
|
|
let error!: Error;
|
|
|
|
await page
|
|
|
|
.evaluate(body => {
|
|
|
|
return body?.innerHTML;
|
|
|
|
}, bodyHandle)
|
|
|
|
.catch(error_ => {
|
|
|
|
return (error = error_);
|
|
|
|
});
|
|
|
|
expect(error).toBeTruthy();
|
2024-02-12 16:28:21 +00:00
|
|
|
expect(error.message).atLeastOneToContain([
|
|
|
|
'JSHandles can be evaluated only in the context they were created',
|
|
|
|
"Trying to evaluate JSHandle from different frames. Usually this means you're using a handle from a page on a different page.",
|
|
|
|
]);
|
2022-09-08 10:32:39 +00:00
|
|
|
});
|
|
|
|
it('should simulate a user gesture', async () => {
|
2023-06-21 19:41:09 +00:00
|
|
|
const {page} = await getTestState();
|
2020-04-09 05:56:25 +00:00
|
|
|
|
2019-07-30 20:19:56 +00:00
|
|
|
const result = await page.evaluate(() => {
|
|
|
|
document.body.appendChild(document.createTextNode('test'));
|
|
|
|
document.execCommand('selectAll');
|
|
|
|
return document.execCommand('copy');
|
|
|
|
});
|
2019-02-06 21:49:14 +00:00
|
|
|
expect(result).toBe(true);
|
2018-11-21 03:59:59 +00:00
|
|
|
});
|
2022-09-08 10:32:39 +00:00
|
|
|
it('should not throw an error when evaluation does a navigation', async () => {
|
2023-06-21 19:41:09 +00:00
|
|
|
const {page, server} = await getTestState();
|
2020-05-07 10:54:55 +00:00
|
|
|
|
2022-09-08 10:32:39 +00:00
|
|
|
await page.goto(server.PREFIX + '/one-style.html');
|
2023-09-18 16:04:02 +00:00
|
|
|
const onRequest = server.waitForRequest('/empty.html');
|
2022-09-08 10:32:39 +00:00
|
|
|
const result = await page.evaluate(() => {
|
|
|
|
(window as any).location = '/empty.html';
|
|
|
|
return [42];
|
|
|
|
});
|
|
|
|
expect(result).toEqual([42]);
|
2023-09-18 16:04:02 +00:00
|
|
|
await onRequest;
|
2022-09-08 10:32:39 +00:00
|
|
|
});
|
2020-06-12 13:55:51 +00:00
|
|
|
it('should transfer 100Mb of data from page to node.js', async function () {
|
2023-03-24 08:35:45 +00:00
|
|
|
this.timeout(25_000);
|
2023-06-21 19:41:09 +00:00
|
|
|
const {page} = await getTestState();
|
2020-05-07 10:54:55 +00:00
|
|
|
|
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 a = await page.evaluate(() => {
|
2022-06-15 10:09:22 +00:00
|
|
|
return Array(100 * 1024 * 1024 + 1).join('a');
|
|
|
|
});
|
2020-06-12 13:55:51 +00:00
|
|
|
expect(a.length).toBe(100 * 1024 * 1024);
|
|
|
|
});
|
2023-05-22 12:52:31 +00:00
|
|
|
it('should throw error with detailed information on exception inside promise', async () => {
|
2023-06-21 19:41:09 +00:00
|
|
|
const {page} = await getTestState();
|
2020-04-09 05:56:25 +00:00
|
|
|
|
2022-06-15 10:09:22 +00:00
|
|
|
let error!: Error;
|
2020-05-07 10:54:55 +00:00
|
|
|
await page
|
2022-06-15 10:09:22 +00:00
|
|
|
.evaluate(() => {
|
|
|
|
return new Promise(() => {
|
|
|
|
throw new Error('Error in promise');
|
|
|
|
});
|
|
|
|
})
|
2022-06-22 13:25:44 +00:00
|
|
|
.catch(error_ => {
|
2022-06-15 10:09:22 +00:00
|
|
|
return (error = error_);
|
|
|
|
});
|
2019-08-15 19:03:48 +00:00
|
|
|
expect(error.message).toContain('Error in promise');
|
|
|
|
});
|
2023-10-30 12:02:04 +00:00
|
|
|
|
|
|
|
it('should return properly serialize objects with unknown type fields', async () => {
|
|
|
|
const {page} = await getTestState();
|
|
|
|
await page.setContent(
|
|
|
|
"<img src='data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAADUlEQVR42mNkYPhfDwAChwGA60e6kgAAAABJRU5ErkJggg=='>"
|
|
|
|
);
|
|
|
|
|
|
|
|
const result = await page.evaluate(async () => {
|
|
|
|
const image = document.querySelector('img')!;
|
|
|
|
const imageBitmap = await createImageBitmap(image);
|
|
|
|
|
|
|
|
return {
|
|
|
|
a: 'foo',
|
|
|
|
b: imageBitmap,
|
|
|
|
};
|
|
|
|
});
|
|
|
|
|
|
|
|
expect(result).toEqual({
|
|
|
|
a: 'foo',
|
|
|
|
b: undefined,
|
|
|
|
});
|
|
|
|
});
|
2018-11-21 03:59:59 +00:00
|
|
|
});
|
|
|
|
|
2022-09-08 10:32:39 +00:00
|
|
|
describe('Page.evaluateOnNewDocument', function () {
|
2020-05-07 10:54:55 +00:00
|
|
|
it('should evaluate before anything else on the page', async () => {
|
2023-06-21 19:41:09 +00:00
|
|
|
const {page, server} = await getTestState();
|
2020-04-09 05:56:25 +00:00
|
|
|
|
2020-05-07 10:54:55 +00:00
|
|
|
await page.evaluateOnNewDocument(function () {
|
2022-06-15 10:09:22 +00:00
|
|
|
(globalThis as any).injected = 123;
|
2018-11-21 03:59:59 +00:00
|
|
|
});
|
|
|
|
await page.goto(server.PREFIX + '/tamperable.html');
|
2022-06-15 10:09:22 +00:00
|
|
|
expect(
|
|
|
|
await page.evaluate(() => {
|
|
|
|
return (globalThis as any).result;
|
|
|
|
})
|
|
|
|
).toBe(123);
|
2018-11-21 03:59:59 +00:00
|
|
|
});
|
2020-05-07 10:54:55 +00:00
|
|
|
it('should work with CSP', async () => {
|
2023-06-21 19:41:09 +00:00
|
|
|
const {page, server} = await getTestState();
|
2020-04-09 05:56:25 +00:00
|
|
|
|
2018-11-21 03:59:59 +00:00
|
|
|
server.setCSP('/empty.html', 'script-src ' + server.PREFIX);
|
2020-05-07 10:54:55 +00:00
|
|
|
await page.evaluateOnNewDocument(function () {
|
2022-06-15 10:09:22 +00:00
|
|
|
(globalThis as any).injected = 123;
|
2018-11-21 03:59:59 +00:00
|
|
|
});
|
|
|
|
await page.goto(server.PREFIX + '/empty.html');
|
2022-06-15 10:09:22 +00:00
|
|
|
expect(
|
|
|
|
await page.evaluate(() => {
|
|
|
|
return (globalThis as any).injected;
|
|
|
|
})
|
|
|
|
).toBe(123);
|
2018-11-21 03:59:59 +00:00
|
|
|
|
|
|
|
// Make sure CSP works.
|
2022-06-22 13:25:44 +00:00
|
|
|
await page.addScriptTag({content: 'window.e = 10;'}).catch(error => {
|
2022-06-15 10:09:22 +00:00
|
|
|
return void error;
|
|
|
|
});
|
|
|
|
expect(
|
|
|
|
await page.evaluate(() => {
|
|
|
|
return (window as any).e;
|
|
|
|
})
|
|
|
|
).toBe(undefined);
|
2018-11-21 03:59:59 +00:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2023-05-26 07:56:45 +00:00
|
|
|
describe('Page.removeScriptToEvaluateOnNewDocument', function () {
|
|
|
|
it('should remove new document script', async () => {
|
2023-06-21 19:41:09 +00:00
|
|
|
const {page, server} = await getTestState();
|
2023-05-26 07:56:45 +00:00
|
|
|
|
|
|
|
const {identifier} = await page.evaluateOnNewDocument(function () {
|
|
|
|
(globalThis as any).injected = 123;
|
|
|
|
});
|
|
|
|
await page.goto(server.PREFIX + '/tamperable.html');
|
|
|
|
expect(
|
|
|
|
await page.evaluate(() => {
|
|
|
|
return (globalThis as any).result;
|
|
|
|
})
|
|
|
|
).toBe(123);
|
|
|
|
|
|
|
|
await page.removeScriptToEvaluateOnNewDocument(identifier);
|
|
|
|
await page.reload();
|
|
|
|
expect(
|
|
|
|
await page.evaluate(() => {
|
|
|
|
return (globalThis as any).result || null;
|
|
|
|
})
|
|
|
|
).toBe(null);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2020-05-07 10:54:55 +00:00
|
|
|
describe('Frame.evaluate', function () {
|
2022-05-02 06:21:19 +00:00
|
|
|
it('should have different execution contexts', async () => {
|
2023-06-21 19:41:09 +00:00
|
|
|
const {page, server} = await getTestState();
|
2020-04-09 05:56:25 +00:00
|
|
|
|
2018-11-21 03:59:59 +00:00
|
|
|
await page.goto(server.EMPTY_PAGE);
|
2023-04-25 13:02:25 +00:00
|
|
|
await attachFrame(page, 'frame1', server.EMPTY_PAGE);
|
|
|
|
expect(page.frames()).toHaveLength(2);
|
2022-06-15 10:09:22 +00:00
|
|
|
await page.frames()[0]!.evaluate(() => {
|
|
|
|
return ((globalThis as any).FOO = 'foo');
|
|
|
|
});
|
|
|
|
await page.frames()[1]!.evaluate(() => {
|
|
|
|
return ((globalThis as any).FOO = 'bar');
|
|
|
|
});
|
|
|
|
expect(
|
|
|
|
await page.frames()[0]!.evaluate(() => {
|
|
|
|
return (globalThis as any).FOO;
|
|
|
|
})
|
|
|
|
).toBe('foo');
|
|
|
|
expect(
|
|
|
|
await page.frames()[1]!.evaluate(() => {
|
|
|
|
return (globalThis as any).FOO;
|
|
|
|
})
|
|
|
|
).toBe('bar');
|
2018-11-21 03:59:59 +00:00
|
|
|
});
|
2022-05-02 06:21:19 +00:00
|
|
|
it('should have correct execution contexts', async () => {
|
2023-06-21 19:41:09 +00:00
|
|
|
const {page, server} = await getTestState();
|
2020-04-09 05:56:25 +00:00
|
|
|
|
2019-02-02 02:40:40 +00:00
|
|
|
await page.goto(server.PREFIX + '/frames/one-frame.html');
|
2023-04-25 13:02:25 +00:00
|
|
|
expect(page.frames()).toHaveLength(2);
|
2020-05-07 10:54:55 +00:00
|
|
|
expect(
|
2022-06-15 10:09:22 +00:00
|
|
|
await page.frames()[0]!.evaluate(() => {
|
|
|
|
return document.body.textContent!.trim();
|
|
|
|
})
|
2020-05-07 10:54:55 +00:00
|
|
|
).toBe('');
|
|
|
|
expect(
|
2022-06-15 10:09:22 +00:00
|
|
|
await page.frames()[1]!.evaluate(() => {
|
|
|
|
return document.body.textContent!.trim();
|
|
|
|
})
|
2020-05-07 10:54:55 +00:00
|
|
|
).toBe(`Hi, I'm frame`);
|
2019-02-02 02:40:40 +00:00
|
|
|
});
|
2020-05-07 10:54:55 +00:00
|
|
|
it('should execute after cross-site navigation', async () => {
|
2023-06-21 19:41:09 +00:00
|
|
|
const {page, server} = await getTestState();
|
2020-04-09 05:56:25 +00:00
|
|
|
|
2018-11-21 03:59:59 +00:00
|
|
|
await page.goto(server.EMPTY_PAGE);
|
|
|
|
const mainFrame = page.mainFrame();
|
2022-06-15 10:09:22 +00:00
|
|
|
expect(
|
|
|
|
await mainFrame.evaluate(() => {
|
|
|
|
return window.location.href;
|
|
|
|
})
|
|
|
|
).toContain('localhost');
|
2018-11-21 03:59:59 +00:00
|
|
|
await page.goto(server.CROSS_PROCESS_PREFIX + '/empty.html');
|
2022-06-15 10:09:22 +00:00
|
|
|
expect(
|
|
|
|
await mainFrame.evaluate(() => {
|
|
|
|
return window.location.href;
|
|
|
|
})
|
|
|
|
).toContain('127');
|
2018-11-21 03:59:59 +00:00
|
|
|
});
|
|
|
|
});
|
2020-04-09 05:56:25 +00:00
|
|
|
});
|