Skip to content

Instantly share code, notes, and snippets.

@andygock
Last active June 29, 2021 12:25
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save andygock/9ab33cf9ed3ae0ce2145a09a3a4bafda to your computer and use it in GitHub Desktop.
Save andygock/9ab33cf9ed3ae0ce2145a09a3a4bafda to your computer and use it in GitHub Desktop.
Cypress.io testing multiple file drag and drop uploads
// drag and drop multiple files
Cypress.Commands.add("upload_files", (selector, fileUrlArray, type = "") => {
const files = [];
fileUrlArray.forEach((fileUrl) => {
cy.fixture(fileUrl, "base64")
.then(Cypress.Blob.base64StringToBlob)
.then((blob) => {
const nameSegments = fileUrl.split("/");
const name = nameSegments[nameSegments.length - 1];
files.push(new File([blob], name, { type }));
});
});
const event = { dataTransfer: { files: files } };
return cy.get(selector).trigger("drop", event);
});
// example test - files should be in the 'fixtures/' dir
describe("test drag and drop upload", () => {
it("test upload of 2 files", () => {
// drag and drop
cy.upload_files(".drop", ["myfile1.csv", "myfile2.csv"]);
// check for response
cy.get(".output").should("contain", "some text");
});
});
@kylejw2
Copy link

kylejw2 commented Jun 28, 2021

This solution gave me the error: Failed to execute 'atob' on 'Window': The string to be decoded is not correctly encoded. Is this something you are also seeing?

@andygock
Copy link
Author

This solution gave me the error: Failed to execute 'atob' on 'Window': The string to be decoded is not correctly encoded. Is this something you are also seeing?

Though it was working many years ago, it's very possible or likely it's not compatible with more recent versions of Cypress. I haven't checked or looked at it since then.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment