adm"/>
使用 adm
我试图使用
adm-zip
和 cypress
验证 zip 文件内容,但是 cypress 抛出内存不足错误。 Zip 文件可以包含 .txt 或 .pdf 或 .ppt 或 .docx 文件。我想在 zip 文件中验证以下内容:
a) no of files
b) type of files
“赛普拉斯”:“v12.8.1”,
"adm-zip": "^0.5.10",
“路径”:“^0.12.7”
注意:使用此
中的 Adm-zip测试.spec.js
import HelperFunctions from "../../../support/helperFunctions";
const helperFunctions = new HelperFunctions();
it('Download test', function () {
cy.get('button[type="submit"]').contains("Download").click({force:true});
helperFunctions.validateZip();
})
helperFunctions.js
validateZip () {
const downloadsFolder = Cypress.config('downloadsFolder')
const downloadedFilename = path.join(downloadsFolder, 'ComprehensionStrategyTeachingResourcePackSummarisingZipFile.zip')
// wait for the file to be fully downloaded by reading it (as binary)
// and checking its length
cy.readFile(downloadedFilename, 'binary', { timeout: 35000 }).should('have.length.gt', 300)
// unzipping and validating a zip file requires the direct access to the file system
// thus it is easier to perform the checks from the `setupNodeEvents` function in the Cypress configuration that runs in Node
// see the "on('task')" code in the `setupNodeEvents` function to see how we can read and validate a Zip file
cy.task('validateZipFile', downloadedFilename)
}
cypress.config.js
const AdmZip = require("adm-zip");
const { defineConfig } = require('cypress')
module.exports = defineConfig({
e2e: {
setupNodeEvents(on, config) {
.....
on("task", {
validateZipFile: (filename) => {
return validateZipFile(filename);
},
});
return config;
},
baseUrl: "/",
specPattern: "cypress/e2e/**/*.spec.{js,jsx,ts,tsx}",
},
})
function validateZipFile (filename) {
// now let's validate the downloaded ZIP file
// Tip: use to load and unzip the Zip contents
console.log('loading zip', filename)
const zip = new AdmZip(filename)
const zipEntries = zip.getEntries()
const names = zipEntries.map((entry) => entry.entryName).sort()
console.log('zip file %s has entries %o', filename, names)
// any other validations?
return null
}
回答如下:
如果主要问题是内存,我会将
cy.readFile()
更改为任务,这样您就不会将整个文件读入 Cypress 内存中。
变化:
在nodejs中添加一个使用
获取文件大小的任务,这应该比将文件读入Cypress浏览器内存更加内存友好fs.statSync()
将
移动到任务,因为它是本机 nodejs 函数path.join()
返回执行的任何验证的摘要(例如 zip 中的名称列表)并在测试中断言验证
除此之外,一切都很好。
cypress.config.js
const { defineConfig } = require('cypress')
const AdmZip = require("adm-zip");
const path = require('path')
const fs = require('fs')
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
},
},
})
function validateZipFile(filename) {
const zip = new AdmZip(filename)
const zipEntries = zip.getEntries()
const names = zipEntries.map(entry => entry.entryName).sort()
return names
}
测试
function validateZip () {
const downloadedFilename = 'sample-large-zip-file.zip'
cy.task('getZipFileSize', downloadedFilename)
.should('eq', 5216156)
cy.task('validateZipFile', downloadedFilename)
.should('deep.eq', ['sample-mpg-file.mpg'])
}
it('Download test', function () {
cy.get('button[type="submit"]')
.contains('Download')
.click({ force: true })
validateZip()
})
更多推荐
使用 adm
发布评论