Verify file upload and download functionality
Preconditions (2)
✅ Expected Result: File uploads successfully with confirmation message and downloaded file content matches the uploaded file exactly
describe('File Upload and Download Test', () => { it('should upload a file and verify download content', () => { cy.visit('/file-upload') // Upload file cy.get('[data-cy=file-upload-input]').attachFile('testfile.txt') cy.get('[data-cy=upload-submit]').click() // Verify upload success message cy.get('[data-cy=upload-success]').should('be.visible').and('contain.text', 'Upload successful') // Intercept download request cy.intercept('GET', '/download/testfile.txt').as('fileDownload') // Click download link cy.get('[data-cy=download-link]').click() // Wait for download request to complete cy.wait('@fileDownload').then((interception) => { // Verify downloaded file content matches fixture cy.fixture('testfile.txt', 'utf8').then((fixtureContent) => { expect(interception.response.body).to.equal(fixtureContent) }) }) }) })
This test visits the file upload page and uses attachFile to upload a test file from fixtures. It clicks the submit button and checks for a visible success message to confirm upload.
For download, it intercepts the GET request to the file URL, clicks the download link, and waits for the request to finish. Then it compares the downloaded file content from the response body with the original fixture content to ensure they match exactly.
Selectors use data-cy attributes for stability. No hardcoded waits are used; instead, cy.intercept and cy.wait ensure synchronization.
Now add data-driven testing with 3 different files to upload and verify
attachFile command from a plugin to simulate file uploads.readFile reads files, downloadFile is not a core Cypress command, and uploadFile does not exist.cy.readFile('cypress/fixtures/sample.txt').then((content) => { cy.log(content) })cy.readFile command reads the file content as a string or JSON depending on file type.cy.log(content) will output that string in the test runner console.cy.get('input[type=file]').attachFile('image.png')
cy.get('button#submit').click()
cy.readFile('cypress/downloads/image.png').should('exist')attachFile usage is correct with the file input selector.readFile reads file content; it does not support should('exist') to check file presence. Instead, use cy.task or other methods.cy.readFile reads the file from disk.Pass the test and this pattern moves unknown