forked from cypress-io/cypress-example-recipes
-
Notifications
You must be signed in to change notification settings - Fork 0
/
location-href-spec.js
135 lines (119 loc) · 4.13 KB
/
location-href-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
// @ts-check
/// <reference types="cypress" />
import {
validateCsv, validateCsvFile, downloadByClicking,
deleteDownloadsFolder,
} from './utils'
const path = require('path')
describe('file download', () => {
beforeEach(deleteDownloadsFolder)
const downloadsFolder = Cypress.config('downloadsFolder')
context('using location.href', () => {
describe('CSV file', () => {
// 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('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('grabbed via 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
validateCsv(csv)
})
})
it('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
const filename = path.join(downloadsFolder, 'records.csv')
cy.writeFile(filename, csv, 'utf8')
validateCsv(csv)
})
})
})
// NOTE: the Command Log says the file has been downloaded, but it's not there
it.skip('downloaded via out own anchor click', () => {
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(() => {
// we can go back to the original page
cy.visit('/')
downloadByClicking(downloadUrl, 'records.csv')
// hmm, the file does not seem to be downloaded
// despite the command log's message
validateCsvFile('records.csv')
})
})
})
describe('PDF file', () => {
// NOTE: work in progress, depends strongly on each browser
it.skip('is downloaded', () => {
cy.visit('/')
cy.get('[data-cy=download-pdf-href]').invoke('attr', 'target', '_blank').click()
cy.location('pathname').should('equal', '/why-cypress.pdf')
})
})
})
})