import { Condition } from '../Condition' ; import { Page , Dialog } from 'playwright' ; import { clearTimeout , setTimeout } from 'timers' ; const isDialog = ( thing ) : thing is Dialog => { return typeof thing ?. message == 'function' ; } ; /**
 * TODO: Generalize waitFor such that we can avoid needing waitForEvent custom
 * handler.
 */ export class DialogCondition extends Condition { toString ( ) { return 'waiting for dialog to appear' ; } hasWaitFor = false ; public async waitFor ( ) { return true ; } public async waitForEvent ( page ) { return new Promise < Dialog > ( ( yeah , nah ) => { const timeout = setTimeout ( nah , Number ( this . timeout ) ) ; page . once ( 'dialog' , ( dialog ) => { if ( isDialog ( dialog ) ) { clearTimeout ( timeout ) ; yeah ( dialog ) ; } } ) ; } ) . catch ( ( ) => null ) ; } }