/
Cast.ts
187 lines (182 loc) · 6.37 KB
/
Cast.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
import { Ability, Actor } from '../screenplay';
/**
* Serenity/JS uses the concept of a _**cast of actors**_ to centralise the process of configuring the {@apilink Actor|actors} and assigning their {@apilink Ability|abilities}.
*
* When you invoke {@apilink actorCalled} for the first time in a test scenario,
* Serenity/JS {@apilink Actor|instantiates a new actor}
* and passes it through the {@apilink Cast.prepare} method.
* Specifying a **custom cast** gives you an opportunity to configure the actor with the abilities
* they need before it's returned to the caller,
* or configure the actors differently **depending on their name**.
* It also helps you to avoid having to configure abilities individually in every test scenario.
*
* :::tip Remember
* A **cast** is responsible for assigning **abilities** to **actors** in a central location.
* :::
*
* ## Configuring a cast of actors for the entire test suite
*
* When working with relatively **simple scenarios** where all the actors should always receive the same set of abilities,
* you can {@apilink configure} Serenity/JS to use a generic {@apilink Cast.where}:
*
* ```typescript
* import { Cast, configure } from '@serenity-js/core'
* import { CallAnApi } from '@serenity-js/rest'
*
* configure({
* actors: Cast.where(actor => actor.whoCan(
* CallAnApi.at('http://api.example.org'),
* // other abilities
* ))
* })
* ```
*
* If you're using Serenity/JS with one of the [supported test runners](/handbook/test-runners/),
* you might prefer to use your test runner's native configuration mechanism
* instead of invoking {@apilink configure} explicitly.
*
* :::tip configure vs engage
* Calling {@apilink configure} resets the entire Serenity/JS configuration
* and should be done exactly once in your entire test suite.
* If you want to retain the configuration but reset the cast, use {@apilink engage} instead.
* :::
*
* Learn more about configuring Serenity/JS with:
* - [Cucumber.js](/handbook/test-runners/cucumber)
* - [Jasmine](/handbook/test-runners/jasmine)
* - [Mocha](/handbook/test-runners/mocha)
* - [Playwright Test](/handbook/test-runners/playwright-test)
* - [Protractor](/handbook/test-runners/protractor)
* - [WebdriverIO](/handbook/test-runners/webdriverio)
*
* ## Engaging a cast of actors for the specific scenario
*
* If you want to retain Serenity/JS configuration, but set a different {@apilink Cast}
* for the given test scenario you should use {@apilink engage} instead of {@apilink configure}.
*
* This approach is useful for example when your entire test suite is dedicated to interacting with the system
* under test via its REST APIs, and you have a handful of scenarios that need a web browser.
*
* ```ts
* import { describe, beforeEach } from 'mocha'
* import { engage, Cast } from '@serenity-js/core';
* import { BrowseTheWebWithPlaywright } from '@serenity-js/playwright'
* import { Browser, chromium } from 'playwright'
*
* describe('My UI feature', () => {
* beforeEach(async () => {
* const browser = await chromium.launch({ headless: true })
* engage(Cast.where(actor => actor.whoCan(BrowseTheWebWithPlaywright.using(browser))))
* })
*
* // test scenarios
* })
* ```
*
* ## Writing custom casts for complex scenarios
*
* In **complex scenarios** that involve multiple **actors with different abilities**,
* you should create a custom implementation of the {@apilink Cast}.
*
* Examples of such scenarios include those where actors use separate browser instances, interact with different REST APIs,
* or start with different data in their {@apilink Notepad|notepads}.
*
* ### Defining a custom cast of actors interacting with a Web UI
*
* ```ts
* import { beforeEach } from 'mocha'
* import { engage, Actor, Cast } from '@serenity-js/core'
* import { BrowseTheWebWithPlaywright, PlaywrightOptions } from '@serenity-js/playwright'
* import { Browser, chromium } from 'playwright'
*
* export class UIActors implements Cast {
* constructor(
* private readonly browser: Browser,
* private readonly options?: PlaywrightOptions,
* ) {
* }
*
* prepare(actor: Actor): Actor {
* return actor.whoCan(
* BrowseTheWebWithPlaywright.using(this.browser, this.options),
* )
* }
* }
*
* beforeEach(async () => {
* const browser = await chromium.launch({ headless: true })
* engage(new UIActors(browser));
* });
* ```
*
* ### Preparing actors differently based on their name
*
* ```ts
* import { beforeEach } from 'mocha'
* import { actorCalled, engage, Cast } from '@serenity-js/core'
* import { BrowseTheWebWithPlaywright } from '@serenity-js/playwright'
* import { CallAnApi } from '@serenity-js/rest'
* import { Browser, chromium } from 'playwright'
*
* class Actors implements Cast {
* constructor(
* private readonly browser: Browser,
* private readonly options: PlaywrightOptions,
* ) {
* }
*
* prepare(actor: Actor) {
* switch (actor.name) {
* case 'James':
* return actor.whoCan(BrowseTheWebWithPlaywright.using(this.browser, this.options));
* default:
* return actor.whoCan(CallAnApi.at(this.options.baseURL));
* }
* }
* }
*
* beforeEach(async () => {
* const browser = await chromium.launch({ headless: true })
* engage(new Actors(browser, { baseURL: 'https://example.org' }));
* });
*
* actorCalled('James') // returns an actor using a browser
* actorCalled('Alice') // returns an actor interacting with an API
* ```
*
* @group Stage
*/
export abstract class Cast {
/**
* Creates a generic `Cast` implementation, where new actors receive the abilities
* configured by the `prepareFunction`.
*
* @param prepareFunction
*/
static where(prepareFunction: (actor: Actor) => Actor): Cast {
return new class GenericCast extends Cast {
prepare(actor: Actor): Actor {
return prepareFunction(actor);
}
}
}
/**
* Configures an {@apilink Actor} instantiated when {@apilink Stage.actor} is invoked.
*
* @param actor
*
* #### Learn more
* - {@apilink engage}
*/
abstract prepare(actor: Actor): Actor;
}
/**
* @package
*/
class GenericCastWithActorsOfExactSameAbilities implements Cast {
constructor(private readonly abilities: Ability[]) {
}
prepare(actor: Actor): Actor {
return actor.whoCan(...this.abilities);
}
}