Test Overview
This test automates uploading a file on a web page and verifies that the file upload was successful by checking the displayed file name.
This test automates uploading a file on a web page and verifies that the file upload was successful by checking the displayed file name.
from selenium import webdriver from selenium.webdriver.common.by import By from selenium.webdriver.support.ui import WebDriverWait from selenium.webdriver.support import expected_conditions as EC import unittest import os class TestFileUpload(unittest.TestCase): def setUp(self): self.driver = webdriver.Chrome() self.driver.get('https://example.com/upload') def test_file_upload(self): driver = self.driver # Locate the file input element by its id file_input = WebDriverWait(driver, 10).until( EC.presence_of_element_located((By.ID, 'file-upload')) ) # Prepare the file path to upload file_path = os.path.abspath('testfile.txt') # Send the file path to the input element file_input.send_keys(file_path) # Click the upload button upload_button = driver.find_element(By.ID, 'upload-button') upload_button.click() # Wait for the uploaded file name to appear uploaded_file_name = WebDriverWait(driver, 10).until( EC.visibility_of_element_located((By.ID, 'uploaded-file-name')) ) # Assert the uploaded file name matches the file uploaded self.assertEqual(uploaded_file_name.text, 'testfile.txt') def tearDown(self): self.driver.quit() if __name__ == '__main__': unittest.main()
| Step | Action | System State | Assertion | Result |
|---|---|---|---|---|
| 1 | Test starts and opens Chrome browser | Browser window opens at URL https://example.com/upload | - | PASS |
| 2 | Waits for file input element with id 'file-upload' to be present | File input element is visible on the page | Presence of file input element | PASS |
| 3 | Sends absolute file path of 'testfile.txt' to file input element | File path is entered into the file input field | - | PASS |
| 4 | Finds and clicks the upload button with id 'upload-button' | Upload button is clicked, upload process starts | - | PASS |
| 5 | Waits for uploaded file name element with id 'uploaded-file-name' to be visible | Uploaded file name 'testfile.txt' is displayed on the page | Uploaded file name text equals 'testfile.txt' | PASS |
| 6 | Test ends and browser closes | Browser window closes | - | PASS |