1

When I run my test file in cypress, I noticed that except running my commands, it runs also many xhr commands. Is there a solution to stop (not run) xhr?

enter image description here

AlexWhite
  • 123
  • 2
  • 15

1 Answers1

1

It's you app that triggers the xhr POST calls.

You can prevent them going out to the server with cy.intercept(), but be careful not to stop anything that will break the app.

Ref StaticResponse

By passing in a StaticResponse as the last argument, you can statically define (stub) a response for matched requests

cy.intercept('POST', 'https://mc.yandex.ru/webvisor/**', {})  // stub the yandex calls
cy.visit(...)

The log entries will still show. If this is your concern (not stubbing), you will need to hook into log events. Please advise.

Michael Hines
  • 928
  • 1
  • 7
  • Should I intercept it before my commands or after? Imagine I have there "type" command. – AlexWhite Oct 24 '21 at 11:17
  • Before - always call intercept before the thing that triggers the xhr. – Michael Hines Oct 25 '21 at 06:42
  • But if the xhr are caused by `.type()` then maybe it's part of the functionality and you don't want to stub them. – Michael Hines Oct 25 '21 at 06:45
  • No, it's not part of functionality. Is it possible to intercept all POST request before a command? For example, I have there some more requests except only yandex. Should I intercept them one by one? – AlexWhite Oct 25 '21 at 10:17
  • Can try smaller match string like `cy.intercept('POST', 'https://mc.yandex.ru/**', {})` or `cy.intercept('POST', '**/webvisor/**', {})`. Find correct match string is tricky without actual website to test. – Michael Hines Oct 26 '21 at 02:30