2017-05-11 07:06:41 +00:00
|
|
|
/**
|
|
|
|
* Copyright 2017 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.
|
|
|
|
*/
|
|
|
|
|
2022-06-22 13:25:44 +00:00
|
|
|
import {Protocol} from 'devtools-protocol';
|
|
|
|
import type {Readable} from 'stream';
|
2022-10-19 07:06:31 +00:00
|
|
|
import type {Browser} from '../api/Browser.js';
|
|
|
|
import type {BrowserContext} from '../api/BrowserContext.js';
|
2022-10-10 13:30:12 +00:00
|
|
|
import {
|
|
|
|
GeolocationOptions,
|
|
|
|
MediaFeature,
|
|
|
|
Metrics,
|
|
|
|
Page,
|
|
|
|
PageEmittedEvents,
|
|
|
|
ScreenshotClip,
|
|
|
|
ScreenshotOptions,
|
|
|
|
WaitForOptions,
|
|
|
|
WaitTimeoutOptions,
|
|
|
|
} from '../api/Page.js';
|
2022-08-17 12:39:41 +00:00
|
|
|
import {assert} from '../util/assert.js';
|
2022-09-01 15:09:57 +00:00
|
|
|
import {
|
|
|
|
createDeferredPromise,
|
|
|
|
DeferredPromise,
|
|
|
|
} from '../util/DeferredPromise.js';
|
|
|
|
import {isErrorLike} from '../util/ErrorLike.js';
|
|
|
|
import {Accessibility} from './Accessibility.js';
|
2022-09-14 12:18:12 +00:00
|
|
|
import {
|
|
|
|
CDPSession,
|
|
|
|
CDPSessionEmittedEvents,
|
|
|
|
isTargetClosedError,
|
|
|
|
} from './Connection.js';
|
2022-06-22 13:25:44 +00:00
|
|
|
import {ConsoleMessage, ConsoleMessageType} from './ConsoleMessage.js';
|
|
|
|
import {Coverage} from './Coverage.js';
|
|
|
|
import {Dialog} from './Dialog.js';
|
2022-06-23 09:31:43 +00:00
|
|
|
import {ElementHandle} from './ElementHandle.js';
|
2022-06-22 13:25:44 +00:00
|
|
|
import {EmulationManager} from './EmulationManager.js';
|
|
|
|
import {FileChooser} from './FileChooser.js';
|
2022-09-01 15:52:08 +00:00
|
|
|
import {
|
|
|
|
Frame,
|
|
|
|
FrameAddScriptTagOptions,
|
|
|
|
FrameAddStyleTagOptions,
|
2022-09-15 06:22:20 +00:00
|
|
|
FrameWaitForFunctionOptions,
|
2022-09-01 15:52:08 +00:00
|
|
|
} from './Frame.js';
|
2022-08-17 15:45:45 +00:00
|
|
|
import {FrameManager, FrameManagerEmittedEvents} from './FrameManager.js';
|
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
|
|
|
import {HTTPRequest} from './HTTPRequest.js';
|
|
|
|
import {HTTPResponse} from './HTTPResponse.js';
|
|
|
|
import {Keyboard, Mouse, MouseButton, Touchscreen} from './Input.js';
|
2022-11-10 16:11:18 +00:00
|
|
|
import {WaitForSelectorOptions} from './IsolatedWorld.js';
|
|
|
|
import {MAIN_WORLD} from './IsolatedWorlds.js';
|
2022-06-23 09:31:43 +00:00
|
|
|
import {JSHandle} from './JSHandle.js';
|
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
|
|
|
import {
|
|
|
|
Credentials,
|
|
|
|
NetworkConditions,
|
|
|
|
NetworkManagerEmittedEvents,
|
|
|
|
} from './NetworkManager.js';
|
|
|
|
import {LowerCasePaperFormat, PDFOptions, _paperFormats} from './PDFOptions.js';
|
|
|
|
import {Viewport} from './PuppeteerViewport.js';
|
|
|
|
import {Target} from './Target.js';
|
2022-07-21 18:50:46 +00:00
|
|
|
import {TargetManagerEmittedEvents} from './TargetManager.js';
|
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
|
|
|
import {TaskQueue} from './TaskQueue.js';
|
|
|
|
import {TimeoutSettings} from './TimeoutSettings.js';
|
|
|
|
import {Tracing} from './Tracing.js';
|
2022-07-06 07:05:37 +00:00
|
|
|
import {EvaluateFunc, HandleFor, NodeFor} from './types.js';
|
2022-06-14 11:16:21 +00:00
|
|
|
import {
|
2022-06-27 07:24:23 +00:00
|
|
|
createJSHandle,
|
2022-06-14 11:16:21 +00:00
|
|
|
debugError,
|
|
|
|
evaluationString,
|
|
|
|
getExceptionMessage,
|
|
|
|
getReadableAsBuffer,
|
|
|
|
getReadableFromProtocolStream,
|
2022-09-01 15:09:57 +00:00
|
|
|
importFS,
|
2022-06-14 11:16:21 +00:00
|
|
|
isNumber,
|
|
|
|
isString,
|
|
|
|
pageBindingDeliverErrorString,
|
|
|
|
pageBindingDeliverErrorValueString,
|
|
|
|
pageBindingDeliverResultString,
|
|
|
|
pageBindingInitString,
|
|
|
|
releaseObject,
|
|
|
|
valueFromRemoteObject,
|
|
|
|
waitForEvent,
|
|
|
|
waitWithTimeout,
|
2022-08-17 12:39:41 +00:00
|
|
|
} from './util.js';
|
2022-06-22 13:25:44 +00:00
|
|
|
import {WebWorker} from './WebWorker.js';
|
2017-10-11 07:55:48 +00:00
|
|
|
|
2020-06-26 07:24:56 +00:00
|
|
|
/**
|
2022-09-21 06:10:50 +00:00
|
|
|
* @internal
|
2020-06-12 10:10:12 +00:00
|
|
|
*/
|
2022-09-21 06:10:50 +00:00
|
|
|
export class CDPPage extends Page {
|
2020-06-12 10:10:12 +00:00
|
|
|
/**
|
|
|
|
* @internal
|
|
|
|
*/
|
2022-06-13 09:16:25 +00:00
|
|
|
static async _create(
|
2020-05-07 10:54:55 +00:00
|
|
|
client: CDPSession,
|
|
|
|
target: Target,
|
|
|
|
ignoreHTTPSErrors: boolean,
|
2021-09-23 12:37:35 +00:00
|
|
|
defaultViewport: Viewport | null,
|
|
|
|
screenshotTaskQueue: TaskQueue
|
2022-09-21 06:10:50 +00:00
|
|
|
): Promise<CDPPage> {
|
|
|
|
const page = new CDPPage(
|
2021-09-23 12:37:35 +00:00
|
|
|
client,
|
|
|
|
target,
|
|
|
|
ignoreHTTPSErrors,
|
|
|
|
screenshotTaskQueue
|
|
|
|
);
|
2022-06-13 09:16:25 +00:00
|
|
|
await page.#initialize();
|
2022-06-14 11:55:35 +00:00
|
|
|
if (defaultViewport) {
|
2022-09-14 12:18:12 +00:00
|
|
|
try {
|
|
|
|
await page.setViewport(defaultViewport);
|
|
|
|
} catch (err) {
|
|
|
|
if (isErrorLike(err) && isTargetClosedError(err)) {
|
|
|
|
debugError(err);
|
|
|
|
} else {
|
|
|
|
throw err;
|
|
|
|
}
|
|
|
|
}
|
2022-06-14 11:55:35 +00:00
|
|
|
}
|
2017-06-21 20:51:06 +00:00
|
|
|
return page;
|
|
|
|
}
|
|
|
|
|
2022-06-13 09:16:25 +00:00
|
|
|
#closed = false;
|
|
|
|
#client: CDPSession;
|
|
|
|
#target: Target;
|
|
|
|
#keyboard: Keyboard;
|
|
|
|
#mouse: Mouse;
|
|
|
|
#timeoutSettings = new TimeoutSettings();
|
|
|
|
#touchscreen: Touchscreen;
|
|
|
|
#accessibility: Accessibility;
|
|
|
|
#frameManager: FrameManager;
|
|
|
|
#emulationManager: EmulationManager;
|
|
|
|
#tracing: Tracing;
|
|
|
|
#pageBindings = new Map<string, Function>();
|
|
|
|
#coverage: Coverage;
|
|
|
|
#javascriptEnabled = true;
|
|
|
|
#viewport: Viewport | null;
|
|
|
|
#screenshotTaskQueue: TaskQueue;
|
|
|
|
#workers = new Map<string, WebWorker>();
|
2022-08-10 21:34:29 +00:00
|
|
|
#fileChooserPromises = new Set<DeferredPromise<FileChooser>>();
|
2020-05-05 12:53:22 +00:00
|
|
|
|
2022-06-13 09:16:25 +00:00
|
|
|
#disconnectPromise?: Promise<Error>;
|
|
|
|
#userDragInterceptionEnabled = false;
|
2020-05-05 12:53:22 +00:00
|
|
|
|
2020-06-12 10:10:12 +00:00
|
|
|
/**
|
|
|
|
* @internal
|
|
|
|
*/
|
2021-09-23 12:37:35 +00:00
|
|
|
constructor(
|
|
|
|
client: CDPSession,
|
|
|
|
target: Target,
|
|
|
|
ignoreHTTPSErrors: boolean,
|
|
|
|
screenshotTaskQueue: TaskQueue
|
|
|
|
) {
|
2017-06-21 20:51:06 +00:00
|
|
|
super();
|
2022-06-13 09:16:25 +00:00
|
|
|
this.#client = client;
|
|
|
|
this.#target = target;
|
|
|
|
this.#keyboard = new Keyboard(client);
|
|
|
|
this.#mouse = new Mouse(client, this.#keyboard);
|
|
|
|
this.#touchscreen = new Touchscreen(client, this.#keyboard);
|
|
|
|
this.#accessibility = new Accessibility(client);
|
|
|
|
this.#frameManager = new FrameManager(
|
2020-05-07 10:54:55 +00:00
|
|
|
client,
|
|
|
|
this,
|
|
|
|
ignoreHTTPSErrors,
|
2022-06-13 09:16:25 +00:00
|
|
|
this.#timeoutSettings
|
2020-05-07 10:54:55 +00:00
|
|
|
);
|
2022-06-13 09:16:25 +00:00
|
|
|
this.#emulationManager = new EmulationManager(client);
|
|
|
|
this.#tracing = new Tracing(client);
|
|
|
|
this.#coverage = new Coverage(client);
|
|
|
|
this.#screenshotTaskQueue = screenshotTaskQueue;
|
|
|
|
this.#viewport = null;
|
2017-06-21 20:51:06 +00:00
|
|
|
|
2022-07-21 18:50:46 +00:00
|
|
|
this.#target
|
|
|
|
._targetManager()
|
|
|
|
.addTargetInterceptor(this.#client, this.#onAttachedToTarget);
|
|
|
|
|
|
|
|
this.#target
|
|
|
|
._targetManager()
|
|
|
|
.on(TargetManagerEmittedEvents.TargetGone, this.#onDetachedFromTarget);
|
2018-05-21 21:31:11 +00:00
|
|
|
|
2022-06-22 13:25:44 +00:00
|
|
|
this.#frameManager.on(FrameManagerEmittedEvents.FrameAttached, event => {
|
2022-06-15 10:42:21 +00:00
|
|
|
return this.emit(PageEmittedEvents.FrameAttached, event);
|
|
|
|
});
|
2022-06-22 13:25:44 +00:00
|
|
|
this.#frameManager.on(FrameManagerEmittedEvents.FrameDetached, event => {
|
2022-06-15 10:42:21 +00:00
|
|
|
return this.emit(PageEmittedEvents.FrameDetached, event);
|
|
|
|
});
|
2022-06-22 13:25:44 +00:00
|
|
|
this.#frameManager.on(FrameManagerEmittedEvents.FrameNavigated, event => {
|
2022-06-15 10:42:21 +00:00
|
|
|
return this.emit(PageEmittedEvents.FrameNavigated, event);
|
|
|
|
});
|
2017-06-21 20:51:06 +00:00
|
|
|
|
2022-08-10 21:34:29 +00:00
|
|
|
const networkManager = this.#frameManager.networkManager;
|
2022-06-22 13:25:44 +00:00
|
|
|
networkManager.on(NetworkManagerEmittedEvents.Request, event => {
|
2022-06-15 10:42:21 +00:00
|
|
|
return this.emit(PageEmittedEvents.Request, event);
|
|
|
|
});
|
2021-03-17 14:42:35 +00:00
|
|
|
networkManager.on(
|
|
|
|
NetworkManagerEmittedEvents.RequestServedFromCache,
|
2022-06-22 13:25:44 +00:00
|
|
|
event => {
|
2022-06-15 10:42:21 +00:00
|
|
|
return this.emit(PageEmittedEvents.RequestServedFromCache, event);
|
|
|
|
}
|
2020-05-07 10:54:55 +00:00
|
|
|
);
|
2022-06-22 13:25:44 +00:00
|
|
|
networkManager.on(NetworkManagerEmittedEvents.Response, event => {
|
2022-06-15 10:42:21 +00:00
|
|
|
return this.emit(PageEmittedEvents.Response, event);
|
|
|
|
});
|
2022-06-22 13:25:44 +00:00
|
|
|
networkManager.on(NetworkManagerEmittedEvents.RequestFailed, event => {
|
2022-06-15 10:42:21 +00:00
|
|
|
return this.emit(PageEmittedEvents.RequestFailed, event);
|
|
|
|
});
|
2022-06-22 13:25:44 +00:00
|
|
|
networkManager.on(NetworkManagerEmittedEvents.RequestFinished, event => {
|
2022-06-15 10:42:21 +00:00
|
|
|
return this.emit(PageEmittedEvents.RequestFinished, event);
|
|
|
|
});
|
2017-06-29 06:09:28 +00:00
|
|
|
|
2022-06-15 10:42:21 +00:00
|
|
|
client.on('Page.domContentEventFired', () => {
|
|
|
|
return this.emit(PageEmittedEvents.DOMContentLoaded);
|
|
|
|
});
|
|
|
|
client.on('Page.loadEventFired', () => {
|
|
|
|
return this.emit(PageEmittedEvents.Load);
|
|
|
|
});
|
2022-06-22 13:25:44 +00:00
|
|
|
client.on('Runtime.consoleAPICalled', event => {
|
2022-06-15 10:42:21 +00:00
|
|
|
return this.#onConsoleAPI(event);
|
|
|
|
});
|
2022-06-22 13:25:44 +00:00
|
|
|
client.on('Runtime.bindingCalled', event => {
|
2022-06-15 10:42:21 +00:00
|
|
|
return this.#onBindingCalled(event);
|
|
|
|
});
|
2022-06-22 13:25:44 +00:00
|
|
|
client.on('Page.javascriptDialogOpening', event => {
|
2022-06-15 10:42:21 +00:00
|
|
|
return this.#onDialog(event);
|
|
|
|
});
|
2022-06-22 13:25:44 +00:00
|
|
|
client.on('Runtime.exceptionThrown', exception => {
|
2022-06-15 10:42:21 +00:00
|
|
|
return this.#handleException(exception.exceptionDetails);
|
|
|
|
});
|
|
|
|
client.on('Inspector.targetCrashed', () => {
|
|
|
|
return this.#onTargetCrashed();
|
|
|
|
});
|
2022-06-22 13:25:44 +00:00
|
|
|
client.on('Performance.metrics', event => {
|
2022-06-15 10:42:21 +00:00
|
|
|
return this.#emitMetrics(event);
|
|
|
|
});
|
2022-06-22 13:25:44 +00:00
|
|
|
client.on('Log.entryAdded', event => {
|
2022-06-15 10:42:21 +00:00
|
|
|
return this.#onLogEntryAdded(event);
|
|
|
|
});
|
2022-06-22 13:25:44 +00:00
|
|
|
client.on('Page.fileChooserOpened', event => {
|
2022-06-15 10:42:21 +00:00
|
|
|
return this.#onFileChooser(event);
|
|
|
|
});
|
2022-06-13 09:16:25 +00:00
|
|
|
this.#target._isClosedPromise.then(() => {
|
2022-07-21 18:50:46 +00:00
|
|
|
this.#target
|
|
|
|
._targetManager()
|
|
|
|
.removeTargetInterceptor(this.#client, this.#onAttachedToTarget);
|
|
|
|
|
|
|
|
this.#target
|
|
|
|
._targetManager()
|
|
|
|
.off(TargetManagerEmittedEvents.TargetGone, this.#onDetachedFromTarget);
|
2020-07-06 10:34:55 +00:00
|
|
|
this.emit(PageEmittedEvents.Close);
|
2022-06-13 09:16:25 +00:00
|
|
|
this.#closed = true;
|
2018-05-25 23:53:57 +00:00
|
|
|
});
|
2017-08-15 18:13:05 +00:00
|
|
|
}
|
|
|
|
|
2022-07-21 18:50:46 +00:00
|
|
|
#onDetachedFromTarget = (target: Target) => {
|
|
|
|
const sessionId = target._session()?.id();
|
|
|
|
|
|
|
|
this.#frameManager.onDetachedFromTarget(target);
|
|
|
|
|
|
|
|
const worker = this.#workers.get(sessionId!);
|
|
|
|
if (!worker) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
this.#workers.delete(sessionId!);
|
|
|
|
this.emit(PageEmittedEvents.WorkerDestroyed, worker);
|
|
|
|
};
|
|
|
|
|
|
|
|
#onAttachedToTarget = async (createdTarget: Target) => {
|
2022-08-10 21:34:29 +00:00
|
|
|
this.#frameManager.onAttachedToTarget(createdTarget);
|
2022-07-21 18:50:46 +00:00
|
|
|
if (createdTarget._getTargetInfo().type === 'worker') {
|
|
|
|
const session = createdTarget._session();
|
|
|
|
assert(session);
|
|
|
|
const worker = new WebWorker(
|
|
|
|
session,
|
|
|
|
createdTarget.url(),
|
|
|
|
this.#addConsoleMessage.bind(this),
|
|
|
|
this.#handleException.bind(this)
|
|
|
|
);
|
|
|
|
this.#workers.set(session.id(), worker);
|
|
|
|
this.emit(PageEmittedEvents.WorkerCreated, worker);
|
|
|
|
}
|
|
|
|
if (createdTarget._session()) {
|
|
|
|
this.#target
|
|
|
|
._targetManager()
|
|
|
|
.addTargetInterceptor(
|
|
|
|
createdTarget._session()!,
|
|
|
|
this.#onAttachedToTarget
|
|
|
|
);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2022-06-13 09:16:25 +00:00
|
|
|
async #initialize(): Promise<void> {
|
2022-09-14 12:18:12 +00:00
|
|
|
try {
|
|
|
|
await Promise.all([
|
2022-09-16 05:35:51 +00:00
|
|
|
this.#frameManager.initialize(),
|
2022-09-14 12:18:12 +00:00
|
|
|
this.#client.send('Performance.enable'),
|
|
|
|
this.#client.send('Log.enable'),
|
|
|
|
]);
|
|
|
|
} catch (err) {
|
|
|
|
if (isErrorLike(err) && isTargetClosedError(err)) {
|
|
|
|
debugError(err);
|
|
|
|
} else {
|
|
|
|
throw err;
|
|
|
|
}
|
|
|
|
}
|
2019-07-23 04:30:49 +00:00
|
|
|
}
|
|
|
|
|
2022-06-13 09:16:25 +00:00
|
|
|
async #onFileChooser(
|
2020-07-10 10:51:52 +00:00
|
|
|
event: Protocol.Page.FileChooserOpenedEvent
|
2020-05-07 10:54:55 +00:00
|
|
|
): Promise<void> {
|
2022-08-10 21:34:29 +00:00
|
|
|
if (!this.#fileChooserPromises.size) {
|
2022-06-14 11:55:35 +00:00
|
|
|
return;
|
|
|
|
}
|
2022-08-10 21:34:29 +00:00
|
|
|
|
2022-06-13 09:16:25 +00:00
|
|
|
const frame = this.#frameManager.frame(event.frameId);
|
2022-08-10 21:34:29 +00:00
|
|
|
assert(frame, 'This should never happen.');
|
|
|
|
|
2022-08-09 12:55:18 +00:00
|
|
|
// This is guaranteed to be an HTMLInputElement handle by the event.
|
2022-08-10 21:34:29 +00:00
|
|
|
const handle = (await frame.worlds[MAIN_WORLD].adoptBackendNode(
|
2022-08-09 12:55:18 +00:00
|
|
|
event.backendNodeId
|
|
|
|
)) as ElementHandle<HTMLInputElement>;
|
2022-08-10 21:34:29 +00:00
|
|
|
|
2022-08-09 12:55:18 +00:00
|
|
|
const fileChooser = new FileChooser(handle, event);
|
2022-08-10 21:34:29 +00:00
|
|
|
for (const promise of this.#fileChooserPromises) {
|
|
|
|
promise.resolve(fileChooser);
|
2022-06-14 11:55:35 +00:00
|
|
|
}
|
2022-08-10 21:34:29 +00:00
|
|
|
this.#fileChooserPromises.clear();
|
2019-07-23 04:30:49 +00:00
|
|
|
}
|
|
|
|
|
2021-07-13 09:37:39 +00:00
|
|
|
/**
|
2022-11-24 12:10:29 +00:00
|
|
|
* @internal
|
2021-07-13 09:37:39 +00:00
|
|
|
*/
|
2022-11-24 12:10:29 +00:00
|
|
|
_client(): CDPSession {
|
|
|
|
return this.#client;
|
|
|
|
}
|
|
|
|
|
2022-09-21 06:10:50 +00:00
|
|
|
override isDragInterceptionEnabled(): boolean {
|
2022-06-13 09:16:25 +00:00
|
|
|
return this.#userDragInterceptionEnabled;
|
2021-07-13 09:37:39 +00:00
|
|
|
}
|
|
|
|
|
2022-09-21 06:10:50 +00:00
|
|
|
override isJavaScriptEnabled(): boolean {
|
2022-06-13 09:16:25 +00:00
|
|
|
return this.#javascriptEnabled;
|
2020-06-10 15:15:02 +00:00
|
|
|
}
|
|
|
|
|
2022-09-21 06:10:50 +00:00
|
|
|
override waitForFileChooser(
|
|
|
|
options: WaitTimeoutOptions = {}
|
|
|
|
): Promise<FileChooser> {
|
2022-09-06 15:03:56 +00:00
|
|
|
const needsEnable = this.#fileChooserPromises.size === 0;
|
2022-06-22 13:25:44 +00:00
|
|
|
const {timeout = this.#timeoutSettings.timeout()} = options;
|
2022-08-30 14:24:51 +00:00
|
|
|
const promise = createDeferredPromise<FileChooser>({
|
|
|
|
message: `Waiting for \`FileChooser\` failed: ${timeout}ms exceeded`,
|
|
|
|
timeout,
|
|
|
|
});
|
2022-08-10 21:34:29 +00:00
|
|
|
this.#fileChooserPromises.add(promise);
|
2022-09-06 15:03:56 +00:00
|
|
|
let enablePromise: Promise<void> | undefined;
|
|
|
|
if (needsEnable) {
|
|
|
|
enablePromise = this.#client.send('Page.setInterceptFileChooserDialog', {
|
|
|
|
enabled: true,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
return Promise.all([promise, enablePromise])
|
|
|
|
.then(([result]) => {
|
|
|
|
return result;
|
|
|
|
})
|
|
|
|
.catch(error => {
|
|
|
|
this.#fileChooserPromises.delete(promise);
|
|
|
|
throw error;
|
|
|
|
});
|
2019-07-23 04:30:49 +00:00
|
|
|
}
|
|
|
|
|
2022-09-21 06:10:50 +00:00
|
|
|
override async setGeolocation(options: GeolocationOptions): Promise<void> {
|
2022-06-22 13:25:44 +00:00
|
|
|
const {longitude, latitude, accuracy = 0} = options;
|
2022-06-14 11:55:35 +00:00
|
|
|
if (longitude < -180 || longitude > 180) {
|
2020-05-07 10:54:55 +00:00
|
|
|
throw new Error(
|
|
|
|
`Invalid longitude "${longitude}": precondition -180 <= LONGITUDE <= 180 failed.`
|
|
|
|
);
|
2022-06-14 11:55:35 +00:00
|
|
|
}
|
|
|
|
if (latitude < -90 || latitude > 90) {
|
2020-05-07 10:54:55 +00:00
|
|
|
throw new Error(
|
|
|
|
`Invalid latitude "${latitude}": precondition -90 <= LATITUDE <= 90 failed.`
|
|
|
|
);
|
2022-06-14 11:55:35 +00:00
|
|
|
}
|
|
|
|
if (accuracy < 0) {
|
2020-05-07 10:54:55 +00:00
|
|
|
throw new Error(
|
|
|
|
`Invalid accuracy "${accuracy}": precondition 0 <= ACCURACY failed.`
|
|
|
|
);
|
2022-06-14 11:55:35 +00:00
|
|
|
}
|
2022-06-13 09:16:25 +00:00
|
|
|
await this.#client.send('Emulation.setGeolocationOverride', {
|
2020-05-07 10:54:55 +00:00
|
|
|
longitude,
|
|
|
|
latitude,
|
|
|
|
accuracy,
|
|
|
|
});
|
2018-08-31 17:04:12 +00:00
|
|
|
}
|
|
|
|
|
2022-09-21 06:10:50 +00:00
|
|
|
override target(): Target {
|
2022-06-13 09:16:25 +00:00
|
|
|
return this.#target;
|
2018-01-11 03:33:22 +00:00
|
|
|
}
|
|
|
|
|
2022-09-21 06:10:50 +00:00
|
|
|
override browser(): Browser {
|
2022-06-13 09:16:25 +00:00
|
|
|
return this.#target.browser();
|
2018-04-17 17:37:17 +00:00
|
|
|
}
|
|
|
|
|
2022-09-21 06:10:50 +00:00
|
|
|
override browserContext(): BrowserContext {
|
2022-06-13 09:16:25 +00:00
|
|
|
return this.#target.browserContext();
|
2018-12-12 23:08:31 +00:00
|
|
|
}
|
|
|
|
|
2022-06-13 09:16:25 +00:00
|
|
|
#onTargetCrashed(): void {
|
2017-08-15 18:13:05 +00:00
|
|
|
this.emit('error', new Error('Page crashed!'));
|
2017-06-21 20:51:06 +00:00
|
|
|
}
|
|
|
|
|
2022-06-13 09:16:25 +00:00
|
|
|
#onLogEntryAdded(event: Protocol.Log.EntryAddedEvent): void {
|
2022-06-22 13:25:44 +00:00
|
|
|
const {level, text, args, source, url, lineNumber} = event.entry;
|
2022-06-14 11:55:35 +00:00
|
|
|
if (args) {
|
2022-06-22 13:25:44 +00:00
|
|
|
args.map(arg => {
|
2022-06-15 10:42:21 +00:00
|
|
|
return releaseObject(this.#client, arg);
|
|
|
|
});
|
2022-06-14 11:55:35 +00:00
|
|
|
}
|
|
|
|
if (source !== 'worker') {
|
2020-05-07 10:54:55 +00:00
|
|
|
this.emit(
|
2020-07-06 10:34:55 +00:00
|
|
|
PageEmittedEvents.Console,
|
2022-06-22 13:25:44 +00:00
|
|
|
new ConsoleMessage(level, text, [], [{url, lineNumber}])
|
2020-05-07 10:54:55 +00:00
|
|
|
);
|
2022-06-14 11:55:35 +00:00
|
|
|
}
|
2018-04-28 04:40:09 +00:00
|
|
|
}
|
|
|
|
|
2022-09-21 06:10:50 +00:00
|
|
|
override mainFrame(): Frame {
|
2022-06-13 09:16:25 +00:00
|
|
|
return this.#frameManager.mainFrame();
|
2017-06-21 20:51:06 +00:00
|
|
|
}
|
2017-06-21 20:36:04 +00:00
|
|
|
|
2022-09-21 06:10:50 +00:00
|
|
|
override get keyboard(): Keyboard {
|
2022-06-13 09:16:25 +00:00
|
|
|
return this.#keyboard;
|
2017-07-18 01:49:52 +00:00
|
|
|
}
|
|
|
|
|
2022-09-21 06:10:50 +00:00
|
|
|
override get touchscreen(): Touchscreen {
|
2022-06-13 09:16:25 +00:00
|
|
|
return this.#touchscreen;
|
2017-09-02 02:03:51 +00:00
|
|
|
}
|
|
|
|
|
2022-09-21 06:10:50 +00:00
|
|
|
override get coverage(): Coverage {
|
2022-06-13 09:16:25 +00:00
|
|
|
return this.#coverage;
|
2018-01-03 03:53:53 +00:00
|
|
|
}
|
|
|
|
|
2022-09-21 06:10:50 +00:00
|
|
|
override get tracing(): Tracing {
|
2022-06-13 09:16:25 +00:00
|
|
|
return this.#tracing;
|
2017-08-02 17:45:11 +00:00
|
|
|
}
|
|
|
|
|
2022-09-21 06:10:50 +00:00
|
|
|
override get accessibility(): Accessibility {
|
2022-06-13 09:16:25 +00:00
|
|
|
return this.#accessibility;
|
2018-11-02 01:54:51 +00:00
|
|
|
}
|
|
|
|
|
2022-09-21 06:10:50 +00:00
|
|
|
override frames(): Frame[] {
|
2022-06-13 09:16:25 +00:00
|
|
|
return this.#frameManager.frames();
|
2017-06-21 20:51:06 +00:00
|
|
|
}
|
2017-06-21 20:36:04 +00:00
|
|
|
|
2022-09-21 06:10:50 +00:00
|
|
|
override workers(): WebWorker[] {
|
2022-06-13 09:16:25 +00:00
|
|
|
return Array.from(this.#workers.values());
|
2018-05-21 21:31:11 +00:00
|
|
|
}
|
|
|
|
|
2022-09-21 06:10:50 +00:00
|
|
|
override async setRequestInterception(value: boolean): Promise<void> {
|
2022-08-10 21:34:29 +00:00
|
|
|
return this.#frameManager.networkManager.setRequestInterception(value);
|
2017-06-21 20:51:06 +00:00
|
|
|
}
|
2017-06-15 07:20:37 +00:00
|
|
|
|
2022-09-21 06:10:50 +00:00
|
|
|
override async setDragInterception(enabled: boolean): Promise<void> {
|
2022-06-13 09:16:25 +00:00
|
|
|
this.#userDragInterceptionEnabled = enabled;
|
2022-06-22 13:25:44 +00:00
|
|
|
return this.#client.send('Input.setInterceptDrags', {enabled});
|
2021-06-04 10:25:36 +00:00
|
|
|
}
|
|
|
|
|
2022-09-21 06:10:50 +00:00
|
|
|
override setOfflineMode(enabled: boolean): Promise<void> {
|
2022-08-10 21:34:29 +00:00
|
|
|
return this.#frameManager.networkManager.setOfflineMode(enabled);
|
2017-10-13 21:41:39 +00:00
|
|
|
}
|
|
|
|
|
2022-09-21 06:10:50 +00:00
|
|
|
override emulateNetworkConditions(
|
2021-01-21 09:00:57 +00:00
|
|
|
networkConditions: NetworkConditions | null
|
|
|
|
): Promise<void> {
|
2022-08-10 21:34:29 +00:00
|
|
|
return this.#frameManager.networkManager.emulateNetworkConditions(
|
|
|
|
networkConditions
|
|
|
|
);
|
2021-01-21 09:00:57 +00:00
|
|
|
}
|
|
|
|
|
2022-09-21 06:10:50 +00:00
|
|
|
override setDefaultNavigationTimeout(timeout: number): void {
|
2022-06-13 09:16:25 +00:00
|
|
|
this.#timeoutSettings.setDefaultNavigationTimeout(timeout);
|
2019-01-29 01:16:12 +00:00
|
|
|
}
|
|
|
|
|
2022-09-21 06:10:50 +00:00
|
|
|
override setDefaultTimeout(timeout: number): void {
|
2022-06-13 09:16:25 +00:00
|
|
|
this.#timeoutSettings.setDefaultTimeout(timeout);
|
2018-01-10 21:04:01 +00:00
|
|
|
}
|
|
|
|
|
2022-09-21 06:10:50 +00:00
|
|
|
override getDefaultTimeout(): number {
|
2022-09-05 17:43:39 +00:00
|
|
|
return this.#timeoutSettings.timeout();
|
|
|
|
}
|
|
|
|
|
2022-09-21 06:10:50 +00:00
|
|
|
override async $<Selector extends string>(
|
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
|
|
|
selector: Selector
|
2022-07-06 07:05:37 +00:00
|
|
|
): Promise<ElementHandle<NodeFor<Selector>> | null> {
|
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 this.mainFrame().$(selector);
|
|
|
|
}
|
|
|
|
|
2022-09-21 06:10:50 +00:00
|
|
|
override async $$<Selector extends string>(
|
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
|
|
|
selector: Selector
|
2022-07-06 07:05:37 +00:00
|
|
|
): Promise<Array<ElementHandle<NodeFor<Selector>>>> {
|
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 this.mainFrame().$$(selector);
|
2017-08-15 21:54:02 +00:00
|
|
|
}
|
2017-10-06 22:35:02 +00:00
|
|
|
|
2022-09-21 06:10:50 +00:00
|
|
|
override async evaluateHandle<
|
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
|
|
|
Params extends unknown[],
|
|
|
|
Func extends EvaluateFunc<Params> = EvaluateFunc<Params>
|
|
|
|
>(
|
|
|
|
pageFunction: Func | string,
|
2022-06-24 06:40:08 +00:00
|
|
|
...args: Params
|
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
|
|
|
): Promise<HandleFor<Awaited<ReturnType<Func>>>> {
|
2017-11-19 00:27:52 +00:00
|
|
|
const context = await this.mainFrame().executionContext();
|
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 context.evaluateHandle(pageFunction, ...args);
|
2017-10-06 22:35:02 +00:00
|
|
|
}
|
2017-08-15 21:54:02 +00:00
|
|
|
|
2022-09-21 06:10:50 +00:00
|
|
|
override async queryObjects<Prototype>(
|
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
|
|
|
prototypeHandle: JSHandle<Prototype>
|
|
|
|
): Promise<JSHandle<Prototype[]>> {
|
2017-11-19 00:27:52 +00:00
|
|
|
const context = await this.mainFrame().executionContext();
|
2022-08-25 15:02:44 +00:00
|
|
|
assert(!prototypeHandle.disposed, 'Prototype JSHandle is disposed!');
|
|
|
|
const remoteObject = prototypeHandle.remoteObject();
|
|
|
|
assert(
|
|
|
|
remoteObject.objectId,
|
|
|
|
'Prototype JSHandle must not be referencing primitive value'
|
|
|
|
);
|
|
|
|
const response = await context._client.send('Runtime.queryObjects', {
|
|
|
|
prototypeObjectId: remoteObject.objectId,
|
|
|
|
});
|
|
|
|
return createJSHandle(context, response.objects) as HandleFor<Prototype[]>;
|
2017-10-11 21:41:20 +00:00
|
|
|
}
|
|
|
|
|
2022-09-21 06:10:50 +00:00
|
|
|
override async $eval<
|
2022-07-06 07:05:37 +00:00
|
|
|
Selector extends string,
|
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
|
|
|
Params extends unknown[],
|
|
|
|
Func extends EvaluateFunc<
|
2022-07-06 07:05:37 +00:00
|
|
|
[ElementHandle<NodeFor<Selector>>, ...Params]
|
|
|
|
> = EvaluateFunc<[ElementHandle<NodeFor<Selector>>, ...Params]>
|
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
|
|
|
>(
|
|
|
|
selector: Selector,
|
|
|
|
pageFunction: Func | string,
|
2022-06-24 06:40:08 +00:00
|
|
|
...args: Params
|
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
|
|
|
): Promise<Awaited<ReturnType<Func>>> {
|
|
|
|
return this.mainFrame().$eval(selector, pageFunction, ...args);
|
2017-08-31 22:38:01 +00:00
|
|
|
}
|
|
|
|
|
2022-09-21 06:10:50 +00:00
|
|
|
override async $$eval<
|
2022-07-06 07:05:37 +00:00
|
|
|
Selector extends string,
|
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
|
|
|
Params extends unknown[],
|
|
|
|
Func extends EvaluateFunc<
|
2022-07-06 07:05:37 +00:00
|
|
|
[Array<NodeFor<Selector>>, ...Params]
|
|
|
|
> = EvaluateFunc<[Array<NodeFor<Selector>>, ...Params]>
|
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
|
|
|
>(
|
|
|
|
selector: Selector,
|
|
|
|
pageFunction: Func | string,
|
2022-06-24 06:40:08 +00:00
|
|
|
...args: Params
|
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
|
|
|
): Promise<Awaited<ReturnType<Func>>> {
|
|
|
|
return this.mainFrame().$$eval(selector, pageFunction, ...args);
|
2017-08-23 05:56:55 +00:00
|
|
|
}
|
|
|
|
|
2022-09-21 06:10:50 +00:00
|
|
|
override async $x(expression: string): Promise<Array<ElementHandle<Node>>> {
|
2018-01-03 23:37:08 +00:00
|
|
|
return this.mainFrame().$x(expression);
|
2017-12-20 00:23:45 +00:00
|
|
|
}
|
|
|
|
|
2022-09-21 06:10:50 +00:00
|
|
|
override async cookies(
|
|
|
|
...urls: string[]
|
|
|
|
): Promise<Protocol.Network.Cookie[]> {
|
2020-05-07 10:54:55 +00:00
|
|
|
const originalCookies = (
|
2022-06-13 09:16:25 +00:00
|
|
|
await this.#client.send('Network.getCookies', {
|
2020-05-07 10:54:55 +00:00
|
|
|
urls: urls.length ? urls : [this.url()],
|
|
|
|
})
|
|
|
|
).cookies;
|
2020-04-16 07:20:27 +00:00
|
|
|
|
|
|
|
const unsupportedCookieAttributes = ['priority'];
|
2020-05-07 10:54:55 +00:00
|
|
|
const filterUnsupportedAttributes = (
|
|
|
|
cookie: Protocol.Network.Cookie
|
|
|
|
): Protocol.Network.Cookie => {
|
2022-05-31 14:34:16 +00:00
|
|
|
for (const attr of unsupportedCookieAttributes) {
|
|
|
|
delete (cookie as unknown as Record<string, unknown>)[attr];
|
|
|
|
}
|
2020-04-16 07:20:27 +00:00
|
|
|
return cookie;
|
|
|
|
};
|
|
|
|
return originalCookies.map(filterUnsupportedAttributes);
|
2017-08-24 19:21:46 +00:00
|
|
|
}
|
|
|
|
|
2022-09-21 06:10:50 +00:00
|
|
|
override async deleteCookie(
|
2020-07-10 10:51:52 +00:00
|
|
|
...cookies: Protocol.Network.DeleteCookiesRequest[]
|
2020-05-07 10:54:55 +00:00
|
|
|
): Promise<void> {
|
2017-08-24 19:21:46 +00:00
|
|
|
const pageURL = this.url();
|
|
|
|
for (const cookie of cookies) {
|
|
|
|
const item = Object.assign({}, cookie);
|
2022-06-14 11:55:35 +00:00
|
|
|
if (!cookie.url && pageURL.startsWith('http')) {
|
|
|
|
item.url = pageURL;
|
|
|
|
}
|
2022-06-13 09:16:25 +00:00
|
|
|
await this.#client.send('Network.deleteCookies', item);
|
2017-08-24 19:21:46 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-09-21 06:10:50 +00:00
|
|
|
override async setCookie(
|
|
|
|
...cookies: Protocol.Network.CookieParam[]
|
|
|
|
): Promise<void> {
|
2017-12-16 09:17:20 +00:00
|
|
|
const pageURL = this.url();
|
|
|
|
const startsWithHTTP = pageURL.startsWith('http');
|
2022-06-22 13:25:44 +00:00
|
|
|
const items = cookies.map(cookie => {
|
2017-08-24 19:21:46 +00:00
|
|
|
const item = Object.assign({}, cookie);
|
2022-06-14 11:55:35 +00:00
|
|
|
if (!item.url && startsWithHTTP) {
|
|
|
|
item.url = pageURL;
|
|
|
|
}
|
2020-05-07 10:54:55 +00:00
|
|
|
assert(
|
|
|
|
item.url !== 'about:blank',
|
|
|
|
`Blank page can not have cookie "${item.name}"`
|
|
|
|
);
|
|
|
|
assert(
|
|
|
|
!String.prototype.startsWith.call(item.url || '', 'data:'),
|
|
|
|
`Data URL page can not have cookie "${item.name}"`
|
|
|
|
);
|
2017-08-24 19:21:46 +00:00
|
|
|
return item;
|
|
|
|
});
|
|
|
|
await this.deleteCookie(...items);
|
2022-06-14 11:55:35 +00:00
|
|
|
if (items.length) {
|
2022-06-22 13:25:44 +00:00
|
|
|
await this.#client.send('Network.setCookies', {cookies: items});
|
2022-06-14 11:55:35 +00:00
|
|
|
}
|
2017-08-24 19:21:46 +00:00
|
|
|
}
|
|
|
|
|
2022-09-21 06:10:50 +00:00
|
|
|
override async addScriptTag(
|
2022-09-01 15:09:57 +00:00
|
|
|
options: FrameAddScriptTagOptions
|
|
|
|
): Promise<ElementHandle<HTMLScriptElement>> {
|
2017-10-12 08:26:44 +00:00
|
|
|
return this.mainFrame().addScriptTag(options);
|
2017-06-21 20:51:06 +00:00
|
|
|
}
|
2017-06-21 20:36:04 +00:00
|
|
|
|
2022-09-21 06:10:50 +00:00
|
|
|
override async addStyleTag(
|
2022-09-01 15:52:08 +00:00
|
|
|
options: Omit<FrameAddStyleTagOptions, 'url'>
|
|
|
|
): Promise<ElementHandle<HTMLStyleElement>>;
|
2022-09-21 06:10:50 +00:00
|
|
|
override async addStyleTag(
|
2022-09-01 15:52:08 +00:00
|
|
|
options: FrameAddStyleTagOptions
|
|
|
|
): Promise<ElementHandle<HTMLLinkElement>>;
|
2022-09-21 06:10:50 +00:00
|
|
|
override async addStyleTag(
|
2022-09-01 15:52:08 +00:00
|
|
|
options: FrameAddStyleTagOptions
|
|
|
|
): Promise<ElementHandle<HTMLStyleElement | HTMLLinkElement>> {
|
2017-10-12 08:26:44 +00:00
|
|
|
return this.mainFrame().addStyleTag(options);
|
2017-06-21 20:51:06 +00:00
|
|
|
}
|
|
|
|
|
2022-09-21 06:10:50 +00:00
|
|
|
override async exposeFunction(
|
2020-05-07 10:54:55 +00:00
|
|
|
name: string,
|
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
|
|
|
pptrFunction: Function | {default: Function}
|
2020-05-07 10:54:55 +00:00
|
|
|
): Promise<void> {
|
2022-06-14 11:55:35 +00:00
|
|
|
if (this.#pageBindings.has(name)) {
|
2020-05-07 10:54:55 +00:00
|
|
|
throw new Error(
|
|
|
|
`Failed to add page binding with name ${name}: window['${name}'] already exists!`
|
|
|
|
);
|
2022-06-14 11:55:35 +00:00
|
|
|
}
|
2021-09-29 16:32:49 +00:00
|
|
|
|
|
|
|
let exposedFunction: Function;
|
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
|
|
|
switch (typeof pptrFunction) {
|
|
|
|
case 'function':
|
|
|
|
exposedFunction = pptrFunction;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
exposedFunction = pptrFunction.default;
|
|
|
|
break;
|
2021-09-29 16:32:49 +00:00
|
|
|
}
|
|
|
|
|
2022-06-13 09:16:25 +00:00
|
|
|
this.#pageBindings.set(name, exposedFunction);
|
2017-06-21 20:51:06 +00:00
|
|
|
|
2022-06-14 11:16:21 +00:00
|
|
|
const expression = pageBindingInitString('exposedFun', name);
|
2022-06-22 13:25:44 +00:00
|
|
|
await this.#client.send('Runtime.addBinding', {name: name});
|
2022-06-13 09:16:25 +00:00
|
|
|
await this.#client.send('Page.addScriptToEvaluateOnNewDocument', {
|
2020-05-07 10:54:55 +00:00
|
|
|
source: expression,
|
|
|
|
});
|
|
|
|
await Promise.all(
|
2022-06-22 13:25:44 +00:00
|
|
|
this.frames().map(frame => {
|
2022-06-15 10:42:21 +00:00
|
|
|
return frame.evaluate(expression).catch(debugError);
|
|
|
|
})
|
2020-05-07 10:54:55 +00:00
|
|
|
);
|
2017-06-21 20:51:06 +00:00
|
|
|
}
|
2017-06-21 20:36:04 +00:00
|
|
|
|
2022-09-21 06:10:50 +00:00
|
|
|
override async authenticate(credentials: Credentials): Promise<void> {
|
2022-08-10 21:34:29 +00:00
|
|
|
return this.#frameManager.networkManager.authenticate(credentials);
|
2017-09-11 23:32:13 +00:00
|
|
|
}
|
|
|
|
|
2022-09-21 06:10:50 +00:00
|
|
|
override async setExtraHTTPHeaders(
|
|
|
|
headers: Record<string, string>
|
|
|
|
): Promise<void> {
|
2022-08-10 21:34:29 +00:00
|
|
|
return this.#frameManager.networkManager.setExtraHTTPHeaders(headers);
|
2017-06-21 20:51:06 +00:00
|
|
|
}
|
|
|
|
|
2022-09-21 06:10:50 +00:00
|
|
|
override async setUserAgent(
|
2021-06-29 16:29:55 +00:00
|
|
|
userAgent: string,
|
|
|
|
userAgentMetadata?: Protocol.Emulation.UserAgentMetadata
|
|
|
|
): Promise<void> {
|
2022-08-10 21:34:29 +00:00
|
|
|
return this.#frameManager.networkManager.setUserAgent(
|
|
|
|
userAgent,
|
|
|
|
userAgentMetadata
|
|
|
|
);
|
2017-06-21 20:51:06 +00:00
|
|
|
}
|
2017-05-11 07:06:41 +00:00
|
|
|
|
2022-09-21 06:10:50 +00:00
|
|
|
override async metrics(): Promise<Metrics> {
|
2022-06-13 09:16:25 +00:00
|
|
|
const response = await this.#client.send('Performance.getMetrics');
|
|
|
|
return this.#buildMetricsObject(response.metrics);
|
2017-10-10 21:50:38 +00:00
|
|
|
}
|
|
|
|
|
2022-06-13 09:16:25 +00:00
|
|
|
#emitMetrics(event: Protocol.Performance.MetricsEvent): void {
|
2020-07-06 10:34:55 +00:00
|
|
|
this.emit(PageEmittedEvents.Metrics, {
|
2017-10-10 21:50:38 +00:00
|
|
|
title: event.title,
|
2022-06-13 09:16:25 +00:00
|
|
|
metrics: this.#buildMetricsObject(event.metrics),
|
2017-10-10 21:50:38 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2022-06-13 09:16:25 +00:00
|
|
|
#buildMetricsObject(metrics?: Protocol.Performance.Metric[]): Metrics {
|
2022-05-31 14:34:16 +00:00
|
|
|
const result: Record<
|
|
|
|
Protocol.Performance.Metric['name'],
|
|
|
|
Protocol.Performance.Metric['value']
|
|
|
|
> = {};
|
2017-10-10 21:50:38 +00:00
|
|
|
for (const metric of metrics || []) {
|
2022-05-31 14:34:16 +00:00
|
|
|
if (supportedMetrics.has(metric.name)) {
|
|
|
|
result[metric.name] = metric.value;
|
|
|
|
}
|
2017-10-10 21:50:38 +00:00
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2022-06-13 09:16:25 +00:00
|
|
|
#handleException(exceptionDetails: Protocol.Runtime.ExceptionDetails): void {
|
2022-06-14 11:16:21 +00:00
|
|
|
const message = getExceptionMessage(exceptionDetails);
|
2018-05-25 23:44:25 +00:00
|
|
|
const err = new Error(message);
|
|
|
|
err.stack = ''; // Don't report clientside error with a node stack attached
|
2020-07-06 10:34:55 +00:00
|
|
|
this.emit(PageEmittedEvents.PageError, err);
|
2017-06-21 20:51:06 +00:00
|
|
|
}
|
|
|
|
|
2022-06-13 09:16:25 +00:00
|
|
|
async #onConsoleAPI(
|
2020-07-10 10:51:52 +00:00
|
|
|
event: Protocol.Runtime.ConsoleAPICalledEvent
|
2020-05-07 10:54:55 +00:00
|
|
|
): Promise<void> {
|
2019-01-31 00:19:02 +00:00
|
|
|
if (event.executionContextId === 0) {
|
|
|
|
// DevTools protocol stores the last 1000 console messages. These
|
|
|
|
// messages are always reported even for removed execution contexts. In
|
|
|
|
// this case, they are marked with executionContextId = 0 and are
|
|
|
|
// reported upon enabling Runtime agent.
|
|
|
|
//
|
|
|
|
// Ignore these messages since:
|
|
|
|
// - there's no execution context we can use to operate with message
|
|
|
|
// arguments
|
|
|
|
// - these messages are reported before Puppeteer clients can subscribe
|
|
|
|
// to the 'console'
|
|
|
|
// page event.
|
|
|
|
//
|
2019-11-26 12:12:25 +00:00
|
|
|
// @see https://github.com/puppeteer/puppeteer/issues/3865
|
2019-01-31 00:19:02 +00:00
|
|
|
return;
|
|
|
|
}
|
2022-06-13 09:16:25 +00:00
|
|
|
const context = this.#frameManager.executionContextById(
|
2021-10-28 09:25:49 +00:00
|
|
|
event.executionContextId,
|
2022-06-13 09:16:25 +00:00
|
|
|
this.#client
|
2020-05-07 10:54:55 +00:00
|
|
|
);
|
2022-06-22 13:25:44 +00:00
|
|
|
const values = event.args.map(arg => {
|
2022-06-27 07:24:23 +00:00
|
|
|
return createJSHandle(context, arg);
|
2022-06-15 10:42:21 +00:00
|
|
|
});
|
2022-06-13 09:16:25 +00:00
|
|
|
this.#addConsoleMessage(event.type, values, event.stackTrace);
|
2018-06-07 18:21:35 +00:00
|
|
|
}
|
|
|
|
|
2022-06-13 09:16:25 +00:00
|
|
|
async #onBindingCalled(
|
2020-07-10 10:51:52 +00:00
|
|
|
event: Protocol.Runtime.BindingCalledEvent
|
2020-05-07 10:54:55 +00:00
|
|
|
): Promise<void> {
|
2022-06-22 13:25:44 +00:00
|
|
|
let payload: {type: string; name: string; seq: number; args: unknown[]};
|
2020-10-23 10:45:47 +00:00
|
|
|
try {
|
|
|
|
payload = JSON.parse(event.payload);
|
|
|
|
} catch {
|
|
|
|
// The binding was either called by something in the page or it was
|
|
|
|
// called before our wrapper was initialized.
|
|
|
|
return;
|
|
|
|
}
|
2022-06-22 13:25:44 +00:00
|
|
|
const {type, name, seq, args} = payload;
|
2022-06-14 11:55:35 +00:00
|
|
|
if (type !== 'exposedFun' || !this.#pageBindings.has(name)) {
|
|
|
|
return;
|
|
|
|
}
|
2018-11-15 22:51:34 +00:00
|
|
|
let expression = null;
|
|
|
|
try {
|
2022-06-13 09:16:25 +00:00
|
|
|
const pageBinding = this.#pageBindings.get(name);
|
2022-05-31 14:34:16 +00:00
|
|
|
assert(pageBinding);
|
|
|
|
const result = await pageBinding(...args);
|
2022-06-14 11:16:21 +00:00
|
|
|
expression = pageBindingDeliverResultString(name, seq, result);
|
2018-11-15 22:51:34 +00:00
|
|
|
} catch (error) {
|
2022-06-14 11:55:35 +00:00
|
|
|
if (isErrorLike(error)) {
|
2022-06-14 11:16:21 +00:00
|
|
|
expression = pageBindingDeliverErrorString(
|
2020-05-07 10:54:55 +00:00
|
|
|
name,
|
|
|
|
seq,
|
|
|
|
error.message,
|
|
|
|
error.stack
|
|
|
|
);
|
2022-06-14 11:55:35 +00:00
|
|
|
} else {
|
|
|
|
expression = pageBindingDeliverErrorValueString(name, seq, error);
|
|
|
|
}
|
2018-11-15 22:51:34 +00:00
|
|
|
}
|
2022-06-13 09:16:25 +00:00
|
|
|
this.#client
|
2020-05-07 10:54:55 +00:00
|
|
|
.send('Runtime.evaluate', {
|
|
|
|
expression,
|
|
|
|
contextId: event.executionContextId,
|
|
|
|
})
|
|
|
|
.catch(debugError);
|
2018-06-18 20:41:03 +00:00
|
|
|
}
|
|
|
|
|
2022-06-13 09:16:25 +00:00
|
|
|
#addConsoleMessage(
|
2022-05-31 14:34:16 +00:00
|
|
|
eventType: ConsoleMessageType,
|
2020-05-07 10:54:55 +00:00
|
|
|
args: JSHandle[],
|
|
|
|
stackTrace?: Protocol.Runtime.StackTrace
|
|
|
|
): void {
|
2020-07-06 10:34:55 +00:00
|
|
|
if (!this.listenerCount(PageEmittedEvents.Console)) {
|
2022-06-22 13:25:44 +00:00
|
|
|
args.forEach(arg => {
|
2022-06-15 10:42:21 +00:00
|
|
|
return arg.dispose();
|
|
|
|
});
|
2017-07-25 04:43:54 +00:00
|
|
|
return;
|
|
|
|
}
|
2017-10-10 17:54:20 +00:00
|
|
|
const textTokens = [];
|
2018-06-07 18:21:35 +00:00
|
|
|
for (const arg of args) {
|
2022-08-11 09:45:35 +00:00
|
|
|
const remoteObject = arg.remoteObject();
|
2022-06-14 11:55:35 +00:00
|
|
|
if (remoteObject.objectId) {
|
|
|
|
textTokens.push(arg.toString());
|
|
|
|
} else {
|
|
|
|
textTokens.push(valueFromRemoteObject(remoteObject));
|
|
|
|
}
|
2017-10-10 17:54:20 +00:00
|
|
|
}
|
2020-09-25 13:27:13 +00:00
|
|
|
const stackTraceLocations = [];
|
|
|
|
if (stackTrace) {
|
|
|
|
for (const callFrame of stackTrace.callFrames) {
|
|
|
|
stackTraceLocations.push({
|
|
|
|
url: callFrame.url,
|
|
|
|
lineNumber: callFrame.lineNumber,
|
|
|
|
columnNumber: callFrame.columnNumber,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
2020-05-07 10:54:55 +00:00
|
|
|
const message = new ConsoleMessage(
|
2022-05-31 14:34:16 +00:00
|
|
|
eventType,
|
2020-05-07 10:54:55 +00:00
|
|
|
textTokens.join(' '),
|
|
|
|
args,
|
2020-09-25 13:27:13 +00:00
|
|
|
stackTraceLocations
|
2020-05-07 10:54:55 +00:00
|
|
|
);
|
2020-07-06 10:34:55 +00:00
|
|
|
this.emit(PageEmittedEvents.Console, message);
|
2017-06-21 20:51:06 +00:00
|
|
|
}
|
|
|
|
|
2022-06-13 09:16:25 +00:00
|
|
|
#onDialog(event: Protocol.Page.JavascriptDialogOpeningEvent): void {
|
2017-06-22 20:38:10 +00:00
|
|
|
let dialogType = null;
|
2020-06-09 08:19:42 +00:00
|
|
|
const validDialogTypes = new Set<Protocol.Page.DialogType>([
|
|
|
|
'alert',
|
|
|
|
'confirm',
|
|
|
|
'prompt',
|
|
|
|
'beforeunload',
|
|
|
|
]);
|
|
|
|
|
|
|
|
if (validDialogTypes.has(event.type)) {
|
|
|
|
dialogType = event.type as Protocol.Page.DialogType;
|
|
|
|
}
|
2018-05-31 23:53:51 +00:00
|
|
|
assert(dialogType, 'Unknown javascript dialog type: ' + event.type);
|
2020-05-05 12:53:22 +00:00
|
|
|
|
2020-05-07 10:54:55 +00:00
|
|
|
const dialog = new Dialog(
|
2022-06-13 09:16:25 +00:00
|
|
|
this.#client,
|
2020-05-07 10:54:55 +00:00
|
|
|
dialogType,
|
|
|
|
event.message,
|
|
|
|
event.defaultPrompt
|
|
|
|
);
|
2020-07-06 10:34:55 +00:00
|
|
|
this.emit(PageEmittedEvents.Dialog, dialog);
|
2017-06-21 20:51:06 +00:00
|
|
|
}
|
|
|
|
|
2021-03-18 19:57:32 +00:00
|
|
|
/**
|
|
|
|
* Resets default white background
|
|
|
|
*/
|
2022-06-13 09:16:25 +00:00
|
|
|
async #resetDefaultBackgroundColor() {
|
|
|
|
await this.#client.send('Emulation.setDefaultBackgroundColorOverride');
|
2021-03-18 19:57:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Hides default white background
|
|
|
|
*/
|
2022-06-13 09:16:25 +00:00
|
|
|
async #setTransparentBackgroundColor(): Promise<void> {
|
|
|
|
await this.#client.send('Emulation.setDefaultBackgroundColorOverride', {
|
2022-06-22 13:25:44 +00:00
|
|
|
color: {r: 0, g: 0, b: 0, a: 0},
|
2021-03-18 19:57:32 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2022-09-21 06:10:50 +00:00
|
|
|
override url(): string {
|
2017-07-14 21:05:27 +00:00
|
|
|
return this.mainFrame().url();
|
2017-06-21 20:51:06 +00:00
|
|
|
}
|
2017-06-21 20:36:04 +00:00
|
|
|
|
2022-09-21 06:10:50 +00:00
|
|
|
override async content(): Promise<string> {
|
2022-06-13 09:16:25 +00:00
|
|
|
return await this.#frameManager.mainFrame().content();
|
2017-08-21 16:02:30 +00:00
|
|
|
}
|
|
|
|
|
2022-09-21 06:10:50 +00:00
|
|
|
override async setContent(
|
|
|
|
html: string,
|
|
|
|
options: WaitForOptions = {}
|
|
|
|
): Promise<void> {
|
2022-06-13 09:16:25 +00:00
|
|
|
await this.#frameManager.mainFrame().setContent(html, options);
|
2017-06-21 20:51:06 +00:00
|
|
|
}
|
|
|
|
|
2022-09-21 06:10:50 +00:00
|
|
|
override async goto(
|
2020-05-07 10:54:55 +00:00
|
|
|
url: string,
|
2022-06-22 13:25:44 +00:00
|
|
|
options: WaitForOptions & {referer?: string} = {}
|
2022-05-31 14:34:16 +00:00
|
|
|
): Promise<HTTPResponse | null> {
|
2022-06-13 09:16:25 +00:00
|
|
|
return await this.#frameManager.mainFrame().goto(url, options);
|
2017-07-18 01:13:04 +00:00
|
|
|
}
|
|
|
|
|
2022-09-21 06:10:50 +00:00
|
|
|
override async reload(
|
|
|
|
options?: WaitForOptions
|
|
|
|
): Promise<HTTPResponse | null> {
|
2022-02-09 14:49:25 +00:00
|
|
|
const result = await Promise.all([
|
2020-07-10 10:51:52 +00:00
|
|
|
this.waitForNavigation(options),
|
2022-06-13 09:16:25 +00:00
|
|
|
this.#client.send('Page.reload'),
|
2020-07-10 10:51:52 +00:00
|
|
|
]);
|
2020-03-31 08:48:09 +00:00
|
|
|
|
2020-05-05 12:53:22 +00:00
|
|
|
return result[0];
|
2017-07-19 01:54:24 +00:00
|
|
|
}
|
2017-07-18 01:13:04 +00:00
|
|
|
|
2022-09-21 06:10:50 +00:00
|
|
|
override async waitForNavigation(
|
2020-05-07 10:54:55 +00:00
|
|
|
options: WaitForOptions = {}
|
2020-05-29 10:49:30 +00:00
|
|
|
): Promise<HTTPResponse | null> {
|
2022-06-13 09:16:25 +00:00
|
|
|
return await this.#frameManager.mainFrame().waitForNavigation(options);
|
2017-06-21 20:51:06 +00:00
|
|
|
}
|
2017-06-21 20:36:04 +00:00
|
|
|
|
2022-06-13 09:16:25 +00:00
|
|
|
#sessionClosePromise(): Promise<Error> {
|
2022-06-14 11:55:35 +00:00
|
|
|
if (!this.#disconnectPromise) {
|
2022-06-22 13:25:44 +00:00
|
|
|
this.#disconnectPromise = new Promise(fulfill => {
|
2022-06-15 10:42:21 +00:00
|
|
|
return this.#client.once(CDPSessionEmittedEvents.Disconnected, () => {
|
|
|
|
return fulfill(new Error('Target closed'));
|
|
|
|
});
|
|
|
|
});
|
2022-06-14 11:55:35 +00:00
|
|
|
}
|
2022-06-13 09:16:25 +00:00
|
|
|
return this.#disconnectPromise;
|
2019-08-21 17:26:48 +00:00
|
|
|
}
|
|
|
|
|
2022-09-21 06:10:50 +00:00
|
|
|
override async waitForRequest(
|
2021-04-06 19:39:12 +00:00
|
|
|
urlOrPredicate: string | ((req: HTTPRequest) => boolean | Promise<boolean>),
|
2022-06-22 13:25:44 +00:00
|
|
|
options: {timeout?: number} = {}
|
2020-05-29 08:38:40 +00:00
|
|
|
): Promise<HTTPRequest> {
|
2022-06-22 13:25:44 +00:00
|
|
|
const {timeout = this.#timeoutSettings.timeout()} = options;
|
2022-06-14 11:16:21 +00:00
|
|
|
return waitForEvent(
|
2022-08-10 21:34:29 +00:00
|
|
|
this.#frameManager.networkManager,
|
2020-07-07 15:43:55 +00:00
|
|
|
NetworkManagerEmittedEvents.Request,
|
2022-10-05 18:46:34 +00:00
|
|
|
async request => {
|
2022-06-14 11:55:35 +00:00
|
|
|
if (isString(urlOrPredicate)) {
|
|
|
|
return urlOrPredicate === request.url();
|
|
|
|
}
|
|
|
|
if (typeof urlOrPredicate === 'function') {
|
2022-10-05 18:46:34 +00:00
|
|
|
return !!(await urlOrPredicate(request));
|
2022-06-14 11:55:35 +00:00
|
|
|
}
|
2020-05-07 10:54:55 +00:00
|
|
|
return false;
|
|
|
|
},
|
|
|
|
timeout,
|
2022-06-13 09:16:25 +00:00
|
|
|
this.#sessionClosePromise()
|
2020-05-07 10:54:55 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2022-09-21 06:10:50 +00:00
|
|
|
override async waitForResponse(
|
2021-04-06 19:39:12 +00:00
|
|
|
urlOrPredicate:
|
|
|
|
| string
|
|
|
|
| ((res: HTTPResponse) => boolean | Promise<boolean>),
|
2022-06-22 13:25:44 +00:00
|
|
|
options: {timeout?: number} = {}
|
2020-05-29 10:49:30 +00:00
|
|
|
): Promise<HTTPResponse> {
|
2022-06-22 13:25:44 +00:00
|
|
|
const {timeout = this.#timeoutSettings.timeout()} = options;
|
2022-06-14 11:16:21 +00:00
|
|
|
return waitForEvent(
|
2022-08-10 21:34:29 +00:00
|
|
|
this.#frameManager.networkManager,
|
2020-07-07 15:43:55 +00:00
|
|
|
NetworkManagerEmittedEvents.Response,
|
2022-06-22 13:25:44 +00:00
|
|
|
async response => {
|
2022-06-14 11:55:35 +00:00
|
|
|
if (isString(urlOrPredicate)) {
|
|
|
|
return urlOrPredicate === response.url();
|
|
|
|
}
|
|
|
|
if (typeof urlOrPredicate === 'function') {
|
2020-11-25 10:35:47 +00:00
|
|
|
return !!(await urlOrPredicate(response));
|
2022-06-14 11:55:35 +00:00
|
|
|
}
|
2020-05-07 10:54:55 +00:00
|
|
|
return false;
|
|
|
|
},
|
|
|
|
timeout,
|
2022-06-13 09:16:25 +00:00
|
|
|
this.#sessionClosePromise()
|
2020-05-07 10:54:55 +00:00
|
|
|
);
|
2018-07-12 21:36:31 +00:00
|
|
|
}
|
|
|
|
|
2022-09-21 06:10:50 +00:00
|
|
|
override async waitForNetworkIdle(
|
2022-06-22 13:25:44 +00:00
|
|
|
options: {idleTime?: number; timeout?: number} = {}
|
2021-09-11 20:28:12 +00:00
|
|
|
): Promise<void> {
|
2022-06-22 13:25:44 +00:00
|
|
|
const {idleTime = 500, timeout = this.#timeoutSettings.timeout()} = options;
|
2021-09-11 20:28:12 +00:00
|
|
|
|
2022-08-10 21:34:29 +00:00
|
|
|
const networkManager = this.#frameManager.networkManager;
|
2021-09-11 20:28:12 +00:00
|
|
|
|
2022-05-31 14:34:16 +00:00
|
|
|
let idleResolveCallback: () => void;
|
2022-06-22 13:25:44 +00:00
|
|
|
const idlePromise = new Promise<void>(resolve => {
|
2021-09-11 20:28:12 +00:00
|
|
|
idleResolveCallback = resolve;
|
|
|
|
});
|
|
|
|
|
2022-05-31 14:34:16 +00:00
|
|
|
let abortRejectCallback: (error: Error) => void;
|
2021-09-11 20:28:12 +00:00
|
|
|
const abortPromise = new Promise<Error>((_, reject) => {
|
|
|
|
abortRejectCallback = reject;
|
|
|
|
});
|
|
|
|
|
2022-05-31 14:34:16 +00:00
|
|
|
let idleTimer: NodeJS.Timeout;
|
2022-06-15 10:42:21 +00:00
|
|
|
const onIdle = () => {
|
|
|
|
return idleResolveCallback();
|
|
|
|
};
|
2021-09-11 20:28:12 +00:00
|
|
|
|
|
|
|
const cleanup = () => {
|
|
|
|
idleTimer && clearTimeout(idleTimer);
|
|
|
|
abortRejectCallback(new Error('abort'));
|
|
|
|
};
|
|
|
|
|
|
|
|
const evaluate = () => {
|
|
|
|
idleTimer && clearTimeout(idleTimer);
|
2022-06-14 11:55:35 +00:00
|
|
|
if (networkManager.numRequestsInProgress() === 0) {
|
2021-09-11 20:28:12 +00:00
|
|
|
idleTimer = setTimeout(onIdle, idleTime);
|
2022-06-14 11:55:35 +00:00
|
|
|
}
|
2021-09-11 20:28:12 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
evaluate();
|
|
|
|
|
|
|
|
const eventHandler = () => {
|
|
|
|
evaluate();
|
|
|
|
return false;
|
|
|
|
};
|
|
|
|
|
2022-06-15 10:42:21 +00:00
|
|
|
const listenToEvent = (event: symbol) => {
|
|
|
|
return waitForEvent(
|
|
|
|
networkManager,
|
|
|
|
event,
|
|
|
|
eventHandler,
|
|
|
|
timeout,
|
|
|
|
abortPromise
|
|
|
|
);
|
|
|
|
};
|
2021-09-11 20:28:12 +00:00
|
|
|
|
|
|
|
const eventPromises = [
|
|
|
|
listenToEvent(NetworkManagerEmittedEvents.Request),
|
|
|
|
listenToEvent(NetworkManagerEmittedEvents.Response),
|
|
|
|
];
|
|
|
|
|
|
|
|
await Promise.race([
|
|
|
|
idlePromise,
|
|
|
|
...eventPromises,
|
2022-06-13 09:16:25 +00:00
|
|
|
this.#sessionClosePromise(),
|
2021-09-11 20:28:12 +00:00
|
|
|
]).then(
|
2022-06-22 13:25:44 +00:00
|
|
|
r => {
|
2021-09-11 20:28:12 +00:00
|
|
|
cleanup();
|
|
|
|
return r;
|
|
|
|
},
|
2022-06-22 13:25:44 +00:00
|
|
|
error => {
|
2021-09-11 20:28:12 +00:00
|
|
|
cleanup();
|
|
|
|
throw error;
|
|
|
|
}
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2022-09-21 06:10:50 +00:00
|
|
|
override async waitForFrame(
|
2021-10-28 09:25:49 +00:00
|
|
|
urlOrPredicate: string | ((frame: Frame) => boolean | Promise<boolean>),
|
2022-06-22 13:25:44 +00:00
|
|
|
options: {timeout?: number} = {}
|
2021-10-28 09:25:49 +00:00
|
|
|
): Promise<Frame> {
|
2022-06-22 13:25:44 +00:00
|
|
|
const {timeout = this.#timeoutSettings.timeout()} = options;
|
2021-10-28 09:25:49 +00:00
|
|
|
|
2022-05-31 14:34:16 +00:00
|
|
|
let predicate: (frame: Frame) => Promise<boolean>;
|
2022-06-14 11:16:21 +00:00
|
|
|
if (isString(urlOrPredicate)) {
|
2022-06-15 10:42:21 +00:00
|
|
|
predicate = (frame: Frame) => {
|
|
|
|
return Promise.resolve(urlOrPredicate === frame.url());
|
|
|
|
};
|
2022-05-31 14:34:16 +00:00
|
|
|
} else {
|
|
|
|
predicate = (frame: Frame) => {
|
|
|
|
const value = urlOrPredicate(frame);
|
|
|
|
if (typeof value === 'boolean') {
|
|
|
|
return Promise.resolve(value);
|
|
|
|
}
|
|
|
|
return value;
|
|
|
|
};
|
2021-10-28 09:25:49 +00:00
|
|
|
}
|
|
|
|
|
2022-05-31 14:34:16 +00:00
|
|
|
const eventRace: Promise<Frame> = Promise.race([
|
2022-06-14 11:16:21 +00:00
|
|
|
waitForEvent(
|
2022-06-13 09:16:25 +00:00
|
|
|
this.#frameManager,
|
2021-10-28 09:25:49 +00:00
|
|
|
FrameManagerEmittedEvents.FrameAttached,
|
|
|
|
predicate,
|
|
|
|
timeout,
|
2022-06-13 09:16:25 +00:00
|
|
|
this.#sessionClosePromise()
|
2021-10-28 09:25:49 +00:00
|
|
|
),
|
2022-06-14 11:16:21 +00:00
|
|
|
waitForEvent(
|
2022-06-13 09:16:25 +00:00
|
|
|
this.#frameManager,
|
2021-10-28 09:25:49 +00:00
|
|
|
FrameManagerEmittedEvents.FrameNavigated,
|
|
|
|
predicate,
|
|
|
|
timeout,
|
2022-06-13 09:16:25 +00:00
|
|
|
this.#sessionClosePromise()
|
2021-10-28 09:25:49 +00:00
|
|
|
),
|
2022-06-22 13:25:44 +00:00
|
|
|
...this.frames().map(async frame => {
|
2022-05-31 14:34:16 +00:00
|
|
|
if (await predicate(frame)) {
|
|
|
|
return frame;
|
2022-04-07 13:05:36 +00:00
|
|
|
}
|
2022-05-31 14:34:16 +00:00
|
|
|
return await eventRace;
|
|
|
|
}),
|
2022-04-07 13:05:36 +00:00
|
|
|
]);
|
2022-05-31 14:34:16 +00:00
|
|
|
|
|
|
|
return eventRace;
|
2021-10-28 09:25:49 +00:00
|
|
|
}
|
|
|
|
|
2022-09-21 06:10:50 +00:00
|
|
|
override async goBack(
|
|
|
|
options: WaitForOptions = {}
|
|
|
|
): Promise<HTTPResponse | null> {
|
2022-06-13 09:16:25 +00:00
|
|
|
return this.#go(-1, options);
|
2017-07-19 02:11:37 +00:00
|
|
|
}
|
|
|
|
|
2022-09-21 06:10:50 +00:00
|
|
|
override async goForward(
|
|
|
|
options: WaitForOptions = {}
|
|
|
|
): Promise<HTTPResponse | null> {
|
2022-06-13 09:16:25 +00:00
|
|
|
return this.#go(+1, options);
|
2017-07-19 02:11:37 +00:00
|
|
|
}
|
|
|
|
|
2022-06-13 09:16:25 +00:00
|
|
|
async #go(
|
2020-05-07 10:54:55 +00:00
|
|
|
delta: number,
|
|
|
|
options: WaitForOptions
|
2020-05-29 10:49:30 +00:00
|
|
|
): Promise<HTTPResponse | null> {
|
2022-06-13 09:16:25 +00:00
|
|
|
const history = await this.#client.send('Page.getNavigationHistory');
|
2017-07-19 02:11:37 +00:00
|
|
|
const entry = history.entries[history.currentIndex + delta];
|
2022-06-14 11:55:35 +00:00
|
|
|
if (!entry) {
|
|
|
|
return null;
|
|
|
|
}
|
2020-07-10 10:51:52 +00:00
|
|
|
const result = await Promise.all([
|
2017-09-11 23:21:51 +00:00
|
|
|
this.waitForNavigation(options),
|
2022-06-22 13:25:44 +00:00
|
|
|
this.#client.send('Page.navigateToHistoryEntry', {entryId: entry.id}),
|
2017-09-11 23:21:51 +00:00
|
|
|
]);
|
2020-05-05 12:53:22 +00:00
|
|
|
return result[0];
|
2017-07-19 02:11:37 +00:00
|
|
|
}
|
|
|
|
|
2022-09-21 06:10:50 +00:00
|
|
|
override async bringToFront(): Promise<void> {
|
2022-06-13 09:16:25 +00:00
|
|
|
await this.#client.send('Page.bringToFront');
|
2017-11-07 21:17:36 +00:00
|
|
|
}
|
|
|
|
|
2022-09-21 06:10:50 +00:00
|
|
|
override async setJavaScriptEnabled(enabled: boolean): Promise<void> {
|
2022-06-14 11:55:35 +00:00
|
|
|
if (this.#javascriptEnabled === enabled) {
|
|
|
|
return;
|
|
|
|
}
|
2022-06-13 09:16:25 +00:00
|
|
|
this.#javascriptEnabled = enabled;
|
|
|
|
await this.#client.send('Emulation.setScriptExecutionDisabled', {
|
2020-05-07 10:54:55 +00:00
|
|
|
value: !enabled,
|
|
|
|
});
|
2017-08-23 21:08:56 +00:00
|
|
|
}
|
|
|
|
|
2022-09-21 06:10:50 +00:00
|
|
|
override async setBypassCSP(enabled: boolean): Promise<void> {
|
2022-06-22 13:25:44 +00:00
|
|
|
await this.#client.send('Page.setBypassCSP', {enabled});
|
2018-04-06 23:35:50 +00:00
|
|
|
}
|
|
|
|
|
2022-09-21 06:10:50 +00:00
|
|
|
override async emulateMediaType(type?: string): Promise<void> {
|
2020-05-07 10:54:55 +00:00
|
|
|
assert(
|
2022-06-15 10:09:22 +00:00
|
|
|
type === 'screen' ||
|
|
|
|
type === 'print' ||
|
|
|
|
(type ?? undefined) === undefined,
|
2020-05-07 10:54:55 +00:00
|
|
|
'Unsupported media type: ' + type
|
|
|
|
);
|
2022-06-13 09:16:25 +00:00
|
|
|
await this.#client.send('Emulation.setEmulatedMedia', {
|
2020-05-07 10:54:55 +00:00
|
|
|
media: type || '',
|
|
|
|
});
|
2019-10-23 11:55:00 +00:00
|
|
|
}
|
|
|
|
|
2022-09-21 06:10:50 +00:00
|
|
|
override async emulateCPUThrottling(factor: number | null): Promise<void> {
|
2021-06-22 11:43:11 +00:00
|
|
|
assert(
|
|
|
|
factor === null || factor >= 1,
|
|
|
|
'Throttling rate should be greater or equal to 1'
|
|
|
|
);
|
2022-06-13 09:16:25 +00:00
|
|
|
await this.#client.send('Emulation.setCPUThrottlingRate', {
|
2021-06-22 11:43:11 +00:00
|
|
|
rate: factor !== null ? factor : 1,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2022-09-21 06:10:50 +00:00
|
|
|
override async emulateMediaFeatures(
|
|
|
|
features?: MediaFeature[]
|
|
|
|
): Promise<void> {
|
2022-06-14 11:55:35 +00:00
|
|
|
if (!features) {
|
|
|
|
await this.#client.send('Emulation.setEmulatedMedia', {});
|
|
|
|
}
|
2019-10-23 11:55:00 +00:00
|
|
|
if (Array.isArray(features)) {
|
2022-05-31 14:34:16 +00:00
|
|
|
for (const mediaFeature of features) {
|
2019-10-23 11:55:00 +00:00
|
|
|
const name = mediaFeature.name;
|
2020-05-07 10:54:55 +00:00
|
|
|
assert(
|
2021-02-11 13:59:50 +00:00
|
|
|
/^(?:prefers-(?:color-scheme|reduced-motion)|color-gamut)$/.test(
|
|
|
|
name
|
|
|
|
),
|
2020-05-07 10:54:55 +00:00
|
|
|
'Unsupported media feature: ' + name
|
|
|
|
);
|
2022-05-31 14:34:16 +00:00
|
|
|
}
|
2022-06-13 09:16:25 +00:00
|
|
|
await this.#client.send('Emulation.setEmulatedMedia', {
|
2020-05-07 10:54:55 +00:00
|
|
|
features: features,
|
|
|
|
});
|
2019-10-23 11:55:00 +00:00
|
|
|
}
|
2017-08-18 23:49:02 +00:00
|
|
|
}
|
|
|
|
|
2022-09-21 06:10:50 +00:00
|
|
|
override async emulateTimezone(timezoneId?: string): Promise<void> {
|
2019-10-23 13:49:39 +00:00
|
|
|
try {
|
2022-06-13 09:16:25 +00:00
|
|
|
await this.#client.send('Emulation.setTimezoneOverride', {
|
2020-05-07 10:54:55 +00:00
|
|
|
timezoneId: timezoneId || '',
|
|
|
|
});
|
2020-04-28 13:16:28 +00:00
|
|
|
} catch (error) {
|
2022-06-10 13:27:42 +00:00
|
|
|
if (isErrorLike(error) && error.message.includes('Invalid timezone')) {
|
2019-10-23 13:49:39 +00:00
|
|
|
throw new Error(`Invalid timezone ID: ${timezoneId}`);
|
2022-06-10 13:27:42 +00:00
|
|
|
}
|
2020-04-28 13:16:28 +00:00
|
|
|
throw error;
|
2019-10-23 13:49:39 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-09-21 06:10:50 +00:00
|
|
|
override async emulateIdleState(overrides?: {
|
2020-09-14 09:31:23 +00:00
|
|
|
isUserActive: boolean;
|
|
|
|
isScreenUnlocked: boolean;
|
|
|
|
}): Promise<void> {
|
|
|
|
if (overrides) {
|
2022-06-13 09:16:25 +00:00
|
|
|
await this.#client.send('Emulation.setIdleOverride', {
|
2020-09-14 09:31:23 +00:00
|
|
|
isUserActive: overrides.isUserActive,
|
|
|
|
isScreenUnlocked: overrides.isScreenUnlocked,
|
|
|
|
});
|
|
|
|
} else {
|
2022-06-13 09:16:25 +00:00
|
|
|
await this.#client.send('Emulation.clearIdleOverride');
|
2020-09-14 09:31:23 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-09-21 06:10:50 +00:00
|
|
|
override async emulateVisionDeficiency(
|
2020-07-22 09:04:53 +00:00
|
|
|
type?: Protocol.Emulation.SetEmulatedVisionDeficiencyRequest['type']
|
|
|
|
): Promise<void> {
|
|
|
|
const visionDeficiencies = new Set<
|
|
|
|
Protocol.Emulation.SetEmulatedVisionDeficiencyRequest['type']
|
|
|
|
>([
|
2020-06-23 05:18:46 +00:00
|
|
|
'none',
|
|
|
|
'achromatopsia',
|
|
|
|
'blurredVision',
|
|
|
|
'deuteranopia',
|
|
|
|
'protanopia',
|
|
|
|
'tritanopia',
|
|
|
|
]);
|
2020-05-26 15:14:20 +00:00
|
|
|
try {
|
|
|
|
assert(
|
|
|
|
!type || visionDeficiencies.has(type),
|
|
|
|
`Unsupported vision deficiency: ${type}`
|
|
|
|
);
|
2022-06-13 09:16:25 +00:00
|
|
|
await this.#client.send('Emulation.setEmulatedVisionDeficiency', {
|
2020-05-26 15:14:20 +00:00
|
|
|
type: type || 'none',
|
|
|
|
});
|
|
|
|
} catch (error) {
|
|
|
|
throw error;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-09-21 06:10:50 +00:00
|
|
|
override async setViewport(viewport: Viewport): Promise<void> {
|
2022-06-13 09:16:25 +00:00
|
|
|
const needsReload = await this.#emulationManager.emulateViewport(viewport);
|
|
|
|
this.#viewport = viewport;
|
2022-06-14 11:55:35 +00:00
|
|
|
if (needsReload) {
|
|
|
|
await this.reload();
|
|
|
|
}
|
2017-06-21 20:51:06 +00:00
|
|
|
}
|
|
|
|
|
2022-09-21 06:10:50 +00:00
|
|
|
override viewport(): Viewport | null {
|
2022-06-13 09:16:25 +00:00
|
|
|
return this.#viewport;
|
2017-06-21 20:51:06 +00:00
|
|
|
}
|
2017-06-21 20:36:04 +00:00
|
|
|
|
2022-09-21 06:10:50 +00:00
|
|
|
override async evaluate<
|
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
|
|
|
Params extends unknown[],
|
|
|
|
Func extends EvaluateFunc<Params> = EvaluateFunc<Params>
|
|
|
|
>(
|
|
|
|
pageFunction: Func | string,
|
2022-06-24 06:40:08 +00:00
|
|
|
...args: Params
|
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
|
|
|
): Promise<Awaited<ReturnType<Func>>> {
|
|
|
|
return this.#frameManager.mainFrame().evaluate(pageFunction, ...args);
|
2017-06-21 20:51:06 +00:00
|
|
|
}
|
2017-06-21 20:36:04 +00:00
|
|
|
|
2022-09-21 06:10:50 +00:00
|
|
|
override async evaluateOnNewDocument<
|
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
|
|
|
Params extends unknown[],
|
|
|
|
Func extends (...args: Params) => unknown = (...args: Params) => unknown
|
|
|
|
>(pageFunction: Func | string, ...args: Params): Promise<void> {
|
2022-06-14 11:16:21 +00:00
|
|
|
const source = evaluationString(pageFunction, ...args);
|
2022-06-13 09:16:25 +00:00
|
|
|
await this.#client.send('Page.addScriptToEvaluateOnNewDocument', {
|
2020-05-07 10:54:55 +00:00
|
|
|
source,
|
|
|
|
});
|
2017-06-21 20:51:06 +00:00
|
|
|
}
|
2017-06-20 01:03:01 +00:00
|
|
|
|
2022-09-21 06:10:50 +00:00
|
|
|
override async setCacheEnabled(enabled = true): Promise<void> {
|
2022-08-10 21:34:29 +00:00
|
|
|
await this.#frameManager.networkManager.setCacheEnabled(enabled);
|
2018-02-08 05:58:48 +00:00
|
|
|
}
|
|
|
|
|
2022-09-21 06:10:50 +00:00
|
|
|
override async screenshot(
|
|
|
|
options: ScreenshotOptions = {}
|
|
|
|
): Promise<Buffer | string> {
|
2022-05-31 14:34:16 +00:00
|
|
|
let screenshotType = Protocol.Page.CaptureScreenshotRequestFormat.Png;
|
2017-12-04 22:04:36 +00:00
|
|
|
// options.type takes precedence over inferring the type from options.path
|
2020-06-19 14:39:03 +00:00
|
|
|
// because it may be a 0-length file with no extension created beforehand
|
|
|
|
// (i.e. as a temp file).
|
2017-12-04 22:04:36 +00:00
|
|
|
if (options.type) {
|
2022-05-31 14:34:16 +00:00
|
|
|
screenshotType =
|
|
|
|
options.type as Protocol.Page.CaptureScreenshotRequestFormat;
|
2017-12-04 22:04:36 +00:00
|
|
|
} else if (options.path) {
|
2020-09-14 11:39:33 +00:00
|
|
|
const filePath = options.path;
|
|
|
|
const extension = filePath
|
|
|
|
.slice(filePath.lastIndexOf('.') + 1)
|
|
|
|
.toLowerCase();
|
2022-05-31 14:34:16 +00:00
|
|
|
switch (extension) {
|
|
|
|
case 'png':
|
|
|
|
screenshotType = Protocol.Page.CaptureScreenshotRequestFormat.Png;
|
|
|
|
break;
|
|
|
|
case 'jpeg':
|
|
|
|
case 'jpg':
|
|
|
|
screenshotType = Protocol.Page.CaptureScreenshotRequestFormat.Jpeg;
|
|
|
|
break;
|
|
|
|
case 'webp':
|
|
|
|
screenshotType = Protocol.Page.CaptureScreenshotRequestFormat.Webp;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
throw new Error(
|
|
|
|
`Unsupported screenshot type for extension \`.${extension}\``
|
|
|
|
);
|
|
|
|
}
|
2017-06-21 20:51:06 +00:00
|
|
|
}
|
2017-12-04 22:04:36 +00:00
|
|
|
|
2017-06-21 20:51:06 +00:00
|
|
|
if (options.quality) {
|
2020-05-07 10:54:55 +00:00
|
|
|
assert(
|
2022-05-31 14:34:16 +00:00
|
|
|
screenshotType === Protocol.Page.CaptureScreenshotRequestFormat.Jpeg ||
|
|
|
|
screenshotType === Protocol.Page.CaptureScreenshotRequestFormat.Webp,
|
2020-05-07 10:54:55 +00:00
|
|
|
'options.quality is unsupported for the ' +
|
|
|
|
screenshotType +
|
|
|
|
' screenshots'
|
|
|
|
);
|
|
|
|
assert(
|
|
|
|
typeof options.quality === 'number',
|
|
|
|
'Expected options.quality to be a number but found ' +
|
|
|
|
typeof options.quality
|
|
|
|
);
|
|
|
|
assert(
|
|
|
|
Number.isInteger(options.quality),
|
|
|
|
'Expected options.quality to be an integer'
|
|
|
|
);
|
|
|
|
assert(
|
|
|
|
options.quality >= 0 && options.quality <= 100,
|
|
|
|
'Expected options.quality to be between 0 and 100 (inclusive), got ' +
|
|
|
|
options.quality
|
|
|
|
);
|
2017-06-21 20:51:06 +00:00
|
|
|
}
|
2020-05-07 10:54:55 +00:00
|
|
|
assert(
|
|
|
|
!options.clip || !options.fullPage,
|
|
|
|
'options.clip and options.fullPage are exclusive'
|
|
|
|
);
|
2017-06-21 20:51:06 +00:00
|
|
|
if (options.clip) {
|
2020-05-07 10:54:55 +00:00
|
|
|
assert(
|
|
|
|
typeof options.clip.x === 'number',
|
|
|
|
'Expected options.clip.x to be a number but found ' +
|
|
|
|
typeof options.clip.x
|
|
|
|
);
|
|
|
|
assert(
|
|
|
|
typeof options.clip.y === 'number',
|
|
|
|
'Expected options.clip.y to be a number but found ' +
|
|
|
|
typeof options.clip.y
|
|
|
|
);
|
|
|
|
assert(
|
|
|
|
typeof options.clip.width === 'number',
|
|
|
|
'Expected options.clip.width to be a number but found ' +
|
|
|
|
typeof options.clip.width
|
|
|
|
);
|
|
|
|
assert(
|
|
|
|
typeof options.clip.height === 'number',
|
|
|
|
'Expected options.clip.height to be a number but found ' +
|
|
|
|
typeof options.clip.height
|
|
|
|
);
|
|
|
|
assert(
|
|
|
|
options.clip.width !== 0,
|
|
|
|
'Expected options.clip.width not to be 0.'
|
|
|
|
);
|
|
|
|
assert(
|
|
|
|
options.clip.height !== 0,
|
|
|
|
'Expected options.clip.height not to be 0.'
|
|
|
|
);
|
2017-06-21 20:51:06 +00:00
|
|
|
}
|
2022-06-15 10:42:21 +00:00
|
|
|
return this.#screenshotTaskQueue.postTask(() => {
|
|
|
|
return this.#screenshotTask(screenshotType, options);
|
|
|
|
});
|
2017-06-21 20:51:06 +00:00
|
|
|
}
|
|
|
|
|
2022-06-13 09:16:25 +00:00
|
|
|
async #screenshotTask(
|
2021-09-21 14:26:20 +00:00
|
|
|
format: Protocol.Page.CaptureScreenshotRequestFormat,
|
2022-05-31 14:34:16 +00:00
|
|
|
options: ScreenshotOptions = {}
|
2020-05-07 10:54:55 +00:00
|
|
|
): Promise<Buffer | string> {
|
2022-06-13 09:16:25 +00:00
|
|
|
await this.#client.send('Target.activateTarget', {
|
|
|
|
targetId: this.#target._targetId,
|
2020-05-07 10:54:55 +00:00
|
|
|
});
|
2019-01-15 22:34:31 +00:00
|
|
|
let clip = options.clip ? processClip(options.clip) : undefined;
|
2022-10-25 10:55:19 +00:00
|
|
|
let captureBeyondViewport = options.captureBeyondViewport ?? true;
|
|
|
|
const fromSurface = options.fromSurface;
|
2017-07-26 22:28:44 +00:00
|
|
|
|
2017-07-17 19:15:06 +00:00
|
|
|
if (options.fullPage) {
|
2022-06-13 09:16:25 +00:00
|
|
|
const metrics = await this.#client.send('Page.getLayoutMetrics');
|
2021-07-01 11:23:38 +00:00
|
|
|
// Fallback to `contentSize` in case of using Firefox.
|
2022-06-22 13:25:44 +00:00
|
|
|
const {width, height} = metrics.cssContentSize || metrics.contentSize;
|
2017-08-11 01:42:30 +00:00
|
|
|
|
2021-02-03 13:30:46 +00:00
|
|
|
// Overwrite clip for full page.
|
2022-10-25 10:55:19 +00:00
|
|
|
clip = undefined;
|
2021-04-06 13:14:31 +00:00
|
|
|
|
|
|
|
if (!captureBeyondViewport) {
|
2021-05-12 14:48:30 +00:00
|
|
|
const {
|
|
|
|
isMobile = false,
|
|
|
|
deviceScaleFactor = 1,
|
|
|
|
isLandscape = false,
|
2022-06-13 09:16:25 +00:00
|
|
|
} = this.#viewport || {};
|
2021-05-12 14:48:30 +00:00
|
|
|
const screenOrientation: Protocol.Emulation.ScreenOrientation =
|
|
|
|
isLandscape
|
2022-06-22 13:25:44 +00:00
|
|
|
? {angle: 90, type: 'landscapePrimary'}
|
|
|
|
: {angle: 0, type: 'portraitPrimary'};
|
2022-06-13 09:16:25 +00:00
|
|
|
await this.#client.send('Emulation.setDeviceMetricsOverride', {
|
2021-04-06 13:14:31 +00:00
|
|
|
mobile: isMobile,
|
|
|
|
width,
|
|
|
|
height,
|
|
|
|
deviceScaleFactor,
|
|
|
|
screenOrientation,
|
|
|
|
});
|
|
|
|
}
|
2022-10-25 10:55:19 +00:00
|
|
|
} else if (!clip) {
|
|
|
|
captureBeyondViewport = false;
|
2017-06-21 20:51:06 +00:00
|
|
|
}
|
2022-10-25 10:55:19 +00:00
|
|
|
|
2020-05-07 10:54:55 +00:00
|
|
|
const shouldSetDefaultBackground =
|
2021-09-21 14:26:20 +00:00
|
|
|
options.omitBackground && (format === 'png' || format === 'webp');
|
2021-03-18 19:57:32 +00:00
|
|
|
if (shouldSetDefaultBackground) {
|
2022-06-13 09:16:25 +00:00
|
|
|
await this.#setTransparentBackgroundColor();
|
2021-03-18 19:57:32 +00:00
|
|
|
}
|
2021-04-06 13:14:31 +00:00
|
|
|
|
2022-06-13 09:16:25 +00:00
|
|
|
const result = await this.#client.send('Page.captureScreenshot', {
|
2020-05-07 10:54:55 +00:00
|
|
|
format,
|
|
|
|
quality: options.quality,
|
2022-10-25 10:55:19 +00:00
|
|
|
clip: clip && {
|
|
|
|
...clip,
|
|
|
|
scale: clip.scale ?? 1,
|
|
|
|
},
|
2021-04-06 13:14:31 +00:00
|
|
|
captureBeyondViewport,
|
2022-06-27 12:37:37 +00:00
|
|
|
fromSurface,
|
2020-05-07 10:54:55 +00:00
|
|
|
});
|
2021-03-18 19:57:32 +00:00
|
|
|
if (shouldSetDefaultBackground) {
|
2022-06-13 09:16:25 +00:00
|
|
|
await this.#resetDefaultBackgroundColor();
|
2021-03-18 19:57:32 +00:00
|
|
|
}
|
2017-07-17 19:15:06 +00:00
|
|
|
|
2022-06-14 11:55:35 +00:00
|
|
|
if (options.fullPage && this.#viewport) {
|
2022-06-13 09:16:25 +00:00
|
|
|
await this.setViewport(this.#viewport);
|
2022-06-14 11:55:35 +00:00
|
|
|
}
|
2017-07-17 19:15:06 +00:00
|
|
|
|
2020-05-07 10:54:55 +00:00
|
|
|
const buffer =
|
|
|
|
options.encoding === 'base64'
|
|
|
|
? result.data
|
|
|
|
: Buffer.from(result.data, 'base64');
|
2021-03-15 09:05:15 +00:00
|
|
|
|
|
|
|
if (options.path) {
|
2022-06-09 11:03:44 +00:00
|
|
|
try {
|
2022-07-07 19:09:07 +00:00
|
|
|
const fs = (await importFS()).promises;
|
2022-06-09 11:03:44 +00:00
|
|
|
await fs.writeFile(options.path, buffer);
|
|
|
|
} catch (error) {
|
|
|
|
if (error instanceof TypeError) {
|
|
|
|
throw new Error(
|
|
|
|
'Screenshots can only be written to a file path in a Node-like environment.'
|
|
|
|
);
|
|
|
|
}
|
|
|
|
throw error;
|
2021-03-15 09:05:15 +00:00
|
|
|
}
|
2020-09-28 09:35:35 +00:00
|
|
|
}
|
2017-06-21 20:51:06 +00:00
|
|
|
return buffer;
|
2019-01-15 22:34:31 +00:00
|
|
|
|
2022-09-07 05:27:40 +00:00
|
|
|
function processClip(clip: ScreenshotClip): ScreenshotClip {
|
2019-01-15 22:34:31 +00:00
|
|
|
const x = Math.round(clip.x);
|
|
|
|
const y = Math.round(clip.y);
|
|
|
|
const width = Math.round(clip.width + clip.x - x);
|
|
|
|
const height = Math.round(clip.height + clip.y - y);
|
2022-09-07 05:27:40 +00:00
|
|
|
return {x, y, width, height, scale: clip.scale};
|
2019-01-15 22:34:31 +00:00
|
|
|
}
|
2017-06-21 20:51:06 +00:00
|
|
|
}
|
|
|
|
|
2022-09-21 06:10:50 +00:00
|
|
|
override async createPDFStream(options: PDFOptions = {}): Promise<Readable> {
|
2018-11-12 20:59:21 +00:00
|
|
|
const {
|
|
|
|
scale = 1,
|
|
|
|
displayHeaderFooter = false,
|
|
|
|
headerTemplate = '',
|
|
|
|
footerTemplate = '',
|
|
|
|
printBackground = false,
|
|
|
|
landscape = false,
|
|
|
|
pageRanges = '',
|
|
|
|
preferCSSPageSize = false,
|
|
|
|
margin = {},
|
2021-03-18 19:57:32 +00:00
|
|
|
omitBackground = false,
|
2021-09-14 16:11:14 +00:00
|
|
|
timeout = 30000,
|
2018-11-12 20:59:21 +00:00
|
|
|
} = options;
|
2017-06-21 20:51:06 +00:00
|
|
|
|
2017-06-22 20:38:10 +00:00
|
|
|
let paperWidth = 8.5;
|
|
|
|
let paperHeight = 11;
|
2017-06-21 20:51:06 +00:00
|
|
|
if (options.format) {
|
2022-05-31 14:34:16 +00:00
|
|
|
const format =
|
2022-06-13 09:16:25 +00:00
|
|
|
_paperFormats[options.format.toLowerCase() as LowerCasePaperFormat];
|
2018-05-31 23:53:51 +00:00
|
|
|
assert(format, 'Unknown paper format: ' + options.format);
|
2017-06-21 20:51:06 +00:00
|
|
|
paperWidth = format.width;
|
|
|
|
paperHeight = format.height;
|
|
|
|
} else {
|
|
|
|
paperWidth = convertPrintParameterToInches(options.width) || paperWidth;
|
2020-05-07 10:54:55 +00:00
|
|
|
paperHeight =
|
|
|
|
convertPrintParameterToInches(options.height) || paperHeight;
|
2017-06-21 20:51:06 +00:00
|
|
|
}
|
|
|
|
|
2018-11-12 20:59:21 +00:00
|
|
|
const marginTop = convertPrintParameterToInches(margin.top) || 0;
|
|
|
|
const marginLeft = convertPrintParameterToInches(margin.left) || 0;
|
|
|
|
const marginBottom = convertPrintParameterToInches(margin.bottom) || 0;
|
|
|
|
const marginRight = convertPrintParameterToInches(margin.right) || 0;
|
2017-06-21 20:51:06 +00:00
|
|
|
|
2021-03-18 19:57:32 +00:00
|
|
|
if (omitBackground) {
|
2022-06-13 09:16:25 +00:00
|
|
|
await this.#setTransparentBackgroundColor();
|
2021-03-18 19:57:32 +00:00
|
|
|
}
|
|
|
|
|
2022-06-13 09:16:25 +00:00
|
|
|
const printCommandPromise = this.#client.send('Page.printToPDF', {
|
2019-06-15 05:36:06 +00:00
|
|
|
transferMode: 'ReturnAsStream',
|
2018-11-12 20:59:21 +00:00
|
|
|
landscape,
|
|
|
|
displayHeaderFooter,
|
|
|
|
headerTemplate,
|
|
|
|
footerTemplate,
|
|
|
|
printBackground,
|
|
|
|
scale,
|
|
|
|
paperWidth,
|
|
|
|
paperHeight,
|
|
|
|
marginTop,
|
|
|
|
marginBottom,
|
|
|
|
marginLeft,
|
|
|
|
marginRight,
|
|
|
|
pageRanges,
|
2020-05-07 10:54:55 +00:00
|
|
|
preferCSSPageSize,
|
2017-06-21 20:51:06 +00:00
|
|
|
});
|
2021-03-18 19:57:32 +00:00
|
|
|
|
2022-06-14 11:16:21 +00:00
|
|
|
const result = await waitWithTimeout(
|
2021-09-14 16:11:14 +00:00
|
|
|
printCommandPromise,
|
|
|
|
'Page.printToPDF',
|
|
|
|
timeout
|
|
|
|
);
|
|
|
|
|
2021-03-18 19:57:32 +00:00
|
|
|
if (omitBackground) {
|
2022-06-13 09:16:25 +00:00
|
|
|
await this.#resetDefaultBackgroundColor();
|
2021-03-18 19:57:32 +00:00
|
|
|
}
|
|
|
|
|
2022-05-31 14:34:16 +00:00
|
|
|
assert(result.stream, '`stream` is missing from `Page.printToPDF');
|
2022-06-14 11:16:21 +00:00
|
|
|
return getReadableFromProtocolStream(this.#client, result.stream);
|
2021-06-23 12:51:38 +00:00
|
|
|
}
|
|
|
|
|
2022-09-21 06:10:50 +00:00
|
|
|
override async pdf(options: PDFOptions = {}): Promise<Buffer> {
|
2022-06-22 13:25:44 +00:00
|
|
|
const {path = undefined} = options;
|
2021-06-23 12:51:38 +00:00
|
|
|
const readable = await this.createPDFStream(options);
|
2022-06-14 11:16:21 +00:00
|
|
|
const buffer = await getReadableAsBuffer(readable, path);
|
2022-05-31 14:34:16 +00:00
|
|
|
assert(buffer, 'Could not create buffer');
|
|
|
|
return buffer;
|
2017-06-21 20:51:06 +00:00
|
|
|
}
|
|
|
|
|
2022-09-21 06:10:50 +00:00
|
|
|
override async title(): Promise<string> {
|
2017-07-25 18:37:46 +00:00
|
|
|
return this.mainFrame().title();
|
2017-06-21 20:51:06 +00:00
|
|
|
}
|
2017-05-11 07:06:41 +00:00
|
|
|
|
2022-09-21 06:10:50 +00:00
|
|
|
override async close(
|
2022-06-22 13:25:44 +00:00
|
|
|
options: {runBeforeUnload?: boolean} = {runBeforeUnload: undefined}
|
2020-05-07 10:54:55 +00:00
|
|
|
): Promise<void> {
|
2022-06-13 09:16:25 +00:00
|
|
|
const connection = this.#client.connection();
|
2020-05-07 10:54:55 +00:00
|
|
|
assert(
|
2022-06-13 09:16:25 +00:00
|
|
|
connection,
|
2020-05-07 10:54:55 +00:00
|
|
|
'Protocol error: Connection closed. Most likely the page has been closed.'
|
|
|
|
);
|
2018-05-02 22:51:45 +00:00
|
|
|
const runBeforeUnload = !!options.runBeforeUnload;
|
|
|
|
if (runBeforeUnload) {
|
2022-06-13 09:16:25 +00:00
|
|
|
await this.#client.send('Page.close');
|
2018-05-02 22:51:45 +00:00
|
|
|
} else {
|
2022-06-13 09:16:25 +00:00
|
|
|
await connection.send('Target.closeTarget', {
|
|
|
|
targetId: this.#target._targetId,
|
2020-05-07 10:54:55 +00:00
|
|
|
});
|
2022-06-13 09:16:25 +00:00
|
|
|
await this.#target._isClosedPromise;
|
2018-05-02 22:51:45 +00:00
|
|
|
}
|
2017-06-21 20:51:06 +00:00
|
|
|
}
|
2017-06-28 01:27:22 +00:00
|
|
|
|
2022-09-21 06:10:50 +00:00
|
|
|
override isClosed(): boolean {
|
2022-06-13 09:16:25 +00:00
|
|
|
return this.#closed;
|
2018-05-25 23:53:57 +00:00
|
|
|
}
|
|
|
|
|
2022-09-21 06:10:50 +00:00
|
|
|
override get mouse(): Mouse {
|
2022-06-13 09:16:25 +00:00
|
|
|
return this.#mouse;
|
2017-07-22 03:29:31 +00:00
|
|
|
}
|
|
|
|
|
2022-09-21 06:10:50 +00:00
|
|
|
override click(
|
2020-05-07 10:54:55 +00:00
|
|
|
selector: string,
|
|
|
|
options: {
|
|
|
|
delay?: number;
|
2020-07-02 11:15:39 +00:00
|
|
|
button?: MouseButton;
|
2020-05-07 10:54:55 +00:00
|
|
|
clickCount?: number;
|
|
|
|
} = {}
|
|
|
|
): Promise<void> {
|
2018-02-05 22:58:03 +00:00
|
|
|
return this.mainFrame().click(selector, options);
|
2017-07-22 03:29:31 +00:00
|
|
|
}
|
|
|
|
|
2022-09-21 06:10:50 +00:00
|
|
|
override focus(selector: string): Promise<void> {
|
2018-02-05 22:58:03 +00:00
|
|
|
return this.mainFrame().focus(selector);
|
2017-06-28 01:27:22 +00:00
|
|
|
}
|
|
|
|
|
2022-09-21 06:10:50 +00:00
|
|
|
override hover(selector: string): Promise<void> {
|
2018-02-05 22:58:03 +00:00
|
|
|
return this.mainFrame().hover(selector);
|
2017-06-28 01:27:22 +00:00
|
|
|
}
|
|
|
|
|
2022-09-21 06:10:50 +00:00
|
|
|
override select(selector: string, ...values: string[]): Promise<string[]> {
|
2017-11-02 05:06:04 +00:00
|
|
|
return this.mainFrame().select(selector, ...values);
|
2017-09-25 09:23:34 +00:00
|
|
|
}
|
|
|
|
|
2022-09-21 06:10:50 +00:00
|
|
|
override tap(selector: string): Promise<void> {
|
2018-02-05 22:58:03 +00:00
|
|
|
return this.mainFrame().tap(selector);
|
|
|
|
}
|
|
|
|
|
2022-09-21 06:10:50 +00:00
|
|
|
override type(
|
2020-05-07 10:54:55 +00:00
|
|
|
selector: string,
|
|
|
|
text: string,
|
2022-06-22 13:25:44 +00:00
|
|
|
options?: {delay: number}
|
2020-05-07 10:54:55 +00:00
|
|
|
): Promise<void> {
|
2018-02-05 22:58:03 +00:00
|
|
|
return this.mainFrame().type(selector, text, options);
|
2017-07-19 21:43:07 +00:00
|
|
|
}
|
|
|
|
|
2022-09-21 06:10:50 +00:00
|
|
|
override waitForTimeout(milliseconds: number): Promise<void> {
|
2020-07-28 08:37:49 +00:00
|
|
|
return this.mainFrame().waitForTimeout(milliseconds);
|
|
|
|
}
|
|
|
|
|
2022-09-21 06:10:50 +00:00
|
|
|
override async waitForSelector<Selector extends string>(
|
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
|
|
|
selector: Selector,
|
2022-08-26 10:55:30 +00:00
|
|
|
options: WaitForSelectorOptions = {}
|
2022-07-06 07:05:37 +00:00
|
|
|
): Promise<ElementHandle<NodeFor<Selector>> | null> {
|
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 this.mainFrame().waitForSelector(selector, options);
|
2017-07-07 22:39:02 +00:00
|
|
|
}
|
2017-07-10 18:21:46 +00:00
|
|
|
|
2022-09-21 06:10:50 +00:00
|
|
|
override waitForXPath(
|
2020-05-07 10:54:55 +00:00
|
|
|
xpath: string,
|
|
|
|
options: {
|
|
|
|
visible?: boolean;
|
|
|
|
hidden?: boolean;
|
|
|
|
timeout?: number;
|
|
|
|
} = {}
|
2022-07-06 07:05:37 +00:00
|
|
|
): Promise<ElementHandle<Node> | null> {
|
2018-01-22 23:16:20 +00:00
|
|
|
return this.mainFrame().waitForXPath(xpath, options);
|
|
|
|
}
|
|
|
|
|
2022-09-21 06:10:50 +00:00
|
|
|
override waitForFunction<
|
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
|
|
|
Params extends unknown[],
|
|
|
|
Func extends EvaluateFunc<Params> = EvaluateFunc<Params>
|
|
|
|
>(
|
|
|
|
pageFunction: Func | string,
|
2022-09-15 06:22:20 +00:00
|
|
|
options: FrameWaitForFunctionOptions = {},
|
2022-06-24 06:40:08 +00:00
|
|
|
...args: Params
|
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
|
|
|
): Promise<HandleFor<Awaited<ReturnType<Func>>>> {
|
2017-07-27 22:17:43 +00:00
|
|
|
return this.mainFrame().waitForFunction(pageFunction, options, ...args);
|
|
|
|
}
|
2017-05-11 07:06:41 +00:00
|
|
|
}
|
|
|
|
|
2020-05-05 12:53:22 +00:00
|
|
|
const supportedMetrics = new Set<string>([
|
2017-10-10 21:50:38 +00:00
|
|
|
'Timestamp',
|
2017-10-12 08:17:06 +00:00
|
|
|
'Documents',
|
|
|
|
'Frames',
|
|
|
|
'JSEventListeners',
|
|
|
|
'Nodes',
|
2017-10-10 21:50:38 +00:00
|
|
|
'LayoutCount',
|
|
|
|
'RecalcStyleCount',
|
|
|
|
'LayoutDuration',
|
|
|
|
'RecalcStyleDuration',
|
|
|
|
'ScriptDuration',
|
|
|
|
'TaskDuration',
|
|
|
|
'JSHeapUsedSize',
|
|
|
|
'JSHeapTotalSize',
|
|
|
|
]);
|
|
|
|
|
2017-08-21 23:39:04 +00:00
|
|
|
const unitToPixels = {
|
2020-05-07 10:54:55 +00:00
|
|
|
px: 1,
|
|
|
|
in: 96,
|
|
|
|
cm: 37.8,
|
|
|
|
mm: 3.78,
|
2017-05-14 18:29:42 +00:00
|
|
|
};
|
|
|
|
|
2020-05-07 10:54:55 +00:00
|
|
|
function convertPrintParameterToInches(
|
|
|
|
parameter?: string | number
|
|
|
|
): number | undefined {
|
2022-06-14 11:55:35 +00:00
|
|
|
if (typeof parameter === 'undefined') {
|
|
|
|
return undefined;
|
|
|
|
}
|
2017-06-22 20:38:10 +00:00
|
|
|
let pixels;
|
2022-06-14 11:16:21 +00:00
|
|
|
if (isNumber(parameter)) {
|
2017-06-21 20:51:06 +00:00
|
|
|
// Treat numbers as pixel values to be aligned with phantom's paperSize.
|
2022-06-01 09:11:09 +00:00
|
|
|
pixels = parameter;
|
2022-06-14 11:16:21 +00:00
|
|
|
} else if (isString(parameter)) {
|
2022-06-01 09:11:09 +00:00
|
|
|
const text = parameter;
|
2017-06-22 20:38:10 +00:00
|
|
|
let unit = text.substring(text.length - 2).toLowerCase();
|
|
|
|
let valueText = '';
|
2022-05-31 14:34:16 +00:00
|
|
|
if (unit in unitToPixels) {
|
2017-06-21 20:51:06 +00:00
|
|
|
valueText = text.substring(0, text.length - 2);
|
2017-05-14 18:29:42 +00:00
|
|
|
} else {
|
2017-06-21 20:51:06 +00:00
|
|
|
// In case of unknown unit try to parse the whole parameter as number of pixels.
|
|
|
|
// This is consistent with phantom's paperSize behavior.
|
|
|
|
unit = 'px';
|
|
|
|
valueText = text;
|
|
|
|
}
|
2017-08-21 23:39:04 +00:00
|
|
|
const value = Number(valueText);
|
2018-05-31 23:53:51 +00:00
|
|
|
assert(!isNaN(value), 'Failed to parse parameter value: ' + text);
|
2022-05-31 14:34:16 +00:00
|
|
|
pixels = value * unitToPixels[unit as keyof typeof unitToPixels];
|
2017-06-21 20:51:06 +00:00
|
|
|
} else {
|
2020-05-07 10:54:55 +00:00
|
|
|
throw new Error(
|
|
|
|
'page.pdf() Cannot handle parameter type: ' + typeof parameter
|
|
|
|
);
|
2017-06-21 20:51:06 +00:00
|
|
|
}
|
|
|
|
return pixels / 96;
|
2017-06-11 08:32:59 +00:00
|
|
|
}
|