使用 adm

编程入门 行业动态 更新时间:2024-10-06 01:40:28

使用 <a href=https://www.elefans.com/category/jswz/34/1718311.html style=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中添加一个使用

    fs.statSync()
    获取文件大小的任务,这应该比将文件读入Cypress浏览器内存更加内存友好

  • path.join()
    移动到任务,因为它是本机 nodejs 函数

  • 返回执行的任何验证的摘要(例如 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

本文发布于:2024-05-13 14:48:09,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1759720.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:adm

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!