How can add wait in following cy.task() functions like validateZipFile
and getZipFileSize
in the in the cypress.config.js file ?
test.spec.js
cy.get('button[type="submit"]').contains("Download").click({force:true});
helperFunctions.validateZip("Booking Results Resource Pack - Task");
// helperFunctions.js
validateZip (text) {
const dfilename = text.replace(/-|_|s/g,"");
const downloadedFilename = dfilename+"ZipFile.zip";
cy.task('getZipFileSize', downloadedFilename)
.should('eq', 6149237)
cy.task('validateZipFile', downloadedFilename)
.should('deep.eq', [
'__Booking-Resource__Question-Cards__Booking_BW.pdf',
'__Booking-Resource__Question-Cards__Booking-Colour.pdf'
]);
}
cypress.config.js
const AdmZip = require("adm-zip");
const { defineConfig } = require('cypress')
module.exports = defineConfig({
e2e: {
setupNodeEvents(on, config) {
.....
on('task', {
validateZipFile: filename => {
const downloadsFolder = config.downloadsFolder
return validateZipFile(path.join(downloadsFolder, filename))
},
getZipFileSize: filename => {
const downloadsFolder = config.downloadsFolder
const stats = fs.statSync(path.join(downloadsFolder, filename))
return stats.size
}
});
return config;
},
baseUrl: "https://staging-qa.someurl.com/",
specPattern: "cypress/e2e/**/*.spec.{js,jsx,ts,tsx}",
},
})
function validateZipFile(filename) {
const zip = new AdmZip(filename)
const zipEntries = zip.getEntries()
const names = zipEntries.map(entry => entry.entryName).sort()
return names
}
2
Answers
Assuming there is nothing on the web page to indicate the download has finished:
There is this article by Yevhen Laichenkov from Kiev, Cypress: How to verify that file is downloaded with cy-verify-downloads.
He mentions the memory issues with large files and
cy.readFile()
, has built a library cy-verify-downloads.cypress.config.js
helper
Test (no change)
Polling for the download
You can build a task based on poll nodejs library.
This method allows you to
true/false
from the task and use the result in a shouldTest
Testing the poll task
To check the above task I added a dummy file
test1.pdf
to downloads, then ran a test that checked for that file and also for a non-existent filetest2.pdf
Equivalent plain-javascript task
This is the same task but using plain javascript
You can avoid using 3-rd party libs by creating a function like this and invoking it at the start of your tasks: