在一种情况下,我必须使用 Cypress 命令验证下载文件的数据。文件类型:- pdf、Word、Excel。
我有被调用的服务器 API 操作的 URL,作为响应,它返回 pdf 文件。
我需要使用 Cypress 命令和 Typescript(插件和类型)来实现。
我能够获取下载状态,甚至 response.body 也有一些文本,但它需要一些解析器来解析响应正文。 下面是我试过的代码。
const oReq = new XMLHttpRequest();
oReq.open("GET", href as string, true);
oReq.responseType = "arraybuffer";
oReq.onload = () => {
if (oReq.readyState === oReq.DONE) {
if (oReq.status === 200) {
// tried parsing the response.
// looking for any parser which can parse the given reponse body into Text or json
}
}
}
cy.request(href).then((response) => {
expect(response.status).to.equal(200);
expect(response.body).not.to.null;
const headerValue = response.headers["content-disposition"];
// expect(headerValue).to.equal("attachment; filename=ExperimentEntityList.<FileExtension-PDF | XLSX | DOCX>");
/// have tried with YAML parser and the "FS" module that cypress and ends up in different console error
// YAML parser causes console error about unidentified character "P".
// FS module code is shown below
});
import * as fs from "fs";
function GetPDFContent()
{
// throws console that fs object doesn't have readFile and same with readFileSync method.
fs.readFile("url")..
fs.readFileSync("url")..
}
要求:
- 阅读PDF文件的内容
- 读取XLS(x)文件的内容
- 阅读 doc(x) 文件的内容。
在 cypress 自动化脚本的 typescript 中无法成功读取 PDF 和 DOc(x) 文件的内容。通过互联网上的各种博客安装 pdfparser、pdfreader、yaml 解析器、filereader 等。但是,它们都不起作用。我已经使用上面提到的代码来读取文件。 并检查相应命令的书面注释。
对于 xlsx 文件,我通过使用解析 Response.body 的 XSLX 解析器插件找到了解决方案,我可以迭代并获取内容。 我正在为 PDF 和 Doc(x) 文件寻找类似的解析器。
这件事谁都知道。请分享!!!
注意:括号或语法不是问题。如果在上面的示例代码中找到,那么它在复制/粘贴过程中会丢失。
编辑:
我找到了使用 Cypress 命令读取和验证 PDF 文件内容的解决方案。感谢理查德马森, @Richard:但是,问题是当我有 PDF 文件的完整 url 时。喜欢 - http://domainname/upload/files/pdf/pdfname.pdf。然后我可以阅读内容并验证它。但是如果我的问题是我有一个像“http://domainname/controller/action?pdf=someid”这样的 url,它返回 pdf 文件响应并且节点命令没有正确编码它并且 pdf 文件没有被正确解析.
小问题
有谁知道如何使用 PDF 数据的响应流使用 node/cypress 命令创建 pdf 文件。我已经尝试过 Axios 插件、http、xmlhttprequest plutins。
最佳答案
您需要一个插件来访问像 pdf-parser 这样的库,它在 NodeJs 环境中工作(即使用像 fs 这样的 Node 命令)。
最好的引用是 Powerful cy.task
这是根据您的场景调整此模式的示例。
cypress/plugins/index.js
const fs = require('fs')
const path = require('path')
const pdf = require('pdf-parse');
const repoRoot = path.join(__dirname, '..', '..') // assumes pdf at project root
const parsePdf = async (pdfName) => {
const pdfPathname = path.join(repoRoot, pdfName)
let dataBuffer = fs.readFileSync(pdfPathname);
return await pdf(dataBuffer) // use async/await since pdf returns a promise
}
module.exports = (on, config) => {
on('task', {
getPdfContent (pdfName) {
return parsePdf(pdfName)
}
})
}
spec.js
it('tests a pdf', () => {
cy.task('getPdfContent', 'mypdf.pdf').then(content => {
// test you pdf content here, with expect(this and that)...
})
})
我还没有对此进行测试,因此您可能会发现一些皱纹需要解决。
pdf 的位置是 repoRoot
,我理解它是指 /cypress/plugins
上两层的项目根文件夹。由于涉及下载,您可能需要调整路径。您没有提供足够的信息来理解完整的测试逻辑,我让您自行调整。
内容返回的形式取决于所使用的 pdf 库。看起来 pdf-parse 给出了一个 Json 对象,应该很容易测试。
在调用 cy.task('getPdfContent')
之后,您可以选择各种 cy
命令,例如 .should()
和 .contains ()
但我会使用 .then()
并在回调中对内容使用 expect()
。
关于typescript - 使用 cypress 命令验证下载文件 (PDF/Word/Excel) 的数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53627113/