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';
|
2020-07-28 08:37:49 +00:00
|
|
|
import sinon from 'sinon';
|
2022-07-06 07:05:37 +00:00
|
|
|
import {ElementHandle} from '../../lib/cjs/puppeteer/common/ElementHandle.js';
|
2020-06-23 05:18:46 +00:00
|
|
|
import {
|
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
|
|
|
describeFailsFirefox,
|
2020-05-07 10:54:55 +00:00
|
|
|
getTestState,
|
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
|
|
|
itFailsFirefox,
|
2020-05-07 10:54:55 +00:00
|
|
|
setupTestBrowserHooks,
|
|
|
|
setupTestPageAndContextHooks,
|
2022-06-15 10:09:22 +00:00
|
|
|
} from './mocha-utils.js';
|
2020-04-09 05:56:25 +00:00
|
|
|
|
2020-07-13 09:22:26 +00:00
|
|
|
import utils from './utils.js';
|
2018-03-20 03:00:12 +00:00
|
|
|
|
2020-05-07 10:54:55 +00:00
|
|
|
describe('ElementHandle specs', function () {
|
2020-04-09 05:56:25 +00:00
|
|
|
setupTestBrowserHooks();
|
|
|
|
setupTestPageAndContextHooks();
|
|
|
|
|
2020-05-07 10:54:55 +00:00
|
|
|
describeFailsFirefox('ElementHandle.boundingBox', function () {
|
|
|
|
it('should work', async () => {
|
2022-06-22 13:25:44 +00:00
|
|
|
const {page, server} = getTestState();
|
2018-03-20 03:00:12 +00:00
|
|
|
|
2022-06-22 13:25:44 +00:00
|
|
|
await page.setViewport({width: 500, height: 500});
|
2018-03-20 03:00:12 +00:00
|
|
|
await page.goto(server.PREFIX + '/grid.html');
|
2022-06-15 10:09:22 +00:00
|
|
|
const elementHandle = (await page.$('.box:nth-of-type(13)'))!;
|
2018-03-20 03:00:12 +00:00
|
|
|
const box = await elementHandle.boundingBox();
|
2022-06-22 13:25:44 +00:00
|
|
|
expect(box).toEqual({x: 100, y: 50, width: 50, height: 50});
|
2018-03-20 03:00:12 +00:00
|
|
|
});
|
2020-05-07 10:54:55 +00:00
|
|
|
it('should handle nested frames', async () => {
|
2022-06-22 13:25:44 +00:00
|
|
|
const {page, server, isChrome} = getTestState();
|
2020-04-09 05:56:25 +00:00
|
|
|
|
2022-06-22 13:25:44 +00:00
|
|
|
await page.setViewport({width: 500, height: 500});
|
2018-03-20 03:00:12 +00:00
|
|
|
await page.goto(server.PREFIX + '/frames/nested-frames.html');
|
2022-06-15 10:09:22 +00:00
|
|
|
const nestedFrame = page.frames()[1]!.childFrames()[1]!;
|
|
|
|
const elementHandle = (await nestedFrame.$('div'))!;
|
2018-03-20 03:00:12 +00:00
|
|
|
const box = await elementHandle.boundingBox();
|
2022-06-14 11:55:35 +00:00
|
|
|
if (isChrome) {
|
2022-06-22 13:25:44 +00:00
|
|
|
expect(box).toEqual({x: 28, y: 182, width: 264, height: 18});
|
2022-06-14 11:55:35 +00:00
|
|
|
} else {
|
2022-06-22 13:25:44 +00:00
|
|
|
expect(box).toEqual({x: 28, y: 182, width: 254, height: 18});
|
2022-06-14 11:55:35 +00:00
|
|
|
}
|
2018-03-20 03:00:12 +00:00
|
|
|
});
|
2020-05-07 10:54:55 +00:00
|
|
|
it('should return null for invisible elements', async () => {
|
2022-06-22 13:25:44 +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 style="display:none">hi</div>');
|
2022-06-15 10:09:22 +00:00
|
|
|
const element = (await page.$('div'))!;
|
2018-03-20 03:00:12 +00:00
|
|
|
expect(await element.boundingBox()).toBe(null);
|
|
|
|
});
|
2020-05-07 10:54:55 +00:00
|
|
|
it('should force a layout', 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
|
|
|
await page.setViewport({width: 500, height: 500});
|
2020-05-07 10:54:55 +00:00
|
|
|
await page.setContent(
|
|
|
|
'<div style="width: 100px; height: 100px">hello</div>'
|
|
|
|
);
|
2022-06-15 10:09:22 +00:00
|
|
|
const elementHandle = (await page.$('div'))!;
|
|
|
|
await page.evaluate((element: HTMLElement) => {
|
|
|
|
return (element.style.height = '200px');
|
|
|
|
}, elementHandle);
|
2018-03-20 03:00:12 +00:00
|
|
|
const box = await elementHandle.boundingBox();
|
2022-06-22 13:25:44 +00:00
|
|
|
expect(box).toEqual({x: 8, y: 8, width: 100, height: 200});
|
2018-03-20 03:00:12 +00:00
|
|
|
});
|
2020-05-07 10:54:55 +00:00
|
|
|
it('should work with SVG nodes', async () => {
|
2022-06-22 13:25:44 +00:00
|
|
|
const {page} = getTestState();
|
2020-04-09 05:56:25 +00:00
|
|
|
|
2018-06-27 01:03:00 +00:00
|
|
|
await page.setContent(`
|
|
|
|
<svg xmlns="http://www.w3.org/2000/svg" width="500" height="500">
|
|
|
|
<rect id="theRect" x="30" y="50" width="200" height="300"></rect>
|
|
|
|
</svg>
|
|
|
|
`);
|
2022-07-06 07:05:37 +00:00
|
|
|
const element = (await page.$(
|
|
|
|
'#therect'
|
|
|
|
)) as ElementHandle<SVGRectElement>;
|
2018-06-27 01:03:00 +00:00
|
|
|
const pptrBoundingBox = await element.boundingBox();
|
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 webBoundingBox = await page.evaluate(e => {
|
2018-06-27 01:03:00 +00:00
|
|
|
const rect = e.getBoundingClientRect();
|
2022-06-22 13:25:44 +00:00
|
|
|
return {x: rect.x, y: rect.y, width: rect.width, height: rect.height};
|
2018-06-27 01:03:00 +00:00
|
|
|
}, element);
|
|
|
|
expect(pptrBoundingBox).toEqual(webBoundingBox);
|
|
|
|
});
|
2018-03-20 03:00:12 +00:00
|
|
|
});
|
|
|
|
|
2020-05-07 10:54:55 +00:00
|
|
|
describeFailsFirefox('ElementHandle.boxModel', function () {
|
|
|
|
it('should work', async () => {
|
2022-06-22 13:25:44 +00:00
|
|
|
const {page, server} = getTestState();
|
2020-04-09 05:56:25 +00:00
|
|
|
|
2018-03-30 00:25:52 +00:00
|
|
|
await page.goto(server.PREFIX + '/resetcss.html');
|
2018-03-29 20:52:28 +00:00
|
|
|
|
2018-03-30 00:25:52 +00:00
|
|
|
// Step 1: Add Frame and position it absolutely.
|
|
|
|
await utils.attachFrame(page, 'frame1', server.PREFIX + '/resetcss.html');
|
|
|
|
await page.evaluate(() => {
|
2022-06-15 10:09:22 +00:00
|
|
|
const frame = document.querySelector<HTMLElement>('#frame1')!;
|
2020-06-23 05:18:46 +00:00
|
|
|
frame.style.position = 'absolute';
|
|
|
|
frame.style.left = '1px';
|
|
|
|
frame.style.top = '2px';
|
2018-03-30 00:25:52 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
// Step 2: Add div and position it absolutely inside frame.
|
2022-06-15 10:09:22 +00:00
|
|
|
const frame = page.frames()[1]!;
|
2020-05-07 10:54:55 +00:00
|
|
|
const divHandle = (
|
|
|
|
await frame.evaluateHandle(() => {
|
|
|
|
const div = document.createElement('div');
|
|
|
|
document.body.appendChild(div);
|
2020-06-23 05:18:46 +00:00
|
|
|
div.style.boxSizing = 'border-box';
|
|
|
|
div.style.position = 'absolute';
|
|
|
|
div.style.borderLeft = '1px solid black';
|
|
|
|
div.style.paddingLeft = '2px';
|
|
|
|
div.style.marginLeft = '3px';
|
|
|
|
div.style.left = '4px';
|
|
|
|
div.style.top = '5px';
|
|
|
|
div.style.width = '6px';
|
|
|
|
div.style.height = '7px';
|
2020-05-07 10:54:55 +00:00
|
|
|
return div;
|
|
|
|
})
|
2022-06-15 10:09:22 +00:00
|
|
|
).asElement()!;
|
2018-03-30 00:25:52 +00:00
|
|
|
|
|
|
|
// Step 3: query div's boxModel and assert box values.
|
2022-06-15 10:09:22 +00:00
|
|
|
const box = (await divHandle.boxModel())!;
|
2018-03-30 00:25:52 +00:00
|
|
|
expect(box.width).toBe(6);
|
|
|
|
expect(box.height).toBe(7);
|
2022-06-15 10:09:22 +00:00
|
|
|
expect(box.margin[0]!).toEqual({
|
2018-03-30 00:25:52 +00:00
|
|
|
x: 1 + 4, // frame.left + div.left
|
|
|
|
y: 2 + 5,
|
|
|
|
});
|
2022-06-15 10:09:22 +00:00
|
|
|
expect(box.border[0]!).toEqual({
|
2018-03-30 00:25:52 +00:00
|
|
|
x: 1 + 4 + 3, // frame.left + div.left + div.margin-left
|
|
|
|
y: 2 + 5,
|
|
|
|
});
|
2022-06-15 10:09:22 +00:00
|
|
|
expect(box.padding[0]!).toEqual({
|
2018-03-30 00:25:52 +00:00
|
|
|
x: 1 + 4 + 3 + 1, // frame.left + div.left + div.marginLeft + div.borderLeft
|
|
|
|
y: 2 + 5,
|
|
|
|
});
|
2022-06-15 10:09:22 +00:00
|
|
|
expect(box.content[0]!).toEqual({
|
2018-03-30 00:25:52 +00:00
|
|
|
x: 1 + 4 + 3 + 1 + 2, // frame.left + div.left + div.marginLeft + div.borderLeft + dif.paddingLeft
|
|
|
|
y: 2 + 5,
|
|
|
|
});
|
2018-03-29 20:52:28 +00:00
|
|
|
});
|
|
|
|
|
2020-05-07 10:54:55 +00:00
|
|
|
it('should return null for invisible elements', async () => {
|
2022-06-22 13:25:44 +00:00
|
|
|
const {page} = getTestState();
|
2020-04-09 05:56:25 +00:00
|
|
|
|
2018-03-29 20:52:28 +00:00
|
|
|
await page.setContent('<div style="display:none">hi</div>');
|
2022-06-15 10:09:22 +00:00
|
|
|
const element = (await page.$('div'))!;
|
2018-03-29 20:52:28 +00:00
|
|
|
expect(await element.boxModel()).toBe(null);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2020-05-07 10:54:55 +00:00
|
|
|
describe('ElementHandle.contentFrame', function () {
|
|
|
|
itFailsFirefox('should work', async () => {
|
2022-06-22 13:25:44 +00:00
|
|
|
const {page, server} = getTestState();
|
2020-04-09 05:56:25 +00:00
|
|
|
|
2018-03-20 03:00:12 +00:00
|
|
|
await page.goto(server.EMPTY_PAGE);
|
2019-02-09 03:24:40 +00:00
|
|
|
await utils.attachFrame(page, 'frame1', server.EMPTY_PAGE);
|
2022-06-15 10:09:22 +00:00
|
|
|
const elementHandle = (await page.$('#frame1'))!;
|
2018-03-20 03:00:12 +00:00
|
|
|
const frame = await elementHandle.contentFrame();
|
2022-06-15 10:09:22 +00:00
|
|
|
expect(frame).toBe(page.frames()[1]!);
|
2018-03-20 03:00:12 +00:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2020-05-07 10:54:55 +00:00
|
|
|
describe('ElementHandle.click', function () {
|
2021-05-05 12:21:14 +00:00
|
|
|
// See https://github.com/puppeteer/puppeteer/issues/7175
|
|
|
|
itFailsFirefox('should work', async () => {
|
2022-06-22 13:25:44 +00:00
|
|
|
const {page, server} = getTestState();
|
2020-04-09 05:56:25 +00:00
|
|
|
|
2018-03-20 03:00:12 +00:00
|
|
|
await page.goto(server.PREFIX + '/input/button.html');
|
2022-06-15 10:09:22 +00:00
|
|
|
const button = (await page.$('button'))!;
|
2018-03-20 03:00:12 +00:00
|
|
|
await button.click();
|
2022-06-15 10:09:22 +00:00
|
|
|
expect(
|
|
|
|
await page.evaluate(() => {
|
|
|
|
return (globalThis as any).result;
|
|
|
|
})
|
|
|
|
).toBe('Clicked');
|
2018-03-20 03:00:12 +00:00
|
|
|
});
|
2020-05-07 10:54:55 +00:00
|
|
|
it('should work for Shadow DOM v1', async () => {
|
2022-06-22 13:25:44 +00:00
|
|
|
const {page, server} = getTestState();
|
2020-04-09 05:56:25 +00:00
|
|
|
|
2018-03-20 03:00:12 +00:00
|
|
|
await page.goto(server.PREFIX + '/shadow.html');
|
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 buttonHandle = await page.evaluateHandle(() => {
|
2020-06-23 05:18:46 +00:00
|
|
|
// @ts-expect-error button is expected to be in the page's scope.
|
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
|
|
|
return button as HTMLButtonElement;
|
2022-06-15 10:09:22 +00:00
|
|
|
});
|
2020-07-01 11:44:08 +00:00
|
|
|
await buttonHandle.click();
|
2020-06-23 05:18:46 +00:00
|
|
|
expect(
|
2022-06-15 10:09:22 +00:00
|
|
|
await page.evaluate(() => {
|
2020-06-23 05:18:46 +00:00
|
|
|
// @ts-expect-error clicked is expected to be in the page's scope.
|
2022-06-15 10:09:22 +00:00
|
|
|
return clicked;
|
|
|
|
})
|
2020-06-23 05:18:46 +00:00
|
|
|
).toBe(true);
|
2018-03-20 03:00:12 +00:00
|
|
|
});
|
2020-05-07 10:54:55 +00:00
|
|
|
it('should work for TextNodes', async () => {
|
2022-06-22 13:25:44 +00:00
|
|
|
const {page, server} = getTestState();
|
2020-04-09 05:56:25 +00:00
|
|
|
|
2018-03-20 03:00:12 +00:00
|
|
|
await page.goto(server.PREFIX + '/input/button.html');
|
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 buttonTextNode = await page.evaluateHandle(() => {
|
|
|
|
return document.querySelector('button')!.firstChild as HTMLElement;
|
2022-06-15 10:09:22 +00:00
|
|
|
});
|
|
|
|
let error!: Error;
|
2022-06-22 13:25:44 +00:00
|
|
|
await buttonTextNode.click().catch(error_ => {
|
2022-06-15 10:09:22 +00:00
|
|
|
return (error = error_);
|
|
|
|
});
|
2018-03-20 03:00:12 +00:00
|
|
|
expect(error.message).toBe('Node is not of type HTMLElement');
|
|
|
|
});
|
2020-05-07 10:54:55 +00:00
|
|
|
it('should throw for detached nodes', async () => {
|
2022-06-22 13:25:44 +00:00
|
|
|
const {page, server} = getTestState();
|
2020-04-09 05:56:25 +00:00
|
|
|
|
2018-03-20 03:00:12 +00:00
|
|
|
await page.goto(server.PREFIX + '/input/button.html');
|
2022-06-15 10:09:22 +00:00
|
|
|
const button = (await page.$('button'))!;
|
|
|
|
await page.evaluate((button: HTMLElement) => {
|
|
|
|
return button.remove();
|
|
|
|
}, button);
|
|
|
|
let error!: Error;
|
2022-06-22 13:25:44 +00:00
|
|
|
await button.click().catch(error_ => {
|
2022-06-15 10:09:22 +00:00
|
|
|
return (error = error_);
|
|
|
|
});
|
2018-03-20 03:00:12 +00:00
|
|
|
expect(error.message).toBe('Node is detached from document');
|
|
|
|
});
|
2020-05-07 10:54:55 +00:00
|
|
|
it('should throw for hidden nodes', async () => {
|
2022-06-22 13:25:44 +00:00
|
|
|
const {page, server} = getTestState();
|
2020-04-09 05:56:25 +00:00
|
|
|
|
2018-03-20 03:00:12 +00:00
|
|
|
await page.goto(server.PREFIX + '/input/button.html');
|
2022-06-15 10:09:22 +00:00
|
|
|
const button = (await page.$('button'))!;
|
|
|
|
await page.evaluate((button: HTMLElement) => {
|
|
|
|
return (button.style.display = 'none');
|
|
|
|
}, button);
|
2022-06-22 13:25:44 +00:00
|
|
|
const error = await button.click().catch(error_ => {
|
2022-06-15 10:09:22 +00:00
|
|
|
return error_;
|
|
|
|
});
|
2020-05-07 10:54:55 +00:00
|
|
|
expect(error.message).toBe(
|
2021-09-14 16:38:58 +00:00
|
|
|
'Node is either not clickable or not an HTMLElement'
|
2020-05-07 10:54:55 +00:00
|
|
|
);
|
2018-03-20 03:00:12 +00:00
|
|
|
});
|
2020-05-07 10:54:55 +00:00
|
|
|
it('should throw for recursively hidden nodes', async () => {
|
2022-06-22 13:25:44 +00:00
|
|
|
const {page, server} = getTestState();
|
2020-04-09 05:56:25 +00:00
|
|
|
|
2018-03-20 03:00:12 +00:00
|
|
|
await page.goto(server.PREFIX + '/input/button.html');
|
2022-06-15 10:09:22 +00:00
|
|
|
const button = (await page.$('button'))!;
|
|
|
|
await page.evaluate((button: HTMLElement) => {
|
|
|
|
return (button.parentElement!.style.display = 'none');
|
|
|
|
}, button);
|
2022-06-22 13:25:44 +00:00
|
|
|
const error = await button.click().catch(error_ => {
|
2022-06-15 10:09:22 +00:00
|
|
|
return error_;
|
|
|
|
});
|
2020-05-07 10:54:55 +00:00
|
|
|
expect(error.message).toBe(
|
2021-09-14 16:38:58 +00:00
|
|
|
'Node is either not clickable or not an HTMLElement'
|
2020-05-07 10:54:55 +00:00
|
|
|
);
|
2018-03-20 03:00:12 +00:00
|
|
|
});
|
2020-06-12 13:55:51 +00:00
|
|
|
it('should throw for <br> elements', async () => {
|
2022-06-22 13:25:44 +00:00
|
|
|
const {page} = getTestState();
|
2020-04-09 05:56:25 +00:00
|
|
|
|
2018-03-20 03:00:12 +00:00
|
|
|
await page.setContent('hello<br>goodbye');
|
2022-06-15 10:09:22 +00:00
|
|
|
const br = (await page.$('br'))!;
|
2022-06-22 13:25:44 +00:00
|
|
|
const error = await br.click().catch(error_ => {
|
2022-06-15 10:09:22 +00:00
|
|
|
return error_;
|
|
|
|
});
|
2020-05-07 10:54:55 +00:00
|
|
|
expect(error.message).toBe(
|
2021-09-14 16:38:58 +00:00
|
|
|
'Node is either not clickable or not an HTMLElement'
|
2020-05-07 10:54:55 +00:00
|
|
|
);
|
2018-03-20 03:00:12 +00:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2021-12-09 11:51:14 +00:00
|
|
|
describe('Element.waitForSelector', () => {
|
|
|
|
it('should wait correctly with waitForSelector on an element', async () => {
|
2022-06-22 13:25:44 +00:00
|
|
|
const {page} = getTestState();
|
2022-07-06 07:05:37 +00:00
|
|
|
const waitFor = page.waitForSelector('.foo') as Promise<
|
|
|
|
ElementHandle<HTMLDivElement>
|
|
|
|
>;
|
2021-12-09 11:51:14 +00:00
|
|
|
// Set the page content after the waitFor has been started.
|
|
|
|
await page.setContent(
|
|
|
|
'<div id="not-foo"></div><div class="bar">bar2</div><div class="foo">Foo1</div>'
|
|
|
|
);
|
2022-06-15 10:09:22 +00:00
|
|
|
let element = (await waitFor)!;
|
2021-12-09 11:51:14 +00:00
|
|
|
expect(element).toBeDefined();
|
|
|
|
|
2022-07-06 07:05:37 +00:00
|
|
|
const innerWaitFor = element.waitForSelector('.bar') as Promise<
|
|
|
|
ElementHandle<HTMLDivElement>
|
|
|
|
>;
|
2022-06-22 13:25:44 +00:00
|
|
|
await element.evaluate(el => {
|
2021-12-09 11:51:14 +00:00
|
|
|
el.innerHTML = '<div class="bar">bar1</div>';
|
|
|
|
});
|
2022-06-15 10:09:22 +00:00
|
|
|
element = (await innerWaitFor)!;
|
2021-12-09 11:51:14 +00:00
|
|
|
expect(element).toBeDefined();
|
|
|
|
expect(
|
2022-06-22 13:25:44 +00:00
|
|
|
await element.evaluate(el => {
|
2022-06-15 10:09:22 +00:00
|
|
|
return (el as HTMLElement).innerText;
|
|
|
|
})
|
2021-12-09 11:51:14 +00:00
|
|
|
).toStrictEqual('bar1');
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2022-05-10 10:45:20 +00:00
|
|
|
describe('Element.waitForXPath', () => {
|
|
|
|
it('should wait correctly with waitForXPath on an element', async () => {
|
2022-06-22 13:25:44 +00:00
|
|
|
const {page} = getTestState();
|
2022-05-10 10:45:20 +00:00
|
|
|
// Set the page content after the waitFor has been started.
|
|
|
|
await page.setContent(
|
|
|
|
`<div id=el1>
|
|
|
|
el1
|
|
|
|
<div id=el2>
|
|
|
|
el2
|
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
<div id=el3>
|
|
|
|
el3
|
|
|
|
</div>`
|
|
|
|
);
|
|
|
|
|
2022-07-06 07:05:37 +00:00
|
|
|
const el2 = (await page.waitForSelector(
|
|
|
|
'#el1'
|
|
|
|
)) as ElementHandle<HTMLDivElement>;
|
|
|
|
|
|
|
|
for (const path of ['//div', './/div']) {
|
|
|
|
const e = (await el2.waitForXPath(
|
|
|
|
path
|
|
|
|
)) as ElementHandle<HTMLDivElement>;
|
|
|
|
expect(
|
|
|
|
await e.evaluate(el => {
|
|
|
|
return el.id;
|
|
|
|
})
|
|
|
|
).toStrictEqual('el2');
|
|
|
|
}
|
2022-05-10 10:45:20 +00:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2020-05-07 10:54:55 +00:00
|
|
|
describe('ElementHandle.hover', function () {
|
2021-11-19 07:50:31 +00:00
|
|
|
it('should work', async () => {
|
2022-06-22 13:25:44 +00:00
|
|
|
const {page, server} = getTestState();
|
2020-04-09 05:56:25 +00:00
|
|
|
|
2018-03-20 03:00:12 +00:00
|
|
|
await page.goto(server.PREFIX + '/input/scrollable.html');
|
2022-06-15 10:09:22 +00:00
|
|
|
const button = (await page.$('#button-6'))!;
|
2018-03-20 03:00:12 +00:00
|
|
|
await button.hover();
|
2020-05-07 10:54:55 +00:00
|
|
|
expect(
|
2022-06-15 10:09:22 +00:00
|
|
|
await page.evaluate(() => {
|
|
|
|
return document.querySelector('button:hover')!.id;
|
|
|
|
})
|
2020-05-07 10:54:55 +00:00
|
|
|
).toBe('button-6');
|
2018-03-20 03:00:12 +00:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2020-05-07 10:54:55 +00:00
|
|
|
describe('ElementHandle.isIntersectingViewport', function () {
|
|
|
|
it('should work', async () => {
|
2022-06-22 13:25:44 +00:00
|
|
|
const {page, server} = getTestState();
|
2020-04-09 05:56:25 +00:00
|
|
|
|
2018-07-12 04:04:36 +00:00
|
|
|
await page.goto(server.PREFIX + '/offscreenbuttons.html');
|
|
|
|
for (let i = 0; i < 11; ++i) {
|
2022-06-15 10:09:22 +00:00
|
|
|
const button = (await page.$('#btn' + i))!;
|
2018-07-12 04:04:36 +00:00
|
|
|
// All but last button are visible.
|
|
|
|
const visible = i < 10;
|
|
|
|
expect(await button.isIntersectingViewport()).toBe(visible);
|
|
|
|
}
|
2018-07-12 00:51:04 +00:00
|
|
|
});
|
2021-09-15 20:56:50 +00:00
|
|
|
it('should work with threshold', async () => {
|
2022-06-22 13:25:44 +00:00
|
|
|
const {page, server} = getTestState();
|
2021-09-15 20:56:50 +00:00
|
|
|
|
|
|
|
await page.goto(server.PREFIX + '/offscreenbuttons.html');
|
|
|
|
// a button almost cannot be seen
|
|
|
|
// sometimes we expect to return false by isIntersectingViewport1
|
2022-06-15 10:09:22 +00:00
|
|
|
const button = (await page.$('#btn11'))!;
|
2021-09-15 20:56:50 +00:00
|
|
|
expect(
|
|
|
|
await button.isIntersectingViewport({
|
|
|
|
threshold: 0.001,
|
|
|
|
})
|
|
|
|
).toBe(false);
|
|
|
|
});
|
|
|
|
it('should work with threshold of 1', async () => {
|
2022-06-22 13:25:44 +00:00
|
|
|
const {page, server} = getTestState();
|
2021-09-15 20:56:50 +00:00
|
|
|
|
|
|
|
await page.goto(server.PREFIX + '/offscreenbuttons.html');
|
|
|
|
// a button almost cannot be seen
|
|
|
|
// sometimes we expect to return false by isIntersectingViewport1
|
2022-06-15 10:09:22 +00:00
|
|
|
const button = (await page.$('#btn0'))!;
|
2021-09-15 20:56:50 +00:00
|
|
|
expect(
|
|
|
|
await button.isIntersectingViewport({
|
|
|
|
threshold: 1,
|
|
|
|
})
|
|
|
|
).toBe(true);
|
|
|
|
});
|
2018-07-12 00:51:04 +00:00
|
|
|
});
|
2020-04-30 11:45:52 +00:00
|
|
|
|
2020-05-07 10:54:55 +00:00
|
|
|
describe('Custom queries', function () {
|
2020-04-30 11:45:52 +00:00
|
|
|
this.afterEach(() => {
|
2022-06-22 13:25:44 +00:00
|
|
|
const {puppeteer} = getTestState();
|
2020-10-23 14:28:38 +00:00
|
|
|
puppeteer.clearCustomQueryHandlers();
|
2020-04-30 11:45:52 +00:00
|
|
|
});
|
2020-05-07 10:54:55 +00:00
|
|
|
it('should register and unregister', async () => {
|
2022-06-22 13:25:44 +00:00
|
|
|
const {page, puppeteer} = getTestState();
|
2020-04-30 11:45:52 +00:00
|
|
|
await page.setContent('<div id="not-foo"></div><div id="foo"></div>');
|
|
|
|
|
|
|
|
// Register.
|
2020-10-23 14:28:38 +00:00
|
|
|
puppeteer.registerCustomQueryHandler('getById', {
|
2022-06-15 10:09:22 +00:00
|
|
|
queryOne: (_element, selector) => {
|
|
|
|
return document.querySelector(`[id="${selector}"]`);
|
|
|
|
},
|
2020-07-17 05:29:42 +00:00
|
|
|
});
|
2022-07-06 07:05:37 +00:00
|
|
|
const element = (await page.$(
|
|
|
|
'getById/foo'
|
|
|
|
)) as ElementHandle<HTMLDivElement>;
|
2020-07-10 10:52:13 +00:00
|
|
|
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(element => {
|
2022-06-15 10:09:22 +00:00
|
|
|
return element.id;
|
|
|
|
}, element)
|
2020-07-10 10:52:13 +00:00
|
|
|
).toBe('foo');
|
2020-10-23 14:28:38 +00:00
|
|
|
const handlerNamesAfterRegistering = puppeteer.customQueryHandlerNames();
|
|
|
|
expect(handlerNamesAfterRegistering.includes('getById')).toBeTruthy();
|
2020-04-30 11:45:52 +00:00
|
|
|
|
|
|
|
// Unregister.
|
2020-10-23 14:28:38 +00:00
|
|
|
puppeteer.unregisterCustomQueryHandler('getById');
|
2020-04-30 11:45:52 +00:00
|
|
|
try {
|
|
|
|
await page.$('getById/foo');
|
2020-06-23 05:18:46 +00:00
|
|
|
throw new Error('Custom query handler name not set - throw expected');
|
2020-04-30 11:45:52 +00:00
|
|
|
} catch (error) {
|
2020-05-07 10:54:55 +00:00
|
|
|
expect(error).toStrictEqual(
|
|
|
|
new Error(
|
|
|
|
'Query set to use "getById", but no query handler of that name was found'
|
|
|
|
)
|
|
|
|
);
|
2020-04-30 11:45:52 +00:00
|
|
|
}
|
2021-05-12 14:48:30 +00:00
|
|
|
const handlerNamesAfterUnregistering =
|
|
|
|
puppeteer.customQueryHandlerNames();
|
2020-10-23 14:28:38 +00:00
|
|
|
expect(handlerNamesAfterUnregistering.includes('getById')).toBeFalsy();
|
2020-04-30 11:45:52 +00:00
|
|
|
});
|
|
|
|
it('should throw with invalid query names', () => {
|
|
|
|
try {
|
2022-06-22 13:25:44 +00:00
|
|
|
const {puppeteer} = getTestState();
|
2020-10-23 14:28:38 +00:00
|
|
|
puppeteer.registerCustomQueryHandler('1/2/3', {
|
2022-06-15 10:09:22 +00:00
|
|
|
queryOne: () => {
|
|
|
|
return document.querySelector('foo');
|
|
|
|
},
|
2020-10-12 09:30:35 +00:00
|
|
|
});
|
2020-06-23 05:18:46 +00:00
|
|
|
throw new Error(
|
|
|
|
'Custom query handler name was invalid - throw expected'
|
2020-05-07 10:54:55 +00:00
|
|
|
);
|
2020-04-30 11:45:52 +00:00
|
|
|
} catch (error) {
|
2020-05-07 10:54:55 +00:00
|
|
|
expect(error).toStrictEqual(
|
|
|
|
new Error('Custom query handler names may only contain [a-zA-Z]')
|
|
|
|
);
|
2020-04-30 11:45:52 +00:00
|
|
|
}
|
|
|
|
});
|
2020-05-07 10:54:55 +00:00
|
|
|
it('should work for multiple elements', async () => {
|
2022-06-22 13:25:44 +00:00
|
|
|
const {page, puppeteer} = getTestState();
|
2020-05-07 10:54:55 +00:00
|
|
|
await page.setContent(
|
|
|
|
'<div id="not-foo"></div><div class="foo">Foo1</div><div class="foo baz">Foo2</div>'
|
|
|
|
);
|
2020-10-23 14:28:38 +00:00
|
|
|
puppeteer.registerCustomQueryHandler('getByClass', {
|
2022-06-15 10:09:22 +00:00
|
|
|
queryAll: (_element, selector) => {
|
2022-07-06 07:05:37 +00:00
|
|
|
return [...document.querySelectorAll(`.${selector}`)];
|
2022-06-15 10:09:22 +00:00
|
|
|
},
|
2020-07-17 05:29:42 +00:00
|
|
|
});
|
2022-07-06 07:05:37 +00:00
|
|
|
const elements = (await page.$$('getByClass/foo')) as Array<
|
|
|
|
ElementHandle<HTMLDivElement>
|
|
|
|
>;
|
2020-05-07 10:54:55 +00:00
|
|
|
const classNames = await Promise.all(
|
2022-06-22 13:25:44 +00:00
|
|
|
elements.map(async element => {
|
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
|
|
|
return await page.evaluate(element => {
|
|
|
|
return element.className;
|
|
|
|
}, element);
|
2022-06-15 10:09:22 +00:00
|
|
|
})
|
2020-05-07 10:54:55 +00:00
|
|
|
);
|
2020-04-30 11:45:52 +00:00
|
|
|
|
|
|
|
expect(classNames).toStrictEqual(['foo', 'foo baz']);
|
|
|
|
});
|
2020-05-07 10:54:55 +00:00
|
|
|
it('should eval correctly', async () => {
|
2022-06-22 13:25:44 +00:00
|
|
|
const {page, puppeteer} = getTestState();
|
2020-05-07 10:54:55 +00:00
|
|
|
await page.setContent(
|
|
|
|
'<div id="not-foo"></div><div class="foo">Foo1</div><div class="foo baz">Foo2</div>'
|
|
|
|
);
|
2020-10-23 14:28:38 +00:00
|
|
|
puppeteer.registerCustomQueryHandler('getByClass', {
|
2022-06-15 10:09:22 +00:00
|
|
|
queryAll: (_element, selector) => {
|
2022-07-06 07:05:37 +00:00
|
|
|
return [...document.querySelectorAll(`.${selector}`)];
|
2022-06-15 10:09:22 +00:00
|
|
|
},
|
|
|
|
});
|
2022-06-22 13:25:44 +00:00
|
|
|
const elements = await page.$$eval('getByClass/foo', divs => {
|
2022-06-15 10:09:22 +00:00
|
|
|
return divs.length;
|
2020-07-17 05:29:42 +00:00
|
|
|
});
|
2020-04-30 11:45:52 +00:00
|
|
|
|
|
|
|
expect(elements).toBe(2);
|
|
|
|
});
|
2020-05-07 10:54:55 +00:00
|
|
|
it('should wait correctly with waitForSelector', async () => {
|
2022-06-22 13:25:44 +00:00
|
|
|
const {page, puppeteer} = getTestState();
|
2020-10-23 14:28:38 +00:00
|
|
|
puppeteer.registerCustomQueryHandler('getByClass', {
|
2022-06-15 10:09:22 +00:00
|
|
|
queryOne: (element, selector) => {
|
2022-07-06 07:05:37 +00:00
|
|
|
return (element as Element).querySelector(`.${selector}`);
|
2022-06-15 10:09:22 +00:00
|
|
|
},
|
2020-07-17 05:29:42 +00:00
|
|
|
});
|
2020-04-30 11:45:52 +00:00
|
|
|
const waitFor = page.waitForSelector('getByClass/foo');
|
|
|
|
|
|
|
|
// Set the page content after the waitFor has been started.
|
2020-05-07 10:54:55 +00:00
|
|
|
await page.setContent(
|
|
|
|
'<div id="not-foo"></div><div class="foo">Foo1</div>'
|
|
|
|
);
|
2020-04-30 11:45:52 +00:00
|
|
|
const element = await waitFor;
|
|
|
|
|
|
|
|
expect(element).toBeDefined();
|
|
|
|
});
|
2020-07-28 08:37:49 +00:00
|
|
|
|
2021-12-09 11:51:14 +00:00
|
|
|
it('should wait correctly with waitForSelector on an element', async () => {
|
2022-06-22 13:25:44 +00:00
|
|
|
const {page, puppeteer} = getTestState();
|
2021-12-09 11:51:14 +00:00
|
|
|
puppeteer.registerCustomQueryHandler('getByClass', {
|
2022-06-15 10:09:22 +00:00
|
|
|
queryOne: (element, selector) => {
|
2022-07-06 07:05:37 +00:00
|
|
|
return (element as Element).querySelector(`.${selector}`);
|
2022-06-15 10:09:22 +00:00
|
|
|
},
|
2021-12-09 11:51:14 +00:00
|
|
|
});
|
2022-07-06 07:05:37 +00:00
|
|
|
const waitFor = page.waitForSelector('getByClass/foo') as Promise<
|
|
|
|
ElementHandle<HTMLElement>
|
|
|
|
>;
|
2021-12-09 11:51:14 +00:00
|
|
|
|
|
|
|
// Set the page content after the waitFor has been started.
|
|
|
|
await page.setContent(
|
|
|
|
'<div id="not-foo"></div><div class="bar">bar2</div><div class="foo">Foo1</div>'
|
|
|
|
);
|
2022-06-15 10:09:22 +00:00
|
|
|
let element = (await waitFor)!;
|
2021-12-09 11:51:14 +00:00
|
|
|
expect(element).toBeDefined();
|
|
|
|
|
2022-07-06 07:05:37 +00:00
|
|
|
const innerWaitFor = element.waitForSelector('getByClass/bar') as Promise<
|
|
|
|
ElementHandle<HTMLElement>
|
|
|
|
>;
|
2021-12-09 11:51:14 +00:00
|
|
|
|
2022-06-22 13:25:44 +00:00
|
|
|
await element.evaluate(el => {
|
2021-12-09 11:51:14 +00:00
|
|
|
el.innerHTML = '<div class="bar">bar1</div>';
|
|
|
|
});
|
|
|
|
|
2022-06-15 10:09:22 +00:00
|
|
|
element = (await innerWaitFor)!;
|
2021-12-09 11:51:14 +00:00
|
|
|
expect(element).toBeDefined();
|
|
|
|
expect(
|
2022-06-22 13:25:44 +00:00
|
|
|
await element.evaluate(el => {
|
2022-07-06 07:05:37 +00:00
|
|
|
return el.innerText;
|
2022-06-15 10:09:22 +00:00
|
|
|
})
|
2021-12-09 11:51:14 +00:00
|
|
|
).toStrictEqual('bar1');
|
|
|
|
});
|
|
|
|
|
2020-05-07 10:54:55 +00:00
|
|
|
it('should wait correctly with waitFor', async () => {
|
2020-07-28 08:37:49 +00:00
|
|
|
/* page.waitFor is deprecated so we silence the warning to avoid test noise */
|
|
|
|
sinon.stub(console, 'warn').callsFake(() => {});
|
2022-06-22 13:25:44 +00:00
|
|
|
const {page, puppeteer} = getTestState();
|
2020-10-23 14:28:38 +00:00
|
|
|
puppeteer.registerCustomQueryHandler('getByClass', {
|
2022-06-15 10:09:22 +00:00
|
|
|
queryOne: (element, selector) => {
|
2022-07-06 07:05:37 +00:00
|
|
|
return (element as Element).querySelector(`.${selector}`);
|
2022-06-15 10:09:22 +00:00
|
|
|
},
|
2020-07-17 05:29:42 +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 waitFor = page.waitForSelector('getByClass/foo');
|
2020-04-30 11:45:52 +00:00
|
|
|
|
|
|
|
// Set the page content after the waitFor has been started.
|
2020-05-07 10:54:55 +00:00
|
|
|
await page.setContent(
|
|
|
|
'<div id="not-foo"></div><div class="foo">Foo1</div>'
|
|
|
|
);
|
2020-04-30 11:45:52 +00:00
|
|
|
const element = await waitFor;
|
|
|
|
|
|
|
|
expect(element).toBeDefined();
|
|
|
|
});
|
2020-07-17 05:29:42 +00:00
|
|
|
it('should work when both queryOne and queryAll are registered', async () => {
|
2022-06-22 13:25:44 +00:00
|
|
|
const {page, puppeteer} = getTestState();
|
2020-07-17 05:29:42 +00:00
|
|
|
await page.setContent(
|
|
|
|
'<div id="not-foo"></div><div class="foo"><div id="nested-foo" class="foo"/></div><div class="foo baz">Foo2</div>'
|
|
|
|
);
|
2020-10-23 14:28:38 +00:00
|
|
|
puppeteer.registerCustomQueryHandler('getByClass', {
|
2022-06-15 10:09:22 +00:00
|
|
|
queryOne: (element, selector) => {
|
2022-07-06 07:05:37 +00:00
|
|
|
return (element as Element).querySelector(`.${selector}`);
|
2022-06-15 10:09:22 +00:00
|
|
|
},
|
|
|
|
queryAll: (element, selector) => {
|
2022-07-06 07:05:37 +00:00
|
|
|
return [...(element as Element).querySelectorAll(`.${selector}`)];
|
2022-06-15 10:09:22 +00:00
|
|
|
},
|
2020-07-17 05:29:42 +00:00
|
|
|
});
|
|
|
|
|
2022-06-15 10:09:22 +00:00
|
|
|
const element = (await page.$('getByClass/foo'))!;
|
2020-07-17 05:29:42 +00:00
|
|
|
expect(element).toBeDefined();
|
|
|
|
|
|
|
|
const elements = await page.$$('getByClass/foo');
|
|
|
|
expect(elements.length).toBe(3);
|
|
|
|
});
|
|
|
|
it('should eval when both queryOne and queryAll are registered', async () => {
|
2022-06-22 13:25:44 +00:00
|
|
|
const {page, puppeteer} = getTestState();
|
2020-07-17 05:29:42 +00:00
|
|
|
await page.setContent(
|
|
|
|
'<div id="not-foo"></div><div class="foo">text</div><div class="foo baz">content</div>'
|
|
|
|
);
|
2020-10-23 14:28:38 +00:00
|
|
|
puppeteer.registerCustomQueryHandler('getByClass', {
|
2022-06-15 10:09:22 +00:00
|
|
|
queryOne: (element, selector) => {
|
2022-07-06 07:05:37 +00:00
|
|
|
return (element as Element).querySelector(`.${selector}`);
|
2022-06-15 10:09:22 +00:00
|
|
|
},
|
|
|
|
queryAll: (element, selector) => {
|
2022-07-06 07:05:37 +00:00
|
|
|
return [...(element as Element).querySelectorAll(`.${selector}`)];
|
2022-06-15 10:09:22 +00:00
|
|
|
},
|
2020-07-17 05:29:42 +00:00
|
|
|
});
|
|
|
|
|
2022-06-22 13:25:44 +00:00
|
|
|
const txtContent = await page.$eval('getByClass/foo', div => {
|
2022-06-15 10:09:22 +00:00
|
|
|
return div.textContent;
|
|
|
|
});
|
2020-07-17 05:29:42 +00:00
|
|
|
expect(txtContent).toBe('text');
|
|
|
|
|
2022-06-22 13:25:44 +00:00
|
|
|
const txtContents = await page.$$eval('getByClass/foo', divs => {
|
2022-06-15 10:09:22 +00:00
|
|
|
return divs
|
2022-06-22 13:25:44 +00:00
|
|
|
.map(d => {
|
2022-06-15 10:09:22 +00:00
|
|
|
return d.textContent;
|
|
|
|
})
|
|
|
|
.join('');
|
|
|
|
});
|
2020-07-17 05:29:42 +00:00
|
|
|
expect(txtContents).toBe('textcontent');
|
|
|
|
});
|
2020-04-30 11:45:52 +00:00
|
|
|
});
|
2020-04-09 05:56:25 +00:00
|
|
|
});
|