import { AnswersQuestions , Interaction , UsesAbilities } from '@serenity-js/core' ; import { BrowseTheWeb } from '../abilities' ; desc Instructs the  {@link @serenity-js/core/lib/screenplay/actor~Actor} to
 *  close browser tabs or windows.
 *
 *  example example example example see Switch export class Close { desc Closes any windows other than the original one that
     *  the  {@link @serenity-js/core/lib/screenplay/actor~Actor} *  has  Navigate d to.
     *
     *  When the windows are closed, it switches the context
     *  back to the original window.
     *
     *  static returns { link serenity-js see Switch static anyNewWindows ( ) { return new CloseWindowsOtherThan ( actor => BrowseTheWeb . as ( actor ) . getOriginalWindowHandle ( ) , `#actor closes any new windows` ) ; } desc static returns { link serenity-js see Switch static currentWindow ( ) { return new CloseCurrentWindow ( ) ; } } package class CloseWindowsOtherThan extends Interaction { constructor ( private readonly windowToKeepBy , private readonly description = `#actor closes several windows` ) { super ( ) ; } performAs ( actor ) { return this . windowToKeepBy ( actor ) . then ( windowToKeep => this . windowsOtherThan ( windowToKeep , actor ) . then ( windowsToClose => this . closeAll ( windowsToClose , actor ) ) . then ( ( ) => this . switchTo ( windowToKeep , actor ) ) ) ; } toString ( ) { return this . description ; } param { string } windowToKeep param { UsesAbilities & AnswersQuestions } actor private private windowsOtherThan ( windowToKeep , actor ) { return BrowseTheWeb . as ( actor ) . getAllWindowHandles ( ) . then ( allWindows => this . isDefined ( windowToKeep ) && allWindows . length > 1 ? allWindows . filter ( handle => handle !== windowToKeep ) : [ ] ) ; } param { string [ ] } windows param { UsesAbilities & AnswersQuestions } actor private private closeAll ( windows , actor ) { return windows . reduce ( ( previous , handle ) => { return previous . then ( ( ) => BrowseTheWeb . as ( actor ) . switchToWindow ( handle ) ) . then ( ( ) => BrowseTheWeb . as ( actor ) . closeCurrentWindow ( ) ) ; } , Promise . resolve ( ) ) ; } param { string } window param { UsesAbilities & AnswersQuestions } actor private private switchTo ( window , actor ) { return this . isDefined ( window ) ? BrowseTheWeb . as ( actor ) . switchToWindow ( window ) : Promise . resolve ( ) ; } param { any } value private private isDefined ( value ) { return value !== undefined && value !== null ; } } package class CloseCurrentWindow extends Interaction { performAs ( actor ) { return BrowseTheWeb . as ( actor ) . closeCurrentWindow ( ) ; } toString ( ) { return `#actor closes current browser window` ; } }