Actions

User facing API for generating complex user gestures. This class should not be instantiated directly. Instead, users should create new instances by calling WebDriver.actions().

Action Ticks

Action sequences are divided into a series of "ticks". At each tick, the WebDriver remote end will perform a single action for each device included in the action sequence. At tick 0, the driver will perform the first action defined for each device, at tick 1 the second action for each device, and so on until all actions have been executed. If an individual device does not have an action defined at a particular tick, it will automatically pause.

By default, action sequences will be synchronized so only one device has a define action in each tick. Consider the following code sample:

const actions = driver.actions();

await actions
    .keyDown(SHIFT)
    .move({origin: el})
    .press()
    .release()
    .keyUp(SHIFT)
    .perform();

This sample produces the following sequence of ticks:

DeviceTick 1Tick 2Tick 3Tick 4Tick 5
KeyboardkeyDown(SHIFT)pause()pause()pause()keyUp(SHIFT)
Mousepause()move({origin: el})press()release()pause()

If you'd like the remote end to execute actions with multiple devices simultaneously, you may pass {async: true} when creating the actions builder. With synchronization disabled ({async: true}), the ticks from our previous example become:

DeviceTick 1Tick 2Tick 3
KeyboardkeyDown(SHIFT)keyUp(SHIFT)
Mousemove({origin: el})press()release()

When synchronization is disabled, it is your responsibility to insert pauses for each device, as needed:

const actions = driver.actions({async: true});
const kb = actions.keyboard();
const mouse = actions.mouse();

actions.keyDown(SHIFT).pause(kb).pause(kb).key(SHIFT);
actions.pause(mouse).move({origin: el}).press().release();
actions.perform();

With pauses insert for individual devices, we're back to:

DeviceTick 1Tick 2Tick 3Tick 4
KeyboardkeyDown(SHIFT)pause()pause()keyUp(SHIFT)
Mousepause()move({origin: el})press()release()

Tick Durations

The length of each action tick is however long it takes the remote end to execute the actions for every device in that tick. Most actions are "instantaneous", however, pause and pointer move actions allow you to specify a duration for how long that action should take. The remote end will always wait for all actions within a tick to finish before starting the next tick, so a device may implicitly pause while waiting for other devices to finish.

DeviceTick 1Tick 2
Pointer 1move({duration: 200})press()
Pointer 2move({duration: 300})press()

In table above, the move for Pointer 1 should only take 200 ms, but the remote end will wait for the move for Pointer 2 to finish (an additional 100 ms) before proceeding to Tick 2.

This implicit waiting also applies to pauses. In the table below, even though the keyboard only defines a pause of 100 ms, the remote end will wait an additional 200 ms for the mouse move to finish before moving to Tick 2.

DeviceTick 1Tick 2
Keyboardpause(100)keyDown(SHIFT)
Mousemove({duration: 300})

Constructor

new Actions(executornon-null, options)

Parameters:
NameTypeDescription
executorExecutor

The object to execute the configured actions with.

optionsObject

Options for this action sequence (see class description for details).

Methods

clear() → (non-null) {Promise.<void>}

Releases all keys, pointers, and clears internal state.

Returns:

a promise that will resolve when finished clearing all action state.

Type: 
Promise.<void>

click(element) → (non-null) {Actions}

Short-hand for performing a simple left-click (down/up) with the mouse.

Parameters:
NameTypeDescription
element
Returns:

a self reference.

Type: 
Actions

contextClick(element) → (non-null) {Actions}

Short-hand for performing a simple right-click (down/up) with the mouse.

Parameters:
NameTypeDescription
element
Returns:

a self reference.

Type: 
Actions

doubleClick(element) → (non-null) {Actions}

Short-hand for performing a double left-click with the mouse.

Parameters:
NameTypeDescription
element
Returns:

a self reference.

Type: 
Actions

dragAndDrop(from, to) → (non-null) {Actions}

Configures a drag-and-drop action consisting of the following steps:

  1. Move to the center of the from element (element to be dragged).
  2. Press the left mouse button.
  3. If the to target is a WebElement, move the mouse to its center. Otherwise, move the mouse by the specified offset.
  4. Release the left mouse button.
Parameters:
NameTypeDescription
from
to
Returns:

a self reference.

Type: 
Actions

insert(devicenon-null, …actionsnon-null) → (non-null) {Actions}

Appends actions to the end of the current sequence for the given device. If device synchronization is enabled, after inserting the actions, pauses will be inserted for all other devices to ensure all action sequences are the same length.

Parameters:
NameTypeAttributesDescription
deviceDevice

the device to update.

actionsAction<repeatable>

the actions to insert.

Returns:

a self reference.

Type: 
Actions

keyDown(key) → (non-null) {Actions}

Inserts an action to press a single key.

Parameters:
NameTypeDescription
keyKey | string | number

the key to press. This key may be specified as a Key value, a specific unicode code point, or a string containing a single unicode code point.

Returns:

a self reference.

Type: 
Actions

keyUp(key) → (non-null) {Actions}

