英文:
Login a user by JWT token in cypress
问题
I have a scenario where I need to call multiple APIs to obtain the access key and secret key to fetch an AWS Secret Manager secret key. I then use those keys to generate a JWT token and log in the user. Although I can successfully log in, any command I write after that results in an exception being thrown.
> CypressError: Cypress detected that you returned a promise from a
> command while also invoking one or more cy commands in that promise.
code:
helper class:
generateTokenAndLogin(email: string) {
cy.request({
url: "https://xxx/get-access-key",
}).then((access) => {
this.accessKey = access.body.value;
cy.request({
url: "https://xxx/get-secret-key",
}).then((secret) => {
this.secretKey = secret.body.value;
//this is returning secret key correctly
this.getSecretKey().then((response) => {
//fetch secret from aws
cy.task("jwt", {
payload: {
email: email,
},
privateKey: response.SecretString as string,
}).then((token) => {
cy.visit(`myweb.com/login?id_token=${token}`);
});
});
});
});
}
inside module.exports
on('task', {
jwt(data: any) {
const token = jwt.sign(data.payload, data.privateKey, { algorithm: 'RS256', expiresIn: '1h'});
return token;
}
});
cypress test:
describe('Testing', () => {
const jwtHelper = new JWTHelper();
before(()=> {
jwtHelper.generateToken();
})
it('JWT', () => {
cy.get('left-nav').should('be.visible'); //failing on all subsequent commands
});
});
The Cypress login function is successful when there is no command after that, all the subsequent commands are failing.
英文:
I have a scenario where I need to call multiple APIs to obtain the access key and secret key to fetch an AWS Secret Manager secret key. I then use those keys to generate a JWT token and log in the user. Although I can successfully log in, any command I write after that results in an exception being thrown.
> CypressError: Cypress detected that you returned a promise from a
> command while also invoking one or more cy commands in that promise.
code:
helper class:
generateTokenAndLogin(email: string) {
cy.request({
url: "https://xxx/get-access-key",
}).then((access) => {
this.accessKey = access.body.value;
cy.request({
url: "https://xxx/get-secret-key",
}).then((secret) => {
this.secretKey = secret.body.value;
//this is returning secret key correctly
this.getSecretKey().then((response) => {
//fetch secret from aws
cy.task("jwt", {
payload: {
email: email,
},
privateKey: response.SecretString as string,
}).then((token) => {
cy.visit(`myweb.com/login?id_token=${token}`);
});
});
});
});
}
inside module.exports
on('task', {
jwt(data: any) {
const token = jwt.sign(data.payload, data.privateKey, { algorithm: 'RS256', expiresIn: '1h'});
return token;
}
});
cypress test:
describe('Testing', () => {
const jwtHelper = new JWTHelper();
before(()=> {
jwtHelper.generateToken();
})
it('JWT', () => {
cy.get('left-nav').should('be.visible'); //failing on all subsequent commands
});
});
The Cypress login function is successful when there is no command after that, all the subsequent commands are failing.
答案1
得分: 2
这可能是深层嵌套的 cy.visit()
导致的问题,你可以在错误消息的正文中进行检查。
如果是这样,尝试将令牌保存为环境变量。
generateTokenAndLogin(email: string) {
cy.request('https://xxx/get-access-key').then((access) => {
this.accessKey = access.body.value;
cy.request('https://xxx/get-secret-key').then((secret) => {
this.secretKey = secret.body.value;
// 此处正确返回秘钥
this.getSecretKey().then((response) => {
// 从 AWS 获取秘钥
cy.task("jwt", {
payload: {
email: email,
},
privateKey: response.SecretString as string,
})
.then(token => Cypress.env('token', token))
});
});
});
}
const jwtHelper = new JWTHelper();
before(() => {
jwtHelper.generateToken();
})
beforeEach(() => {
cy.visit(`myweb.com/login?id_token=${Cypress.env('token')}`)
})
it('JWT', () => {
cy.get('left-nav').should('be.visible');
})
英文:
It's probably the deeply nested cy.visit()
that causes the problem, you can check in the body of the error message.
If so, try saving the token as an environment variable.
generateTokenAndLogin(email: string) {
cy.request('https://xxx/get-access-key').then((access) => {
this.accessKey = access.body.value;
cy.request('https://xxx/get-secret-key').then((secret) => {
this.secretKey = secret.body.value;
//this is returning secret key correctly
this.getSecretKey().then((response) => {
//fetch secret from aws
cy.task("jwt", {
payload: {
email: email,
},
privateKey: response.SecretString as string,
})
.then(token => Cypress.env('token', token))
});
});
});
}
const jwtHelper = new JWTHelper();
before(() => {
jwtHelper.generateToken();
})
beforeEach(() => {
cy.visit(`myweb.com/login?id_token=${Cypress.env('token')}`)
})
it('JWT', () => {
cy.get('left-nav').should('be.visible');
})
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论