gpt4 book ai didi

cypress - 如何使用 cypress 触发 filepond 上传?

转载 作者:行者123 更新时间:2023-12-04 08:26:35 25 4
gpt4 key购买 nike

我正在尝试使用 cypress 为使用 Vue Filepond 处理上传的媒体应用程序编写 e2e 测试。 ,但我无法触发文件上传。我怎样才能做到这一点?

最佳答案

经过一番搜索,我找到了 this code snippet由 github 用户 DragorWW 发布

Cypress.Commands.add("uploadFile", (selector, fileUrl, type = "") => {
return cy.get(selector).then(subject => {
return cy
.fixture(fileUrl, "base64")
.then(Cypress.Blob.base64StringToBlob)
.then(blob => {
return cy.window().then(win => {
const el = subject[0];
const nameSegments = fileUrl.split("/");
const name = nameSegments[nameSegments.length - 1];
const testFile = new win.File([blob], name, { type });
const dataTransfer = new DataTransfer();
dataTransfer.items.add(testFile);
el.files = dataTransfer.files;
return cy.wrap(subject).trigger('change');
});
});
});
});

它做到了。把它放在我的测试文件的顶部后,我可以简单地写
cy.uploadFile("input[type=file]", "some-audio-file.wav", "audio/wav")
并且上传被触发。

关于cypress - 如何使用 cypress 触发 filepond 上传?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65234122/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com