Zoette
Zoette

Reputation: 1281

Upload xlsx file with Cypress using cypress-file-upload

I'm using cypress-file-upload in order to upload an xlsx file into a form.

I've tried 2 solutions.

// First one
cy.fixture(fileName).then((fileContent) => {
    cy.get("input[type='file']").attachFile({
        fileContent,
        fileName,
        encoding : 'UTF-8',
        mimeType : 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet'
    })
})

// Second one
cy.get("input[type='file']").attachFile({
    filePath : "file/path",
    encoding : 'utf-8',
    mimeType : 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet'
})

With both solutions, the file is uploaded with no Cypress error, but the form rejects the file with a generic error message.

Any idea? I bet there's something about encoding but I can't find what... Thank you

Upvotes: 0

Views: 2399

Answers (1)

Rahul L
Rahul L

Reputation: 4349

Try with the code below. Reference

Edited:

const fileName = 'upload_1.xlsx';

cy.fixture(fileName, 'binary')
    .then(Cypress.Blob.binaryStringToBlob)
    .then(fileContent => {
        cy.get("input[type='file']").attachFile({ fileContent, fileName, mimeType: 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet', encoding:'utf8' })
    })

Upvotes: 3

Related Questions