cypress icon indicating copy to clipboard operation
cypress copied to clipboard

Ability to prevent/stop loading 'Page load'

Open MehranShafqat opened this issue 4 years ago • 44 comments

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

MehranShafqat avatar Apr 20 '20 22:04 MehranShafqat

@jennifer-shehane please have a look

MehranShafqat avatar Apr 20 '20 22:04 MehranShafqat

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.

jennifer-shehane avatar Apr 21 '20 07:04 jennifer-shehane

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.

Screen Shot 2020-04-24 at 4 32 16 PM

jennifer-shehane avatar Apr 24 '20 10:04 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. image

norvinino avatar Sep 25 '20 21:09 norvinino

@ jennifer-shehane

norvinino avatar Sep 25 '20 22:09 norvinino

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. image

Any solution on this ?

anjalichhabra29 avatar Sep 29 '20 09:09 anjalichhabra29

👍 , this would be a really useful feature

tuan231195 avatar Oct 07 '20 02:10 tuan231195

@norvinino No, there is no solution for this which is why this issue is open.

jennifer-shehane avatar Oct 08 '20 08:10 jennifer-shehane

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.

elliottyates avatar Oct 13 '20 20:10 elliottyates

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.)

elliottyates avatar Oct 14 '20 00:10 elliottyates

is there any ETA on this?

Thank you!

FYI: what is the best solution to stop page LOAD option and move to another step?

ZVince9 avatar Oct 15 '20 10:10 ZVince9

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.

jimpriest avatar Jan 20 '21 20:01 jimpriest

Hi Team I am also facing similar issues When i click on a button one unexpected popup (window,URL) opens up then cypress fails

suchitrachavan avatar Jan 21 '21 03:01 suchitrachavan

image

suchitrachavan avatar Jan 21 '21 03:01 suchitrachavan

@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 avatar Jan 21 '21 06:01 jennifer-shehane

@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

suchitrachavan avatar Jan 21 '21 16:01 suchitrachavan

Hi! This is huge blocker for my test suite. Did anyone looked into this problem?

aminao-toptal avatar Jan 25 '21 14:01 aminao-toptal

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 avatar Jan 26 '21 09:01 jennifer-shehane

@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.

szanelato avatar Apr 07 '21 14:04 szanelato

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! Screen Shot 2021-06-08 at 10 40 28 AM

anastasiiaols avatar Jun 08 '21 17:06 anastasiiaols

Hi, I am also facing the same issue when downloading files. Please suggest any work around for this.

manasaj111 avatar Jun 10 '21 09:06 manasaj111

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 👍

manasaj111 avatar Jun 10 '21 10:06 manasaj111

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.

melibe23 avatar Dec 14 '21 23:12 melibe23

I am facing the same issue in cypress image But in selenium, I am able to complete my tests. waiting for the solution/fix in cypress.

chaitanyaraju2 avatar Feb 20 '22 17:02 chaitanyaraju2

In my case I was able to manually trigger the Page Load event: https://github.com/cypress-io/cypress/issues/14857#issuecomment-1044633756

samtsai avatar Feb 21 '22 16:02 samtsai

Complete blocker for me. We need a clean way to tell Cypress not to wait for a non-existent page.

sbarba avatar Jun 18 '22 04:06 sbarba

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.

astraljames avatar Jul 28 '22 12:07 astraljames

Heyy just tried this just throw me the following error: 9

JoaqPareja avatar Nov 09 '22 22:11 JoaqPareja

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.

tina-zeng avatar Dec 06 '22 08:12 tina-zeng

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?

MadiyarTastanbekov avatar Dec 17 '22 08:12 MadiyarTastanbekov