Switch
Instructs the Actor to switch to a different frame, inline frame, or browser window/tab.
Examples:
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']`));
}
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(),
);
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
);
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(),
);
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:
See also:
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 |
|
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 |
|
public static |
toNewWindow(): SwitchToNewWindow Switches the current browsing context for future commands to the most recently opened browser tab/window. |
|
public static |
toOriginalWindow(): SwitchToOriginalWindow 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 iframe
s.
Tests:
See:
public static toFrame(targetOrIndex: Answerable<ElementFinder|number|string>): SwitchToFrame source
Switches the current browsing context
for future commands to a frame
or an inline frame
identified by its name, index or Question<ElementFinder>
.
Params:
Name | Type | Attribute | Description |
targetOrIndex | Answerable<ElementFinder|number|string> |
Returns:
SwitchToFrame |
Tests:
- Switch when working with frames toFrame()
- Switch when working with frames toFrame() should switch to an iframe identified by Question
- Switch when working with frames toFrame() should switch to an iframe identified by index
- Switch when working with frames toFrame() should switch to an iframe identified by name
- Switch when working with frames toFrame().and() should perform any activities in the context of the frame it switched to
public static toNewWindow(): SwitchToNewWindow 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:
SwitchToNewWindow |
public static toOriginalWindow(): SwitchToOriginalWindow 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:
SwitchToOriginalWindow |
public static toParentFrame(): Interaction source
Sets the current browsing context
for future commands to the parent of the current browsing context,
i.e. an iframe
in which the current iframe
is nested.
If the current context is the top-level browsing context, the context remains unchanged.
Tests:
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:
Name | Type | Attribute | Description |
nameOrHandleOrIndex | Answerable<string|number> |
Returns:
SwitchToWindow |
Tests:
- Switch when working with windows toWindow() should switch to a window identified by its index
- Switch when working with windows toWindow() should switch to a window identified by its name
- Switch when working with windows toWindow()
- Switch when working with windows toWindow().and() should perform any activities in the context of the window it switched to