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

Wait

Instructs the Actor to wait before proceeding either for a set Duration or until a given Expectation is met.

Under the hood, Wait uses WebdriverIO "explicit waiting" mechanism.

Examples:

Example widget
 <!--
     After about 1 second, the text will change from 'Loading...' to 'Ready!'
 -->
 <h1 id="status">Loading...</h1>
 <script>
     (function () {
         setTimeout(function () {
             document.getElementById('status').textContent = 'Ready!'
         }, 1000);
     })();
 </script>
Lean Page Object describing the widget
 import { by, Target } from '@serenity-js/webdriverio';

 class App {
     static status = Target.the('status widget')
         .located(by.id('status'));
 }
Waiting for a set amount of time
 import { actorCalled, Duration } from '@serenity-js/core';
 import { BrowseTheWeb, Wait } from '@serenity-js/webdriverio';
 import { Ensure, equals } from '@serenity-js/assertions';

 actorCalled('Wendy')
     .whoCan(BrowseTheWeb.using(browser))
     .attemptsTo(
         Wait.for(Duration.ofSeconds(1.5)),
         Ensure.that(App.status, equals('Ready!')),
     );

 // Please note that while the above implementation works,
 // this approach is inefficient because at best
 // the actor might wait too long and at worst the test
 // might become "flaky" if any external interference
 // (like network glitches, animations taking a bit too long etc.)
 // makes the actor wait not long enough.
Waiting until a condition is met
 import { actorCalled } from '@serenity-js/core';
 import { BrowseTheWeb, Wait, Text } from '@serenity-js/webdriverio';
 import { equals } from '@serenity-js/assertions';

 actorCalled('Wendy')
     .whoCan(BrowseTheWeb.using(browser))
     .attemptsTo(
         Wait.until(Text.of(App.status), equals('Ready!')),
         // app is ready, proceed with the scenario
     );

 // Wait.until makes the Actor keep asking a Question,
 // in this case Text.of(App.status), until the answer meets
 // the expectation, or a timeout expires (default: 5s).
 //
 // Please note that both Ensure and Wait can be used with
 // the same expectations, like `equals`.
Changing the default timeout
 import { actorCalled, Duration } from '@serenity-js/core';
 import { BrowseTheWeb, Wait, Text } from '@serenity-js/webdriverio';
 import { equals } from '@serenity-js/assertions';

 actorCalled('Wendy')
     .whoCan(BrowseTheWeb.using(browser))
     .attemptsTo(
         Wait.upTo(Duration.ofSeconds(3))
             .until(Text.of(App.status), equals('Ready!')),
         // app is ready, proceed with the scenario
     );

Tests:

Static Member Summary

Static Public Members
public static

Default timeout of 5 seconds used with Wait.until.

Static Method Summary

Static Public Methods
public static

for(duration: Answerable<Duration>): Interaction

Instantiates a version of this Interaction configured to wait for a set duration.

public static

until(actual: Answerable<Actual>, expectation: <any, Actual>): Interaction

Instantiates a version of this Interaction configured to wait until the answer to the question (actual) meets the expectation, or a Wait.Default_Timeout expires.

public static

upTo(duration: Duration): WaitBuilder

Instantiates a version of this Interaction configured to wait until the answer to the question (actual) meets the expectation, or a custom timeout expires.

Static Public Members

public static Default_Timeout: Duration source

Default timeout of 5 seconds used with Wait.until.

Static Public Methods

public static for(duration: Answerable<Duration>): Interaction source

Instantiates a version of this Interaction configured to wait for a set duration.

Params:

NameTypeAttributeDescription
duration Answerable<Duration>

A set duration the Actor should wait for before proceeding

Returns:

Interaction

public static until(actual: Answerable<Actual>, expectation: <any, Actual>): Interaction source

Instantiates a version of this Interaction configured to wait until the answer to the question (actual) meets the expectation, or a Wait.Default_Timeout expires.

Params:

NameTypeAttributeDescription
actual Answerable<Actual>

A Question that the Actor will keep asking until the answer meets the Expectation provided

expectation <any, Actual>

An Expectation to be met before proceeding

Returns:

Interaction

public static upTo(duration: Duration): WaitBuilder source

Instantiates a version of this Interaction configured to wait until the answer to the question (actual) meets the expectation, or a custom timeout expires.

Params:

NameTypeAttributeDescription
duration Duration

Custom timeout to override Wait.Default_Timeout

Returns:

WaitBuilder