2018-11-21 03:59:59 +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';
|
2023-02-15 23:09:31 +00:00
|
|
|
|
2020-06-23 05:18:46 +00:00
|
|
|
import {
|
2020-05-07 10:54:55 +00:00
|
|
|
getTestState,
|
|
|
|
setupTestBrowserHooks,
|
|
|
|
setupTestPageAndContextHooks,
|
2022-06-15 10:09:22 +00:00
|
|
|
} 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 () {
|
2020-04-09 05:56:25 +00:00
|
|
|
setupTestBrowserHooks();
|
|
|
|
setupTestPageAndContextHooks();
|
2018-11-21 03:59:59 +00:00
|
|
|
|
2020-05-07 10:54:55 +00:00
|
|
|
describe('Page.evaluate', function () {
|
|
|
|
it('should work', async () => {
|
2022-06-22 13:25:44 +00:00
|
|
|
const {page} = 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 () => {
|
2022-06-22 13:25:44 +00:00
|
|
|
const {page} = 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 () => {
|
2022-06-22 13:25:44 +00:00
|
|
|
const {page} = 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 () => {
|
2022-06-22 13:25:44 +00:00
|
|
|
const {page} = 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 () => {
|
2022-06-22 13:25:44 +00:00
|
|
|
const {page} = 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 () => {
|
2022-06-22 13:25:44 +00:00
|
|
|
const {page} = 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 () => {
|
2022-06-22 13:25:44 +00:00
|
|
|
const {page} = 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 () => {
|
2022-06-22 13:25:44 +00:00
|
|
|
const {page} = 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 () => {
|
2022-06-22 13:25:44 +00:00
|
|
|
const {page} = 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 () => {
|
2022-06-22 13:25:44 +00:00
|
|
|
const {page, server} = 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);
|
|
|
|
});
|
2022-09-08 10:32:39 +00:00
|
|
|
it('should return undefined for objects with symbols', async () => {
|
|
|
|
const {page} = getTestState();
|
|
|
|
|
|
|
|
expect(
|
|
|
|
await page.evaluate(() => {
|
|
|
|
return [Symbol('foo4')];
|
|
|
|
})
|
|
|
|
).toBe(undefined);
|
|
|
|
});
|
2020-05-07 10:54:55 +00:00
|
|
|
it('should work with function shorthands', async () => {
|
2022-06-22 13:25:44 +00:00
|
|
|
const {page} = 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 () => {
|
2022-06-22 13:25:44 +00:00
|
|
|
const {page} = 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 () => {
|
2022-06-22 13:25:44 +00:00
|
|
|
const {page} = 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 () => {
|
2022-06-22 13:25:44 +00:00
|
|
|
const {page} = 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 () => {
|
2022-06-22 13:25:44 +00:00
|
|
|
const {page, server} = 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 () => {
|
2022-06-22 13:25:44 +00:00
|
|
|
const {page} = 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 () {
|
2022-06-15 10:09:22 +00:00
|
|
|
return await (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 () => {
|
2022-06-22 13:25:44 +00:00
|
|
|
const {page} = 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 () => {
|
2022-06-22 13:25:44 +00:00
|
|
|
const {page} = 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 () => {
|
2022-06-22 13:25:44 +00:00
|
|
|
const {page} = 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 () => {
|
2022-06-22 13:25:44 +00:00
|
|
|
const {page} = 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 () => {
|
2022-06-22 13:25:44 +00:00
|
|
|
const {page} = 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 () => {
|
2022-06-22 13:25:44 +00:00
|
|
|
const {page} = 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 () => {
|
2022-06-22 13:25:44 +00:00
|
|
|
const {page} = 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 () => {
|
2022-06-22 13:25:44 +00:00
|
|
|
const {page} = 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 () => {
|
2022-06-22 13:25:44 +00:00
|
|
|
const {page} = 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 () => {
|
2022-06-22 13:25:44 +00:00
|
|
|
const {page} = 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 () => {
|
2022-06-22 13:25:44 +00:00
|
|
|
const {page} = 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 () => {
|
|
|
|
const {page} = 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 () => {
|
|
|
|
const {page} = getTestState();
|
|
|
|
|
|
|
|
const result = await page.evaluate(() => {
|
|
|
|
return {
|
|
|
|
promise: new Promise(resolve => {
|
|
|
|
setTimeout(resolve, 1000);
|
|
|
|
}),
|
|
|
|
};
|
|
|
|
});
|
|
|
|
expect(result).toEqual({
|
|
|
|
promise: {},
|
|
|
|
});
|
|
|
|
});
|
2022-09-08 10:32:39 +00:00
|
|
|
it('should fail for circular object', async () => {
|
2022-06-22 13:25:44 +00:00
|
|
|
const {page} = getTestState();
|
2020-04-09 05:56:25 +00:00
|
|
|
|
2018-11-21 03:59:59 +00:00
|
|
|
const result = await page.evaluate(() => {
|
2022-06-22 13:25:44 +00:00
|
|
|
const a: {[x: string]: any} = {};
|
|
|
|
const b = {a};
|
2022-06-15 10:09:22 +00:00
|
|
|
a['b'] = b;
|
2018-11-21 03:59:59 +00:00
|
|
|
return a;
|
|
|
|
});
|
|
|
|
expect(result).toBe(undefined);
|
|
|
|
});
|
2022-09-08 10:32:39 +00:00
|
|
|
it('should be able to throw a tricky error', async () => {
|
2022-06-22 13:25:44 +00:00
|
|
|
const {page} = getTestState();
|
2020-04-09 05:56:25 +00:00
|
|
|
|
2022-06-15 10:09:22 +00:00
|
|
|
const windowHandle = await page.evaluateHandle(() => {
|
|
|
|
return window;
|
|
|
|
});
|
2022-08-11 09:45:35 +00:00
|
|
|
const errorText = await windowHandle.jsonValue().catch(error_ => {
|
2022-06-22 13:25:44 +00:00
|
|
|
return error_.message;
|
|
|
|
});
|
2020-05-07 10:54:55 +00:00
|
|
|
const error = await page
|
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
|
|
|
.evaluate(errorText => {
|
2020-05-07 10:54:55 +00:00
|
|
|
throw new Error(errorText);
|
|
|
|
}, errorText)
|
2022-06-22 13:25:44 +00:00
|
|
|
.catch(error_ => {
|
2022-06-15 10:09:22 +00:00
|
|
|
return error_;
|
|
|
|
});
|
2019-06-11 00:35:40 +00:00
|
|
|
expect(error.message).toContain(errorText);
|
|
|
|
});
|
2020-05-07 10:54:55 +00:00
|
|
|
it('should accept a string', async () => {
|
2022-06-22 13:25:44 +00:00
|
|
|
const {page} = 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 () => {
|
2022-06-22 13:25:44 +00:00
|
|
|
const {page} = 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 () => {
|
2022-06-22 13:25:44 +00:00
|
|
|
const {page} = 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 () => {
|
2022-06-22 13:25:44 +00:00
|
|
|
const {page} = getTestState();
|
2020-04-09 05:56:25 +00:00
|
|
|
|
2018-11-21 03:59:59 +00:00
|
|
|
await page.setContent('<section>42</section>');
|
2022-06-15 10:09:22 +00:00
|
|
|
const 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 () => {
|
2022-06-22 13:25:44 +00:00
|
|
|
const {page} = getTestState();
|
2020-05-07 10:54:55 +00:00
|
|
|
|
2020-06-12 13:55:51 +00:00
|
|
|
await page.setContent('<section>39</section>');
|
2022-06-15 10:09:22 +00:00
|
|
|
const element = (await page.$('section'))!;
|
2020-06-12 13:55:51 +00:00
|
|
|
expect(element).toBeTruthy();
|
|
|
|
await element.dispose();
|
2022-06-15 10:09:22 +00:00
|
|
|
let error!: Error;
|
2020-06-12 13:55:51 +00:00
|
|
|
await page
|
2022-06-15 10:09:22 +00:00
|
|
|
.evaluate((e: HTMLElement) => {
|
|
|
|
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 () => {
|
|
|
|
const {page, server} = getTestState();
|
|
|
|
|
2023-04-25 13:02:25 +00:00
|
|
|
await attachFrame(page, 'frame1', server.EMPTY_PAGE);
|
2022-09-08 10:32:39 +00:00
|
|
|
const bodyHandle = await page.frames()[1]!.$('body');
|
|
|
|
let error!: Error;
|
|
|
|
await page
|
|
|
|
.evaluate(body => {
|
|
|
|
return body?.innerHTML;
|
|
|
|
}, bodyHandle)
|
|
|
|
.catch(error_ => {
|
|
|
|
return (error = error_);
|
|
|
|
});
|
|
|
|
expect(error).toBeTruthy();
|
|
|
|
expect(error.message).toContain(
|
|
|
|
'JSHandles can be evaluated only in the context they were created'
|
|
|
|
);
|
|
|
|
});
|
|
|
|
it('should simulate a user gesture', async () => {
|
2022-06-22 13:25:44 +00:00
|
|
|
const {page} = 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 throw a nice error after a navigation', async () => {
|
2022-06-22 13:25:44 +00:00
|
|
|
const {page} = getTestState();
|
2020-04-09 05:56:25 +00:00
|
|
|
|
2018-11-21 03:59:59 +00:00
|
|
|
const executionContext = await page.mainFrame().executionContext();
|
|
|
|
|
|
|
|
await Promise.all([
|
|
|
|
page.waitForNavigation(),
|
2022-06-15 10:09:22 +00:00
|
|
|
executionContext.evaluate(() => {
|
|
|
|
return window.location.reload();
|
|
|
|
}),
|
2018-11-21 03:59:59 +00:00
|
|
|
]);
|
2020-05-07 10:54:55 +00:00
|
|
|
const error = await executionContext
|
2022-06-15 10:09:22 +00:00
|
|
|
.evaluate(() => {
|
|
|
|
return null;
|
|
|
|
})
|
2022-06-22 13:25:44 +00:00
|
|
|
.catch(error_ => {
|
2022-06-15 10:09:22 +00:00
|
|
|
return error_;
|
|
|
|
});
|
2020-06-23 05:18:46 +00:00
|
|
|
expect((error as Error).message).toContain('navigation');
|
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 () => {
|
|
|
|
const {page, server} = 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');
|
|
|
|
const result = await page.evaluate(() => {
|
|
|
|
(window as any).location = '/empty.html';
|
|
|
|
return [42];
|
|
|
|
});
|
|
|
|
expect(result).toEqual([42]);
|
|
|
|
});
|
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);
|
2022-06-22 13:25:44 +00:00
|
|
|
const {page} = 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 () => {
|
2022-06-22 13:25:44 +00:00
|
|
|
const {page} = 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');
|
|
|
|
});
|
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 () => {
|
2022-06-22 13:25:44 +00:00
|
|
|
const {page, server} = 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 () => {
|
2022-06-22 13:25:44 +00:00
|
|
|
const {page, server} = 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
|
|
|
});
|
|
|
|
});
|
|
|
|
|
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 () => {
|
2022-06-22 13:25:44 +00:00
|
|
|
const {page, server} = 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 () => {
|
2022-06-22 13:25:44 +00:00
|
|
|
const {page, server} = 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 () => {
|
2022-06-22 13:25:44 +00:00
|
|
|
const {page, server} = 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
|
|
|
});
|