question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Cypress not downloading file into `downloadsFolder` when app code using `window.location.href`

See original GitHub issue

Current behavior

Current app code has an event handler that links to remote url for file download like this:

<!doctype html>
<html lang="en">
<body>
<script>
  function startDownload() {
    window.location.href = 'https://drive.google.com/uc?export=download&id=1GLsHhpE__lmegbaiN9QSkeXa73kXe8nx';
  }
</script>
<button onclick="startDownload()">
</body>
</html>

The file will download, but it won’t go into the downloadsFolder as specified in cypress.json. Instead, it goes to my machine’s user’s Downloads directory (probably browser default).

Desired behavior

The downloaded file should go to the Cypress downloadsFolder directory. My use case is downloading a file into that directory and then uploading / importing it once again.

Test code to reproduce

Using the above html snippet and calling it index.html:

it('should download zip file into downloadsFolder config location', () => {
  cy.visit('index.html')
  cy.get('button').click()
})

Cypress Version

8.7.0

Other

No response

Issue Analytics

  • State:open
  • Created a year ago
  • Comments:5 (2 by maintainers)

github_iconTop GitHub Comments

1reaction
amsan7commented, Apr 16, 2022

@PCourteille Unfortunately no, I am using Chrome on Mac.

1reaction
PCourteillecommented, Apr 11, 2022

Hi Alessandro,

Are you in Firefox ? I struggled with the same issue for several days, but I think I finally found why.

In the file following file: AppData\Local\Cypress\Cache\9.5.3\Cypress\resources\app\packages\server\lib\browsers\firefox.js

Part of the code is here to set the startup option for Firefox inside the “user.js” file: AppData\Roaming\Cypress\cy\production\browsers\firefox-stable\interactive\user.js

If I correctly understand, the “user.js” file expect the path with the backslash escaped, so something like:

C:\\MyProject

But the value received is already:

C:\MyProject

I’m not a developer, so I expect someone will give us better solutions, but I think we have at least two options :

Option 1:

hotfix directly in Cypress code

Inside “firefox.js”, replace the line:

'browser.download.dir': options.downloadsFolder

by:

'browser.download.dir': options.downloadsFolder.replace(/\\/g, "\\\\")

Option 2:

use a plugin to update the value with “\” Create a plugin, something like:

module.exports = (on, config) => {
	on('before:browser:launch', (browser = {}, options) => {
		if (browser.family === 'firefox') {
			options.preferences['browser.download.folderList'] = 2;
			options.preferences['browser.download.dir'] = config['downloadsFolder'].replace(/\\/g, "\\\\");
			return options;
		}
	});
}

Note:

You can check the value set in Firefox with “about:config” : image

I hope my answer will help you, Best regards, Paul

Read more comments on GitHub >

github_iconTop Results From Across the Web

Issue handling file download from cypress - Stack Overflow
I solved it with the index.js file in the plugins folder by doing the following stuff: const cypressTypeScriptPreprocessor = require('.
Read more >
Test file download in Cypress | BrowserStack Docs
Learn how to test file downloads for your Cypress test running on BrowserStack. Overview. In Cypress, you can download a file from a...
Read more >
Troubleshooting | Cypress Documentation
On Windows, you'll need to run the command in a command prompt terminal (not PowerShell). set DEBUG=cypress:* cypress run. If you have issues...
Read more >
cypress-page-object-model - NPM Package Overview - Socket
Start using Socket to analyze cypress-page-object-model and its 0 dependencies to secure your app from supply chain attacks.
Read more >
angular download excel file from web api - You.com
3.3 against 5.2.1. The latter one included a code for closing the stream in the GetAsByteArray procedure. So, I just added those lines...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found