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

Cypress tests hang waiting on user to interact with download dialog #433

Closed
josephong opened this issue Feb 16, 2017 · 21 comments
Closed

Cypress tests hang waiting on user to interact with download dialog #433

josephong opened this issue Feb 16, 2017 · 21 comments
Assignees
Labels
topic: native events type: feature New feature that does not currently exist

Comments

@josephong
Copy link

Sorry if this is addressed in the documentation – I couldn't find anything on it. When I run a spec, programmatically clicking on a link that downloads a file triggers a download dialog. However, in order to make the test progress, I have to manually dismiss the dialog.

Is there an option that will close download dialogs automatically?

@brian-mann
Copy link
Member

Nope this requires native events, which have not been implemented in Cypress.

There is an issue open #311 but I haven't written the explanation for it. It's orthogonal to #310 though which can give you some perspective.

Currently as a workaround just:

  • don't click the link
  • bind to the event which triggers the dialog (in Cypress) and call e.preventDefault() on it

@bahmutov
Copy link
Contributor

if the test e.preventDefault() on the button, for example, it would just disable the button's action we need and trying to test, right?

@brian-mann
Copy link
Member

Okay what you should do is isolate the method that ends up making the HTTP request which sends the disposition headers which caused the file prompt to appear.

Then just use cy.stub to stub out that method and make sure its invoked with the right arguments. Then the thing creating the side effect is isolated and well tested without it doing anything.

You'll need this globally exposed somehow, either attached to some global App object, or perhaps just in test mode expose the function itself. Few different ways to solve this.

@jennifer-shehane jennifer-shehane added the type: feature New feature that does not currently exist label Feb 23, 2017
@jennifer-shehane jennifer-shehane added stage: needs investigating Someone from Cypress needs to look at this topic: native events labels Oct 30, 2017
@kutlaykural
Copy link

kutlaykural commented Jun 21, 2018

Hi @brian-mann
I am triying to stub a request which has disposition headers.
Is there any difference between "XHR and Fetch" request and Doc for Cypress

screen shot 2018-06-21 at 16 01 30

@brian-mann
Copy link
Member

brian-mann commented Jun 21, 2018

Just use cy.request to programmatically make the request that would send the right headers to download the file and assert on the headers. That's all you need to do to know that the browser would have downloaded the file.

Alternatively you could set some flags in chrome to force it to accept downloads to a default location and then just assert that the file is present on the file system.

@tnrich
Copy link
Contributor

tnrich commented Dec 3, 2018

@brian-mann could you explain more about

set some flags in chrome to force it to accept downloads to a default location and then just assert that the file is present on the file system.

How exactly would one do that in their cypress run command?

Thanks!

@jennifer-shehane
Copy link
Member

@tnrich There is an example of setting chrome flags here.

@tnrich
Copy link
Contributor

tnrich commented Dec 14, 2018

@jennifer-shehane hmm interesting, I looked briefly but didn't see a flag for auto-accepting downloads. Do you see one?

@jennifer-shehane
Copy link
Member

No, I was unable to find an appropriate Chrome flag either. @brian-mann you remember which flag you were suggesting?

set some flags in chrome to force it to accept downloads to a default location and then just assert that the file is present on the file system.

@rajfnu
Copy link

rajfnu commented Jan 9, 2019

@jennifer-shehane wondering if you find the chrome flag

@lorensr
Copy link

lorensr commented Jan 25, 2019

This workaround prevents Chrome from trying to download the file:

    cy.server();
    cy.route({
      url: '/foo',
      status: 304,
      response: '',
    }).as('foo');

@harunhasdal
Copy link

harunhasdal commented Mar 6, 2019

Chrome preferences to avoid the download popup are;

{
  'download.default_directory': <full path to a download directory>,
  'profile.default_content_settings.popups': 0
};

However I'm not sure if cypress cli allows setting these.

@jennifer-shehane
Copy link
Member

@harunhasdal This API comes from Selenium. I don't think there's a way to do this through Cypress currently. I may be wrong, but I think we need to extend Cypress to create an API for these prefs to be passed in - then we pass them onto the Chromium profile.

Chromium code using prefs: https://chromium.googlesource.com/chromium/src/+/master/chrome/common/pref_names.cc#1427

@harunhasdal
Copy link

@jennifer-shehane thanks for the link, the correct chromium preferences seem to be instead of the selenium ones mentioned above.

download.default_directory
download.prompt_for_download

I believe, it would be a very useful to be able to configure chromium with a Cypress API. Not only limited to this issue.

@jennifer-shehane
Copy link
Member

@harunhasdal Can you open a new issue to ask for specifying Chromium preferences in Cypress? There isn't an issue open for this yet.

@Chase817
Copy link

@jennifer-shehane Is there any workaround to disable the save prompt when automating downloads in headless Electron?

@jennifer-shehane
Copy link
Member

@Chase817 I'm not aware of a workaround.

@kamituel
Copy link

kamituel commented May 2, 2019

@Chase817 Two of the approaches we're using in our tests are described here: #949 (comment)

Those don't prevent Electron from showing the "save" prompt per se, but instead work around in a way that no file has to actually be downloaded (and yet you get file contents to assert on).

@jennifer-shehane
Copy link
Member

Closing as duplicate of #949

@jennifer-shehane jennifer-shehane removed the stage: needs investigating Someone from Cypress needs to look at this label Jul 24, 2019
@jennifer-shehane
Copy link
Member

File download is now supported in Cypress 6.3.0.

You can now test file downloads in Cypress without the download prompt displaying. Any files downloaded while testing file downloads will be stored in the downloadsFolder which is set to cypress/downloads by default. The downloadsFolder will be deleted before each run unless trashAssetsBeforeRuns is set to false.

This means that if you had any code written to prevent the download prompt or to configure the download location (like in a before:browser:launch event handler), you can remove all of these workarounds.

Our file download recipe has been updated, so you can see some ways to test different types of files after they've been downloaded there.

If you're encountering any bugs while testing file downloads, please open a new issue.

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

No branches or pull requests