Bird
0
0

Given this Cypress test:

medium📝 Debug Q7 of 15
Cypress - File Operations
Given this Cypress test:
cy.downloadFile('https://example.com/report.pdf', 'cypress/downloads/report.pdf')
cy.readFile('cypress/downloads/report.pdf').should('contain', 'Summary')

The test fails even though the file downloads correctly. What is the likely cause?
Acy.readFile runs before the download completes, causing a race condition
BThe file path in cy.readFile is incorrect
CThe 'contain' assertion is invalid for PDF files
Dcy.downloadFile does not support PDF files
Step-by-Step Solution
Solution:
  1. Step 1: Analyze asynchronous behavior

    cy.downloadFile is asynchronous; cy.readFile may execute before download finishes.
  2. Step 2: Understand race condition

    Without chaining or waiting, the test reads the file too early.
  3. Step 3: Evaluate other options

    File path is correct, 'contain' can be used on text files but PDF content requires parsing, and cy.downloadFile supports PDFs.
  4. Final Answer:

    cy.readFile runs before the download completes, causing a race condition -> Option A
  5. Quick Check:

    Ensure download completes before reading file [OK]
Quick Trick: Chain commands to wait for download completion [OK]
Common Mistakes:
  • Not waiting for download to finish before reading file
  • Assuming 'contain' works directly on binary files like PDFs

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Cypress Quizzes