Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

329
Visualizações
CYPRESS- error during type a value from API request to input

When I try to type OTP code (I get it from API), cypress shows an error cypress screenshot

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

If i typed a random string instead getOtpCode() it's working. Do you know how to resolve that problem? Thanks for any help :)

about 4 years ago · Juan Pablo Isaza
2 Respostas
Responde à pergunta

0

It looks like you need to perform getOtpCode() first, because of the async cy.request().

Also the function needs to return the cy.request() to allow .then() to be used after it.

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

Alternately, change the function to a custom command. You will not need to return anything as Cypress sets the current subject to the result of the last .then() in the cy.request() chain.

The custom command can be defined globally.

cypress/support/commands.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']);
})

test

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

0

For the function do this:

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

In your test file do this:

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda