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

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

问题描述

在一种情况下,我必须使用赛普拉斯命令来验证下载文件的数据. FileType:-pdf,word,excel. 我有被调用的Server API Action的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.

无法成功读取赛普拉斯自动化脚本的打字稿中的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文件,我使用XSLX解析器插件找到了解决方案,该插件解析Response.body,可以迭代并获取内容. 我正在寻找适用于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.

我找到了使用赛普拉斯commadns读取和验证PDF文件内容的解决方案.多亏了Richard Matsen, @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.

小问题

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

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

我尚未对此进行测试,因此您可能会发现一些皱纹可以熨平.

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天全站免登陆