October DealsAmazon USOctober deal check: compare before you payAmazon US: current deals, useful picks and tech finds.Check DealsClean PCRecommendedOne scan can reveal what keeps slowing WindowsLook for cleanup and repair opportunities.Run ScanOctober DealsAmazon USDeal season is back - check today's better picksAmazon US: current deals, useful picks and tech finds.See Picks×
Skip to content
World desk6 min

How to Upload and Download Files with Cypress

A practical Cypress guide to selecting files, testing browser downloads, choosing between fixture and API workflows, and troubleshooting common failures.
Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

For a browser-level upload, use Cypress’s .selectFile() command on the file input, usually with a project-relative path. For a browser-triggered download, trigger the application’s download action, then read the resulting file from Cypress’s downloadsFolder and assert on its contents. Use cy.request() and cy.writeFile() instead when you want to test an HTTP transfer directly rather than the browser UI.

Upload a file through the browser UI

.selectFile() is the Cypress command for populating a file input. It must be chained from a command that yields a DOM element; in ordinary selection mode, that subject must be one input[type="file"] or a connected label.

As an Amazon Associate I earn from qualifying purchases.

Use a project file

When the file already exists in the project, pass its project-relative path directly:

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
cy.get('input[type="file"]')
  .selectFile('cypress/fixtures/example.pdf')

Cypress recommends this approach for files on disk because it avoids many encoding-related pitfalls.

Use a fixture alias for reusable binary data

For a fixed binary fixture you want to load and refer to by alias, use null encoding so Cypress yields a buffer:

cy.fixture('images/avatar.png', null).as('avatar')
cy.get('input[type="file"]').selectFile('@avatar')

cy.fixture() caches content for a path and encoding. If the file changes during a test or is created by the application, use cy.readFile() instead; since Cypress 13, cy.readFile() is a query and rereads the file as chained assertions retry.

Generate file contents in the test

.selectFile() also accepts an object with contents, fileName, mimeType, and lastModified. Contents can be a string, TypedArray or Cypress.Buffer, a path, or an alias. Set a meaningful filename so the application receives the expected name. Cypress can infer MIME type from a recognized extension; set mimeType explicitly when needed. If omitted, lastModified defaults to the current time.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
const contents = Cypress.Buffer.from('id,namen1,Adan')

cy.get('input[type="file"]').selectFile({
  contents,
  fileName: 'users.csv',
  mimeType: 'text/csv'
})

Upload multiple files or simulate a drop

Pass an array for multiple files, but only if the input has the multiple property. To test a drop target, make that target the subject and set action: 'drag-drop'. If the application listens for drops at the page level, body can be the subject because drop events bubble to document listeners.

cy.get('input[type="file"][multiple]')
  .selectFile([
    'cypress/fixtures/first.pdf',
    'cypress/fixtures/second.pdf'
  ])

cy.get('[data-cy="drop-zone"]')
  .selectFile('cypress/fixtures/example.pdf', { action: 'drag-drop' })

A hidden file input can be selected with { force: true } when the visible upload control activates it but Cypress’s normal actionability checks prevent the test from interacting with it. Use that only when it reflects how the application’s UI works.

Choose the right upload workflow

What you are testing Use Why
Existing file selected in the UI .selectFile('project-relative-path') Direct path is the preferred disk-file approach and avoids needless encoding conversions.
Fixed test fixture reused by a test cy.fixture(), optionally aliased Convenient for stable test data; use null encoding for binary fixtures.
Generated file or custom filename and type .selectFile({ contents, fileName, mimeType }) Build contents in the test and control the file metadata seen by the application.
Drag-and-drop interaction .selectFile(file, { action: 'drag-drop' }) Exercises the drop-target interaction rather than ordinary file selection.
API upload, not the browser picker cy.request() with FormData Tests the HTTP upload route without simulating a user selecting a file.

Test an API upload directly

For multipart uploads through cy.request(), Cypress documents using FormData; it preserves file bytes and supplies the multipart boundary. Leave the form option unset: that option is for URL-encoded forms. This route tests the API, not the browser file-input interaction.

Use the browser method when the user-facing selection or drop behavior matters. Use the request method when the requirement is specifically that the upload endpoint accepts the file and processes it.

What’s actually slowing this PC down?

Pick the symptom - the matching free tool is one click away.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Test a browser-triggered download

Cypress saves files downloaded by the application into the configured downloadsFolder rather than opening the browser’s native Save As dialog or download shelf. The documented default folder is cypress/downloads.

cy.get('[data-cy="export"]').click()
cy.readFile('cypress/downloads/report.csv')
  .should('contain', 'Total')

Assert what the product requirement actually promises: expected text for a CSV or text file, required fields after parsing JSON, or exact bytes and encoding when those matter. cy.readFile() accepts explicit encodings and null to yield a Buffer, and its retry behavior is useful when the application creates the download during the test.

Use a direct request when there is no browser download interaction to test

