Testing web applications

How to test file uploads and downloads

Uploading an attachment and downloading a report connect a web application to work outside the browser. Users need to attach a document to the right record or obtain data they can use elsewhere. When testing PDF, CSV and XLSX files, check content, access and recovery from a transfer failure as well.

A success message can appear before the result is ready

In an illustrative customer portal, an employee uploads a PDF to an order and later downloads a spreadsheet of orders for a selected period. The test confirms a green notification and the presence of a downloaded file. Yet the attachment might belong to another order, the report might include records outside the filter, and the spreadsheet might lose leading zeros in identifiers.

Separate the outcome into transfer, processing and use. Check that the transfer completed, the application accepted or created the file, and an authorised user received the correct content. If an import runs in the background, an “accepted” response does not mean processing has finished. The test needs an observable final state and an agreed timeout.

After a failure, users should know whether they can try again. A vague message without a processing status can lead to a second import or a duplicate attachment.

Define the rules and prepare small reference files

For each feature, document the supported formats, maximum size, number of files, permitted roles and handling of duplicate filenames. Define the content rules as well. A CSV import might require specific columns and encoding, an XLSX import might allow only one worksheet, and a PDF might be an attachment with no further processing. These expectations belong in the requirements rather than the tester’s assumptions.

Prepare small files containing artificial data you know. A prepared test input of this kind is called a fixture. An illustrative spreadsheet can contain three orders, the identifier 00017, a name with accented characters and an item whose name contains a comma. Store the expected records and totals alongside the file. Use a visible reference text in the PDF so documents cannot be confused during comparison.

Use separate records for each run. Two parallel tests uploading attachments with the same name should not modify the same order. The article on test data for E2E tests discusses setup and cleanup. Here, an E2E test means following the complete user journey from choosing a file to checking the result.

Use artificial documents rather than real customer documents or random attachments from the internet. Prepare a damaged format from your own harmless sample. A plain text file renamed to PDF is enough to test a mismatch between extension and content.

Check file selection, limits and rejected input

Start with a valid file. Select it through the form, confirm the upload and verify that it was saved against the correct order. Reload the page and download the document again. This separates the temporary screen state from the saved result.

Then work through boundary cases. For a size limit, test a file below the boundary, exactly on it and just above it using the defined byte counts. The units shown in the interface and the server rule must describe the same limit. Add an empty file, an unsupported format, a cancelled selection and a batch that exceeds the allowed attachment count.

A filename containing spaces, accented characters or longer text checks both display and later download. Two attachments with the same name should follow the product rule, for example remaining separate documents or replacing an existing document after explicit confirmation. A silent overwrite is a defect if the requirements do not allow it.

An extension and a Content-Type header alone are not reliable proof of a file’s type. The OWASP File Upload Cheat Sheet recommends combining type, content, limit and permission checks. For functional testing, derive a case that bypasses the form’s file selector and sends invalid input directly to the server. A security audit covers more than this check of agreed behaviour.

Open the downloaded document and compare its meaning

Before an export, prepare a known application state. Select the period, filter and user account. Calculate expected content from the prepared data so the test does not copy the same defect from the export logic. Check an empty result too. The product might create a file containing headers or explain that there is nothing to export.

Check a PDF using an appropriate document-reading library. Compare the required text, order number and amount, and the page count if the particular template requires it. If layout is part of the test, checking text alone is insufficient. Add a representative visual check for wrapping, tables and clipped data.

Read CSV with a parser that understands the agreed delimiter and quotation rules. Splitting every line at commas fails when a value itself contains a comma. Verify headers, row counts, accented characters, empty values and identifiers with leading zeros. Also decide whether the file is intended for another import or for opening in a spreadsheet application.

For XLSX, check worksheet names, selected cells, data types and formatting wherever it carries meaning. The identifier 00017 should not accidentally become the number 17. For formulas, distinguish the stored formula from the result after recalculation in a supported application. Checking one of these values does not confirm the other.

If the export contains user-supplied text, include the harmless value =1+1. OWASP describes CSV injection, where spreadsheet software treats input as a formula. Define expected handling according to the target application and how the export will be used. This example checks text handling without external connections or commands.

Test permissions through direct URLs as well

