Search code examples
pdfautomationcypressblob

How to get and analyse pdf file with blob url using Cypress


I am working on automation framework for a web application. It based on Cypress. On one of the application pages I have to download a dynamically generated PDF file and to analyse its content. The URL to the PDF file is a blob URL that looks like blob:https://..... When I click on the link it just opens it in the new browser tab. Since it's Cypress I can't work with the new windows. When I try to call cy.visit with the blob URL I receive:

Error: Invalid protocol: blob:

I am aware of the Blob library in the Cypress, but it's hard to me to understand from documentation how it can be used in my case.


Solution

  • I suppose you could use cy.request() to obtain the pdf content

    cy.get(pdf-link-element)
      .invoke('attr', 'href')
      .then(href => {
        cy.request(href).then(pdfContent => {
          ...
        })
      })
    

    That would get you around the window opening, but the next question is how to work with pdfContent which would be a bit raw.

    Maybe cy.writeFile() it to /cypress/downloads and use a pdf parsing library to get something useful to test.