Skip to content

Instantly share code, notes, and snippets.

@bpceee
Created March 17, 2020 03:23
Show Gist options
  • Save bpceee/be13c298ecb6b3147a0189b1ad0c4600 to your computer and use it in GitHub Desktop.
Save bpceee/be13c298ecb6b3147a0189b1ad0c4600 to your computer and use it in GitHub Desktop.
cypress file upload
const uploadFile = (fileName, fileType = '', selector) => {
cy.get(selector).then(subject => {
cy.fixture(fileName, 'base64')
.then(Cypress.Blob.base64StringToBlob)
.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
console.log(el.files)
})
})
// from https://bparkerproductions.com/testing-file-uploads-with-cypress-io/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment