0
0
Selenium Pythontesting~10 mins

File download handling in Selenium Python - Test Execution Trace

Choose your learning style9 modes available
Test Overview

This test automates downloading a file from a web page and verifies that the file is saved correctly in the specified download folder.

Test Code - Selenium
Selenium Python
import os
import time
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.common.by import By

# Setup Chrome options to set download directory
download_dir = os.path.join(os.getcwd(), "downloads")
if not os.path.exists(download_dir):
    os.makedirs(download_dir)

chrome_options = Options()
chrome_options.add_experimental_option("prefs", {
    "download.default_directory": download_dir,
    "download.prompt_for_download": False,
    "download.directory_upgrade": True,
    "safebrowsing.enabled": True
})

driver = webdriver.Chrome(options=chrome_options)

try:
    driver.get("https://www.selenium.dev/documentation/webdriver/browser/downloads/")

    # Find the download link for a sample file
    download_link = driver.find_element(By.LINK_TEXT, "sampleFile.txt")
    download_link.click()

    # Wait for the file to appear in the download directory
    file_path = os.path.join(download_dir, "sampleFile.txt")
    timeout = 15
    start_time = time.time()
    while not os.path.exists(file_path):
        time.sleep(1)
        if time.time() - start_time > timeout:
            break

    # Assert the file exists
    assert os.path.exists(file_path), "File was not downloaded successfully"

finally:
    driver.quit()
Execution Trace - 7 Steps
StepActionSystem StateAssertionResult
1Test starts and Chrome browser opens with download directory setChrome browser window is open with download preferences set to custom folder-PASS
2Browser navigates to the Selenium downloads documentation pagePage loaded showing Selenium documentation with download links-PASS
3Finds the link with text 'sampleFile.txt' on the pageDownload link element is locatedElement with link text 'sampleFile.txt' is foundPASS
4Clicks the download link to start file downloadBrowser initiates file download to the specified folder-PASS
5Waits up to 15 seconds for the file 'sampleFile.txt' to appear in download folderFile appears in the download folder within timeoutFile exists at expected pathPASS
6Asserts that the downloaded file existsFile is confirmed present in the download folderassert os.path.exists(file_path) passesPASS
7Browser closes and test endsBrowser window closed, resources cleaned up-PASS
Failure Scenario
Failing Condition: File does not appear in the download folder within the timeout period
Execution Trace Quiz - 3 Questions
Test your understanding
What does the test verify after clicking the download link?
AThat an alert pops up
BThat the page URL changes
CThat the file appears in the download folder
DThat the browser refreshes
Key Result
Always configure the browser to download files automatically to a known folder and verify the file presence after download to ensure the download functionality works correctly.