Estoy tratando de guardar un token de DB y pasar el token en la siguiente solicitud de API en el cuerpo. He escrito el siguiente código pero no funciona, por favor ayuda.
it.only('Change Password', () => { cy.ResetPassAPI(globalThis.data.dradminemail);// this generates a token in DB const resetoken = cy.task( 'queryDb', `select _Token from tokens WHERE _Member_Id = 25372 and _Token_Type = "resetPassword" ORDER BY _id DESC LIMIT 1`,); // saving the token from DB generate from above cy.request({ method: 'PATCH', url: 'https://xxx/xx/xx/changePasswordWithToken', body: { confirmPassword: 'xxx', password: 'xx', token: resetoken, //passing the saved token from database uid: 1234, }, }); });Puede usar Cypress.env() para esto.
it.only('Change Password', () => { cy.ResetPassAPI(globalThis.data.dradminemail) // this generates a token in DB const resetoken = cy.task( 'queryDb', `select _Token from tokens WHERE _Member_Id = 25372 and _Token_Type = "resetPassword" ORDER BY _id DESC LIMIT 1` ) // saving the token from DB generate from above Cypress.env('token', resetoken) cy.request({ method: 'PATCH', url: 'https://xxx/xx/xx/changePasswordWithToken', body: { confirmPassword: 'xxx', password: 'xx', token: Cypress.env('token'), //passing the saved token from database uid: 1234, }, }) })Puede guardarlo como un alias y luego acceder a él más tarde llamando al alias o usando this palabra clave dentro de function(){} en lugar de la función de flecha () => {}
// use function{} instead of () => {} it.only('Change Password', function() { cy.ResetPassAPI(globalThis.data.dradminemail) cy.task( 'queryDb', `select _Token from tokens WHERE _Member_Id = 25372 and _Token_Type = "resetPassword" ORDER BY _id DESC LIMIT 1`,) // task returns token so we save it with alias .as('resetToken') cy.request({ method: 'PATCH', url: 'https://xxx/xx/xx/changePasswordWithToken', body: { confirmPassword: 'xxx', password: 'xx', token: this.resetToken, uid: 1234, }, }) })Tienes una sintaxis incorrecta para cy.task()
En vez de
const resetoken = cy.task(...)debería ser
cy.task(...).then(resetoken => {Prueba completa:
it('Change Password', () => { cy.ResetPassAPI(globalThis.data.dradminemail); const query = 'select _Token from tokens WHERE _Member_Id = 25372 and _Token_Type = "resetPassword" ORDER BY _id DESC LIMIT 1'; cy.task('queryDb', query) .then(resetoken => { cy.request({ method: 'PATCH', url: 'https://xxx/xx/xx/changePasswordWithToken', body: { confirmPassword: 'xxx', password: 'xx', token: resetoken, uid: 1234, }, }); }); });Probablemente no necesite el token en ningún otro lugar, una vez que se haya enviado el PARCHE.