@@ -11,9 +11,10 @@ describe('file download', () => {
1111
1212 // The next command allow downloads in Electron, Chrome, and Edge
1313 // without any users popups or file save dialogs.
14- if ( Cypress . browser . name !== 'firefox' ) {
14+ if ( ! Cypress . isBrowser ( 'firefox' ) ) {
1515 // since this call returns a promise, must tell Cypress to wait
1616 // for it to be resolved
17+ cy . log ( 'Page.setDownloadBehavior' )
1718 cy . wrap (
1819 Cypress . automation ( 'remote:debugger:protocol' ,
1920 {
@@ -25,9 +26,22 @@ describe('file download', () => {
2526 }
2627 } )
2728
29+ // unfortunately this does not work
30+ // https://github.com/cypress-io/cypress/issues/8594
31+ // beforeEach({ browser: '!firefox' }, () => {
32+ // cy.wrap(
33+ // Cypress.automation('remote:debugger:protocol',
34+ // {
35+ // command: 'Page.setDownloadBehavior',
36+ // params: { behavior: 'allow', downloadPath: downloadsFolder },
37+ // }),
38+ // { log: false }
39+ // )
40+ // })
41+
2842 it ( 'downloads CSV file' , ( ) => {
2943 cy . visit ( '/' )
30- cy . contains ( 'h1 ' , 'Download CSV' )
44+ cy . contains ( 'h3 ' , 'Download CSV' )
3145 cy . get ( '[data-cy=download-csv]' ) . click ( )
3246
3347 cy . log ( '**read downloaded file**' )
@@ -59,7 +73,7 @@ describe('file download', () => {
5973 // let's download a binary file
6074
6175 cy . visit ( '/' )
62- cy . contains ( 'h1 ' , 'Download XLSX' )
76+ cy . contains ( 'h3 ' , 'Download XLSX' )
6377 cy . get ( '[data-cy=download-xlsx]' ) . click ( )
6478
6579 cy . log ( '**confirm downloaded file**' )
@@ -96,8 +110,9 @@ describe('file download', () => {
96110 } )
97111 } )
98112
99- // NOTE: skipped because it is causing failures in CI, but this should still work for other projects
100- it . skip ( 'downloads local PNG image' , ( ) => {
113+ // limiting this test to Chrome browsers
114+ // since in FF we get a cross-origin request error
115+ it ( 'downloads local PNG image' , { browser : '!firefox' } , ( ) => {
101116 // image comes from the same domain as the page
102117 cy . visit ( '/' )
103118 cy . get ( '[data-cy=download-png]' ) . click ( )
@@ -118,26 +133,81 @@ describe('file download', () => {
118133 } )
119134 } )
120135
121- // The next step tries to download file located in
136+ // The next step tries to download an image file located in
122137 // the second domain. It runs in Chromium browsers with
123138 // "chromeWebSecurity": false, but we need to skip it in Firefox
124- it ( 'downloads remote PNG image' , { browser : '!firefox' } , ( ) => {
139+ context ( 'from remote domain' , { browser : '!firefox' } , ( ) => {
140+ it ( 'downloads remote PNG image' , ( ) => {
125141 // image comes from a domain different from the page
126- cy . visit ( '/' )
127- cy . get ( '[data-cy=download-remote-png]' ) . click ( )
142+ cy . visit ( '/' )
143+ cy . get ( '[data-cy=download-remote-png]' ) . click ( )
128144
129- cy . log ( '**confirm downloaded image**' )
130- const downloadedFilename = path . join ( downloadsFolder , 'logo.png' )
145+ cy . log ( '**confirm downloaded image**' )
146+ const downloadedFilename = path . join ( downloadsFolder , 'logo.png' )
131147
132- // ensure the file has been saved before trying to parse it
133- cy . readFile ( downloadedFilename , 'binary' , { timeout : 15000 } )
134- . should ( ( buffer ) => {
148+ // ensure the file has been saved before trying to parse it
149+ cy . readFile ( downloadedFilename , 'binary' , { timeout : 15000 } )
150+ . should ( ( buffer ) => {
135151 // by having length assertion we ensure the file has text
136152 // since we don't know when the browser finishes writing it to disk
137153
138- // Tip: use expect() form to avoid dumping binary contents
139- // of the buffer into the Command Log
140- expect ( buffer . length ) . to . be . gt ( 1000 )
154+ // Tip: use expect() form to avoid dumping binary contents
155+ // of the buffer into the Command Log
156+ expect ( buffer . length ) . to . be . gt ( 1000 )
157+ } )
158+ } )
159+
160+ it ( 'downloads remote TXT file' , ( ) => {
161+ // the text file comes from a domain different from the page
162+ cy . visit ( '/' )
163+ cy . get ( '[data-cy=download-remote-txt]' ) . click ( )
164+
165+ cy . log ( '**confirm downloaded text file**' )
166+ const downloadedFilename = path . join ( downloadsFolder , 'robots.txt' )
167+
168+ cy . readFile ( downloadedFilename ) . should ( ( text ) => {
169+ // validate the downloaded robots.txt file
170+ const lines = text . split ( '\n' )
171+
172+ expect ( lines ) . to . have . length . gt ( 2 )
173+ expect ( lines [ 0 ] ) . to . equal ( 'User-agent: *' )
174+ } )
175+ } )
176+
177+ it ( 'downloads remote JS file' , ( ) => {
178+ // the JavaScript file comes from a domain different from the page
179+ cy . visit ( '/' )
180+ cy . get ( '[data-cy=download-remote-js]' ) . click ( )
181+
182+ cy . log ( '**confirm downloaded JavaScript file**' )
183+ const downloadedFilename = path . join ( downloadsFolder , 'analytics.js' )
184+
185+ cy . readFile ( downloadedFilename ) . should ( ( text ) => {
186+ // validate the downloaded file
187+ const lines = text . split ( '\n' )
188+
189+ expect ( lines ) . to . have . length . gt ( 20 )
190+ } )
191+ } )
192+
193+ // NOTE: because the file is downloaded from a domain we don't control
194+ it . skip ( 'downloads remote CSV file' , ( ) => {
195+ // the site we are about to visit has an error on load,
196+ // so let's ignore it
197+ Cypress . on ( 'uncaught:exception' , ( err , runnable ) => {
198+ // returning false here prevents Cypress from
199+ // failing the test
200+ return false
201+ } )
202+
203+ cy . visit ( 'https://www.appsloveworld.com/sample-csv-file/' )
204+ cy . get ( '.Downloadbutton' ) . first ( ) . click ( )
205+
206+ cy . log ( '**confirm downloaded CSV file**' )
207+ const downloadedFilename = path . join ( downloadsFolder , 'Sample100.csv' )
208+
209+ cy . readFile ( downloadedFilename , { timeout : 15000 } )
210+ . should ( 'have.length.gt' , 100 )
141211 } )
142212 } )
143213} )
0 commit comments