fix: work around a race in waitForFileChooser (#8905)

Closes #6040
This commit is contained in:
Alex Rudenko 2022-09-06 17:03:56 +02:00 committed by GitHub
parent b4f5ea1167
commit 053d960fb5
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -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;
}); });