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.
This test automates downloading a file from a web page and verifies that the file is saved correctly in the specified download folder.
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()
| Step | Action | System State | Assertion | Result |
|---|---|---|---|---|
| 1 | Test starts and Chrome browser opens with download directory set | Chrome browser window is open with download preferences set to custom folder | - | PASS |
| 2 | Browser navigates to the Selenium downloads documentation page | Page loaded showing Selenium documentation with download links | - | PASS |
| 3 | Finds the link with text 'sampleFile.txt' on the page | Download link element is located | Element with link text 'sampleFile.txt' is found | PASS |
| 4 | Clicks the download link to start file download | Browser initiates file download to the specified folder | - | PASS |
| 5 | Waits up to 15 seconds for the file 'sampleFile.txt' to appear in download folder | File appears in the download folder within timeout | File exists at expected path | PASS |
| 6 | Asserts that the downloaded file exists | File is confirmed present in the download folder | assert os.path.exists(file_path) passes | PASS |
| 7 | Browser closes and test ends | Browser window closed, resources cleaned up | - | PASS |