|
5 | 5 | */ |
6 | 6 |
|
7 | 7 | import {logger} from './logger.js'; |
8 | | -import type {Page, Protocol, CdpPage} from './third_party/index.js'; |
| 8 | +import type {Page, Protocol, CdpPage, Dialog} from './third_party/index.js'; |
9 | 9 | import type {PredefinedNetworkConditions} from './third_party/index.js'; |
10 | 10 |
|
11 | 11 | export class WaitForHelper { |
@@ -126,17 +126,16 @@ export class WaitForHelper { |
126 | 126 |
|
127 | 127 | async waitForEventsAfterAction( |
128 | 128 | action: () => Promise<unknown>, |
129 | | - options?: {timeout?: number; dialog?: 'accept' | 'dismiss'}, |
| 129 | + options?: {timeout?: number; handleDialog?: boolean}, |
130 | 130 | ): Promise<void> { |
131 | | - const dialogHandler = (dialog: any) => { |
132 | | - if (options?.dialog === 'dismiss') { |
133 | | - void dialog.dismiss(); |
134 | | - } else { |
| 131 | + if (options?.handleDialog) { |
| 132 | + const dialogHandler = (dialog: Pick<Dialog, 'accept'>) => { |
135 | 133 | void dialog.accept(); |
136 | | - } |
137 | | - }; |
138 | | - if (options?.dialog) { |
| 134 | + }; |
139 | 135 | this.#page.on('dialog', dialogHandler); |
| 136 | + this.#abortController.signal.addEventListener('abort', () => { |
| 137 | + this.#page.off('dialog', dialogHandler); |
| 138 | + }); |
140 | 139 | } |
141 | 140 |
|
142 | 141 | const navigationFinished = this.waitForNavigationStarted() |
@@ -169,9 +168,6 @@ export class WaitForHelper { |
169 | 168 | logger(error); |
170 | 169 | } finally { |
171 | 170 | this.#abortController.abort(); |
172 | | - if (options?.dialog) { |
173 | | - this.#page.off('dialog', dialogHandler); |
174 | | - } |
175 | 171 | } |
176 | 172 | } |
177 | 173 | } |
|
0 commit comments