使用Cypress -file- Upload上传xlsx文件



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

我试过两种方法。

// 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错误,但是表单拒绝文件并给出一个通用错误消息。

任何想法?我打赌一定有什么关于编码的东西,但我找不到…谢谢你

试试下面的代码。参考

编辑:

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

最新更新