Skip to content

Commit bedb8d6

Browse files
authored
Cross domain file download (#566)
1 parent b25aab7 commit bedb8d6

File tree

5 files changed

+54
-3
lines changed

5 files changed

+54
-3
lines changed

examples/testing-dom__download/README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,6 @@
22

33
![File download in Chrome](images/chrome.png)
44

5-
See [cypress/plugins/index.js](cypress/plugins/index.js) to see how we set the download folder when launching the browser. See the [cypress/integration/spec.js](cypress/integration/spec.js) spec file that downloads and verifies a CSV file and an Excel file.
5+
See [cypress/plugins/index.js](cypress/plugins/index.js) to see how we set the download folder when launching the browser. See the [cypress/integration/spec.js](cypress/integration/spec.js) spec file that downloads and verifies a CSV file, an Excel file, and a PNG image.
66

77
![File download in Firefox](images/firefox.png)

examples/testing-dom__download/cypress.json

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
"fixturesFolder": false,
33
"supportFile": false,
44
"viewportWidth": 500,
5-
"viewportHeight": 300,
6-
"baseUrl": "http://localhost:8070"
5+
"viewportHeight": 500,
6+
"baseUrl": "http://localhost:8070",
7+
"chromeWebSecurity": false
78
}

examples/testing-dom__download/cypress/integration/spec.js

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,4 +95,48 @@ describe('file download', () => {
9595
])
9696
})
9797
})
98+
99+
it('downloads local PNG image', () => {
100+
// image comes from the same domain as the page
101+
cy.visit('/')
102+
cy.get('[data-cy=download-png]').click()
103+
104+
cy.log('**confirm downloaded image**')
105+
106+
const downloadedFilename = path.join(downloadsFolder, 'logo.png')
107+
108+
// ensure the file has been saved before trying to parse it
109+
cy.readFile(downloadedFilename, 'binary', { timeout: 15000 })
110+
.should((buffer) => {
111+
// by having length assertion we ensure the file has text
112+
// since we don't know when the browser finishes writing it to disk
113+
114+
// Tip: use expect() form to avoid dumping binary contents
115+
// of the buffer into the Command Log
116+
expect(buffer.length).to.be.gt(1000)
117+
})
118+
})
119+
120+
// The next step tries to download file located in
121+
// the second domain. It runs in Chromium browsers with
122+
// "chromeWebSecurity": false, but we need to skip it in Firefox
123+
it('downloads remote PNG image', { browser: '!firefox' }, () => {
124+
// image comes from a domain different from the page
125+
cy.visit('/')
126+
cy.get('[data-cy=download-remote-png]').click()
127+
128+
cy.log('**confirm downloaded image**')
129+
const downloadedFilename = path.join(downloadsFolder, 'logo.png')
130+
131+
// ensure the file has been saved before trying to parse it
132+
cy.readFile(downloadedFilename, 'binary', { timeout: 15000 })
133+
.should((buffer) => {
134+
// by having length assertion we ensure the file has text
135+
// since we don't know when the browser finishes writing it to disk
136+
137+
// Tip: use expect() form to avoid dumping binary contents
138+
// of the buffer into the Command Log
139+
expect(buffer.length).to.be.gt(1000)
140+
})
141+
})
98142
})

examples/testing-dom__download/public/index.html

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,4 +5,10 @@ <h1>Download CSV</h1>
55

66
<h1>Download XLSX</h1>
77
<a data-cy="download-xlsx" href="people.xlsx" download>people.xlsx</a>
8+
9+
<h1>Download a local PNG image</h1>
10+
<a data-cy="download-png" href="logo.png" download>logo.png</a>
11+
12+
<h1>Download a remote PNG image</h1>
13+
<a data-cy="download-remote-png" href="https://docs.cypress.io/img/logo.png" download>remote logo.png</a>
814
</body>
6.17 KB
Loading

0 commit comments

Comments
 (0)