-
-
Notifications
You must be signed in to change notification settings - Fork 159
/
test-api.ts
539 lines (491 loc) · 17.5 KB
/
test-api.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
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
import type {
Fixtures,
PlaywrightTestArgs,
PlaywrightTestOptions,
PlaywrightWorkerArgs,
PlaywrightWorkerOptions,
TestInfo,
TestType,
} from '@playwright/test';
import { test as playwrightBaseTest } from '@playwright/test';
import { AnsiDiffFormatter, Cast, Duration, serenity as serenityInstance, TakeNotes } from '@serenity-js/core';
import { SceneFinishes, SceneTagged } from '@serenity-js/core/lib/events';
import { BrowserTag, PlatformTag } from '@serenity-js/core/lib/model';
import { BrowseTheWebWithPlaywright, SerenitySelectorEngines } from '@serenity-js/playwright';
import { CallAnApi } from '@serenity-js/rest';
import { Photographer, TakePhotosOfFailures } from '@serenity-js/web';
import * as os from 'os';
import type { JSONValue } from 'tiny-types';
import { ensure, isFunction, property } from 'tiny-types';
import {
DomainEventBuffer,
PlaywrightStepReporter,
SERENITY_JS_DOMAIN_EVENTS_ATTACHMENT_CONTENT_TYPE
} from '../reporter';
import type { DescribeFunction } from './DescribeFunction';
import { PerformActivitiesAsPlaywrightSteps } from './PerformActivitiesAsPlaywrightSteps';
import type { SerenityFixtures } from './SerenityFixtures';
import type { SerenityOptions } from './SerenityOptions';
const serenitySelectorEngines = new SerenitySelectorEngines();
export const fixtures: Fixtures<Omit<SerenityOptions, 'actors'> & SerenityFixtures, object, PlaywrightTestArgs & PlaywrightTestOptions, PlaywrightWorkerArgs & PlaywrightWorkerOptions> = {
actors: [
// eslint-disable-next-line @typescript-eslint/explicit-module-boundary-types
async ({ contextOptions, baseURL, extraHTTPHeaders, page, proxy }, use): Promise<void> => {
await use(Cast.where(actor => actor.whoCan(
BrowseTheWebWithPlaywright.usingPage(page, contextOptions),
TakeNotes.usingAnEmptyNotepad(),
CallAnApi.using({
baseURL: baseURL,
headers: extraHTTPHeaders,
proxy: proxy && proxy?.server
? asProxyConfig(proxy)
: undefined,
}),
)));
},
{ option: true },
],
playwright: async ({ playwright }, use) => {
await serenitySelectorEngines.ensureRegisteredWith(playwright.selectors);
await use(playwright);
},
defaultActorName: [
'Serena',
{ option: true },
],
cueTimeout: [
Duration.ofSeconds(5),
{ option: true },
],
interactionTimeout: [
Duration.ofSeconds(5),
{ option: true },
],
crew: [
[
Photographer.whoWill(TakePhotosOfFailures)
],
{ option: true },
],
// eslint-disable-next-line no-empty-pattern
platform: async ({}, use) => {
const platform = os.platform();
// https://nodejs.org/api/process.html#process_process_platform
const name = platform === 'win32'
? 'Windows'
: (platform === 'darwin' ? 'macOS' : 'Linux');
await use({ name, version: os.release() });
},
serenity: async ({ crew, cueTimeout, interactionTimeout, platform }, use, info: TestInfo) => {
const domainEventBuffer = new DomainEventBuffer();
serenityInstance.configure({
diffFormatter: new AnsiDiffFormatter(),
cueTimeout: asDuration(cueTimeout),
interactionTimeout: asDuration(interactionTimeout),
crew: [
...crew,
domainEventBuffer,
new PlaywrightStepReporter(info),
],
});
serenityInstance.announce(new SceneTagged(
serenityInstance.currentSceneId(),
new PlatformTag(platform.name, platform.version),
serenityInstance.currentTime(),
));
await use(serenityInstance);
const serialisedEvents: Array<{ type: string, value: JSONValue }> = [];
for (const event of domainEventBuffer.flush()) {
serialisedEvents.push({
type: event.constructor.name,
value: event.toJSON(),
});
if (event instanceof SceneTagged) {
test.info().annotations.push({ type: event.tag.type, description: event.tag.name });
}
}
info.attach('serenity-js-events.json', {
contentType: SERENITY_JS_DOMAIN_EVENTS_ATTACHMENT_CONTENT_TYPE,
body: Buffer.from(JSON.stringify(serialisedEvents), 'utf8'),
});
},
actorCalled: async ({ serenity, actors, browser, browserName, contextOptions }, use) => {
const sceneId = serenity.currentSceneId();
serenity.engage(asCast(actors));
const actorCalled = (name: string) => {
const actor = serenity.theActorCalled(name);
return actor.whoCan(new PerformActivitiesAsPlaywrightSteps(actor, serenity, it));
};
serenity.announce(new SceneTagged(
sceneId,
new BrowserTag(browserName, browser.version()),
serenity.currentTime(),
));
await use(actorCalled);
serenity.announce(
new SceneFinishes(sceneId, serenity.currentTime()),
);
await serenityInstance.waitForNextCue();
},
actor: async ({ actorCalled, defaultActorName }, use) => {
await use(actorCalled(defaultActorName));
},
};
/**
* Serenity/JS BDD-style test API created by {@apilink useBase}.
*/
export type TestApi<TestArgs extends Record<string, any>, WorkerArgs extends Record<string, any>> =
Pick<TestType<TestArgs, WorkerArgs>, 'beforeAll' | 'beforeEach' | 'afterEach' | 'afterAll' | 'expect'> &
{
/**
* Creates a Serenity/JS BDD-style test API around the default Playwright [base test](https://playwright.dev/docs/test-fixtures)
* and using custom test fixtures.
*
* ```typescript
* import { useFixtures } from '@serenity-js/playwright-test'
* import { Log } from '@serenity-js/core'
*
* const { describe, it } = useFixtures<{ message: string }>({
* message: 'Hello world!'
* })
*
* describe('Serenity/JS useFixtures', () => {
*
* it('enables injecting custom test fixtures into test scenarios', async ({ actor, message }) => {
* await actor.attemptsTo(
* Log.the(message),
* )
* })
* })
* ```
*
* Shorthand for [`useBase`](/api/playwright-test/function/useBase/)
*/
useFixtures: <T extends Record<string, any>, W extends Record<string, any> = object>(customFixtures: Fixtures<T, W, TestArgs, WorkerArgs>) => TestApi<TestArgs & T, WorkerArgs & W>,
it: TestType<TestArgs, WorkerArgs>,
test: TestType<TestArgs, WorkerArgs>,
describe: DescribeFunction,
}
function createTestApi<TestArgs extends Record<string, any>, WorkerArgs extends Record<string, any> = object>(baseTest: TestType<TestArgs, WorkerArgs>): TestApi<TestArgs, WorkerArgs> {
return {
useFixtures<T extends Record<string, any>, W extends Record<string, any> = object>(customFixtures: Fixtures<T, W, TestArgs, WorkerArgs>): TestApi<TestArgs & T, WorkerArgs & W> {
return createTestApi(baseTest.extend(customFixtures));
},
beforeAll: baseTest.beforeAll,
beforeEach: baseTest.beforeEach,
afterEach: baseTest.afterEach,
afterAll: baseTest.afterAll,
describe: baseTest.describe,
expect: baseTest.expect,
it: baseTest,
test: baseTest,
};
}
const api = createTestApi(playwrightBaseTest).useFixtures(fixtures);
/**
* Declares a single test scenario.
*
* ## Example
*
* ```typescript
* import { Ensure, equals } from '@serenity-js/assertions'
* import { describe, it } from '@serenity-js/playwright-test'
*
* describe(`Todo List App`, () => {
*
* it(`should allow me to add a todo item`, async ({ actor }) => {
* await actor.attemptsTo(
* startWithAnEmptyList(),
*
* recordItem('Buy some milk'),
*
* Ensure.that(itemNames(), equals([
* 'Buy some milk',
* ])),
* )
* })
*
* it('supports multiple actors using separate browsers', async ({ actorCalled }) => {
* await actorCalled('Alice').attemptsTo(
* startWithAListContaining(
* 'Feed the cat'
* ),
* )
*
* await actorCalled('Bob').attemptsTo(
* startWithAListContaining(
* 'Walk the dog'
* ),
* )
*
* await actorCalled('Alice').attemptsTo(
* Ensure.that(itemNames(), equals([
* 'Feed the cat'
* ])),
* )
*
* await actorCalled('Bob').attemptsTo(
* Ensure.that(itemNames(), equals([
* 'Walk the dog'
* ])),
* )
* })
* })
* ```
*
* ## Learn more
* - [Grouping test scenarios](/api/playwright-test/function/describe/)
* - {@apilink SerenityFixtures}
* - [Playwright Test `test` function](https://playwright.dev/docs/api/class-test#test-call)
* - [Serenity/JS + Playwright Test project template](https://github.com/serenity-js/serenity-js-playwright-test-template/)
*/
export const it = api.it;
/**
* Declares a single test scenario. Alias for [`it`](/api/playwright-test/function/it/).
*/
export const test = api.test;
/**
* Declares a group of test scenarios.
*
* ## Example
*
* ```typescript
* import { Ensure, equals } from '@serenity-js/assertions'
* import { describe, it, test } from '@serenity-js/playwright-test'
* import { Photographer, TakePhotosOfFailures, Value } from '@serenity-js/web'
*
* describe(`Todo List App`, () => {
*
* test.use({
* defaultActorName: 'Serena',
* crew: [
* Photographer.whoWill(TakePhotosOfFailures),
* ],
* })
*
* it(`should allow me to add a todo item`, async ({ actor }) => {
* await actor.attemptsTo(
* startWithAnEmptyList(),
*
* recordItem('Buy some milk'),
*
* Ensure.that(itemNames(), equals([
* 'Buy some milk',
* ])),
* )
* })
*
* it('should clear text input field when an item is added', async ({ actor }) => {
* await actor.attemptsTo(
* startWithAnEmptyList(),
*
* recordItem('Buy some milk'),
*
* Ensure.that(Value.of(newTodoInput()), equals('')),
* )
* })
* })
* ```
*
* ## Learn more
* - Declaring a Serenity/JS [test scenario](/api/playwright-test/function/it/)
* - [Playwright Test `describe` function](https://playwright.dev/docs/api/class-test#test-describe-1)
* - [Serenity/JS + Playwright Test project template](https://github.com/serenity-js/serenity-js-playwright-test-template/)
*/
export const describe = api.describe;
export const beforeAll = api.beforeAll;
export const beforeEach = api.beforeEach;
export const afterEach = api.afterEach;
export const afterAll = api.afterAll;
export const expect = api.expect;
export const useFixtures = api.useFixtures;
/**
* Creates a Serenity/JS BDD-style test API around the given Playwright [base test](https://playwright.dev/docs/test-fixtures).
*
* ## Using default configuration
*
* When your test scenario doesn't require [custom test fixtures](https://playwright.dev/docs/test-fixtures),
* and you're happy with the default [base test](https://playwright.dev/docs/api/class-test#test-call) offered by Playwright,
* you can import test API functions such as [`describe`](/api/playwright-test/function/describe/) and [`it`](/api/playwright-test/function/describe/) directly from `@serenity-js/playwright-test`.
*
* ```typescript
* import { describe, it, test } from '@serenity-js/playwright-test'
* import { Log } from '@serenity-js/core'
*
* // override default fixtures if needed
* test.use({
* defaultActorName: 'Alice'
* })
*
* describe('Serenity/JS default test API', () => {
*
* it('enables easy access to actors and standard Playwright fixtures', async ({ actor, browserName }) => {
* await actor.attemptsTo(
* Log.the(browserName),
* )
* })
* })
* ```
*
* In the above example, importing test API functions directly from `@serenity-js/playwright-test` is the equivalent of the following setup:
*
* ```typescript
* import { test as playwrightBaseTest } from '@playwright/test'
* import { useBase } from '@serenity-js/playwright-test'
*
* const { describe, it, test, beforeEach, afterEach } = useBase(playwrightBaseTest)
* ```
*
* ## Using custom fixtures
*
* When your test scenario requires [custom test fixtures](https://playwright.dev/docs/test-fixtures),
* but you're still happy with the default [base test](https://playwright.dev/docs/api/class-test#test-call) offered by Playwright,
* you can create fixture-aware test API functions such as [`describe`](/api/playwright-test/function/describe/) and [`it`](/api/playwright-test/function/describe/)
* by calling [`useFixtures`](/api/playwright-test/function/useFixtures/).
*
* For example, you can create a test scenario using a static `message` fixture as follows:
*
* ```typescript
* import { useFixtures } from '@serenity-js/playwright-test'
* import { Log } from '@serenity-js/core'
*
* const { describe, it } = useFixtures<{ message: string }>({
* message: 'Hello world!'
* })
*
* describe('Serenity/JS useFixtures', () => {
*
* it('enables injecting custom test fixtures into test scenarios', async ({ actor, message }) => {
* await actor.attemptsTo(
* Log.the(message),
* )
* })
* })
* ```
*
* The value of your test fixtures can be either static or dynamic and based on the value of other fixtures.
*
* To create a dynamic test fixture use the [function syntax](https://playwright.dev/docs/test-fixtures):
*
* ```typescript
* import { Log } from '@serenity-js/core'
* import { useFixtures } from '@serenity-js/playwright-test'
*
* const { describe, it } = useFixtures<{ message: string }>({
* message: async ({ actor }, use) => {
* await use(`Hello, ${ actor.name }`);
* }
* })
*
* describe('Serenity/JS useFixtures', () => {
*
* it('enables injecting custom test fixtures into test scenarios', async ({ actor, message }) => {
* await actor.attemptsTo(
* Log.the(message),
* )
* })
* })
* ```
*
* In the above example, creating test API functions via `useFixtures` is the equivalent of the following setup:
*
* ```typescript
* import { test as playwrightBaseTest } from '@playwright/test'
* import { useBase } from '@serenity-js/playwright-test'
*
* const { describe, it, test, beforeEach, afterEach } = useBase(playwrightBaseTest)
* .useFixtures<{ message: string }>({
* message: async ({ actor }, use) => {
* await use(`Hello, ${ actor.name }`);
* }
* })
* ```
*
* ## Using custom base test
*
* In cases where you need to use a non-default base test, for example when doing [UI component testing](https://playwright.dev/docs/test-components),
* you can create Serenity/JS test API functions around your preferred base test.
*
* ```tsx
* import { test as componentTest } from '@playwright/experimental-ct-react'
* import { Ensure, contain } from '@serenity-js/assertions'
* import { useBase } from '@serenity-js/playwright-test'
* import { Enter, PageElement, CssClasses } from '@serenity-js/web'
*
* import EmailInput from './EmailInput';
*
* const { it, describe } = useBase(componentTest).useFixtures<{ emailAddress: string }>({
* emailAddress: ({ actor }, use) => {
* use(`${ actor.name }@example.org`)
* }
* })
*
* describe('EmailInput', () => {
*
* it('allows valid email addresses', async ({ actor, mount, emailAddress }) => {
* const nativeComponent = await mount(<EmailInput/>);
*
* const component = PageElement.from(nativeComponent);
*
* await actor.attemptsTo(
* Enter.theValue(emailAddress).into(component),
* Ensure.that(CssClasses.of(component), contain('valid')),
* )
* })
* })
* ```
*
* @param baseTest
*/
export function useBase<TestArgs extends Record<string, any>, WorkerArgs extends Record<string, any> = object>(
baseTest: TestType<TestArgs, WorkerArgs>
): TestApi<Omit<SerenityOptions, 'actors'> & SerenityFixtures & TestArgs, WorkerArgs> {
return createTestApi(baseTest)
.useFixtures<TestArgs & Omit<SerenityOptions, 'actors'> & SerenityFixtures, WorkerArgs>(fixtures);
}
/**
* @private
* @param maybeDuration
*/
function asDuration(maybeDuration: number | Duration): Duration {
return maybeDuration instanceof Duration
? maybeDuration
: Duration.ofMilliseconds(maybeDuration);
}
/**
* @private
* @param maybeCast
*/
function asCast(maybeCast: unknown): Cast {
return ensure('actors', maybeCast as Cast, property('prepare', isFunction()));
}
/**
* @private
* @param proxy
*/
function asProxyConfig(proxy: PlaywrightTestOptions['proxy']): {
protocol: string,
host: string,
port?: number,
auth?: { username: string, password: string }
} {
// Playwright defaults to http when proxy.server does not define the protocol
// See https://playwright.dev/docs/api/class-testoptions#test-options-proxy
const hasProtocol = /[\dA-Za-z]+:\/\//.test(proxy.server);
const proxyUrl = hasProtocol
? new URL(proxy.server)
: new URL(`http://${ proxy.server }`);
const host = proxyUrl.hostname;
const port = proxyUrl.port
? Number(proxyUrl.port)
: undefined;
const auth = proxy.username
? { username: proxy.username, password: proxy.password || '' }
: undefined;
return {
protocol: proxyUrl.protocol,
host,
port,
auth
};
}