Online Training: Web test automation with CodeceptJS. Sign up now!

How to use the autoLogin plugin

:thinking:i was connfused when i watched the guide on the official website.
i dont know how to configure it.
i should add the sample to where?
and my case is that i need a sms code and picture code to login

here is the guide url : https://codecept.io/plugins#autologin

I followed the guide and still don’t quite understand how it works. I added a ‘login’ function to the actor({}) object returned in the function in steps_file.js. I still just keep getting Object of type login is not defined in container

I think a more concrete example would be helpful here.

Scouring the net for answers as we speak

If you are using autoLogin plugin then, you will need a config placed in the plugins section specifically for your autoLogin.

Example:

autoLogin: {
            enabled: true,
            saveToFile: true,
            inject: 'loginAs', // use `loginAs` instead of login
            users: {
                admin: {
                    login: (I) => {
                        I.amOnPage('/login-menu');
                        I.fillField('#login_email', cfg.personas.Admin.username);
                        I.fillField('#login_password', cfg.personas.Admin.pwd);
                        I.click('#login_login');
                    },
                    check: (I) => {
                        I.seeInCurrentUrl('/admin/');
                    },
                },
                servicePro: {
                    login: (I) => {
                        I.amOnPage('/login-menu');
                        I.fillField('#login_email', cfg.personas.servicePro.username);
                        I.fillField('#login_password', cfg.personas.servicePro.pwd);
                        I.click('#login_login');
                    },
                    check: (I) => {
                        I.seeInCurrentUrl('/service-pro/new-service-requests');
                    },
                },
                consumer: {
                    login: (I) => {
                        I.amOnPage('/login-menu');
                        I.fillField('#login_email', cfg.personas.consumer.username);
                        I.fillField('#login_password', cfg.personas.consumer.pwd);
                        I.click('#login_login');
                    },
                    check: (I) => {
                        I.seeInCurrentUrl('/consumer/my-projects');
                    },
                },
            },
        },

Then in your test you call it as follows:

Given('I login as a Service Pro', async () => {
    await loginAs('servicePro');
});

If you are using the Actor approach this is a bit different.
Actor Example:

module.exports = function () {
    return actor({

        // Define custom steps here, use 'this' to access default methods of I.
        // It is recommended to place a general 'login' function here.
        async login() {
            this.amOnPage(`/?sba_transferToken=${conf.testAccount.ssoTransferToken}`);
            const balance = await this.grabTextFrom(loginComponent.headerBar);
            if (typeof balance !== 'string') {
                output.error('Balance not found, login failed');
            }
            console.log(`Logged in as user: ${conf.testAccount.userName}`);
        },
    });
};

In your tests you use it like this:

Before(async () => {
    await I.login();
});