2

I looking for a way to intercept all request in cypress. I'm thinking about a thing like that:

 beforeEach(() => {
    cy.intercept({method: 'GET', path: '*'}).as('get')
    cy.intercept({method: 'POST', path: '*'}).as('post')
 })

then:

 afterEcah(() => {
    cy.wait('@get').its('response.statusCode').should('be.oneOf', [200, 304])
    cy.wait('@post').its('response.statusCode').should('be.oneOf', [200, 304, 201])
 })

My problem is sometime in my test i don't have a get or a post request, so my test fail.

Maybe i need a condition is my afterEach() but i can' t figure it out.

Or perhaps the problem is using afterEach() for this purpose.

Any help will be welcome

  • Can you elaborate on your workflow? I don't really understand what you're trying to do. Using `afterEach()` ensures that the calls are only intercepted _after_ the test is executed. – agoff Nov 04 '21 at 17:23
  • The idea is to be able to intercept all request, for example all GET request and control the response status is 200 or 304. – Alexandre Rodriguez Nov 05 '21 at 22:10
  • 1
    You will need to have some idea of all of the calls that you want to intercept. `cy.intercept` will only intercept one call at a time -- so if you have three calls that meet some criteria, you'd need to use `cy.intercept` three times. Additionally, you will need to have those during your test, and not in the `afterEach`, unless those calls happen after the test has completed. – agoff Nov 08 '21 at 14:01

1 Answers1

0

For all requests, you can put your code into cypress/support/e2e.js file:

 beforeEach(() => {
    cy.intercept({method: 'GET', path: '*'}).as('get')
    cy.intercept({method: 'POST', path: '*'}).as('post')
 })