Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

123
Vistas
Saving data from DB and then using it in body for API in cypress

I am trying to save a token from DB and pass the token in the next API request in the body I have written the below code but it's not working, please help.

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,
      },
    });
  });

about 4 years ago · Juan Pablo Isaza
3 Respuestas
Responde la pregunta

0

You can use Cypress.env() for this.

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,
    },
  })
})
about 4 years ago · Juan Pablo Isaza Denunciar

0

You can save it as an alias and then access it later with calling alias or using this keyword within function(){} instead of arrow function () => {}

// 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,
      },
    })
  })
about 4 years ago · Juan Pablo Isaza Denunciar

0

You have the wrong syntax for cy.task()

Instead of

const resetoken = cy.task(...)

it should be

cy.task(...).then(resetoken => {

Full test:

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,
        },
      });
    });
});

You probably don't need the token anywhere else, once the PATCH has been sent.

about 4 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda