mirror of
https://github.com/puppeteer/puppeteer
synced 2024-06-14 14:02:48 +00:00
parent
b4f5ea1167
commit
053d960fb5
@ -763,22 +763,25 @@ export class Page extends EventEmitter {
|
|||||||
* await fileChooser.accept(['/tmp/myfile.pdf']);
|
* await fileChooser.accept(['/tmp/myfile.pdf']);
|
||||||
* ```
|
* ```
|
||||||
*/
|
*/
|
||||||
async waitForFileChooser(
|
waitForFileChooser(options: WaitTimeoutOptions = {}): Promise<FileChooser> {
|
||||||
options: WaitTimeoutOptions = {}
|
const needsEnable = this.#fileChooserPromises.size === 0;
|
||||||
): Promise<FileChooser> {
|
|
||||||
if (!this.#fileChooserPromises.size) {
|
|
||||||
await this.#client.send('Page.setInterceptFileChooserDialog', {
|
|
||||||
enabled: true,
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
const {timeout = this.#timeoutSettings.timeout()} = options;
|
const {timeout = this.#timeoutSettings.timeout()} = options;
|
||||||
const promise = createDeferredPromise<FileChooser>({
|
const promise = createDeferredPromise<FileChooser>({
|
||||||
message: `Waiting for \`FileChooser\` failed: ${timeout}ms exceeded`,
|
message: `Waiting for \`FileChooser\` failed: ${timeout}ms exceeded`,
|
||||||
timeout,
|
timeout,
|
||||||
});
|
});
|
||||||
this.#fileChooserPromises.add(promise);
|
this.#fileChooserPromises.add(promise);
|
||||||
return promise.catch(error => {
|
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);
|
this.#fileChooserPromises.delete(promise);
|
||||||
throw error;
|
throw error;
|
||||||
});
|
});
|
||||||
|
Loading…
Reference in New Issue
Block a user