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

169
Visualizações
How to save intercepted request body to outside variable

how can I save request body to outside variable. I want to do something like this:

let request;
  cy.intercept("POST", "/url**", ( req => {
    request = {...req.body};
    req.reply({
      body: response
    });
  }));

// use the "request" variable
// ...

and want to share this copied variable with other functions, but cy.intercept does not allow you to do this. Are there any workarounds?

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

0

You will need to add a wait to your code, since cy.intercept() is just a declarative event listener. Adding a wait for it's alias ensures that it has been triggered.

Also, since the code is async you will probably need to wrap and alias request to use it in other parts. Using the raw request variable might give you the empty value, depending on context.

Ideally you would do this in a beforeEach() I think. You probably also need to add the trigger for the POST call - is is a cy.visit()?

let request;

cy.intercept("POST", "**/url/**", ( req => {
  request = {...req.body};
  req.reply({
    body: response
  })
})).as('myIntercept')

// Must cy.wait (not cy.get) the first occurrence
cy.wait('@myIntercept') 

cy.wrap(request).as('myRequest')

// use the "request" variable
cy.get('@myRequest').then(request => {...    

or take request from the wait result

cy.intercept("POST", "**/url/**").as('myIntercept')

// Must cy.wait (not cy.get) the first occurrence
cy.wait('@myIntercept').its('request')
  .then(request => {
    ...
  })

// 2nd time use get()
cy.get('@myIntercept').its('request')
  .then(request => {
    ...
  })
about 4 years ago · Juan Pablo Isaza Relatório

0

You can use aliases for this.

let somevalue;

describe('Test Suite', () => {
  it('Test case', () => {
    cy.intercept("POST", "/url**").as('urlReq')

    cy.get('@urlReq').then((urlReq) => {
      // urlReq.body will have the intercepted request body
      somevalue = urlReq.body.key
    })
  })
})
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