Skip to main content
SeleniumDecoded
Back to Challenges

Wait for the Download, Not the Clock

intermediate Python 20 min

An export test sleeps five seconds and then checks the file exists. It fails on slow CI and is useless on Grid. Wait for the finished file properly.

The Scenario

The reports page has an “Export CSV” button that generates a file server-side and streams it to the browser. Locally the test passes. In CI it fails about one run in ten, whenever report generation takes more than five seconds. When the team moved the suite to Selenium Grid, it failed every time: the file was downloaded inside the Node container, and the test looked for it on the CI runner.

There is a second, quieter bug: every test downloads into the same ~/Downloads folder, so a report.csv left over from yesterday makes the test pass even when today’s export is broken.

Your Task

  1. For local runs, give each test its own download directory and wait until the file exists and is complete (no .crdownload/.part temp file, non-zero size).
  2. For Grid runs, enable managed downloads and use the downloads API to wait for and fetch the file.
  3. Assert something about the file’s content, not just its existence.

What Good Looks Like

  • No time.sleep.
  • An isolated download directory per test, so stale files cannot produce false passes.
  • A custom wait condition with a timeout message that names the missing file.
  • Grid runs use enable_downloads with get_downloadable_files() and download_file(). The old GET /session/{id}/se/files/{fileName} endpoint was removed from the Java server in 4.49, so don’t build on it from any binding.

Your task

An export test sleeps five seconds and then checks the file exists. It fails on slow CI and is useless on Grid. Wait for the finished file properly.

Problem Code
import os, time

def test_export_csv(driver):
    driver.get("https://app.example.com/reports")
    driver.find_element(By.ID, "export-csv").click()
    time.sleep(5)
    path = os.path.expanduser("~/Downloads/report.csv")
    assert os.path.exists(path)

Need Help?

Solution

Try to solve the challenge yourself before revealing the solution.

Lessons that cover this