如何使用 cypress 触发 filepond 上传?
How can I trigger filepond uploads using cypress?
我正在尝试使用 cypress 为使用 Vue Filepond 处理上传的媒体应用程序编写端到端测试,但我无法触发文件上传。我该怎么做?
经过一番搜索后,我发现 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 为使用 Vue Filepond 处理上传的媒体应用程序编写端到端测试,但我无法触发文件上传。我该怎么做?
经过一番搜索后,我发现 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")
上传被触发。