下面是我的代码在柏树。如何打印"pdf"内容和验证内容使用柏树。contains或。eq?当我运行代码时,它打印对象{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文件的人都可以参考这两篇关于这个主题的非常好的博客文章:
- https://filiphric.com/testing-pdf-file-with-cypress
- https://glebbahmutov.com/blog/cypress-pdf/
在这个问题中没有问这个问题,但是这里是我关于如何从URL下载文件(在pdf上测试)的一点补充:
cy.request({
url: '<file url>',
gzip: false,
encoding: 'base64',
}).then((response) => {
cy.writeFile(
Cypress.config('downloadsFolder') + '/<name of the file>.pdf',
response.body,
{ encoding: 'base64' }
);
pdf
方法将返回一个对象,所以我猜cy.log()
不能像那样打印它。如果想查看函数在pdf文件中收集了什么,可以对结果进行字符串化:
cy
.log(JSON.stringify(content));
如果你只想从pdf中获取文本,你需要使用text
属性:
cy
.log(content.text);