import {Switch} from '@serenity-js/protractor/lib/screenplay/interactions'
public class | source

Switch

Instructs the Actor to switch to a different frame, inline frame, or browser window/tab.

Examples:

Lean Page Object describing a login form, embedded in an iframe

 import { Target } from '@serenity-js/protractor';
 import { by } from 'protractor';

 class LoginForm {
     static iframe           = Target.the('login form').located(by.tagName('iframe'));
     static usernameField    = Target.the('username field').located(by.css('[data-test="username"]'));
     static passwordField    = Target.the('password field').located(by.css('[data-test="password"]'));
     static submitButton     = Target.the('submit button').located(by.css(`button[type='submit']`));
 }
Switch to an iframe and back

 import { actorCalled } from '@serenity-js/core';
 import { BrowseTheWeb, Switch, Enter, Click } from '@serenity-js/protractor';
 import { protractor } from 'protractor';

 actorCalled('Francesca')
     .whoCan(BrowseTheWeb.using(protractor.browser))
     .attemptsTo(
         Switch.toFrame(LoginForm.iframe),

         Enter.theValue('francesca@example.org').into(LoginForm.usernameField),
         Enter.theValue('correct-horse-battery-staple').into(LoginForm.passwordField),
         Click.on(LoginForm.submitButton),

         Switch.toParentFrame(),
     );
Perform activities in the context of an iframe

 import { actorCalled } from '@serenity-js/core';
 import { BrowseTheWeb, Switch, Enter, Click } from '@serenity-js/protractor';
 import { protractor } from 'protractor';

 actorCalled('Francesca')
     .whoCan(BrowseTheWeb.using(protractor.browser))
     .attemptsTo(
         Switch.toFrame(LoginForm.iframe).and(
             Enter.theValue('francesca@example.org').into(LoginForm.usernameField),
             Enter.theValue('correct-horse-battery-staple').into(LoginForm.passwordField),
             Click.on(LoginForm.submitButton),
         ),
         // Note that Switch.toParentFrame() is invoked automatically
     );
Switch to a new window/tab and back

 import { actorCalled } from '@serenity-js/core';
 import { BrowseTheWeb, Switch, Close } from '@serenity-js/protractor';
 import { protractor } from 'protractor';

 actorCalled('Francesca')
     .whoCan(BrowseTheWeb.using(protractor.browser))
     .attemptsTo(
         Switch.toNewWindow(), // or: Switch.toWindow(...)

         // perform some activities in the context of the new window

         Close.currentWindow(),

         Switch.toOriginalWindow(),
     );
Perform activities in the context of a different window/tab

 import { actorCalled } from '@serenity-js/core';
 import { BrowseTheWeb, Switch, Close } from '@serenity-js/protractor';
 import { protractor } from 'protractor';

 actorCalled('Francesca')
     .whoCan(BrowseTheWeb.using(protractor.browser))
     .attemptsTo(
         Switch.toNewWindow().and(
             // perform some activities in the context of the new window

             Close.currentWindow()
         ),

         // Note that Switch.toOriginalWindow() is invoked automatically
     );

Tests:

Static Method Summary

Static Public Methods
public static

Switches the current browsing context for future commands to the first frame on the page, or the main document when a page contains iframes.

public static

toFrame(targetOrIndex: Answerable<ElementFinder|number|string>): SwitchToFrame

Switches the current browsing context for future commands to a frame or an inline frame identified by its name, index or Question<ElementFinder>.

public static

toNewWindow(): SwitchToWindow

Switches the current browsing context for future commands to the most recently opened browser tab/window.

public static

Switches the current browsing context for future commands to the original window used when the Actor performed an interaction to Navigate.

public static

Sets the current browsing context for future commands to the parent of the current browsing context, i.e.

public static

toWindow(nameOrHandleOrIndex: Answerable<string|number>): SwitchToWindow

Switches the current browsing context for future commands to a browser tab/window identified by its name, index or window handle.

Static Public Methods

public static toDefaultContent(): Interaction source

Switches the current browsing context for future commands to the first frame on the page, or the main document when a page contains iframes.

Returns:

Interaction

public static toNewWindow(): SwitchToWindow source

Switches the current browsing context for future commands to the most recently opened browser tab/window.

Please note that this behaviour might vary in some browsers if there are more than two windows opened at the same time.

Returns:

SwitchToWindow

public static toOriginalWindow(): Interaction source

Switches the current browsing context for future commands to the original window used when the 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:

Interaction

public static toWindow(nameOrHandleOrIndex: Answerable<string|number>): SwitchToWindow source

Switches the current browsing context for future commands to a browser tab/window identified by its name, index or window handle.

Params:

NameTypeAttributeDescription
nameOrHandleOrIndex Answerable<string|number>

Returns:

SwitchToWindow