Skip to content

Instantly share code, notes, and snippets.

@valeriocs
Last active November 8, 2018 19:20
Show Gist options
  • Save valeriocs/e0062e48236c399de82a9f5bc66f18dd to your computer and use it in GitHub Desktop.
Save valeriocs/e0062e48236c399de82a9f5bc66f18dd to your computer and use it in GitHub Desktop.
Cypress Upload custom command.
Cypress.Commands.add('upload_file', (fileName, selector) => {
return cy.get(selector).then(subject => {
return cy.fixture(fileName, 'base64')
.then(Cypress.Blob.base64StringToBlob)
.then(blob => {
const el = subject[0]
const testFile = new File([blob], fileName, { type: 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet' })
const dataTransfer = new DataTransfer()
dataTransfer.items.add(testFile)
el.files = dataTransfer.files
return subject;
})
})
})
@valeriocs
Copy link
Author

valeriocs commented Nov 8, 2018

Example

cy.upload_file(file or 'string.extension', 'selector')

Selector can be css class/id or by input.

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