如何在cypress.config.js文件中的下列cy.task()函数(如validateZipFile
和getZipFileSize
)中添加等待?
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条答案
按热度按时间bsxbgnwa1#
假设网页上没有任何内容指示下载已完成:
这是Yevhen Laichenkov在基辅的文章,Cypress: How to verify that file is downloaded with cy-verify-downloads。
他提到了大文件和
cy.readFile()
的内存问题,并构建了一个cy-verify-downloads库。cypress.config.js
帮助者
测试(无变化)
轮询下载
或者,您可以基于poll nodejs库构建新任务
测试
jchrr9hc2#
你可以通过创建一个像这样的函数并在任务开始时调用它来避免使用第三方库: