使用 cypress-file-upload 使用 Cypress 上传 xlsx 文件

Upload xlsx file with Cypress using cypress-file-upload

我正在使用 cypress-file-upload 将 xlsx 文件上传到表单中。

我已经尝试了 2 种解决方案。

// 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'
})

对于这两种解决方案,文件上传时均未出现 Cypress 错误,但表单拒绝了文件并显示一般错误消息。

有什么想法吗?我敢打赌有一些关于编码的东西,但我找不到什么......谢谢

试试下面的代码。 Reference

已编辑:

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' })
    })