We check if a file downloads correctly to make sure users get the right file without errors.
File download verification in Cypress
cy.downloadFile(url, directory, filename) cy.readFile(filepath)
cy.downloadFile is a custom command often added to Cypress for downloading files.
cy.readFile checks if the file exists and can be read in the specified folder.
cy.downloadFile('https://example.com/report.pdf', 'cypress/downloads', 'report.pdf') cy.readFile('cypress/downloads/report.pdf')
cy.downloadFile('https://example.com/data.csv', 'cypress/downloads', 'data.csv') cy.readFile('cypress/downloads/data.csv')
This test downloads a text file from a URL and checks if it exists in the downloads folder.
describe('File Download Verification', () => { it('should download and verify the file', () => { const url = 'https://example.com/sample.txt' const downloadFolder = 'cypress/downloads' const fileName = 'sample.txt' cy.downloadFile(url, downloadFolder, fileName) cy.readFile(`${downloadFolder}/${fileName}`) }) })
Make sure to add the cy.downloadFile custom command or use a plugin like cypress-downloadfile.
Set the download folder path correctly in your Cypress config to avoid confusion.
Always clean the downloads folder before tests to avoid false positives.
File download verification ensures users get the correct files.
Use cy.downloadFile to download and cy.readFile to check the file.
Clear downloads folder before tests to keep results accurate.
Practice
Solution
Step 1: Understand file download verification
File download verification checks if the file users get is the right one and fully downloaded.Step 2: Identify the main goal
The goal is to confirm the file content and completeness, not speed or layout.Final Answer:
To ensure the downloaded file is correct and complete -> Option BQuick Check:
File correctness = B [OK]
- Confusing download verification with page speed testing
- Thinking it checks page layout after download
- Assuming it speeds up tests
Solution
Step 1: Identify commands related to file handling
cy.downloadFile() downloads files, cy.readFile() reads file contents.Step 2: Choose the command to read file contents
To verify file content, cy.readFile() is used, not cy.downloadFile().Final Answer:
cy.readFile() -> Option AQuick Check:
Read file = cy.readFile() = A [OK]
- Using cy.downloadFile() to read file content
- Assuming cy.getFile() exists
- Confusing cy.checkFile() with real commands
cy.downloadFile('https://example.com/report.pdf', 'cypress/downloads', 'report.pdf')
.then(() => {
cy.readFile('cypress/downloads/report.pdf').should('exist')
})What will this test verify?
Solution
Step 1: Analyze the downloadFile command
The command downloads 'report.pdf' to 'cypress/downloads'.Step 2: Analyze the readFile assertion
It checks if 'report.pdf' exists in the downloads folder after download.Final Answer:
That the file 'report.pdf' exists in the downloads folder after download -> Option DQuick Check:
File existence check = D [OK]
- Assuming it checks file content or emptiness
- Thinking it tests URL reachability
- Confusing pre-download folder state with post-download check
cy.downloadFile('https://example.com/data.csv', 'cypress/downloads', 'data.csv')
cy.readFile('cypress/downloads/data.csv').should('contain', 'Name, Age')Solution
Step 1: Check command sequence and chaining
The download and readFile commands run separately without waiting for download to finish.Step 2: Identify the need for chaining or waiting
Without chaining or waiting, readFile may run before download completes, causing test failure.Final Answer:
Missing chaining or waiting for download to complete before reading file -> Option CQuick Check:
Wait for download before readFile = C [OK]
- Running readFile immediately without waiting
- Assuming cy.downloadFile is not a valid command
- Using wrong assertion method for content check
Solution
Step 1: Ensure download completes before reading file
cy.downloadFile('https://example.com/users.csv', 'cypress/downloads', 'users.csv') .then(() => { cy.readFile('cypress/downloads/users.csv').should('contain', 'ID, Name, Email').and('not.be.empty') }) chains .then() after downloadFile to wait for completion.Step 2: Verify file contains header and is not empty
cy.downloadFile('https://example.com/users.csv', 'cypress/downloads', 'users.csv') .then(() => { cy.readFile('cypress/downloads/users.csv').should('contain', 'ID, Name, Email').and('not.be.empty') }) uses should('contain', ...) and .and('not.be.empty') correctly to check content and non-empty file.Final Answer:
cy.downloadFile('https://example.com/users.csv', 'cypress/downloads', 'users.csv') .then(() => { cy.readFile('cypress/downloads/users.csv').should('contain', 'ID, Name, Email').and('not.be.empty') }) -> Option AQuick Check:
Chain download and readFile with content checks = A [OK]
- Not chaining download and readFile causing timing issues
- Checking file existence only without content validation
- Asserting file is empty instead of not empty
Ready to prove it?
Pass the test and this pattern moves unknown
