【问题标题】:Cypress - How to verify data from a PDF file using cypress commandCypress - 如何使用 cypress 命令验证 PDF 文件中的数据
【发布时间】:2021-10-28 20:08:19
【问题描述】:

以下是我在柏树中的代码。如何使用 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


    【解决方案1】:

    pdf 方法会return an object,所以我猜cy.log() 不能这样打印。如果您想查看 pdf 文件中收集的函数的内容,可以将结果字符串化:

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

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

    cy
      .log(content.text);
    

    【讨论】:

    • 感谢@pavelsaman 的回复我如何添加断言来验证 pdf 中存在的特定文本?我不能这样做:cy.log(content.text).should('have.text', 'Hello') 如何添加断言?
    • 从您分享的赛普拉斯文档中,我得到了这些断言。有什么想法吗? cy.wrap(content.text).should('have.string', 'Hello World!') expect(content.text).to.have.string('Hello world!')
    • 它们是否按预期工作?他们是否正确报告“不匹配”?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-21
    • 2021-11-27
    • 1970-01-01
    • 2021-05-11
    • 2021-05-07
    • 1970-01-01
    相关资源
    最近更新 更多