cypress - 赛普拉斯 - 如何验证下载的文件是否包含动态名称?
问题描述
在 cypress 中,我下载的 xlsx 文件总是以“ABC”开头,然后是一些动态 ID。如何验证文件是否已成功下载并且还包含该动态名称?其次,如果下载的文件类似于“69d644353f126777.xlsx”,那么当名称中的所有内容都是动态的时,我如何验证文件是否已下载。
提前致谢。
解决方案
建议自己的一种方法是使用任务查询下载文件夹,
/cypress/plugins/index.js
const fs = require('fs');
on('task', {
downloads: (downloadspath) => {
return fs.readdirSync(downloadspath)
}
})
测试
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)
})
})
如果您无法将下载内容定向到项目本地的文件夹,请提供完整路径。Node.js(即 Cypress 任务)可以完全访问文件系统。
要获取新文件的名称,请使用过滤器并获取结果中的第一个(也是唯一一个)项目。
const newFile = after.filter(file => !before.includes(file))[0]
推荐阅读
- php - 试图获取非对象的属性“标题”(查看:/home/sporaylq/public_html/core/resources/views/singlepage.blade.php)
- php - 当主键自动递增时,如何自动递增sql中的另一列?
- c# - Visio VSTO 插件“另存为”SVG
- php - 如何从php中的两个日期差异中获取分钟?
- javascript - Electron - 使用远程 url 访问加载文档中的 DOM 元素
- kotlin - 警告(15,5)Kotlin:从未使用参数“args”
- node.js - 无法使用 Hyperledger Fabric node-sdk 初始化通道
- python - 将可检查组合框的文本显示到 QTableWidget
- django - drf-yasg - testproj - 如何静默所有调试输出
- javascript - $(this) 表单选择的值与许多具有相同字段名称的表单