import { VSBrowser , Notification , Workbench , NotificationType , } from "vscode-extension-tester" ; import { delay } from "../helper" ; const WAIT_NOTIFICATION_TIMEOUT = 2000 ; export class NotificationPageObject { private workbench : Workbench ; center : any ; constructor ( workbench ) { this . workbench = workbench ; ( async ( ) => { this . center = await this . workbench . openNotificationsCenter ( ) ; } ) ( ) ; } private async getNotifications ( type = NotificationType . Any ) { const notifications = [ ] ; for await ( const notification of await this . waitNotifications ( ) ) { if ( type == NotificationType . Any || ( await notification . getType ( ) ) == type ) { notifications . push ( notification ) ; } } return notifications ; } async getNotification ( targetText , type , _wait = 1000 ) { const target = new RegExp ( targetText , "i" ) ; let steps = _wait / 500 ; let result = undefined ; while ( result === undefined && steps > 0 ) { const notifications = await this . getNotifications ( type ) ; await delay ( 500 ) ; notifications . reverse ( ) . forEach ( async ( element ) => { if ( ! result ) { const message = await element . getMessage ( ) ; if ( target . exec ( message ) ) { result = element ; } } } ) ; steps -- ; } return result ; } private async waitNotifications ( delay = WAIT_NOTIFICATION_TIMEOUT ) { return await this . center . getNotifications ( NotificationType . Any ) ; } }