Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

327
Views
CYPRESS: error al escribir un valor desde la solicitud de API hasta la entrada

Cuando intento escribir el código OTP (lo obtengo de la API), Cypress muestra un error de captura de pantalla de Cypress

 cy.get('input[data-placeholder="OTP"]').type(getOtpCode(phoneNumber)) 

 function getOtpCode(phone) { var otpCode; cy.request({ method: 'GET', url: 'url' }).then(resp => resp.body) .then(data => data.find(element => element['body']['Message']['Phone'] == phone)) .then(phone => otpCode = phone['body']['Message']['CustomMessage']); return otpCode; }

Si escribí una cadena aleatoria en lugar de getOtpCode(), está funcionando. ¿Sabes cómo resolver ese problema? Gracias por cualquier ayuda :)

about 4 years ago · Juan Pablo Isaza
2 answers
Answer question

0

Parece que primero debe realizar getOtpCode() , debido a async cy.request() .

Además, la función debe devolver cy.request() para permitir que .then() se use después.

 function getOtpCode(phone) { return cy.request({ method: 'GET', url: 'url' }) .then(resp => resp.body) .then(data => data.find(element => element['body']['Message']['Phone'] == phone)) .then(phone => phone['body']['Message']['CustomMessage']); } getOtpCode(phoneNumber).then(otpcode => { cy.get('input[data-placeholder="OTP"]').type(otpcode) })

Alternativamente, cambie la función a un comando personalizado. No necesitará devolver nada ya que Cypress establece el asunto actual en el resultado del último .then() en la cadena cy.request() .

El comando personalizado se puede definir globalmente.

ciprés/soporte/comandos.js

 Cypress.Commands.add('getOtpCode', (phone) => { cy.request({ method: 'GET', url: 'url' }) .then(resp => resp.body) .then(data => data.find(element => element['body']['Message']['Phone'] == phone)) .then(phone => phone['body']['Message']['CustomMessage']); })

prueba

 cy.getOtpCode(phoneNumber).then(otpcode => { cy.get('input[data-placeholder="OTP"]').type(otpcode) })
about 4 years ago · Juan Pablo Isaza Report

0

Para la función haz esto:

 cy.get('input[data-placeholder="OTP"]') .should('be.visible') .type(getOtpCode(phoneNumber)) function getOtpCode(phone) { var otpCode cy.request({ method: 'GET', url: 'url', }) .then((resp) => resp.body) .then((data) => data.find((element) => element['body']['Message']['Phone'] == phone) ) .then((phone) => (otpCode = phone['body']['Message']['CustomMessage'])) return cy.wrap(otpCode) }

En su archivo de prueba, haga esto:

 import className from '../path-to-js-file.js' //replace className with yours const obj = new className() //replace className with yours describe('Test Suite', () => { it('Test Case', () => { obj.getOtpCode(phone).then((otp) => { cy.get('input[data-placeholder="OTP"]').type(otp) }) }) })
about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!