codeceptjs 4.0.0-beta.3 → 4.0.0-beta.5
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +134 -119
- package/bin/codecept.js +12 -2
- package/bin/test-server.js +53 -0
- package/docs/webapi/clearCookie.mustache +1 -1
- package/lib/actor.js +66 -102
- package/lib/ai.js +130 -121
- package/lib/assert/empty.js +3 -5
- package/lib/assert/equal.js +4 -7
- package/lib/assert/include.js +4 -6
- package/lib/assert/throws.js +2 -4
- package/lib/assert/truth.js +2 -2
- package/lib/codecept.js +141 -86
- package/lib/command/check.js +201 -0
- package/lib/command/configMigrate.js +2 -4
- package/lib/command/definitions.js +8 -26
- package/lib/command/dryRun.js +30 -35
- package/lib/command/generate.js +10 -14
- package/lib/command/gherkin/snippets.js +75 -73
- package/lib/command/gherkin/steps.js +1 -1
- package/lib/command/info.js +42 -8
- package/lib/command/init.js +13 -12
- package/lib/command/interactive.js +10 -2
- package/lib/command/list.js +1 -1
- package/lib/command/run-multiple/chunk.js +48 -45
- package/lib/command/run-multiple.js +12 -35
- package/lib/command/run-workers.js +21 -58
- package/lib/command/utils.js +5 -6
- package/lib/command/workers/runTests.js +263 -222
- package/lib/container.js +386 -238
- package/lib/data/context.js +10 -13
- package/lib/data/dataScenarioConfig.js +8 -8
- package/lib/data/dataTableArgument.js +6 -6
- package/lib/data/table.js +5 -11
- package/lib/effects.js +223 -0
- package/lib/element/WebElement.js +327 -0
- package/lib/els.js +158 -0
- package/lib/event.js +21 -17
- package/lib/heal.js +88 -80
- package/lib/helper/AI.js +2 -1
- package/lib/helper/ApiDataFactory.js +4 -7
- package/lib/helper/Appium.js +50 -57
- package/lib/helper/FileSystem.js +3 -3
- package/lib/helper/GraphQLDataFactory.js +4 -4
- package/lib/helper/JSONResponse.js +75 -37
- package/lib/helper/Mochawesome.js +31 -9
- package/lib/helper/Nightmare.js +37 -58
- package/lib/helper/Playwright.js +267 -272
- package/lib/helper/Protractor.js +56 -87
- package/lib/helper/Puppeteer.js +247 -264
- package/lib/helper/REST.js +29 -17
- package/lib/helper/TestCafe.js +22 -47
- package/lib/helper/WebDriver.js +157 -368
- package/lib/helper/extras/PlaywrightPropEngine.js +2 -2
- package/lib/helper/extras/Popup.js +22 -22
- package/lib/helper/network/utils.js +1 -1
- package/lib/helper/testcafe/testcafe-utils.js +27 -28
- package/lib/listener/emptyRun.js +55 -0
- package/lib/listener/exit.js +7 -10
- package/lib/listener/{retry.js → globalRetry.js} +5 -5
- package/lib/listener/globalTimeout.js +165 -0
- package/lib/listener/helpers.js +15 -15
- package/lib/listener/mocha.js +1 -1
- package/lib/listener/result.js +12 -0
- package/lib/listener/retryEnhancer.js +85 -0
- package/lib/listener/steps.js +32 -18
- package/lib/listener/store.js +20 -0
- package/lib/locator.js +1 -1
- package/lib/mocha/asyncWrapper.js +231 -0
- package/lib/{interfaces → mocha}/bdd.js +3 -3
- package/lib/mocha/cli.js +308 -0
- package/lib/mocha/factory.js +104 -0
- package/lib/{interfaces → mocha}/featureConfig.js +32 -12
- package/lib/{interfaces → mocha}/gherkin.js +26 -28
- package/lib/mocha/hooks.js +112 -0
- package/lib/mocha/index.js +12 -0
- package/lib/mocha/inject.js +29 -0
- package/lib/{interfaces → mocha}/scenarioConfig.js +31 -7
- package/lib/mocha/suite.js +82 -0
- package/lib/mocha/test.js +181 -0
- package/lib/mocha/types.d.ts +42 -0
- package/lib/mocha/ui.js +232 -0
- package/lib/output.js +93 -65
- package/lib/pause.js +160 -138
- package/lib/plugin/analyze.js +396 -0
- package/lib/plugin/auth.js +435 -0
- package/lib/plugin/autoDelay.js +8 -8
- package/lib/plugin/autoLogin.js +3 -338
- package/lib/plugin/commentStep.js +6 -1
- package/lib/plugin/coverage.js +10 -22
- package/lib/plugin/customLocator.js +3 -3
- package/lib/plugin/customReporter.js +52 -0
- package/lib/plugin/eachElement.js +1 -1
- package/lib/plugin/fakerTransform.js +1 -1
- package/lib/plugin/heal.js +36 -9
- package/lib/plugin/htmlReporter.js +1947 -0
- package/lib/plugin/pageInfo.js +140 -0
- package/lib/plugin/retryFailedStep.js +17 -18
- package/lib/plugin/retryTo.js +2 -113
- package/lib/plugin/screenshotOnFail.js +17 -58
- package/lib/plugin/selenoid.js +15 -35
- package/lib/plugin/standardActingHelpers.js +4 -1
- package/lib/plugin/stepByStepReport.js +56 -17
- package/lib/plugin/stepTimeout.js +5 -12
- package/lib/plugin/subtitles.js +4 -4
- package/lib/plugin/tryTo.js +3 -102
- package/lib/plugin/wdio.js +8 -10
- package/lib/recorder.js +155 -124
- package/lib/rerun.js +43 -42
- package/lib/result.js +161 -0
- package/lib/secret.js +1 -2
- package/lib/step/base.js +239 -0
- package/lib/step/comment.js +10 -0
- package/lib/step/config.js +50 -0
- package/lib/step/func.js +46 -0
- package/lib/step/helper.js +50 -0
- package/lib/step/meta.js +99 -0
- package/lib/step/record.js +74 -0
- package/lib/step/retry.js +11 -0
- package/lib/step/section.js +55 -0
- package/lib/step.js +21 -332
- package/lib/steps.js +50 -0
- package/lib/store.js +37 -5
- package/lib/template/heal.js +2 -11
- package/lib/test-server.js +323 -0
- package/lib/timeout.js +66 -0
- package/lib/utils.js +351 -218
- package/lib/within.js +75 -55
- package/lib/workerStorage.js +2 -1
- package/lib/workers.js +386 -277
- package/package.json +81 -75
- package/translations/de-DE.js +5 -3
- package/translations/fr-FR.js +5 -4
- package/translations/index.js +1 -0
- package/translations/it-IT.js +4 -3
- package/translations/ja-JP.js +4 -3
- package/translations/nl-NL.js +76 -0
- package/translations/pl-PL.js +4 -3
- package/translations/pt-BR.js +4 -3
- package/translations/ru-RU.js +4 -3
- package/translations/utils.js +9 -0
- package/translations/zh-CN.js +4 -3
- package/translations/zh-TW.js +4 -3
- package/typings/index.d.ts +197 -187
- package/typings/promiseBasedTypes.d.ts +53 -903
- package/typings/types.d.ts +372 -1042
- package/lib/cli.js +0 -257
- package/lib/helper/ExpectHelper.js +0 -391
- package/lib/helper/MockServer.js +0 -221
- package/lib/helper/SoftExpectHelper.js +0 -381
- package/lib/listener/artifacts.js +0 -19
- package/lib/listener/timeout.js +0 -109
- package/lib/mochaFactory.js +0 -113
- package/lib/plugin/debugErrors.js +0 -67
- package/lib/scenario.js +0 -224
- package/lib/ui.js +0 -236
|
@@ -0,0 +1,435 @@
|
|
|
1
|
+
const fs = require('fs')
|
|
2
|
+
const path = require('path')
|
|
3
|
+
const { fileExists } = require('../utils')
|
|
4
|
+
const CommentStep = require('../step/comment')
|
|
5
|
+
const Section = require('../step/section')
|
|
6
|
+
const container = require('../container')
|
|
7
|
+
const store = require('../store')
|
|
8
|
+
const event = require('../event')
|
|
9
|
+
const recorder = require('../recorder')
|
|
10
|
+
const { debug } = require('../output')
|
|
11
|
+
const { isAsyncFunction } = require('../utils')
|
|
12
|
+
|
|
13
|
+
const defaultUser = {
|
|
14
|
+
fetch: I => I.grabCookie(),
|
|
15
|
+
check: () => {},
|
|
16
|
+
restore: (I, cookies) => {
|
|
17
|
+
I.amOnPage('/') // open a page
|
|
18
|
+
I.setCookie(cookies)
|
|
19
|
+
},
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
const defaultConfig = {
|
|
23
|
+
saveToFile: false,
|
|
24
|
+
inject: 'login',
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
/**
|
|
28
|
+
* Logs user in for the first test and reuses session for next tests.
|
|
29
|
+
* Works by saving cookies into memory or file.
|
|
30
|
+
* If a session expires automatically logs in again.
|
|
31
|
+
*
|
|
32
|
+
* > For better development experience cookies can be saved into file, so a session can be reused while writing tests.
|
|
33
|
+
*
|
|
34
|
+
* #### Usage
|
|
35
|
+
*
|
|
36
|
+
* 1. Enable this plugin and configure as described below
|
|
37
|
+
* 2. Define user session names (example: `user`, `editor`, `admin`, etc).
|
|
38
|
+
* 3. Define how users are logged in and how to check that user is logged in
|
|
39
|
+
* 4. Use `login` object inside your tests to log in:
|
|
40
|
+
*
|
|
41
|
+
* ```js
|
|
42
|
+
* // inside a test file
|
|
43
|
+
* // use login to inject auto-login function
|
|
44
|
+
* Feature('Login');
|
|
45
|
+
*
|
|
46
|
+
* Before(({ login }) => {
|
|
47
|
+
* login('user'); // login using user session
|
|
48
|
+
* });
|
|
49
|
+
*
|
|
50
|
+
* // Alternatively log in for one scenario.
|
|
51
|
+
* Scenario('log me in', ( { I, login } ) => {
|
|
52
|
+
* login('admin');
|
|
53
|
+
* I.see('I am logged in');
|
|
54
|
+
* });
|
|
55
|
+
* ```
|
|
56
|
+
*
|
|
57
|
+
* #### Configuration
|
|
58
|
+
*
|
|
59
|
+
* * `saveToFile` (default: false) - save cookies to file. Allows to reuse session between execution.
|
|
60
|
+
* * `inject` (default: `login`) - name of the login function to use
|
|
61
|
+
* * `users` - an array containing different session names and functions to:
|
|
62
|
+
* * `login` - sign in into the system
|
|
63
|
+
* * `check` - check that user is logged in
|
|
64
|
+
* * `fetch` - to get current cookies (by default `I.grabCookie()`)
|
|
65
|
+
* * `restore` - to set cookies (by default `I.amOnPage('/'); I.setCookie(cookie)`)
|
|
66
|
+
*
|
|
67
|
+
* #### How It Works
|
|
68
|
+
*
|
|
69
|
+
* 1. `restore` method is executed. It should open a page and set credentials.
|
|
70
|
+
* 2. `check` method is executed. It should reload a page (so cookies are applied) and check that this page belongs to logged-in user. When you pass the second args `session`, you could perform the validation using passed session.
|
|
71
|
+
* 3. If `restore` and `check` were not successful, `login` is executed
|
|
72
|
+
* 4. `login` should fill in login form
|
|
73
|
+
* 5. After successful login, `fetch` is executed to save cookies into memory or file.
|
|
74
|
+
*
|
|
75
|
+
* #### Example: Simple login
|
|
76
|
+
*
|
|
77
|
+
* ```js
|
|
78
|
+
* auth: {
|
|
79
|
+
* enabled: true,
|
|
80
|
+
* saveToFile: true,
|
|
81
|
+
* inject: 'login',
|
|
82
|
+
* users: {
|
|
83
|
+
* admin: {
|
|
84
|
+
* // loginAdmin function is defined in `steps_file.js`
|
|
85
|
+
* login: (I) => I.loginAdmin(),
|
|
86
|
+
* // if we see `Admin` on page, we assume we are logged in
|
|
87
|
+
* check: (I) => {
|
|
88
|
+
* I.amOnPage('/');
|
|
89
|
+
* I.see('Admin');
|
|
90
|
+
* }
|
|
91
|
+
* }
|
|
92
|
+
* }
|
|
93
|
+
* }
|
|
94
|
+
* ```
|
|
95
|
+
*
|
|
96
|
+
* #### Example: Multiple users
|
|
97
|
+
*
|
|
98
|
+
* ```js
|
|
99
|
+
* auth: {
|
|
100
|
+
* enabled: true,
|
|
101
|
+
* saveToFile: true,
|
|
102
|
+
* inject: 'loginAs', // use `loginAs` instead of login
|
|
103
|
+
* users: {
|
|
104
|
+
* user: {
|
|
105
|
+
* login: (I) => {
|
|
106
|
+
* I.amOnPage('/login');
|
|
107
|
+
* I.fillField('email', 'user@site.com');
|
|
108
|
+
* I.fillField('password', '123456');
|
|
109
|
+
* I.click('Login');
|
|
110
|
+
* },
|
|
111
|
+
* check: (I) => {
|
|
112
|
+
* I.amOnPage('/');
|
|
113
|
+
* I.see('User', '.navbar');
|
|
114
|
+
* },
|
|
115
|
+
* },
|
|
116
|
+
* admin: {
|
|
117
|
+
* login: (I) => {
|
|
118
|
+
* I.amOnPage('/login');
|
|
119
|
+
* I.fillField('email', 'admin@site.com');
|
|
120
|
+
* I.fillField('password', '123456');
|
|
121
|
+
* I.click('Login');
|
|
122
|
+
* },
|
|
123
|
+
* check: (I) => {
|
|
124
|
+
* I.amOnPage('/');
|
|
125
|
+
* I.see('Admin', '.navbar');
|
|
126
|
+
* },
|
|
127
|
+
* },
|
|
128
|
+
* }
|
|
129
|
+
* }
|
|
130
|
+
* ```
|
|
131
|
+
*
|
|
132
|
+
* #### Example: Keep cookies between tests
|
|
133
|
+
*
|
|
134
|
+
* If you decide to keep cookies between tests you don't need to save/retrieve cookies between tests.
|
|
135
|
+
* But you need to login once work until session expires.
|
|
136
|
+
* For this case, disable `fetch` and `restore` methods.
|
|
137
|
+
*
|
|
138
|
+
* ```js
|
|
139
|
+
* helpers: {
|
|
140
|
+
* WebDriver: {
|
|
141
|
+
* // config goes here
|
|
142
|
+
* keepCookies: true; // keep cookies for all tests
|
|
143
|
+
* }
|
|
144
|
+
* },
|
|
145
|
+
* plugins: {
|
|
146
|
+
* auth: {
|
|
147
|
+
* users: {
|
|
148
|
+
* admin: {
|
|
149
|
+
* login: (I) => {
|
|
150
|
+
* I.amOnPage('/login');
|
|
151
|
+
* I.fillField('email', 'admin@site.com');
|
|
152
|
+
* I.fillField('password', '123456');
|
|
153
|
+
* I.click('Login');
|
|
154
|
+
* },
|
|
155
|
+
* check: (I) => {
|
|
156
|
+
* I.amOnPage('/dashboard');
|
|
157
|
+
* I.see('Admin', '.navbar');
|
|
158
|
+
* },
|
|
159
|
+
* fetch: () => {}, // empty function
|
|
160
|
+
* restore: () => {}, // empty funciton
|
|
161
|
+
* }
|
|
162
|
+
* }
|
|
163
|
+
* }
|
|
164
|
+
* }
|
|
165
|
+
* ```
|
|
166
|
+
*
|
|
167
|
+
* #### Example: Getting sessions from local storage
|
|
168
|
+
*
|
|
169
|
+
* If your session is stored in local storage instead of cookies you still can obtain sessions.
|
|
170
|
+
*
|
|
171
|
+
* ```js
|
|
172
|
+
* plugins: {
|
|
173
|
+
* auth: {
|
|
174
|
+
* admin: {
|
|
175
|
+
* login: (I) => I.loginAsAdmin(),
|
|
176
|
+
* check: (I) => I.see('Admin', '.navbar'),
|
|
177
|
+
* fetch: (I) => {
|
|
178
|
+
* return I.executeScript(() => localStorage.getItem('session_id'));
|
|
179
|
+
* },
|
|
180
|
+
* restore: (I, session) => {
|
|
181
|
+
* I.amOnPage('/');
|
|
182
|
+
* I.executeScript((session) => localStorage.setItem('session_id', session), session);
|
|
183
|
+
* },
|
|
184
|
+
* }
|
|
185
|
+
* }
|
|
186
|
+
* }
|
|
187
|
+
* ```
|
|
188
|
+
*
|
|
189
|
+
* #### Tips: Using async function in the auth
|
|
190
|
+
*
|
|
191
|
+
* If you use async functions in the auth plugin, login function should be used with `await` keyword.
|
|
192
|
+
*
|
|
193
|
+
* ```js
|
|
194
|
+
* auth: {
|
|
195
|
+
* enabled: true,
|
|
196
|
+
* saveToFile: true,
|
|
197
|
+
* inject: 'login',
|
|
198
|
+
* users: {
|
|
199
|
+
* admin: {
|
|
200
|
+
* login: async (I) => { // If you use async function in the auth plugin
|
|
201
|
+
* const phrase = await I.grabTextFrom('#phrase')
|
|
202
|
+
* I.fillField('username', 'admin'),
|
|
203
|
+
* I.fillField('password', 'password')
|
|
204
|
+
* I.fillField('phrase', phrase)
|
|
205
|
+
* },
|
|
206
|
+
* check: (I) => {
|
|
207
|
+
* I.amOnPage('/');
|
|
208
|
+
* I.see('Admin');
|
|
209
|
+
* },
|
|
210
|
+
* }
|
|
211
|
+
* }
|
|
212
|
+
* }
|
|
213
|
+
* ```
|
|
214
|
+
*
|
|
215
|
+
* ```js
|
|
216
|
+
* Scenario('login', async ( {I, login} ) => {
|
|
217
|
+
* await login('admin') // you should use `await`
|
|
218
|
+
* })
|
|
219
|
+
* ```
|
|
220
|
+
*
|
|
221
|
+
* #### Tips: Using session to validate user
|
|
222
|
+
*
|
|
223
|
+
* Instead of asserting on page elements for the current user in `check`, you can use the `session` you saved in `fetch`
|
|
224
|
+
*
|
|
225
|
+
* ```js
|
|
226
|
+
* auth: {
|
|
227
|
+
* enabled: true,
|
|
228
|
+
* saveToFile: true,
|
|
229
|
+
* inject: 'login',
|
|
230
|
+
* users: {
|
|
231
|
+
* admin: {
|
|
232
|
+
* login: async (I) => { // If you use async function in the auth plugin
|
|
233
|
+
* const phrase = await I.grabTextFrom('#phrase')
|
|
234
|
+
* I.fillField('username', 'admin'),
|
|
235
|
+
* I.fillField('password', 'password')
|
|
236
|
+
* I.fillField('phrase', phrase)
|
|
237
|
+
* },
|
|
238
|
+
* check: (I, session) => {
|
|
239
|
+
* // Throwing an error in `check` will make CodeceptJS perform the login step for the user
|
|
240
|
+
* if (session.profile.email !== the.email.you.expect@some-mail.com) {
|
|
241
|
+
* throw new Error ('Wrong user signed in');
|
|
242
|
+
* }
|
|
243
|
+
* },
|
|
244
|
+
* }
|
|
245
|
+
* }
|
|
246
|
+
* }
|
|
247
|
+
* ```
|
|
248
|
+
*
|
|
249
|
+
* ```js
|
|
250
|
+
* Scenario('login', async ( {I, login} ) => {
|
|
251
|
+
* await login('admin') // you should use `await`
|
|
252
|
+
* })
|
|
253
|
+
*
|
|
254
|
+
*
|
|
255
|
+
*/
|
|
256
|
+
module.exports = function (config) {
|
|
257
|
+
config = Object.assign(defaultConfig, config)
|
|
258
|
+
Object.keys(config.users).map(
|
|
259
|
+
u =>
|
|
260
|
+
(config.users[u] = {
|
|
261
|
+
...defaultUser,
|
|
262
|
+
...config.users[u],
|
|
263
|
+
}),
|
|
264
|
+
)
|
|
265
|
+
|
|
266
|
+
if (config.saveToFile) {
|
|
267
|
+
// loading from file
|
|
268
|
+
loadCookiesFromFile(config)
|
|
269
|
+
}
|
|
270
|
+
|
|
271
|
+
const loginFunction = async name => {
|
|
272
|
+
const I = container.support('I')
|
|
273
|
+
const userSession = config.users[name]
|
|
274
|
+
|
|
275
|
+
if (!userSession) {
|
|
276
|
+
throw new Error(`User '${name}' was not configured for authorization in auth plugin. Add it to the plugin config`)
|
|
277
|
+
}
|
|
278
|
+
|
|
279
|
+
const test = store.currentTest
|
|
280
|
+
|
|
281
|
+
// we are in BeforeSuite hook
|
|
282
|
+
if (!test) {
|
|
283
|
+
enableAuthBeforeEachTest(name)
|
|
284
|
+
return
|
|
285
|
+
}
|
|
286
|
+
|
|
287
|
+
const section = new Section(`I am logged in as ${name}`)
|
|
288
|
+
|
|
289
|
+
if (config.saveToFile && !store[`${name}_session`]) {
|
|
290
|
+
loadCookiesFromFile(config)
|
|
291
|
+
}
|
|
292
|
+
|
|
293
|
+
if (isPlaywrightSession() && test?.opts?.cookies) {
|
|
294
|
+
if (test.opts.user == name) {
|
|
295
|
+
debug(`Cookies already loaded for ${name}`)
|
|
296
|
+
|
|
297
|
+
alreadyLoggedIn(name)
|
|
298
|
+
return
|
|
299
|
+
} else {
|
|
300
|
+
debug(`Cookies already loaded for ${test.opts.user}, but not for ${name}`)
|
|
301
|
+
await I.deleteCookie()
|
|
302
|
+
}
|
|
303
|
+
}
|
|
304
|
+
|
|
305
|
+
section.start()
|
|
306
|
+
|
|
307
|
+
const cookies = store[`${name}_session`]
|
|
308
|
+
const shouldAwait = isAsyncFunction(userSession.login) || isAsyncFunction(userSession.restore) || isAsyncFunction(userSession.check)
|
|
309
|
+
|
|
310
|
+
const loginAndSave = async () => {
|
|
311
|
+
if (shouldAwait) {
|
|
312
|
+
await userSession.login(I)
|
|
313
|
+
} else {
|
|
314
|
+
userSession.login(I)
|
|
315
|
+
}
|
|
316
|
+
|
|
317
|
+
section.end()
|
|
318
|
+
const cookies = await userSession.fetch(I)
|
|
319
|
+
if (!cookies) {
|
|
320
|
+
debug("Cannot save user session with empty cookies from auto login's fetch method")
|
|
321
|
+
return
|
|
322
|
+
}
|
|
323
|
+
if (config.saveToFile) {
|
|
324
|
+
debug(`Saved user session into file for ${name}`)
|
|
325
|
+
fs.writeFileSync(path.join(global.output_dir, `${name}_session.json`), JSON.stringify(cookies))
|
|
326
|
+
}
|
|
327
|
+
store[`${name}_session`] = cookies
|
|
328
|
+
}
|
|
329
|
+
|
|
330
|
+
if (!cookies) return loginAndSave()
|
|
331
|
+
|
|
332
|
+
recorder.session.start('check login')
|
|
333
|
+
if (shouldAwait) {
|
|
334
|
+
await userSession.restore(I, cookies)
|
|
335
|
+
await userSession.check(I, cookies)
|
|
336
|
+
} else {
|
|
337
|
+
userSession.restore(I, cookies)
|
|
338
|
+
userSession.check(I, cookies)
|
|
339
|
+
}
|
|
340
|
+
section.end()
|
|
341
|
+
recorder.session.catch(err => {
|
|
342
|
+
debug(`Failed auto login for ${name} due to ${err}`)
|
|
343
|
+
debug('Logging in again')
|
|
344
|
+
recorder.session.start('auto login')
|
|
345
|
+
return loginAndSave()
|
|
346
|
+
.then(() => {
|
|
347
|
+
recorder.add(() => recorder.session.restore('auto login'))
|
|
348
|
+
recorder.catch(() => debug('continue'))
|
|
349
|
+
})
|
|
350
|
+
.catch(err => {
|
|
351
|
+
recorder.session.restore('auto login')
|
|
352
|
+
recorder.session.restore('check login')
|
|
353
|
+
section.end()
|
|
354
|
+
recorder.throw(err)
|
|
355
|
+
})
|
|
356
|
+
})
|
|
357
|
+
recorder.add(() => {
|
|
358
|
+
recorder.session.restore('check login')
|
|
359
|
+
})
|
|
360
|
+
|
|
361
|
+
return recorder.promise()
|
|
362
|
+
}
|
|
363
|
+
|
|
364
|
+
function enableAuthBeforeEachTest(name) {
|
|
365
|
+
const suite = store.currentSuite
|
|
366
|
+
if (!suite) return
|
|
367
|
+
|
|
368
|
+
debug(`enabling auth as ${name} for each test of suite ${suite.title}`)
|
|
369
|
+
|
|
370
|
+
// we are setting test opts so they can be picked up by Playwright if it starts browser for this test
|
|
371
|
+
suite.eachTest(test => {
|
|
372
|
+
// preload from store
|
|
373
|
+
if (store[`${name}_session`]) {
|
|
374
|
+
test.opts.cookies = store[`${name}_session`]
|
|
375
|
+
test.opts.user = name
|
|
376
|
+
return
|
|
377
|
+
}
|
|
378
|
+
|
|
379
|
+
if (!config.saveToFile) return
|
|
380
|
+
const cookieFile = path.join(global.output_dir, `${name}_session.json`)
|
|
381
|
+
|
|
382
|
+
if (!fileExists(cookieFile)) {
|
|
383
|
+
return
|
|
384
|
+
}
|
|
385
|
+
|
|
386
|
+
const context = fs.readFileSync(cookieFile).toString()
|
|
387
|
+
test.opts.cookies = JSON.parse(context)
|
|
388
|
+
test.opts.user = name
|
|
389
|
+
})
|
|
390
|
+
|
|
391
|
+
function runLoginFunctionForTest(test) {
|
|
392
|
+
if (!suite.tests.includes(test)) return
|
|
393
|
+
// let's call this function to ensure that authorization happened
|
|
394
|
+
// if no cookies, it will login and save them
|
|
395
|
+
loginFunction(name)
|
|
396
|
+
}
|
|
397
|
+
|
|
398
|
+
// we are in BeforeSuite hook
|
|
399
|
+
event.dispatcher.on(event.test.started, runLoginFunctionForTest)
|
|
400
|
+
event.dispatcher.on(event.suite.after, () => {
|
|
401
|
+
event.dispatcher.off(event.test.started, runLoginFunctionForTest)
|
|
402
|
+
})
|
|
403
|
+
}
|
|
404
|
+
|
|
405
|
+
// adding this to DI container
|
|
406
|
+
const support = {}
|
|
407
|
+
support[config.inject] = loginFunction
|
|
408
|
+
container.append({ support })
|
|
409
|
+
|
|
410
|
+
return loginFunction
|
|
411
|
+
}
|
|
412
|
+
|
|
413
|
+
function loadCookiesFromFile(config) {
|
|
414
|
+
for (const name in config.users) {
|
|
415
|
+
const fileName = path.join(global.output_dir, `${name}_session.json`)
|
|
416
|
+
if (!fileExists(fileName)) continue
|
|
417
|
+
const data = fs.readFileSync(fileName).toString()
|
|
418
|
+
try {
|
|
419
|
+
store[`${name}_session`] = JSON.parse(data)
|
|
420
|
+
} catch (err) {
|
|
421
|
+
throw new Error(`Could not load session from ${fileName}\n${err}`)
|
|
422
|
+
}
|
|
423
|
+
debug(`Loaded user session for ${name}`)
|
|
424
|
+
}
|
|
425
|
+
}
|
|
426
|
+
|
|
427
|
+
function isPlaywrightSession() {
|
|
428
|
+
return !!container.helpers('Playwright')
|
|
429
|
+
}
|
|
430
|
+
|
|
431
|
+
function alreadyLoggedIn(name) {
|
|
432
|
+
const step = new CommentStep('am logged in as')
|
|
433
|
+
step.actor = 'I'
|
|
434
|
+
return step.addToRecorder([name])
|
|
435
|
+
}
|
package/lib/plugin/autoDelay.js
CHANGED
|
@@ -2,8 +2,8 @@ const Container = require('../container')
|
|
|
2
2
|
const store = require('../store')
|
|
3
3
|
const recorder = require('../recorder')
|
|
4
4
|
const event = require('../event')
|
|
5
|
-
const log = require('../output')
|
|
6
|
-
const
|
|
5
|
+
const { log } = require('../output')
|
|
6
|
+
const standardActingHelpers = Container.STANDARD_ACTING_HELPERS
|
|
7
7
|
|
|
8
8
|
const methodsToDelay = ['click', 'fillField', 'checkOption', 'pressKey', 'doubleClick', 'rightClick']
|
|
9
9
|
|
|
@@ -52,13 +52,13 @@ const defaultConfig = {
|
|
|
52
52
|
*
|
|
53
53
|
*/
|
|
54
54
|
module.exports = function (config) {
|
|
55
|
-
|
|
55
|
+
const affectedHelpers = [...standardActingHelpers, 'REST']
|
|
56
56
|
const helpers = Container.helpers()
|
|
57
57
|
let helper
|
|
58
58
|
|
|
59
59
|
config = Object.assign(defaultConfig, config)
|
|
60
60
|
|
|
61
|
-
for (const helperName of
|
|
61
|
+
for (const helperName of affectedHelpers) {
|
|
62
62
|
if (Object.keys(helpers).indexOf(helperName) > -1) {
|
|
63
63
|
helper = helpers[helperName]
|
|
64
64
|
}
|
|
@@ -66,25 +66,25 @@ module.exports = function (config) {
|
|
|
66
66
|
|
|
67
67
|
if (!helper) return // no helpers for auto-delay
|
|
68
68
|
|
|
69
|
-
event.dispatcher.on(event.step.before,
|
|
69
|
+
event.dispatcher.on(event.step.before, step => {
|
|
70
70
|
if (config.methods.indexOf(step.helperMethod) < 0) return // skip non-actions
|
|
71
71
|
|
|
72
72
|
recorder.add('auto-delay', async () => {
|
|
73
73
|
if (store.debugMode) return // no need to delay in debug
|
|
74
74
|
log(`Delaying for ${config.delayBefore}ms`)
|
|
75
|
-
return new Promise(
|
|
75
|
+
return new Promise(resolve => {
|
|
76
76
|
setTimeout(resolve, config.delayBefore)
|
|
77
77
|
})
|
|
78
78
|
})
|
|
79
79
|
})
|
|
80
80
|
|
|
81
|
-
event.dispatcher.on(event.step.after,
|
|
81
|
+
event.dispatcher.on(event.step.after, step => {
|
|
82
82
|
if (config.methods.indexOf(step.helperMethod) < 0) return // skip non-actions
|
|
83
83
|
|
|
84
84
|
recorder.add('auto-delay', async () => {
|
|
85
85
|
if (store.debugMode) return // no need to delay in debug
|
|
86
86
|
log(`Delaying for ${config.delayAfter}ms`)
|
|
87
|
-
return new Promise(
|
|
87
|
+
return new Promise(resolve => {
|
|
88
88
|
setTimeout(resolve, config.delayAfter)
|
|
89
89
|
})
|
|
90
90
|
})
|