Skip to main content

Press

Instructs an actor who has the ability to BrowseTheWeb to send a key press or a sequence of keys to a Web element.

Note: On macOS, some keyboard shortcuts might not work with the devtools protocol.

For example:

Example widget

<form>
<input type="text" name="example" id="example" />
</form>

Lean Page Object describing the widget

import { By, PageElement } from '@serenity-js/web'

class Form {
static exampleInput = () =>
PageElement.located(By.id('example'))
.describedAs('example input')
}

Pressing keys

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

await actorCalled('Priyanka')
.attemptsTo(
Press.the('H', 'i', '!', Key.ENTER).in(Form.exampleInput()),
Ensure.that(Value.of(Form.exampleInput), equals('Hi!')),
)

Learn more

Hierarchy

Index

Methods

staticthe

  • the(...keys: Answerable<string | string[] | Key | Key[]>[]): Activity & { in: (field: Answerable<PageElement<any>>) => Interaction }
  • Instantiates an interaction that instructs the actor to press a sequence of keys,

    When no field is specified, the key sequence will be sent to the currently focused element, and if no element is focused - to the document.body to handle.


    Parameters

    • rest...keys: Answerable<string | string[] | Key | Key[]>[]

      A sequence of one or more keys to press

    Returns Activity & { in: (field: Answerable<PageElement<any>>) => Interaction }

instantiationLocation

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


    Returns FileSystemLocation

toString

  • toString(): string
  • Generates a human-friendly description to be used when reporting this Activity.

    Note: When this activity is reported, token #actor in the description will be replaced with the name of the actor performing this Activity.

    For example, #actor clicks on a button becomes Wendy clicks on a button.


    Returns string

in

  • Send the key sequence to a specific element.


    Parameters

    Returns Interaction

performAs

  • performAs(actor: UsesAbilities & AnswersQuestions): Promise<void>