/
Wait.ts
295 lines (272 loc) · 10.5 KB
/
Wait.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
import { ensure, isGreaterThanOrEqualTo, isInRange } from 'tiny-types';
import { AssertionError, ListItemNotFoundError, RaiseErrors, TimeoutExpiredError } from '../../../errors';
import { d } from '../../../io';
import type { UsesAbilities } from '../../abilities';
import type { Answerable } from '../../Answerable';
import { Interaction } from '../../Interaction';
import type { AnswersQuestions, Expectation, ExpectationOutcome } from '../../questions';
import { ExpectationMet } from '../../questions';
import { ScheduleWork } from '../abilities';
import { Duration } from '../models';
/**
* `Wait` is a synchronisation statement that instructs the {@apilink Actor|actor}
* to wait before proceeding with their next {@apilink Activity|activity},
* either for a set {@apilink Duration|duration}, or until a given {@apilink Expectation|expectation} is met.
*
* You can configure the timeout of the interaction to {@apilink Wait.until}:
* - globally, using {@apilink SerenityConfig.interactionTimeout}
* - locally, on a per-interaction basis using {@apilink Wait.upTo}
*
* :::tip 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
*
* ```html
* <!--
* 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
*
* ```ts
* 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
*
* ```ts
* 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
*
* ```ts
* 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 {@apilink Actor} keep asking the {@apilink 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
*
* ```ts
* 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
* - {@apilink SerenityConfig.interactionTimeout}
* - {@apilink Duration}
* - {@apilink Expectation}
*
* @group Time
*/
export class Wait {
/**
* Minimum timeout that can be used with {@apilink Wait.until},
* defaults to 250 milliseconds,
*/
static readonly minimumTimeout = Duration.ofMilliseconds(250);
/**
* The amount of time {@apilink Wait.until} should wait between condition checks,
* defaults to 500ms.
*
* Use {@apilink WaitUntil.pollingEvery} to override it for a given interaction.
*
* @type {Duration}
*/
static readonly defaultPollingInterval = Duration.ofMilliseconds(500);
/**
* Minimum polling interval of 50ms between condition checks, used with {@apilink Wait.until}.
*/
static readonly minimumPollingInterval = Duration.ofMilliseconds(50);
/**
* Instantiates a version of this {@apilink Interaction}
* configured to wait for a set duration.
*
* @param duration
* A set duration the {@apilink Actor} should wait for before proceeding.
*/
static for(duration: Answerable<Duration>): Interaction {
return new WaitFor(duration);
}
/**
* Instantiates a version of this {@apilink Interaction}
* configured to wait until the answer to the question `actual` meets the `expectation`,
* or the `timeout` expires.
*
* @param timeout
* Custom timeout to override {@apilink SerenityConfig.interactionTimeout}
*/
static upTo(timeout: Duration): { until: <Actual>(actual: Answerable<Actual>, expectation: Expectation<Actual>) => WaitUntil<Actual> } {
return {
until: <Actual>(actual: Answerable<Actual>, expectation: Expectation<Actual>): WaitUntil<Actual> =>
new WaitUntil(actual, expectation, Wait.defaultPollingInterval.isLessThan(timeout) ? Wait.defaultPollingInterval : timeout, timeout),
};
}
/**
* Instantiates a version of this {@apilink Interaction} configured to
* poll every {@apilink Wait.defaultPollingInterval} for the result of the provided
* question (`actual`) until it meets the `expectation`,
* or the timeout expires.
*
* @param actual
* An {@apilink Answerable} that the {@apilink Actor} will keep answering
* until the answer meets the {@apilink Expectation} provided, or the timeout expires.
*
* @param expectation
* An {@apilink Expectation} to be met before proceeding
*/
static until<Actual>(actual: Answerable<Actual>, expectation: Expectation<Actual>): WaitUntil<Actual> {
return new WaitUntil(actual, expectation, Wait.defaultPollingInterval);
}
}
/**
* @package
*/
class WaitFor extends Interaction {
constructor(private readonly duration: Answerable<Duration>) {
super(d`#actor waits for ${ duration }`);
}
async performAs(actor: UsesAbilities & AnswersQuestions): Promise<void> {
const duration = await actor.answer(this.duration);
return ScheduleWork.as(actor).waitFor(duration);
}
}
/**
* Synchronisation statement that instructs the {@apilink Actor} to wait before proceeding until a given {@apilink Expectation} is met.
*
* :::tip
* To instantiate {@apilink Interaction|interaction} to {@apilink WaitUntil}, use the factory method {@apilink Wait.until}.
* :::
*
* ## Learn more
* * {@apilink Wait.until}
*
* @group Time
*/
export class WaitUntil<Actual> extends Interaction {
constructor(
private readonly actual: Answerable<Actual>,
private readonly expectation: Expectation<Actual>,
private readonly pollingInterval: Duration,
private readonly timeout?: Duration,
) {
super(d`#actor waits until ${ actual } does ${ expectation }`);
if (timeout) {
ensure('Timeout', timeout.inMilliseconds(), isGreaterThanOrEqualTo(Wait.minimumTimeout.inMilliseconds()));
ensure('Polling interval', pollingInterval.inMilliseconds(), isInRange(Wait.minimumPollingInterval.inMilliseconds(), timeout.inMilliseconds()));
}
ensure('Polling interval', pollingInterval.inMilliseconds(), isGreaterThanOrEqualTo(Wait.minimumPollingInterval.inMilliseconds()));
}
/**
* Configure how frequently the {@apilink Actor} should check if the answer meets the expectation.
*
* Note that the polling interval defines the delay between subsequent attempts
* to evaluate the expected value, and doesn't include the amount of time
* it take the actor to evaluate the value itself.
*
* @param interval
*/
pollingEvery(interval: Duration): Interaction {
return new WaitUntil(this.actual, this.expectation, interval, this.timeout);
}
/**
* @inheritDoc
*/
async performAs(actor: UsesAbilities & AnswersQuestions): Promise<void> {
await ScheduleWork.as(actor).repeatUntil<ExpectationOutcome>(
() => actor.answer(this.expectation.isMetFor(this.actual)),
{
exitCondition: outcome =>
outcome instanceof ExpectationMet,
delayBetweenInvocations: (invocation) => invocation === 0
? Duration.ofMilliseconds(0)
: this.pollingInterval,
timeout: this.timeout,
errorHandler: (error, outcome) => {
if (error instanceof ListItemNotFoundError) {
return; // ignore, lists might get populated later
}
if (error instanceof TimeoutExpiredError) {
throw RaiseErrors.as(actor).create(AssertionError, {
message: error.message + d` while waiting for ${ this.actual } to ${ this.expectation }`,
expectation: outcome?.expectation,
diff: outcome && { expected: outcome?.expected, actual: outcome?.actual },
location: this.instantiationLocation(),
cause: error,
});
}
throw error;
}
}
);
}
}