Cypress - 如何验证下载的文件是否包含动态名称?

Smh*_*Smh 7 cypress

在 cypress 中,我下载的 xlsx 文件总是以“ABC”开头,然后是一些动态 ID。如何验证文件是否已成功下载并且还包含该动态名称?其次,如果下载的文件类似于“69d644353f126777.xlsx”,那么当名称中的所有内容都是动态时,我如何验证文件是否已下载。

提前致谢。

小智 10

一种建议的方法是使用任务查询下载文件夹,

/cypress/plugins/index.js

const fs = require('fs');

on('task', {
  downloads:  (downloadspath) => {
    return fs.readdirSync(downloadspath)
  }
})
Run Code Online (Sandbox Code Playgroud)

测试

cy.task('downloads', 'my/downloads/folder').then(before => {

  // do the download

  cy.task('downloads', 'my/downloads/folder').then(after => {
    expect(after.length).to.be.eq(before.length +1)  
  })
})
Run Code Online (Sandbox Code Playgroud)

如果您无法将下载定向到项目本地文件夹,请提供完整路径。Node.js(即 Cypress 任务)具有对文件系统的完全访问权限。


要获取新文件的名称,请使用过滤器并获取结果中的第一项(也是唯一一项)。

const newFile = after.filter(file => !before.includes(file))[0]

 
Run Code Online (Sandbox Code Playgroud)