0
0
Selenium Pythontesting~10 mins

File upload handling in Selenium Python - Test Execution Trace

Choose your learning style9 modes available
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.

Test Code - Selenium
Selenium Python
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()
Execution Trace - 6 Steps
StepActionSystem StateAssertionResult
1Test starts and opens Chrome browserBrowser window opens at URL https://example.com/upload-PASS
2Waits for file input element with id 'file-upload' to be presentFile input element is visible on the pagePresence of file input elementPASS
3Sends absolute file path of 'testfile.txt' to file input elementFile path is entered into the file input field-PASS
4Finds and clicks the upload button with id 'upload-button'Upload button is clicked, upload process starts-PASS
5Waits for uploaded file name element with id 'uploaded-file-name' to be visibleUploaded file name 'testfile.txt' is displayed on the pageUploaded file name text equals 'testfile.txt'PASS
6Test ends and browser closesBrowser window closes-PASS
Failure Scenario
Failing Condition: File input element with id 'file-upload' is not found or file path is incorrect
Execution Trace Quiz - 3 Questions
Test your understanding
What does the test verify after uploading the file?
AThe displayed uploaded file name matches the uploaded file
BThe upload button is disabled after clicking
CThe file input field is cleared after upload
DThe page URL changes after upload
Key Result
Always wait explicitly for elements to be present or visible before interacting to avoid flaky tests.