forked from cypress-io/cypress-example-recipes
-
Notifications
You must be signed in to change notification settings - Fork 0
/
remote-download-spec.js
199 lines (167 loc) · 5.86 KB
/
remote-download-spec.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
// @ts-check
/// <reference types="cypress" />
import {
validateCsvList, validateCsvFile, validateExcelFile,
validateTextFile, validateImage, validateZip,
validateBinaryFile, deleteDownloadsFolder,
} from './utils'
const neatCSV = require('neat-csv')
const path = require('path')
describe('file download', () => {
beforeEach(deleteDownloadsFolder)
const downloadsFolder = Cypress.config('downloadsFolder')
// The next step tries to download an image file located in
// the second domain. It runs in Chromium browsers with
// "chromeWebSecurity": false, but we need to skip it in Firefox
context('from remote domain localhost:9000', { browser: '!firefox' }, () => {
it('CSV file', () => {
cy.visit('/')
cy.contains('h3', 'Download CSV')
cy.get('[data-cy=download-remote-csv]').click()
cy.log('**read downloaded file**')
// file path is relative to the working folder
const filename = path.join(downloadsFolder, 'records.csv')
// browser might take a while to download the file,
// so use "cy.readFile" to retry until the file exists
// and has length - and we assume that it has finished downloading then
cy.readFile(filename, { timeout: 15000 })
.should('have.length.gt', 50)
// parse CSV text into objects
.then(neatCSV)
.then(validateCsvList)
})
it('CSV file attribute', () => {
cy.visit('/')
cy.contains('h3', 'Download CSV')
cy.get('[data-cy=download-remote-csv]').should('have.attr', 'download')
cy.get('[data-cy=download-remote-csv]').should('have.attr', 'href')
.and('match', /\/records\.csv$/)
cy.get('[data-cy=download-remote-csv]').click()
validateCsvFile('records.csv')
})
it('Excel file', () => {
cy.visit('/')
cy.get('[data-cy=download-remote-xlsx]').click()
cy.log('**confirm downloaded file**')
validateExcelFile()
})
it('TXT file', () => {
// the text file comes from a domain different from the page
cy.visit('/')
cy.get('[data-cy=download-remote-txt]').click()
cy.log('**confirm downloaded text file**')
validateTextFile()
})
it('PNG image', () => {
// image comes from a domain different from the page
cy.visit('/')
cy.get('[data-cy=download-remote-png]').click()
cy.log('**confirm downloaded image**')
validateImage()
})
it('JS file', () => {
// the JavaScript file comes from a domain different from the page
cy.visit('/')
cy.get('[data-cy=download-remote-js]').click()
cy.log('**confirm downloaded JavaScript file**')
const downloadedFilename = path.join(downloadsFolder, 'analytics.js')
cy.readFile(downloadedFilename).should((text) => {
// validate the downloaded file
const lines = text.split('\n')
expect(lines).to.have.length.gt(20)
})
})
it('ZIP archive', () => {
cy.visit('/')
cy.get('[data-cy=download-remote-zip]').click()
cy.log('**confirm downloaded ZIP**')
validateZip()
})
it('PDF', () => {
cy.visit('/')
cy.get('[data-cy=download-remote-pdf]').click()
cy.log('**confirm downloaded PDF**')
validateBinaryFile('why-cypress.pdf', 97672)
})
})
context('using location.href', () => {
// NOTE: test times out because the browser stays on the CSV file URL
it.skip('CSV file', () => {
cy.visit('/')
cy.get('[data-cy=download-csv-href]').click()
})
it('CSV file becomes the document after download', () => {
cy.visit('/')
cy.intercept('GET', '*.csv', (req) => {
req.reply((res) => {
// show the CSV as plain text in the browser
res.headers['content-type'] = 'text/html; charset=utf-8'
res.send(res.body)
})
})
cy.get('[data-cy=download-csv-href]').click()
// the actual CSV file is NOT downloaded
// the browser shows the CSV file url
cy.location('pathname').should('be.equal', '/records.csv')
// the contents of the CSV file is shown
cy.contains('Adam').should('be.visible')
// now that we have seen the CSV contents,
// we can get back to the original HTML page
cy.go('back')
})
it('CSV file intercept', () => {
cy.visit('/')
// we will set the CSV file contents in this variable
let csv
cy.intercept('GET', '*.csv', (req) => {
req.reply((res) => {
csv = res.body
// redirect the browser back to the original page
res.headers.location = '/'
res.send(302)
})
})
.as('csvDownload')
cy.get('[data-cy=download-csv-href]').click()
cy.wait('@csvDownload')
// we should stay on the original URL
cy.location('pathname').should('be.equal', '/')
.then(() => {
// by now the CSV variable should have CSV text
cy.wrap(csv)
})
.then(neatCSV)
.then(validateCsvList)
})
it('CSV file downloaded via cy.request', { browser: '!firefox' }, () => {
cy.visit('/')
let downloadUrl
cy.intercept('GET', '*.csv', (req) => {
downloadUrl = req.url
req.reply({
statusCode: 302,
location: '/',
})
})
cy.get('[data-cy=download-csv-href]').click()
.should(() => {
// retries until the intercept sets the download URL
expect(downloadUrl).to.be.a('string')
})
.then(() => {
// download URL ourselves and save as a file
cy.request(downloadUrl).its('body').then((csv) => {
// save so we have it as an artifact
cy.writeFile('./cypress/downloads/records.csv', csv, 'utf8')
.then(() => {
// return CSV text for processing
return csv
})
})
// convert into a list and verify
.then(neatCSV)
.then(validateCsvList)
})
})
})
})