我试图使用adm-zipcypress验证压缩文件的内容,但Cypress抛出内存不足错误.Zip文件可以包含.txt、.pdf、.ppt或.docx文件.我想在压缩文件中验证以下内容:

   a) no of files
   b) type of files 

" cypress ":"v12.8.1",

"NAM-ZIP":"GB 0.5.10",

"路径":"^0.12.7"

注:使用此https://www.npmjs.com/package/adm-zip中的Adm-Zip

Test.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: "https://staging-qa.someurl.com/",
    specPattern: "cypress/e2e/**/*.spec.{js,jsx,ts,tsx}",
  },
})
    
    function validateZipFile (filename) {
      // now let's validate the downloaded ZIP file
      // Tip: use https://github.com/cthackers/adm-zip 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内存中.

更改:

  • 添加一个使用fs.statSync()在NodeJS中获取文件大小的任务,这应该比将文件读取到Cypress浏览器内存中更有利于内存

  • path.join()移至任务,因为它是本机NodeJS函数

  • 返回所执行的所有验证的摘要(例如,压缩包中的名称列表),并在测试中断言验证

除此之外,一切看起来都很好.

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
}

Test

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()
})

Javascript相关问答推荐

React redux状态未在React-Router库中呈现

功能和普通对象之间的原型污染

如何从对象嵌套数组的第二级对象中过滤出键

foreach循环中的Typescript字符串索引

react 路由加载程序行为

如何使用JavaScript将文本插入空div

在观察框架中搜索CSV数据

构造HTML表单以使用表单数据创建对象数组

我的角模板订阅后不刷新'

这个值总是返回未定义的-Reaction

Next.js服务器端组件请求,如何发送我的cookie token?

如何在使用rhandsontable生成表时扩展数字输入验证?

JavaScript不重定向配置的PATH

TypeError:无法读取未定义的属性(正在读取';宽度';)

我想将Sitecore搜索面过滤器从多个转换为单个

如何防止ionic 输入中的特殊字符.?

有没有办法更改Chart.js 3.x.x中主要刻度的字体?

JavaScript -复制到剪贴板在Windows计算机上无效

在Press Reaction本机和EXPO av上播放单个文件

输入数据覆盖JSON文件