自从我刚接触自动化和赛普拉斯以来,这个论坛就非常有用。尝试将文件上传到应用程序时,我也遇到问题。
我在command.js中使用了以下代码:
Cypress.Commands.add('uploadFile', { prevSubject: true }, (subject, fileName, fileType = '') => {
cy.fixture(fileName,'binary').then(content => {
return Cypress.Blob.binaryStringToBlob(content, fileType).then(blob => {
const el = subject[0];
const testFile = new File([blob], fileName, {type: fileType});
const dataTransfer = new DataTransfer();
dataTransfer.items.add(testFile);
el.files = dataTransfer.files;
cy.wrap(subject).trigger('change', { force: true });
});
});
});
然后在测试下面的代码:
const fileName = 'comp-test-malte.mp4';
const fileType = 'video/mp4';
cy.get('input[type=file]').uploadFile(fileName, fileType);
这对我来说是成功的一半,因为它显示了上载0%,但是从数据库中得到以下错误,并且文件未上载:
“ Firebase存储:索引0处放置了无效参数:预期的Blob或文件。”
请帮助!
根据以下消息来源,目前Cypress.io似乎不支持此功能:
您可以使用cypress-file-upload npm软件包作为解决方法!
在此线程注释中,您可能会找到合适的解决方法,如下所示:
// sample code found in the thread
.fixture('bear.mp4', 'binary')
.then(Cypress.Blob.binaryStringToBlob)
.then(fileContent => {
cy.get('#upload-video').upload({
fileContent,
fileName: 'bear.mp4',
mimeType: 'video/mp4',
encoding: 'utf8'
})
})
有关更多详细信息,请检查此存储库。
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句