Being new to Cypress, I am faced with the challenge of writing a test case to verify the download of a file. Despite researching various resources on similar test cases, they all involve reading a file with a static name. In my scenario, the file is downloaded after clicking a download button and each time it is downloaded, it will have a dynamic name that follows certain patterns (e.g. starting with fixed characters).
I am striving to achieve something like this in Cypress,
cy.readFile('C:\Users\UserName\Downloads\${Regular expression to match the filename pattern}
Below is an excerpt from the cy.task() documentation, where the task is to check if a file exists or not. However, if the filename is not static, how can this be accomplished?
// in plugins/index.js
const fs = require('fs')
module.exports = (on, config) => {
on('task', {
readFileMaybe (filename) {
if (fs.existsSync(filename)) {
return fs.readFileSync(filename, 'utf8')
}
return null
}
})
}