|
| 1 | +// @ts-check |
| 2 | +/// <reference types="cypress" /> |
| 3 | +import { validateCsvList, validateCsvFile, validateExcelFile, validateTextFile, validateImage, validateZip } from './utils' |
| 4 | +const neatCSV = require('neat-csv') |
| 5 | +const path = require('path') |
| 6 | + |
| 7 | +describe('file download', () => { |
| 8 | + const downloadsFolder = Cypress.config('downloadsFolder') |
| 9 | + |
| 10 | + context('from local domain localhost:8070', () => { |
| 11 | + it('CSV file', () => { |
| 12 | + cy.visit('/') |
| 13 | + cy.contains('h3', 'Download CSV') |
| 14 | + cy.get('[data-cy=download-csv]').click() |
| 15 | + |
| 16 | + cy.log('**read downloaded file**') |
| 17 | + |
| 18 | + // file path is relative to the working folder |
| 19 | + const filename = path.join(downloadsFolder, 'records.csv') |
| 20 | + |
| 21 | + // browser might take a while to download the file, |
| 22 | + // so use "cy.readFile" to retry until the file exists |
| 23 | + // and has length - and we assume that it has finished downloading then |
| 24 | + cy.readFile(filename, { timeout: 15000 }) |
| 25 | + .should('have.length.gt', 50) |
| 26 | + // parse CSV text into objects |
| 27 | + .then(neatCSV) |
| 28 | + .then(validateCsvList) |
| 29 | + }) |
| 30 | + |
| 31 | + it('CSV file using anchor href name', () => { |
| 32 | + cy.visit('/') |
| 33 | + cy.contains('h3', 'Download CSV') |
| 34 | + cy.get('[data-cy=download-csv]').click() |
| 35 | + |
| 36 | + // let's find out the download name |
| 37 | + cy.get('[data-cy=download-csv]').should('have.attr', 'download') |
| 38 | + cy.get('[data-cy=download-csv]').should('have.attr', 'href') |
| 39 | + .then((filename) => { |
| 40 | + expect(filename).to.match(/\.csv$/) |
| 41 | + cy.log(`CSV name **${filename}**`) |
| 42 | + //@ts-ignore |
| 43 | + validateCsvFile(filename) |
| 44 | + }) |
| 45 | + }) |
| 46 | + |
| 47 | + it('Excel file', () => { |
| 48 | + // let's download a binary file |
| 49 | + |
| 50 | + cy.visit('/') |
| 51 | + cy.contains('h3', 'Download XLSX') |
| 52 | + cy.get('[data-cy=download-xlsx]').click() |
| 53 | + |
| 54 | + cy.log('**confirm downloaded file**') |
| 55 | + |
| 56 | + validateExcelFile() |
| 57 | + }) |
| 58 | + |
| 59 | + it('TXT file', { browser: '!firefox' }, () => { |
| 60 | + cy.visit('/') |
| 61 | + cy.get('[data-cy=download-txt]').click() |
| 62 | + |
| 63 | + cy.log('**confirm downloaded text file**') |
| 64 | + validateTextFile() |
| 65 | + }) |
| 66 | + |
| 67 | + // limiting this test to Chrome browsers |
| 68 | + // since in FF we get a cross-origin request error |
| 69 | + it('PNG image', { browser: '!firefox' }, () => { |
| 70 | + // image comes from the same domain as the page |
| 71 | + cy.visit('/') |
| 72 | + cy.get('[data-cy=download-png]').click() |
| 73 | + |
| 74 | + cy.log('**confirm downloaded image**') |
| 75 | + validateImage() |
| 76 | + }) |
| 77 | + |
| 78 | + it('ZIP archive', () => { |
| 79 | + cy.visit('/') |
| 80 | + cy.get('[data-cy=download-zip]').click() |
| 81 | + |
| 82 | + cy.log('**confirm downloaded ZIP**') |
| 83 | + validateZip() |
| 84 | + }) |
| 85 | + }) |
| 86 | + |
| 87 | + it('finds file', { browser: '!firefox', retries: 1 }, () => { |
| 88 | + // imagine we do not know the exact filename after download |
| 89 | + // so let's call a task to find the file on disk before verifying it |
| 90 | + // image comes from the same domain as the page |
| 91 | + cy.visit('/') |
| 92 | + cy.get('[data-cy=download-png]').click() |
| 93 | + |
| 94 | + // give the file time to download |
| 95 | + cy.wait(3000) |
| 96 | + |
| 97 | + cy.log('**find the image**') |
| 98 | + const mask = `${downloadsFolder}/*.png` |
| 99 | + |
| 100 | + cy.task('findFile', mask).then((foundImage) => { |
| 101 | + cy.log(`found image ${foundImage}`) |
| 102 | + cy.log('**confirm downloaded image**') |
| 103 | + validateImage() |
| 104 | + }) |
| 105 | + }) |
| 106 | +}) |
0 commit comments