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

134
Vistas
Cypress intercept only message with specific body

I'm kind of new on Cypress intercept, but I am in need of capturing the response to a GET message, but wait for the message which has a response body with a specific value on its body.

For example, I may be receiving two responses to two GET requests, such as these two:

Event:                     request
cypress_runner.js:190995 Resource type:             xhr
cypress_runner.js:190995 Method:                    GET
cypress_runner.js:190995 Url:                       https://127.0.0.1/api/users/61a68c4a1d2c5258baece19c?_=1638304841558
cypress_runner.js:190995 Matched `cy.intercept()`:  {RouteMatcher: {…}, RouteHandler Type: 'Spy', RouteHandler: undefined, Request: {…}, Response: {…}, …}
cypress_runner.js:190995 Response status code:      200
cypress_runner.js:190995 Response headers:          {date: 'Tue, 30 Nov 2021 20:40:43 GMT', Content-Encoding: 'gzip', server: 'nginx', Vary: 'Accept-Encoding', access-control-allow-methods: 'PUT, GET, POST, DELETE, OPTIONS, PATCH', …}
cypress_runner.js:190995 Response body:             {"name": "Alice", "description": "Created by Cypress"}


Event:                     request
cypress_runner.js:190995 Resource type:             xhr
cypress_runner.js:190995 Method:                    GET
cypress_runner.js:190995 Url:                       https://127.0.0.1/api/users/61a68c4a1d2c5258baece19c?_=1638304841558
cypress_runner.js:190995 Matched `cy.intercept()`:  {RouteMatcher: {…}, RouteHandler Type: 'Spy', RouteHandler: undefined, Request: {…}, Response: {…}, …}
cypress_runner.js:190995 Response status code:      200
cypress_runner.js:190995 Response headers:          {date: 'Tue, 30 Nov 2021 20:40:43 GMT', Content-Encoding: 'gzip', server: 'nginx', Vary: 'Accept-Encoding', access-control-allow-methods: 'PUT, GET, POST, DELETE, OPTIONS, PATCH', …}
cypress_runner.js:190995 Response body:             {"name": "Bob", "description": "Created by Cypress"}

My intercept for now looks like this:

cy.intercept("GET", "/api/users/*").as("waitingForUpdateOnAlice")
cy.wait("@waitingForUpdateOnAlice")

But if the server returns the answer for Bob, then I don't have the chance of continue waiting.

Is there a way to handle this?

Mention: I do NOT have control or access to the trailing id on the url, so I do need to do this filtering only upon the response body.

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

0

First of all, declare a function to be used recursively:

function doIntercept(functionToWait){
    cy.wait("@"+functionToWait).then((res)=>{
        if(res.response.body.name === 'Bob') {
            doIntercept("@"+functionToWait);
        } else {
            assert(res.response.body.name).to.be.eq('Alice');
        }
    })
}

then in your test section:

it("your test", ()=>{
    cy.intercept("GET", "/api/users/*").as("waitingForUpdateOnAlice");
    doIntercept("waitingForUpdateOnAlice");
})
about 4 years ago · Juan Pablo Isaza Denunciar

0

You can alias an individual request. This should accomplish what you'd like.

cy.intercept("GET", "/api/users/*", (req) => {
  if (req.body.name.equals('Alice')) {
    req.alias = 'waitingForUpdateOnAlice'
  }
});

cy.wait('@waitingForUpdateOnAlice');

You might need to play around with the exact form of that req.body.name conditional.

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