Create two model accounts with different access. The first attaches a file to its order. The second attempts to open the attachment using a direct link or request its download through the API, the application’s server interface. Verify that access is denied and the response contains none of the document’s content.

Also check unauthorised uploads to another user’s record, a signed-out user and a role change between export creation and download. For links with limited validity, test the agreed expiry. Some products deliberately support sharing by link. In that case, derive the test from the sharing rules rather than assuming every link requires sign-in.

A hidden button controls only what is displayed. Permissions must also follow the rules when the server receives a request. After a rejected upload, check that no attachment or partially imported data was created against the record.

Define the outcome of interruptions and retries

In the test environment, deliberately interrupt a transfer or simulate an error response. Check the interface state, whether another attempt is available, and the stored data. Test separately what happens when the server finishes an operation but the client never receives confirmation. A retry at that point can create a duplicate.

For an import of three model rows, establish whether the whole batch is processed together or valid rows are accepted while invalid rows are flagged. A subsequent attempt must follow that policy. If the system supports resuming a transfer, verify the order of its parts and the final content too. If it does not, users should receive clear instructions for starting a new upload.

Distinguish an interrupted download from a report-generation failure. A generation error should not produce an apparently successful file containing an HTML error page instead of a spreadsheet. For a repeated download, decide whether it should return the original export or recalculate it from current data.

An illustrative test starts by importing two valid rows and one row without a required identifier. After the first attempt, compare accepted and rejected rows with the application state. Supply the missing value and repeat the agreed procedure. Check the final record count and values to ensure successful rows were not written twice. Instructions must distinguish uploading the entire batch again from submitting only corrected rows. Both procedures can be valid and lead to the same final state without duplicates. The requirements and clear interface instructions determine the expected behaviour.

Distinguish a document defect from a parser limitation

A file-reading library may not check everything users can see. Try it on a reference file with known content before using it. Record which document type and properties it can check.

Format A limitation that can distort the test
CSV A quoted value can contain a newline. Physical line counts therefore need not equal record counts.
PDF A scanned page without a text layer may be unreadable to ordinary text extraction, despite containing visible content.
XLSX A formula’s loaded value may be a stored result from an earlier recalculation, rather than a new calculation.

Python’s csv module handles the agreed delimiter and quoting rules. pypdf does not recognise text from images. If the test needs text from a scan, prepare a suitable check and account for recognition errors as well. For XLSX, openpyxl does not evaluate formulas. Reading stored results does not replace recalculation in a supported spreadsheet application.

When the original attachment must be preserved, comparing bytes or a checksum can be useful. For a new report, establish whether the requirement is an identical file or the same business content. A newly generated document cannot be assessed solely by its name and size. Neither a parser error nor an unsupported reading method should produce a successful content check.

Two exports with the same name must not mix up their results

In the illustrative portal, start two exports with different filters. Both may be named report.csv, but each should contain its own expected set of orders. Save them separately, associate each with its request and compare the records. The last file in a shared directory alone does not verify both exports.

Choose the diagnostic filename within a test directory you control. Check the name supplied by the application separately, rather than use it unchecked as a path outside that directory. Playwright for Python provides set_input_files() for file selection and save_as() for saving a download. The names setInputFiles() and saveAs() in the Node.js documentation refer to the corresponding operations.

Automation must wait for and read the result

Playwright supports setInputFiles() for uploads. Use this method to select a reference file, then verify the application’s subsequent behaviour. Setting the input alone does not confirm a transfer.

For a download, start waiting for the event before clicking and save the result with saveAs(). Read the saved copy using a parser. Playwright’s temporary download files are removed when the browser context closes, so save diagnostic evidence in time.

Each test needs its own destination directory and cleanup. On failure, record the input file, account, filter, expected values and actual difference. Include the resulting document in diagnostics only as permitted by the data-handling rules.

This test suite helps distinguish a transfer failure from a document or access-rule defect. Before the next release, select one upload and one export. Prepare a valid input, a boundary case and denied access for each. Expand the range of formats and combinations once the test demonstrates the final content and application state.

Related topics

You might also be interested in

Catch bugs before they reach users

End-to-end tests of key scenarios can run automatically in CI/CD to catch bugs before they reach customers.