Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Cypress.io - function visit takes a long time, it is waiting until all resources are loaded

I am using a Cypress.io for end to end testing in our team, but we have a problem with function cy.visit() very often.

The website has many resources from our server (css files, js files,....) and some external resources (js files). If you open our website, sometimes it happens that external js file is pending (browser is waiting).

Cypress during the execution of cy.visit() is probably waiting until all resources are loaded. And this is a problem. I dont need to wait for all resources, because for example this external js is for an advert and it is not important for our test.

Can i tell to Cypress something like: "After a few seconds after start loading a page you can exec this test without all resources loaded"?

I have tried onBeforeLoad combine with setTimeout and reload, but it failed :(

cy.visit('https://www.example.org', {
 onBeforeLoad: (win) => {
   setTimeout(function() {cy.reload(); }, 10000);
 }
})

I am so crazy a I dont know what do next. Please help me and sorry for my english :) Thank you! :)

like image 291
Bohumil Havlíček Avatar asked Mar 27 '19 11:03

Bohumil Havlíček


People also ask

How do you make Cypress wait until page loads?

Wait for API responsewait() command. Test will only continue once that command is finished. Finding the right request to intercept is a great way to make sure that Cypress will wait until page loads with all the right data loaded.

How do you wait for an API call to finish in Cypress?

Cypress automatically waits for the network call to complete before proceeding to the next command. // Anti-pattern: placing Cypress commands inside . then callbacks cy. wait('@alias') .

How does Cypress wait?

click() command, Cypress ensures that the element is able to be interacted with (like a real user would). It will automatically wait until the element reaches an "actionable" state by: Not being hidden. Not being covered.


1 Answers

You can block unnecessary domains from loading with the blacklistHosts: [] option in your cypress.json. Just add the domain name of the advertiser (and potentially anything else you don't need, like Google Analytics) to the blacklistHosts array:

{
  // the rest of your cypress.json...
  "blacklistHosts": [
    "cdn.my-advertiser.com"
  ]
}

More information about blacklistHosts is available in the docs.

like image 122
Zach Bloomquist Avatar answered Oct 01 '22 04:10

Zach Bloomquist