import 'cypress-file-upload';
describe('Drag and Drop File Upload Test', () => {
beforeEach(() => {
cy.visit('/file-upload');
});
it('should upload a file via drag and drop', () => {
const fileName = 'sample.txt';
cy.get('#drag-drop-area').should('be.visible');
cy.fixture(fileName).then(fileContent => {
cy.get('#drag-drop-area').attachFile({
fileContent,
fileName,
mimeType: 'text/plain',
encoding: 'utf8'
}, { subjectType: 'drag-n-drop' });
});
cy.get('#uploaded-files').should('contain.text', fileName);
});
});The test starts by visiting the file upload page.
We check that the drag and drop area is visible to ensure the page loaded correctly.
Using cy.fixture(), we load the test file sample.txt from the fixtures folder.
We then use the attachFile command from the cypress-file-upload plugin to simulate dragging and dropping the file onto the upload area.
Finally, we assert that the uploaded files list contains the file name, confirming the upload succeeded.
This approach uses explicit assertions and avoids hardcoded waits, relying on Cypress's automatic retries for stability.