File upload testing made easy.
This package adds a custom Cypress command that allows you to make an abstraction on how exactly you upload files through you HTML controls and focus on testing the functionality.
The package is distributed via npm should be installed as one of your project's devDependencies
:
npm install --save-dev cypress-file-upload
cypress-file-upload
extends Cypress' cy
command.
Add this line to your project's cypress/support/commands.js
:
import 'cypress-file-upload';
Here is a basic example:
const fileName = 'data.json';
cy.fixture(fileName).then(fileContent => {
cy.get('[data-cy="dropzone"]').upload(
{ fileContent, fileName, mimeType: 'application/json' },
{ subjectType: 'drag-n-drop' },
);
});
If you're trying to upload a file that does not supported by default, here is the workflow:
- If a workaround with
encoding
does not work properly,
See more usage guidelines in example.
Exposed command in a nutshell:
cySubject.upload(fileOrArray, processingOpts);
fileOrArray
is an object (or an array of those) that represents file information and contains following properties:
- {String}
fileContent
– raw file content, usually a value obtained fromcy.fixture
- {String}
fileName
– file name (with extension) - {String}
mimeType
– file mime type - {String}
encoding
– (optional) normallycy.fixture
resolves encoding automatically, but in case you've specified it, make sure to provide it here as well
processingOpts
contains following properties:
- {String}
subjectType
– target (aka subject) element kind:'drag-n-drop'
component or plain HTML'input'
element. Defaults to'input'
- {Boolean}
force
– (optional) same as forcy.trigger
it enforces events triggering on HTML subject element. Usually this is necessary when you use hidden HTML controls for your file upload. Defaults tofalse
During the lifetime plugin faced the following issues those you should be aware of:
- Chrome 73 changes related to HTML file input behavior: #34
- Force event triggering (same as for
cy.trigger
) should happen when you use hidden HTML controls #41
What to do in case if the plugin is not working for you? Here are some steps:
- Check Caveats – maybe there is a tricky thing about exactly your issue
- Submit the issue and let us know about you problem
- In case you're using a file with encoding and/or extension that is not yet supported, make sure you've tried to explicitly set the
encoding
property (see API) - State what you experience using the workaround above as well
Thanks goes to these wonderful people (emoji key):
This project follows the all-contributors specification. Contributions of any kind welcome!