cypress
cypress copied to clipboard
Ability to prevent/stop loading 'Page load'
Well I try to stop to load the xhr and continue my test without page load time out error but not working and not even stop the xhr loading. Please guide if im using in correct way or it is actual bug.
cy.window((win) => {
win.stop()
})
Try with black list as well in cypress.json file but did not work
"blacklistHosts": [
"https://www.deepl.com/PHP/backend/docTrans.php?request_type=jsonrpc&il=en"
]
this is xhr I want to stop loading "https://www.deepl.com/PHP/backend/docTrans.php?request_type=jsonrpc&il=en"
Reason why I need this because these is 1 xhr that will load after 5 seconds and this is never ending process in my application under test.
cy version 4.4.0 OS windows 10
@jennifer-shehane please have a look
blacklistHosts
only accepts hosts, not fully qualified urls: https://developer.mozilla.org/en-US/docs/Web/API/URL/host
It doesn't appear that the window.stop
application will work here because once Cypress sees a page load, it waits for the page to load before moving to the next Cypress step.
I can't think of a way to stop loading of a specific XHR in Cypress currently.
I found a way to issue win.stop()
to a loading page and have added an example below to demonstrate. Basically, you need to listen to the window:before:load
event in order to stop the window from loading how you want.
In my example, we visit docs.cypress.io
and click a link that routes us to www.cypress.io
. We can listen in the window:before:load
for the exact href we want to not load and prevent the load there.
it('test', () => {
// set up the listener before the action that will cause reroute
cy.on('window:before:load', (win) => {
// just log the win.location.href for convenience
console.log('WINDOW BEFORE LOAD', win.location.href)
// if we're trying to load the page we want to stop, win.stop()
if (win.location.href === 'https://www.cypress.io/') {
win.stop()
}
})
cy.visit('https://docs.cypress.io')
cy.get('#logo').click()
})
If you want the listener to be global (across all tests), make sure to use Cypress.on
for the listener instead of cy.on
.
HOWEVER, this does not prevent Cypress from listening to the page load. Cypress still waits for the page to load - so since you've prevented the page from loading, the test will now fail after the default 60 second timeout. This doesn't seem like a great solution to me.

Clicking this download link works fine, but I can't stop cypress from waiting for a nonexistant page to load, which prevents the rest of the test from running.
@ jennifer-shehane
Clicking this download link works fine, but I can't stop cypress from waiting for a nonexistant page to load, which prevents the rest of the test from running.
Any solution on this ?
👍 , this would be a really useful feature
@norvinino No, there is no solution for this which is why this issue is open.
In our case, a button in our application fires an XHR to get a temporary URL for a download, then uses window.location.assign()
with response-content-disposition=attachment
in order to download a file. I think it would be possible to check for the file on disk based on the cypress examples, but it appears that the fact cypress waits for the "new page" to load prevents those assertions from running. (window.location.assign()
cannot be stub()
bed, unfortunately, as that would be all we'd need, but that is another matter!)
I think this use case is related to a number of open and closed issues, but wasn't sure where to include it.
The workaround I'm using, I should mention, is the blockHosts
feature, which will stop the load and allow assertions to run. (I suppose that might not work if the file is not being downloaded from a discernibly different host than the app is being served from.)
is there any ETA on this?
Thank you!
FYI: what is the best solution to stop page LOAD option and move to another step?
I'm running into similar issues. Was playing around with the new download feature in 6.3.0 but that functionality seems to be maybe targeted at simply downloading a file from a link.
I am dynamically generating a file (via ColdFusion) and serving it through the browser.
So in 6.3.0 I submit my form, the file is downloaded to cypress/downloads (yay) but Cypress fails while waiting for a page load which never happens (boo)
This is also similar to #8619
Searching it seems there have been quite a few issues opened for some way to stop or intercept a page load.
Hi Team I am also facing similar issues When i click on a button one unexpected popup (window,URL) opens up then cypress fails
@jimpriest Can you open a new issue detailing how this never loads the page when testing file download? We'd like to look into this.
As mentioned, 6.3.0 does offer testing file downloads in case this was anyone elses blocker in this issue. Please update and remove any workaround code you had for testing downloads. If you encounter a new issue - please open a new issue. Thanks.
@jennifer-shehane Do you have any workaround as i tried all the workarounds to handle window and page alerts ...I am using electron browser and can't use chrome & firefox due to security concerns in my organization .Please help
Hi! This is huge blocker for my test suite. Did anyone looked into this problem?
There is no workaround. You need to avoid the action that is causing the redirect at the moment until this feature is added to Cypress.
@jennifer-shehane I'm facing a similar issue when testing a login action. Can you take a look here please? https://github.com/cypress-io/cypress/discussions/15849
Thanks.
Hi, I face the same issue when downloading files. The download itself works fine, but then Cypress waits for some non-existant page load and eventually fails before it can verify the downloaded file. I wasn't able to find any workarounds for it so would much appreciate if the option to stop the page load will be added to Cypress. Is there any ETA for it? Thanks!
Hi, I am also facing the same issue when downloading files. Please suggest any work around for this.
I found a work around here - #14857
cy.window().document().then(function (doc) {
doc.addEventListener('click', () => {
setTimeout(function () { doc.location.reload() }, 5000)
})
cy.get('[ng-click="vm.export()"]').click()
})
Thanks to Sarja 👍
I am facing this issue with my Google Analytics events coverage. On my tests I stubbed Google Analytics events on every action, these events are fired before the new page loads, so I don't need to wait for this load in order to assert, yet Cypress forced the test to wait making the time execution last longer than it could. Is there any workaround that doesn't involved block hosting? Thank you.
I am facing the same issue in cypress
But in selenium, I am able to complete my tests.
waiting for the solution/fix in cypress.
In my case I was able to manually trigger the Page Load event: https://github.com/cypress-io/cypress/issues/14857#issuecomment-1044633756
Complete blocker for me. We need a clean way to tell Cypress not to wait for a non-existent page.
Same here. We can't test any of our downloads as we use an iframe pointing towards an AWS URL. The page load never happens and we can't move forward with testing one of our most critical functions.
Heyy just tried this just throw me the following error:
I've fixed a similar issue. My page redirects with the parameter ?target=http://xxxx.com/xxx, Just replacing the 'http:' with 'https:' can resolve this issue.
I've fixed a similar issue. My page redirects with the parameter ?target=http://xxxx.com/xxx, Just replacing the 'http:' with 'https:' can resolve this issue. hi how did you solve the problem?