Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Mutualise autologin plugin implementation through npm package #3629

Unanswered
Horsty80 asked this question in Q&A
Discussion options

Hey community 😇

I try to mutualise autologin implementation inside a shared library.

My actual codecept.conf.*

 plugins: {
 autoLogin: {
 enabled: true,
 saveToFile: false,
 inject: 'loginAs',
 users: {
 basicUser: getAutoLogin(),
 },
 },

My getAutoLogin methods in another file

export function getAutoLogin() {
 return {
 login: async (I: CodeceptJS.I) => {
 I.customLogin()
 },
 check: async (I: CodeceptJS.I) => {
 I.see('I am logged');
 },
 fetch: async (I: CodeceptJS.I) => {
 return await I.grabCookie();
 },
 restore: async (I: CodeceptJS.I, cookie: CodeceptJS.Cookie) => {
 I.setCookie(cookie);
 },
 };
}

And the customLogin method is defined inside actor with this and locate builder.

const { I } = inject();
export = () =>
 actor({
 customLogin: () => {
 I.amOnPage(url);
 I.fillField(username, locate("$usernameField"));
 I.fillField(password, locate("$passwordField"));
 I.click(loginButton);
 } 
 });

Like this everything is working.
So now i want to use the same method getAutoLogin() with some variable but the main concept is the same throught a npm module.
But isn't working. When i try, i was unable to use locate() or inject() or even I
I've this king of error. (depending on some try of implementation)

ReferenceError: I is not defined
ReferenceError: inject is not defined
ReferenceError: locate is not defined

The only difference is that i'm not using actor() anymore.
And my ts file is like this

export function getAutoLogin() {
 const { I } = inject();
 return {
 login: async (I: CodeceptJS.I) => {
 I.amOnPage(url);
 I.fillField(username, locate("$usernameField"));
 I.fillField(password, locate("$passwordField"));
 I.click(loginButton);
 },
 check: async (I: CodeceptJS.I) => {
 I.see('I am logged');
 },
 fetch: async (I: CodeceptJS.I) => {
 return await I.grabCookie();
 },
 restore: async (I: CodeceptJS.I, cookie: CodeceptJS.Cookie) => {
 I.setCookie(cookie);
 },
 };
}

my codecept.conf.* doesn't change only the import path of getAutoLogin is now from my package than a local file

const { getAutoLogin } = require('@myCustomPackage/codeceptjs');
 plugins: {
 autoLogin: {
 enabled: true,
 saveToFile: false,
 inject: 'loginAs',
 users: {
 basicUser: getAutoLogin(),
 },
 },

How can achieve this kind of tricky stuff ?
My purpose is to mutualise my returned autologin response and use it through a lot of application.
Each application are in a isolated repo, but the login process are the same, that means each app using the same login form.
And repeat the same autologin configuration are a bit annoying. We want to make update only once on the check or login step than update on each application e2e codebase. Only pull new version of the package.

Someone can help me :)
Thanks a lot

You must be logged in to vote

Replies: 1 comment 1 reply

Comment options

hey @Horsty80 sometimes when I need to modify the npm package, I use npm patch-package for that purpose, hope that helps
Like you adjust this https://github.com/codeceptjs/CodeceptJS/blob/3.x/lib/plugin/autoLogin.js file in node_modules and let the patch package cares the rest

You must be logged in to vote
1 reply
Comment options

Thank for the reply, i don't think is that i need to.

I don't want modify autoLogin.js, just use it with a method that i share through a npm package.

If the method is in my e2e test the dedicated method is working (getAutoLogin that return object with login/check/fetch/restore) because the inject dependencies are working fine.
But if the method is get from a npm package isn't working because i can not get access to I

I use this npm package for Helper and it's working fine with inject() method.

I presume that is due to when i use inject(), in autoLogin method, codecept isn't init or something like that i don't know.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet

AltStyle によって変換されたページ (->オリジナル) /