Skip to main content

externalWait

Wait is a synchronisation statement that instructs the actor to wait before proceeding with their next activity, either for a set duration, or until a given expectation is met.

You can configure the timeout of the interaction to Wait.until:

Portable waiting

Serenity/JS implements Wait from scratch, so that the behaviour is consistent no matter the integration tool you use (Playwright, WebdriverIO, Selenium, etc.) or the type of testing you do (Web, REST API, component testing, etc.)

Wait with Web-based tests

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, PageElement, Text } from '@serenity-js/web'

class App {
static status = () =>
Text.of(PageElement.located(By.id('status'))
.describedAs('status widget'))
}

Waiting for a set duration

import { actorCalled, Duration, Wait } from '@serenity-js/core'
import { BrowseTheWebWithPlaywright } from '@serenity-js/playwright'
import { Ensure, equals } from '@serenity-js/assertions'
import { Browser, chromium } from 'playwright'

const browser = await chromium.launch({ headless: true })

await actorCalled('Inês')
.whoCan(BrowseTheWebWithPlaywright.using(browser))
.attemptsTo(
Wait.for(Duration.ofMilliseconds(1_500)),
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, Wait } from '@serenity-js/core'
import { BrowseTheWebWithPlaywright } from '@serenity-js/playwright'
import { equals } from '@serenity-js/assertions'
import { Browser, chromium } from 'playwright'

const browser = await chromium.launch({ headless: true })

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

Wait.until makes the Actor keep asking the 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 or includes.

Changing the default timeout

 import { actorCalled, Duration, Wait } from '@serenity-js/core';
import { BrowseTheWebWithPlaywright } from '@serenity-js/playwright';
import { equals } from '@serenity-js/assertions';
import { Browser, chromium } from 'playwright';

const browser: Browser = await chromium.launch({ headless: true });

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

Learn more

Index

Constructors

externalconstructor

Properties

staticexternalreadonlyminimumTimeout

minimumTimeout: Duration = ...

Minimum timeout that can be used with Wait.until, defaults to 250 milliseconds,

staticexternalreadonlydefaultPollingInterval

defaultPollingInterval: Duration = ...

The amount of time Wait.until should wait between condition checks, defaults to 500ms.

Use WaitUntil.pollingEvery to override it for a given interaction.

staticexternalreadonlyminimumPollingInterval

minimumPollingInterval: Duration = ...

Minimum polling interval of 50ms between condition checks, used with Wait.until.

Methods

staticexternalfor

staticexternalupTo

staticexternaluntil