0

I have a tests written in Cypress. One of the tests uploading one pdf file looks like this:

it('uploads one document', () => {
        cy.get('input[type="file"]')
        .attachFile({ filePath: pdfFilePath, encoding: 'base64' , mimeType: 'application/pdf'});

        cy.get('div.table-body>.table-row')
            .find('.col-upload-name')
            .should('have.text', 'TestDocument.pdf')

        cy.get('a.button-action').contains('Upload').click()

        cy.get('div.table-body>.table-row')
            .find('.status-done')
            .should('contain.text', 'Uploaded')
    })

Where pdfFilePath is string path to pdf file in fixtures directory.

When tests uploading pdf files are at the beginning of the test suite, they pass:

enter image description here

When they are at the end of the test suite, they fail:

enter image description here

and I get this error:

enter image description here

Does anyone has any ideas why is that?

JeyKey
  • 413
  • 1
  • 9
  • 22

1 Answers1

0

I tried reproducing the issue on my side. I was not getting the same error, but it was not adding the file. After adding fileContent it worked for me.

    cy.get('[type="file"]').attachFile({
      fileContent,
      filePath: "filepath",
      fileName: "filename",
      encoding: 'base64', 
      mimeType: 'application/pdf'

});
Dharman
  • 30,962
  • 25
  • 85
  • 135
ItsNotAndy
  • 563
  • 5
  • 17
  • Thanks for the answer. How did you define `fileConent`? – JeyKey Nov 03 '21 at 15:06
  • You arent defining anything with `fileContent`. That just lets cypress know you are passing a raw file. See here for more information https://www.npmjs.com/package/cypress-file-upload – ItsNotAndy Nov 04 '21 at 09:42