0
0
Selenium Pythontesting~10 mins

Clearing input fields in Selenium Python - Test Execution Trace

Choose your learning style9 modes available
Test Overview

This test opens a web page with a text input field, enters some text, then clears the input field. It verifies that the input field is empty after clearing.

Test Code - Selenium
Selenium Python
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.chrome.service import Service
from selenium.webdriver.chrome.options import Options
import unittest

class TestClearInputField(unittest.TestCase):
    def setUp(self):
        options = Options()
        options.add_argument('--headless')
        self.driver = webdriver.Chrome(service=Service(), options=options)
        self.driver.get('https://example.com/input')

    def test_clear_input_field(self):
        driver = self.driver
        input_field = driver.find_element(By.ID, 'username')
        input_field.send_keys('testuser')
        input_field.clear()
        self.assertEqual(input_field.get_attribute('value'), '')

    def tearDown(self):
        self.driver.quit()

if __name__ == '__main__':
    unittest.main()
Execution Trace - 7 Steps
StepActionSystem StateAssertionResult
1Test starts and Chrome browser opens in headless modeBrowser is ready but no page loaded yet-PASS
2Browser navigates to 'https://example.com/input'Page with input field having id 'username' is loaded-PASS
3Find input field by ID 'username'Input field element is located-PASS
4Send keys 'testuser' to input fieldInput field contains text 'testuser'-PASS
5Clear the input field using clear() methodInput field is empty-PASS
6Assert that input field value is empty stringInput field value is ''input_field.get_attribute('value') == ''PASS
7Close the browser and end testBrowser closed-PASS
Failure Scenario
Failing Condition: Input field is not found or clear() does not empty the field
Execution Trace Quiz - 3 Questions
Test your understanding
What does the clear() method do in this test?
AIt submits the form
BIt clicks the input field
CIt empties the text inside the input field
DIt refreshes the page
Key Result
Always verify that the input field is actually cleared by checking its 'value' attribute after calling clear(). This ensures the field is empty before further actions.