Inserts an action to release a single key.

Parameters:
NameTypeDescription
keyKey | string | number

the key to release. This key may be specified as a Key value, a specific unicode code point, or a string containing a single unicode code point.

Returns:

a self reference.

Type: 
Actions

keyboard() → (non-null) {Keyboard}

Returns:

the keyboard device handle.

Type: 
Keyboard

mouse() → (non-null) {Pointer}

Returns:

the mouse pointer device handle.

Type: 
Pointer

move() → (non-null) {Actions}

Inserts an action for moving the mouse x and y pixels relative to the specified origin. The origin may be defined as the mouse's current position, the top-left corner of the viewport, or the center of a specific WebElement. Default is top left corner of the view-port if origin is not specified

You may adjust how long the remote end should take, in milliseconds, to perform the move using the duration parameter (defaults to 100 ms). The number of incremental move events generated over this duration is an implementation detail for the remote end.

Parameters:
TypeDescription
Returns:

a self reference.

Type: 
Actions

pause(durationopt, …devicesnon-null) → (non-null) {Actions}

Inserts a pause action for the specified devices, ensuring each device is idle for a tick. The length of the pause (in milliseconds) may be specified as the first parameter to this method (defaults to 0). Otherwise, you may just specify the individual devices that should pause.

If no devices are specified, a pause action will be created (using the same duration) for every device.

When device synchronization is enabled (the default for new Actions objects), there is no need to specify devices as pausing one automatically pauses the others for the same duration. In other words, the following are all equivalent:

let a1 = driver.actions();
a1.pause(100).perform();

let a2 = driver.actions();
a2.pause(100, a2.keyboard()).perform();
// Synchronization ensures a2.mouse() is automatically paused too.

let a3 = driver.actions();
a3.pause(100, a3.keyboard(), a3.mouse()).perform();

When device synchronization is disabled, you can cause individual devices to pause during a tick. For example, to hold the SHIFT key down while moving the mouse:

let actions = driver.actions({async: true});

actions.keyDown(Key.SHIFT);
actions.pause(actions.mouse())  // Pause for shift down
    .press(Button.LEFT)
    .move({x: 10, y: 10})
    .release(Button.LEFT);
actions
    .pause(
        actions.keyboard(),  // Pause for press left
        actions.keyboard(),  // Pause for move
        actions.keyboard())  // Pause for release left
   .keyUp(Key.SHIFT);
await actions.perform();
Parameters:
NameTypeAttributesDescription
durationnumber | Device<optional>

The length of the pause to insert, in milliseconds. Alternatively, the duration may be omitted (yielding a default 0 ms pause), and the first device to pause may be specified.

devicesDevice<repeatable>

The devices to insert the pause for. If no devices are specified, the pause will be inserted for all devices.

Returns:

a self reference.

Type: 
Actions

(async) perform() → (non-null) {Promise.<void>}

Performs the configured action sequence.

Returns:

a promise that will resolve when all actions have been completed.

Type: 
Promise.<void>

press(buttonopt, non-null) → (non-null) {Actions}

Inserts an action to press a mouse button at the mouse's current location.

Parameters:
NameTypeAttributesDescription
buttonButton<optional>

The button to press; defaults to LEFT.

Returns:

a self reference.

Type: 
Actions

release(buttonopt, non-null) → (non-null) {Actions}

Inserts an action to release a mouse button at the mouse's current location.

Parameters:
NameTypeAttributesDescription
buttonButton<optional>

The button to release; defaults to LEFT.

Returns:

a self reference.

Type: 
Actions

scroll(x, y, deltax, deltay, duration) → (non-null) {Actions}

scrolls a page via the coordinates given

Parameters:
NameTypeDescription
xnumber

starting x coordinate

ynumber

starting y coordinate

deltaxnumber

delta x to scroll to target

deltaynumber

delta y to scroll to target

durationnumber

duration ratio be the ratio of time delta and duration

Returns:

An action to scroll with this device.

Type: 
Actions

sendKeys(…keys) → (non-null) {Actions}

Inserts a sequence of actions to type the provided key sequence. For each key, this will record a pair of keyDown and keyUp actions. An implication of this pairing is that modifier keys (e.g. Key.SHIFT) will always be immediately released. In other words, sendKeys(Key.SHIFT, 'a') is the same as typing sendKeys('a'), not sendKeys('A').

Parameters:
NameTypeAttributesDescription
keysKey | string | number<repeatable>

the keys to type.

Returns:

a self reference.

Type: 
Actions

synchronize(…devicesnon-null) → (non-null) {Actions}

Ensures the action sequence for every device referenced in this action sequence is the same length. For devices whose sequence is too short, this will insert pauses so that every device has an explicit action defined at each tick.

Parameters:
NameTypeAttributesDescription
devicesDevice<repeatable>

The specific devices to synchronize. If unspecified, the action sequences for every device will be synchronized.

Returns:

a self reference.

Type: 
Actions

wheel() → (non-null) {Wheel}

Returns:

the wheel device handle.

Type: 
Wheel