forked from cypress-io/cypress-example-recipes
-
Notifications
You must be signed in to change notification settings - Fork 0
/
local-download-spec.js
172 lines (142 loc) · 5.14 KB
/
local-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
// @ts-check
/// <reference types="cypress" />
import {
validateCsvList, validateCsvFile, validateExcelFile,
validateTextFile, validateImage, validateZip,
validateBinaryFile, deleteDownloadsFolder,
} from './utils'
import { recurse } from 'cypress-recurse'
const neatCSV = require('neat-csv')
const path = require('path')
describe('file download', () => {
const downloadsFolder = Cypress.config('downloadsFolder')
// should we delete all the files in the downloads folder
// before each test?
beforeEach(deleteDownloadsFolder)
context('from local domain localhost:8070', () => {
it('CSV file', () => {
cy.visit('/')
cy.contains('h3', 'Download CSV')
cy.get('[data-cy=download-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 using anchor href name', () => {
cy.visit('/')
cy.contains('h3', 'Download CSV')
cy.get('[data-cy=download-csv]').click()
// let's find out the download name
cy.get('[data-cy=download-csv]').should('have.attr', 'download')
cy.get('[data-cy=download-csv]').should('have.attr', 'href')
.then((filename) => {
expect(filename).to.match(/\.csv$/)
cy.log(`CSV name **${filename}**`)
//@ts-ignore
validateCsvFile(filename)
})
})
it('Excel file', () => {
// let's download a binary file
cy.visit('/')
cy.contains('h3', 'Download XLSX')
cy.get('[data-cy=download-xlsx]').click()
cy.log('**confirm downloaded file**')
validateExcelFile()
})
it('TXT file', { browser: '!firefox' }, () => {
cy.visit('/')
cy.get('[data-cy=download-txt]').click()
cy.log('**confirm downloaded text file**')
validateTextFile()
})
// limiting this test to Chrome browsers
// since in FF we get a cross-origin request error
it('PNG image', { browser: '!firefox' }, () => {
// image comes from the same domain as the page
cy.visit('/')
cy.get('[data-cy=download-png]').click()
cy.log('**confirm downloaded image**')
validateImage()
})
it('ZIP archive', () => {
cy.visit('/')
cy.get('[data-cy=download-zip]').click()
cy.log('**confirm downloaded ZIP**')
validateZip()
})
it('PDF', { browser: '!firefox' }, () => {
cy.visit('/')
cy.get('[data-cy=download-pdf]').click()
cy.log('**confirm downloaded PDF**')
validateBinaryFile('why-cypress.pdf', 97672)
// let's read PDF in the plugin file and confirm its basics
cy.task('readPdf', './cypress/downloads/why-cypress.pdf')
// @ts-ignore
.then(({ numpages, text }) => {
expect(numpages, 'number of PDF pages').to.equal(1)
expect(text, 'has expected text').to.include('Why Cypress?')
})
})
})
context('finds file', () => {
it('after waiting', { browser: '!firefox', retries: 1 }, () => {
// imagine we do not know the exact filename after download
// so let's call a task to find the file on disk before verifying it
// image comes from the same domain as the page
cy.visit('/')
cy.get('[data-cy=download-png]').click()
// give the file time to download
cy.wait(3000)
cy.log('**find the image**')
const mask = `${downloadsFolder}/*.png`
cy.task('findFiles', mask).then((foundImage) => {
expect(foundImage).to.be.a('string')
cy.log(`found image ${foundImage}`)
cy.log('**confirm downloaded image**')
validateImage()
})
})
const isNonEmptyString = (x) => {
return typeof x === 'string' && Boolean(x)
}
// quick unit test to confirm our predicate
// function works as expected
it('isNonEmptyString', () => {
expect(isNonEmptyString()).to.be.false
expect(isNonEmptyString(null)).to.be.false
expect(isNonEmptyString('')).to.be.false
expect(isNonEmptyString('logo.png')).to.be.true
})
it('using recurse', { browser: '!firefox' }, () => {
// imagine we do not know the exact filename after download
// so let's call a task to find the file on disk before verifying it
// image comes from the same domain as the page
cy.visit('/')
cy.get('[data-cy=download-png]').click()
cy.log('**find the image**')
const mask = `${downloadsFolder}/*.png`
recurse(
() => cy.task('findFiles', mask),
isNonEmptyString,
{
delay: 100, // pause 100ms between tries
timeout: 10000, // iterate up to 10 seconds
}
)
.then((foundImage) => {
cy.log(`found image ${foundImage}`)
cy.log('**confirm downloaded image**')
validateImage()
})
})
})
})