Skip to content

Commit dc036b8

Browse files
authored
improve the check method (#3935)
1 parent e39b82b commit dc036b8

File tree

2 files changed

+70
-4
lines changed

2 files changed

+70
-4
lines changed

docs/plugins.md

Lines changed: 35 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@ Scenario('log me in', ( {I, login} ) => {
8989
#### How It Works
9090

9191
1. `restore` method is executed. It should open a page and set credentials.
92-
2. `check` method is executed. It should reload a page (so cookies are applied) and check that this page belongs to logged in user.
92+
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.
9393
3. If `restore` and `check` were not successful, `login` is executed
9494
4. `login` should fill in login form
9595
5. After successful login, `fetch` is executed to save cookies into memory or file.
@@ -240,6 +240,40 @@ Scenario('login', async ( {I, login} ) => {
240240
})
241241
```
242242

243+
#### Tips: Using session to validate user
244+
245+
Instead of asserting on page elements for the current user in `check`, you can use the `session` you saved in `fetch`
246+
247+
```js
248+
autoLogin: {
249+
enabled: true,
250+
saveToFile: true,
251+
inject: 'login',
252+
users: {
253+
admin: {
254+
login: async (I) => { // If you use async function in the autoLogin plugin
255+
const phrase = await I.grabTextFrom('#phrase')
256+
I.fillField('username', 'admin'),
257+
I.fillField('password', 'password')
258+
I.fillField('phrase', phrase)
259+
},
260+
check: (I, session) => {
261+
// Throwing an error in `check` will make CodeceptJS perform the login step for the user
262+
if (session.profile.email !== the.email.you.expect@some-mail.com) {
263+
throw new Error ('Wrong user signed in');
264+
}
265+
},
266+
}
267+
}
268+
}
269+
```
270+
271+
```js
272+
Scenario('login', async ( {I, login} ) => {
273+
await login('admin') // you should use `await`
274+
})
275+
```
276+
243277
### Parameters
244278

245279
- `config`

lib/plugin/autoLogin.js

Lines changed: 35 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ const defaultConfig = {
6161
* #### How It Works
6262
*
6363
* 1. `restore` method is executed. It should open a page and set credentials.
64-
* 2. `check` method is executed. It should reload a page (so cookies are applied) and check that this page belongs to logged in user.
64+
* 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.
6565
* 3. If `restore` and `check` were not successful, `login` is executed
6666
* 4. `login` should fill in login form
6767
* 5. After successful login, `fetch` is executed to save cookies into memory or file.
@@ -212,6 +212,38 @@ const defaultConfig = {
212212
* })
213213
* ```
214214
*
215+
* #### Tips: Using session to validate user
216+
*
217+
* Instead of asserting on page elements for the current user in `check`, you can use the `session` you saved in `fetch`
218+
*
219+
* ```js
220+
* autoLogin: {
221+
* enabled: true,
222+
* saveToFile: true,
223+
* inject: 'login',
224+
* users: {
225+
* admin: {
226+
* login: async (I) => { // If you use async function in the autoLogin plugin
227+
* const phrase = await I.grabTextFrom('#phrase')
228+
* I.fillField('username', 'admin'),
229+
* I.fillField('password', 'password')
230+
* I.fillField('phrase', phrase)
231+
* },
232+
* check: (I, session) => {
233+
* // Throwing an error in `check` will make CodeceptJS perform the login step for the user
234+
* if (session.profile.email !== the.email.you.expect@some-mail.com) {
235+
* throw new Error ('Wrong user signed in');
236+
* }
237+
* },
238+
* }
239+
* }
240+
* }
241+
* ```
242+
*
243+
* ```js
244+
* Scenario('login', async ( {I, login} ) => {
245+
* await login('admin') // you should use `await`
246+
* })
215247
*
216248
*
217249
*/
@@ -264,10 +296,10 @@ module.exports = function (config) {
264296
recorder.session.start('check login');
265297
if (shouldAwait) {
266298
await userSession.restore(I, cookies);
267-
await userSession.check(I);
299+
await userSession.check(I, cookies);
268300
} else {
269301
userSession.restore(I, cookies);
270-
userSession.check(I);
302+
userSession.check(I, cookies);
271303
}
272304
recorder.session.catch((err) => {
273305
debug(`Failed auto login for ${name} due to ${err}`);

0 commit comments

Comments
 (0)