Test Overview
This test checks if a file upload and download feature works correctly on a web page. It verifies that the file is uploaded successfully and that the downloaded file matches the uploaded one.
This test checks if a file upload and download feature works correctly on a web page. It verifies that the file is uploaded successfully and that the downloaded file matches the uploaded one.
describe('File Upload and Download Validation', () => { it('should upload a file and verify download content', () => { cy.visit('https://example.com/file-upload-download') // Upload file const fileName = 'sample.txt' cy.get('input[type=file]').attachFile(fileName) cy.get('#upload-button').click() // Verify upload success message cy.get('#upload-success').should('contain.text', 'Upload successful') // Trigger file download cy.get('#download-link').click() // Read downloaded file and verify content const downloadsFolder = Cypress.config('downloadsFolder') cy.readFile(`${downloadsFolder}/${fileName}`).should('contain', 'This is a sample file for testing.') }) })
| Step | Action | System State | Assertion | Result |
|---|---|---|---|---|
| 1 | Test starts and navigates to the file upload/download page | Browser opens at https://example.com/file-upload-download with upload input and download link visible | — | PASS |
| 2 | Finds file input element and attaches 'sample.txt' file | File input has 'sample.txt' ready for upload | — | PASS |
| 3 | Clicks the upload button to submit the file | Upload process starts | — | PASS |
| 4 | Checks for upload success message '#upload-success' containing 'Upload successful' | Success message displayed on page | Verify text 'Upload successful' is present | PASS |
| 5 | Clicks the download link to download the file | Browser starts downloading 'sample.txt' | — | PASS |
| 6 | Reads the downloaded file from the downloads folder | 'sample.txt' file is present in downloads folder | File content contains 'This is a sample file for testing.' | PASS |
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