首页 > 解决方案 > Cypress - 如何使用 cypress 命令验证 PDF 文件中的数据

问题描述

以下是我在柏树中的代码。如何使用 cypress .contains 或 .eq 打印“pdf”内容并验证内容?当我运行代码时它会打印 object{6} 但我想打印我的 pdf 文件内容。我非常感谢您的帮助。

**Plugins/index.js:**

const fs = require('fs')
const pdf = require('pdf-parse')
const path = require('path')

const repoRoot = path.join("C:/Users/XXXXX/Downloads/loginCy-excel")

const parsePdf = async (pdfName) => {
  const pdfPathname = path.join(repoRoot, pdfName)
  let dataBuffer = fs.readFileSync(pdfPathname);
  return await pdf(dataBuffer)
}

module.exports = (on, config) => {
on('task', {
        getPdfContent (pdfName) {
          return parsePdf(pdfName)
        },
      })

}



**cypress spec file has these code:**

it('tests a pdf', () => {
    cy.task('getPdfContent', 'sample.pdf').then(content => {
        cy.log(content)
})
  })

标签: cypress

解决方案


pdf方法将返回一个 object,所以我想cy.log()不能那样打印它。如果您想查看函数在您的 pdf 文件中收集的内容,可以将结果字符串化:

cy
  .log(JSON.stringify(content));

如果您只想从 pdf 中获取文本,则需要使用text属性:

cy
  .log(content.text);

推荐阅读