Skip to main content

externalSwitch

Instructs an actor who has the ability to BrowseTheWeb to switch the context for future activities to a Switchable, such as a Page or a PageElement.

Please note that when the PageElement implementing Switchable represents an iframe, using Switch will result in switching the top-level browsing context to that iframe.

When the PageElement represents any other HTMLElement, using Switch sets HTMLElement#focus on the specified element. Assuming it can be focused.

Note: The focused element is the element which will receive keyboard press events by default.

Perform activities in the context of an iframe

import { actorCalled } from '@serenity-js/core'
import { By, Click, Enter, PageElement, Switch } from '@serenity-js/web'

// Lean Page Object describing a login form, embedded in an iframe
class LoginForm {
static iframe = () =>
PageElement.located(By.css('iframe'))
.describedAs('login form')

static usernameField = () =>
PageElement.located(By.css('[data-testid="username"]'))
.describedAs('username field')

static passwordField = () =>
PageElement.located(By.css('[data-testid="password"]'))
.describedAs('password field')

static submitButton = () =>
PageElement.located(By.css('button[type="submit"]'))
.describedAs('submit button')
}

await actorCalled('Francesca')
.attemptsTo(
Switch.to(LoginForm.iframe).and(
Enter.theValue('francesca@example.org').into(LoginForm.usernameField()),
Enter.theValue('correct-horse-battery-staple').into(LoginForm.passwordField()),
Click.on(LoginForm.submitButton()),
)
)

Perform activities in the context of another page

import { actorCalled } from '@serenity-js/core'
import { Click, Enter, Switch } from '@serenity-js/web'
import { browser } from '@wdio/globals'

await actorCalled('Francesca')
.whoCan(BrowseTheWebWithWebdriverIO.using(browser))
.attemptsTo(
Switch.to(Page.whichName(startsWith('popup'))).and(
// perform some activities in the context of the new window

// optionally, close the browser tab
Page.current().close(),
),

// Note that switching back to the original page happens automatically
// after the last activity from the list finishes
)

Perform activities in the context of a focused page element

import { Ensure, equals } from '@serenity-js/assertions'
import { actorCalled } from '@serenity-js/core'
import { Key, PageElement, Press, Switch, Value } from '@serenity-js/web'
import { browser } from '@wdio/globals'

const inputField = () =>
PageElement.located(By.css('input'));

await actorCalled('Francesca')
.whoCan(BrowseTheWebWithWebdriverIO.using(browser))
.attemptsTo(
Switch.to(inputField()).and(
Press.the('h', 'e', 'l', 'l', 'o'),
Press.the(Key.Tab),
),
Ensure.that(Value.of(inputField()), equals('hello'))
)

Learn more

Hierarchy

Index

Methods

staticexternalto

externalinstantiationLocation

  • instantiationLocation(): FileSystemLocation
  • Returns the location where this Activity was instantiated.


    Returns FileSystemLocation

externaldescribedBy

  • Resolves the description of this object in the context of the provided actor.


    Parameters

    Returns Promise<string>

externaltoString

  • toString(): string
  • Returns a human-readable description of this object.


    Returns string

externaland

  • Instructs the Actor to switch the context for future activities to a Switchable, such as a Page or a PageElement, perform a sequence of activities, and then switch the context back.


    Parameters

    • externalrest...activities: Activity[]

      A sequence of activities to perform

    Returns Task

externalperformAs