https://cypress.io logo
Tests randomly return bad XRef Entry after readFil...
# i-need-help
f
I need to verify the contents of a PDF. For this I've created a task: on cypress.config.js I have:
Copy code
const pdf = require('pdf-parse');

getPdfContent(pdfName) {
 return pdf(fs.readFileSync('cypress/downloads/' + pdfName))
}
On my test I call
Copy code
cy.task("getPdfContent", "Test PDF File.pdf").then(content => {
  expect(content.text).to.have.string("Testing content")
 })
Sometimes the test passes like a charm, but sometimes I get the error bad XRef Entry. For what I have read this is a problem with the PDF file but the PDF file I download is fine. Furthermore if I rerun the same process with the same file, it works fine. So it looks like the problem is what readFileSync is passing to the pdf() function. My theory is that the process is not finished when pdf() wants to parse it, as it only happens sometimes. But I can't figure out what is going on. Could it be that readFileSync is not working correctly everytime? Is there a way to verify or wait for it to finish before calling pdf() on it?
Update: apparently this issue only happens on the first run. After I open the browser. For some reason subsequent runs go through just fine
n
Is the file always there on time? Could you try with cy.readFile()? These are just suggestions that I would try
f
it is. In fact after first launch the test passes everytime. (even deleting the file)
it is super weird issue
7 Views