Skip to content

Feature request: Allow check in autoLogin plugin to access session data #3759

@jhalborg

Description

@jhalborg

Hi! 👋

Firstly, thanks for your work on this project! 🙂

Today I used patch-package to patch [email protected] for the project I'm working on.

The intention of the patch/PR is to allow the check function in the autoLogin plugin to access the session and perform logic based on that instead of asserting on page elements.

Here is the diff that solved my problem:

diff --git a/node_modules/codeceptjs/lib/plugin/autoLogin.js b/node_modules/codeceptjs/lib/plugin/autoLogin.js
index 0763c29..4420f79 100644
--- a/node_modules/codeceptjs/lib/plugin/autoLogin.js
+++ b/node_modules/codeceptjs/lib/plugin/autoLogin.js
@@ -211,6 +211,38 @@ const defaultConfig = {
  *   await login('admin') // you should use `await`
  * })
  * ```
+ 
+ * #### Tips: Using session to validate user
+ *
+ * Instead of asserting on page elements for the current user in `check`, you can use the `session` you saved in `fetch`
+ *
+ * ```js
+ * autoLogin: {
+ *   enabled: true,
+ *   saveToFile: true,
+ *   inject: 'login',
+ *   users: {
+ *     admin: {
+ *       login: async (I) => {  // If you use async function in the autoLogin plugin
+ *          const phrase = await I.grabTextFrom('#phrase')
+ *          I.fillField('username', 'admin'),
+ *          I.fillField('password', 'password')
+ *          I.fillField('phrase', phrase)
+ *       },
+ *       check: (I, session) => {
+ *          // Throwing an error in `check` will make codecept perform the login step for the user
+ *          if(session.profile.email !== [email protected]) { throw new Error ('Wrong user signed in')}
+ *       },
+ *     }
+ *   }
+ * }
+ * ```
+ *
+ * ```js
+ * Scenario('login', async ( {I, login} ) => {
+ *   await login('admin') // you should use `await`
+ * })
+ * ```
  *
  *
  *
@@ -268,10 +300,10 @@ module.exports = function (config) {
     recorder.session.start('check login');
     if (shouldAwait) {
       await userSession.restore(I, cookies);
-      await userSession.check(I);
+      await userSession.check(I, cookies);
     } else {
       userSession.restore(I, cookies);
-      userSession.check(I);
+      userSession.check(I, cookies);
     }
     recorder.session.catch((err) => {
       debug(`Failed auto login for ${name} due to ${err}`);

This issue body was partially generated by patch-package.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions