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

127
Views
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 answers
Answer question

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 Report

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 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!