'How to cancel a specific request in Cypress?

I am using Cypress for web testing, the web page I am testing uses Facebook and LinkedIn plugins which they are not important during the testing but they won't load because of my Company's internet policies.

Cypress will approximately wait 30 seconds for a response until they get cancelled, it is a long wait that I don't want to waste every time I execute a test. I tried using pageLoadTimeout but it will not cancel all requests, instead it will fail the test.

cy.visit("pageURL", {pageLoadTimeout: 5000});

Cypress cancelling the requests after 30 seconds

Is there a way to cancel those requests specifically?



Solution 1:[1]

in your cypress.json config file add this line:

"blacklistHosts": ["*yourUrlhost1*", "*yourUrlhost2*"]

include each url you want to blacklist

Solution 2:[2]

You probably should use a request handler.

cy.intercept('pageUrl', (req) => { 
  req.destroy();
})

See the official doc here https://docs.cypress.io/api/commands/intercept#Controlling-the-response

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 HRVHackers
Solution 2 Phil