使用 cypress 命令验证下载文件(PDF/Word/Excel)的数据 [英] Verify the data of the downloaded file (PDF/Word/Excel) using cypress commands

查看:15
本文介绍了使用 cypress 命令验证下载文件(PDF/Word/Excel)的数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一种情况,我必须使用赛普拉斯命令验证下载文件的数据.文件类型:- pdf、word、excel.我有被调用的服务器 API 操作的 URL,作为响应,它返回 pdf 文件.我需要使用 Cypress 命令和 Typescript(插件和打字)来实现.

I have one scenario where I have to verify the downloaded file's data using Cypress commands. FileType :- pdf, word, excel. I have the URL of the Server API Action which gets called and In response, it returns the pdf file. I need to implement using Cypress commands and Typescript (plugin and typings).

我能够获得下载的状态,甚至 response.body 也有一些文本,但它需要一些解析器来解析响应正文.下面是我尝试过的代码.

I am able to get the downloaded status and even the response.body has some text but it require some parser to parse the response body. Below is the code that I have tried.

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) 文件的内容.

Requirement:
1) Read Content of PDF File
2) Read Content of XLS(x) file
3) Read content of doc(x) file.

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

Didn't get success in reading content of PDF and DOc(x) file in typescript for the cypress automation script. Gone through various blogs in the internet install pdfparser, pdfreader, yaml parser, filereader and couple of more. But, none of them works. I have used the above mentioned code to read the files. and Check the written comment for the respective command.

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

For the xlsx file I found the solution by using XSLX parser plugin that parse the Response.body which I can iterate and get the content. I am looking for the similar parser for PDF and Doc(x) file.

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

Anyone knows about this. Please share it!!!

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

NOTE: Brackets or syntax is not the problem. If found in above sample code then it would have missed during copy/paste.

我找到了使用 Cypress commadns 读取和验证 PDF 文件内容的解决方案.感谢理查德·马森,@Richard:但是,问题是当我有 PDF 文件的完整 url 时.喜欢 - http://domainname/upload/files/pdf/pdfname.pdf.然后我可以阅读内容并验证它.但如果我的问题是我有一个类似http://domainname/controller/action?pdf=someid",它返回 pdf 文件响应并且 node 命令没有正确编码它并且 pdf 文件没有被正确解析.

I have found the solution to read and verify the PDF file content using Cypress commadns. Thanks to Richard Matsen, @Richard: But, the problem is when I have a full url of the PDF file. Like - http://domainname/upload/files/pdf/pdfname.pdf. Then I can read the content and verify it. But if My problem is I have a url like "http://domainname/controller/action?pdf=someid", which returns the pdf file response and the node command doesn't encode it properly and the pdf file is not parsed properly.

小问题

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

Do anyone knows how to create a pdf file using node/cypress commands using the Response stream of the PDF data. I have tried the Axios plugin, http, xmlhttprequest plutins.

推荐答案

你需要一个插件来访问像 pdf-parser 这样在 NodeJs 环境中工作的库(即使用像 fs 这样的 Node 命令).

You need a plugin to access libraries like pdf-parser which work in the NodeJs environment (i.e use Node commands like fs).

对此的最佳参考是 强大的 cy.task

以下是将此模式适应您的场景的示例.

Here is an example of adapting this pattern to your scenario.

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)...
  })
})

我没有测试过这个,所以你可能会发现一些皱纹需要熨平.

I haven't tested this, so you may find some wrinkles to iron out.

pdf 的位置是 repoRoot,我理解它是指 /cypress/plugins 上方两层的项目根文件夹.由于涉及下载,您可能需要调整路径.你没有提供足够的信息来理解完整的测试逻辑,我让你来调整.

The location of the pdf is repoRoot which I understand to mean the project root folder two levels above /cypress/plugins. You may need to adjust the path since downloading is involved. You have not given enough info to understand the full test logic, I leave it up to you to make the adjustments.

内容返回的形式取决于使用的 pdf 库.看起来 pdf-parse 提供了一个应该易于测试的 Json 对象.
调用 cy.task('getPdfContent') 后,您可以选择各种 cy 命令,例如 .should().contains() 但我会使用 .then() 并在回调中对内容使用 expect().

The form that the content comes back in depends on the pdf library used. It looks like pdf-parse gives a Json object which should be easy to test.
After cy.task('getPdfContent') is called you can choose various cy commands such as .should() and .contains() but I would use .then() and within the callback use expect() on the content.

这篇关于使用 cypress 命令验证下载文件(PDF/Word/Excel)的数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