import { Activity , Answerable , AnswersQuestions , Interaction , LogicError , PerformsActivities , Task , UsesAbilities } from '@serenity-js/core' ; import { ElementFinder } from 'protractor' ; import { WebElementPromise } from 'selenium-webdriver' ; import { BrowseTheWeb } from '../abilities' ; desc Instructs the  {@link @serenity-js/core/lib/screenplay/actor~Actor} *  to switch to a different [frame](https://developer.mozilla.org/en-US/docs/Web/HTML/Element/frame),
 *  [inline frame](https://developer.mozilla.org/en-US/docs/Web/HTML/Element/iframe),
 *  or browser window/tab.
 *
 *  example example example example example see Close see BrowseTheWeb export class Switch { desc param { Answerable < ElementFinder | number | string > } targetOrIndex returns { SwitchToFrame } see Switch . toParentFrame see Switch . toDefaultContent see Target static toFrame ( targetOrIndex ) { return new SwitchToFrame ( targetOrIndex ) ; } desc returns { serenity-js see Switch . toFrame see https see https static toParentFrame ( ) { return new SwitchToParentFrame ( ) ; } desc returns { serenity-js see Switch . toFrame static toDefaultContent ( ) { return new SwitchToDefaultContent ( ) ; } desc param { Answerable < string | number > } nameOrHandleOrIndex returns { SwitchToWindow } static toWindow ( nameOrHandleOrIndex ) { return new SwitchToWindow ( nameOrHandleOrIndex ) ; } desc returns { SwitchToNewWindow } static toNewWindow ( ) { return new SwitchToNewWindow ( ) ; } desc Switches the current [browsing context](https://w3c.github.io/webdriver/#dfn-current-browsing-context)
     *  for future commands to the original window used when the  {@link @serenity-js/core/lib/screenplay/actor~Actor} *  performed an interaction to  Navigate .
     *
     *  **Please note** that this behaviour might vary in some browsers if there are more than two windows opened
     *  at the same time, as window handles might be ordered alphabetically instead of the order in which they were created.
     *
     *  returns { SwitchToOriginalWindow } static toOriginalWindow ( ) { return new SwitchToOriginalWindow ( ) ; } } package class SwitchToFrame extends Interaction { constructor ( private readonly targetOrIndex ) { super ( ) ; } and ( ... activities ) { return new SwitchToFrameAndPerformActivities ( this . targetOrIndex , activities ) ; } performAs ( actor ) { return actor . answer ( this . targetOrIndex ) . then ( ( targetOrIndex ) => BrowseTheWeb . as ( actor ) . switchToFrame ( targetOrIndex instanceof ElementFinder ? targetOrIndex . getWebElement ( ) as unknown as WebElementPromise : targetOrIndex ) ) ; } toString ( ) { return `#actor switches to frame: ${ this . targetOrIndex }` ; } } package class SwitchToFrameAndPerformActivities extends Task { constructor ( private readonly targetOrIndex , private readonly activities ) { super ( ) ; } performAs ( actor ) { return actor . attemptsTo ( new SwitchToFrame ( this . targetOrIndex ) , ... this . activities , new SwitchToParentFrame ( ) ) ; } toString ( ) { return `#actor switches to frame: ${ this . targetOrIndex }` ; } } package class SwitchToParentFrame extends Interaction { performAs ( actor ) { return BrowseTheWeb . as ( actor ) . switchToParentFrame ( ) ; } toString ( ) { return `#actor switches to parent frame` ; } } package class SwitchToDefaultContent extends Interaction { performAs ( actor ) { return BrowseTheWeb . as ( actor ) . switchToDefaultContent ( ) ; } toString ( ) { return `#actor switches to default content` ; } } package class SwitchToWindow extends Interaction { constructor ( private readonly nameOrHandleOrIndex ) { super ( ) ; } and ( ... activities ) { return new SwitchToWindowAndPerformActivities ( this . nameOrHandleOrIndex , activities ) ; } performAs ( actor ) { return actor . answer ( this . nameOrHandleOrIndex ) . then ( index => BrowseTheWeb . as ( actor ) . switchToWindow ( index ) ) ; } toString ( ) { return `#actor switches to window: ${ this . nameOrHandleOrIndex }` ; } } package class SwitchToWindowAndPerformActivities extends Task { constructor ( private readonly nameOrHandleOrIndex , private readonly activities ) { super ( ) ; } performAs ( actor ) { return BrowseTheWeb . as ( actor ) . getCurrentWindowHandle ( ) . then ( currentWindow => { return actor . attemptsTo ( new SwitchToWindow ( this . nameOrHandleOrIndex ) , ... this . activities , new SwitchToWindow ( currentWindow ) ) ; } ) ; } toString ( ) { return `#actor switches to window: ${ this . nameOrHandleOrIndex }` ; } } package class SwitchToNewWindow extends Interaction { constructor ( ) { super ( ) ; } and ( ... activities ) { return new SwitchToNewWindowAndPerformActivities ( activities ) ; } performAs ( actor ) { return Promise . all ( [ BrowseTheWeb . as ( actor ) . getCurrentWindowHandle ( ) , BrowseTheWeb . as ( actor ) . getAllWindowHandles ( ) , ] ) . then ( ( [ currentHandle , handles ] ) => handles . filter ( handle => handle !== currentHandle ) ) . then ( handles => { if ( handles . length === 0 ) { throw new LogicError ( `No new window has been opened to switch to` ) ; } return BrowseTheWeb . as ( actor ) . switchToWindow ( handles [ handles . length - 1 ] ) ; } ) ; } toString ( ) { return `#actor switches to the new browser window` ; } } package class SwitchToNewWindowAndPerformActivities extends Task { constructor ( private readonly activities ) { super ( ) ; } performAs ( actor ) { return BrowseTheWeb . as ( actor ) . getCurrentWindowHandle ( ) . then ( currentWindow => { return actor . attemptsTo ( new SwitchToNewWindow ( ) , ... this . activities , new SwitchToWindow ( currentWindow ) ) ; } ) ; } toString ( ) { return `#actor switches to the new window` ; } } package class SwitchToOriginalWindow extends Interaction { constructor ( ) { super ( ) ; } performAs ( actor ) { return BrowseTheWeb . as ( actor ) . switchToOriginalWindow ( ) ; } toString ( ) { return `#actor switches back to the original browser window` ; } }