首页 文章

使用cypress命令验证下载文件(PDF / Word / Excel)的数据

提问于
浏览
1

我有一个场景,我必须使用赛普拉斯命令验证下载的文件的数据 . 文件类型: - pdf,word,excel . 我有被调用的Server API Action的URL,作为响应,它返回pdf文件 . 我需要使用赛普拉斯命令和Typescript(插件和打字)来实现 .

我能够获得下载状态甚至响应.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 gives consoole 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")..
}

需求:
1)阅读PDF文件的内容
2)读取XLS(x)文件的内容
3)阅读doc(x)文件的内容 .

在cypress自动化脚本的typescript中读取PDF和DOc(x)文件的内容没有成功 . 通过互联网上的各种博客安装pdfparser,pdfreader,yaml解析器,文件阅读器等等 . 但是,它们都不起作用 . 我使用上面提到的代码来读取文件 . 并检查相应命令的书面评论 .

对于xlsx文件,我通过使用XSLX解析器插件找到了解决方案,该插件解析了Response.body,我可以迭代并获取内容 . 我正在寻找PDF和Doc(x)文件的类似解析器 .

任何人都知道这件事 . 请分享!!!

注意:括号或语法不是问题 . 如果在上面的示例代码中找到,则在复制/粘贴期间它会丢失 .

EDIT:

我找到了使用赛普拉斯commadns阅读和验证PDF文件内容的解决方案 . 感谢理查德·马特森,@理查德:但问题是,我有一个完整的PDF文件网址 . 喜欢 - http://domainname/upload/files/pdf/pdfname.pdf . 然后我可以阅读内容并进行验证 . 但是,如果我的问题是我有一个像“http://domainname/controller/action?pdf=someid”的URL,它返回pdf文件响应,并且node命令没有正确编码,并且pdf文件未正确解析 .

Small Question

有谁知道如何使用节点/ cypress命令使用PDF数据的响应流创建pdf文件 . 我尝试过Axios插件,http,xmlhttprequest plutins .

1 回答

  • 0

    您需要一个插件来访问在NodeJs环境中工作的pdf-parser等库(即使用像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 String(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() .

相关问题