If the requirement is to fetch an endpoint response and save it, rather than verify a user-triggered download, Cypress documents pairing cy.request() with cy.writeFile(). For a PDF response, use binary encoding; cy.writeFile() also accepts a Buffer and null encoding for writing bytes without converting them to a string. For Node-side work or large-file metadata checks that do not need a browser transfer, use cy.task(); Cypress’s custom-command examples include a download command implemented through a task.

Configure and clean up downloaded files

downloadsFolder can be set in Cypress configuration; its default is cypress/downloads. Cypress’s trashAssetsBeforeRuns setting defaults to true; before cypress run, it clears the downloads, screenshots, and videos folders, including nested subfolders. Do not rely on a file from an earlier run being present.

Free tools Windows power users keep installed

One-click scans. No signup required.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Downloaded files are generated test assets. Cypress’s test-organization guidance lists cypress/downloads/, cypress/screenshots/, and cypress/videos/ as examples to ignore in source control.

Independent reader supportYour contribution helps us test, update, and keep practical guides available for everyone.Support on Ko-Fi

Troubleshooting uploads and downloads

  • “Element is not an input[type=file] or label”: Ensure the command subject is the actual file input or a connected label. For drag-and-drop, select the drop target and specify action: 'drag-drop'.
  • Multiple-file selection fails: The input must have its multiple property. Use a single file or update the application’s input if multiple selection is intended.
  • A hidden input is not actionable: If the visible control activates that input, use { force: true } on .selectFile() to bypass actionability checks.
  • Path or alias cannot be resolved: Verify that the path is project-root-relative and that the file exists, or that the fixture alias was created and referenced correctly. Cypress waits and retries for an existing path, but may time out if the path or alias cannot resolve.
  • Binary fixture content is corrupted or interpreted as text: Load it with cy.fixture(path, null) or use a Buffer; avoid unnecessary encoding conversions.
  • Downloaded file is missing: Confirm the application action actually starts a download, check the configured downloadsFolder, and account for asset cleanup before cypress run. Read the expected filename, not an assumed location outside that folder.
  • API upload rejects multipart data: For Cypress’s documented FormData workflow, do not set form; Cypress supplies the multipart boundary.

Version notes

Cypress documents .selectFile() as added in 9.3.0, with TypedArray and mimeType support recorded in 9.4.0. The scrollBehavior option history includes a change in 15.20.0. Cypress records cy.readFile() as becoming a query in 13.0.0. These are API history markers, not a statement of the latest Cypress release.

Or skip the browser setup

ScreenshotNeo is a website screenshot API and MCP server, not a substitute for Cypress tests of file-input uploads or browser downloads. If your next task is capturing a page screenshot, its one-call API can return an image or PDF:

curl -G "https://api.screenshotneo.com/v1/shot" -d access_key=YOUR_API_KEY --data-urlencode url=https://stripe.com -o shot.webp

See the ScreenshotNeo API documentation. ScreenshotNeo accepts cookie banners and removes more than 60 known consent platforms, newsletter popups, and chat widgets before capture; each of those steps can be turned off. Bot checks, blank pages, timeouts, failed loads, and cache hits are not billed. Its MCP server offers take_screenshot, get_page_info, and capture_pdf for AI agents. The free plan includes 1,000 screenshots per month with no card; paid plans start at $5 for 3,000 shots.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Sign up for ScreenshotNeo’s free plan to try 1,000 screenshots a month with no card.

Frequently Asked Questions

Can Cypress test a download without opening a browser window?

A browser-triggered application download is saved by Cypress into its downloads folder; a direct endpoint transfer can instead be requested and written to disk.

Can I select a file using a relative path?

Yes. Pass a project-root-relative file path to `.selectFile()`; this is the preferred disk-file workflow.

Product prices and availability are accurate as of the date/time indicated and are subject to change. Any price and availability information displayed on Amazon at the time of purchase will apply.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Leave a Reply

Your email address will not be published. Required fields are marked *

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

More from the Wire

  1. World desk4 min
    How to Spot an AI Voice Scam Before Sending MoneyDon’t rely on how a caller sounds. Pause, call back through a known number, and verify the emergency with another trusted person before sending money.
  2. Mountain View desk4 min
    Google’s SynthID Detector: How to Check AI-Generated Images, Video and AudioGoogle’s SynthID Detector looks for an embedded watermark in supported images, video and audio. Here is what its results do—and do not—show.
  3. Redmond desk20 min
    How to create a link to File or Folder in Windows 11Windows 11 gives you several ways to point to a file or folder without moving or duplicating it. You can create a desktop shortcut,…
Recommended PC Tool
Recommended PC Tool
Windows Errors? Fix Them Before They SpreadFree repair scan
Outdated Drivers Are Slowing You DownFree scan - exact matches

Two free Windows tools

One Free Minute Could Fix That PC

Before you go - each of these free tools takes about a minute and tackles what quietly slows a Windows PC down.

Special offer. View Outbyte info, uninstall instructions, EULA, and Privacy Policy.