Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Ability to prevent/stop loading 'Page load' #7083

Open
MehranShafqat opened this issue Apr 20, 2020 · 47 comments
Open

Ability to prevent/stop loading 'Page load' #7083

MehranShafqat opened this issue Apr 20, 2020 · 47 comments
Labels
type: feature New feature that does not currently exist

Comments

@MehranShafqat
Copy link

MehranShafqat commented Apr 20, 2020

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
Copy link
Author

@jennifer-shehane please have a look

@jennifer-shehane
Copy link
Member

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 jennifer-shehane changed the title cypress not able to stop to load xhr with window object Ability to prevent/stop loading 'Page load' Apr 21, 2020
@cypress-bot cypress-bot bot added the stage: proposal 💡 No work has been done of this issue label Apr 21, 2020
@jennifer-shehane jennifer-shehane added the type: feature New feature that does not currently exist label Apr 21, 2020
@jennifer-shehane
Copy link
Member

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

@norvinino
Copy link

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
Copy link

@ jennifer-shehane

@anjalichhabra29
Copy link

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 ?

@tuan231195
Copy link

👍 , this would be a really useful feature

@jennifer-shehane
Copy link
Member

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

@elliottyates
Copy link

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
Copy link

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

@ZVince9
Copy link

ZVince9 commented Oct 15, 2020

is there any ETA on this?

Thank you!

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

@jimpriest
Copy link

jimpriest commented Jan 20, 2021

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.

@suchitrachavan
Copy link

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
Copy link

image

@jennifer-shehane
Copy link
Member

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

@suchitrachavan
Copy link

@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

@aminao-toptal
Copy link

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

@jennifer-shehane
Copy link
Member

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.

@szanelato
Copy link

@jennifer-shehane I'm facing a similar issue when testing a login action. Can you take a look here please? #15849

Thanks.

@anastasiiaols
Copy link

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

@manasaj111
Copy link

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

@manasaj111
Copy link

manasaj111 commented Jun 10, 2021

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 👍

@melibe23
Copy link

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.

@chaitanyaraju2
Copy link

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.

@samtsai
Copy link
Contributor

samtsai commented Feb 21, 2022

In my case I was able to manually trigger the Page Load event:
#14857 (comment)

@cypress-bot cypress-bot bot added stage: backlog and removed stage: proposal 💡 No work has been done of this issue labels Apr 29, 2022
@sbarba
Copy link

sbarba commented Jun 18, 2022

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

@astraljames
Copy link

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.

@JoaqPareja
Copy link

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

@tina-zeng
Copy link

tina-zeng commented Dec 6, 2022

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.

@MadiyarTastanbekov
Copy link

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
Copy link

Эй, только что попробовал, просто выдал мне следующую ошибку: 9

hi how did you solve the problem

@MadiyarTastanbekov
Copy link

Полный блокировщик для меня. Нам нужен чистый способ сказать Cypress не ждать несуществующей страницы .

hi how did you solve the problem

@tina-zeng
Copy link

Эй, только что попробовал, просто выдал мне следующую ошибку: 9

hi how did you solve the problem

You can try to force visit the (new url) from your screenshot with HTTPS, no need to add scripts to reload.

@violetflash
Copy link

violetflash commented Feb 15, 2023

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

cy.intercept('GET', downloadUrl).as('downloadFile');
cy.doIfDataCySelectorExist(selector, () => {
  cy.window().then(function (win) {
    win.addEventListener('click', () => {
      setTimeout(function () {
        win.location.reload()
      }, 5000)
    })
    cy.getByDataCySelector(selector).click();
    cy.wait('@downloadFile').its('response.statusCode').should('eq', 200);
  })
})

@sbarba
Copy link

sbarba commented Feb 16, 2023

Nearly 3 years now. Should we conclude there's no interest in fixing this?

@boonier
Copy link

boonier commented Feb 21, 2023

Tis a real kicker this. Download file served from a tokenised url here. File downloads absolutely fine. Cypress doesn't know what to do. Triggering the download by setting the window.location window.location = url;

@JudeaDumont
Copy link

I got passed it by just catching the error via pasting this before the submit:

cy.once("fail", (err) =>
{
    return false;
});

@scramlo
Copy link

scramlo commented Apr 5, 2023

I got passed it by just catching the error via pasting this before the submit

LOL! That is one way to do it. This makes tests unfathomably slow as they wait for Cypress to retry for 30 seconds for each download assertion.

@tushki225
Copy link

I am also facing this issue and assuming its still not fixed.
Capture

@JudeaDumont
Copy link

I got passed it by just catching the error via pasting this before the submit

LOL! That is one way to do it. This makes tests unfathomably slow as they wait for Cypress to retry for 30 seconds for each download assertion.

That probably depends on timeout settings and what you are waiting for (might not be a download, like it wasn't for me). But yeah it's hilarious.

One thing you might also try is hitting the endpoint through jQuery instead of cypress. But you might find that equally hilarious for some reason specific to your own random use case. idk.

@adnanerlansyah403
Copy link

Are there any updates for this issue ?

@adnanerlansyah403
Copy link

I'm so frustrating with this issue

@aditya3151
Copy link

Screenshot 2024-01-23 164931
I'm also facing similar issue, when ever I tried clicking on the element the page is loading and rest of test commands are failing

@araujosnathan
Copy link

I am facing the same issue here!
I need to continue with the tests but it is failing because the Cypress waits for a loaded page when it does not exist.

@ErcPotato
Copy link

any solution for this?

@sbarba
Copy link

sbarba commented Jun 11, 2024

They do not care I guess. Dealbreaker for me.

@GULNURKIRCAER
Copy link

Is there still no solution for this page load event issue? This is a big problem that affects the execution of our tests. Please take care of it @cypress-app-bot

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
type: feature New feature that does not currently exist
Projects
None yet
Development

No branches or pull requests