0
0
Selenium Pythontesting~10 mins

Why form handling is common in testing in Selenium Python - Test Execution Impact

Choose your learning style9 modes available
Test Overview

This test checks if a web form accepts input correctly and submits successfully. It verifies that form handling works as expected, which is common because forms collect user data in many applications.

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

class TestFormHandling(unittest.TestCase):
    def setUp(self):
        self.driver = webdriver.Chrome()
        self.driver.get('https://example.com/contact')

    def test_form_submission(self):
        driver = self.driver
        # Wait for form fields to be present
        WebDriverWait(driver, 10).until(EC.presence_of_element_located((By.ID, 'name')))
        # Fill in the form
        driver.find_element(By.ID, 'name').send_keys('Alice')
        driver.find_element(By.ID, 'email').send_keys('alice@example.com')
        driver.find_element(By.ID, 'message').send_keys('Hello, this is a test message.')
        # Submit the form
        driver.find_element(By.ID, 'submit-btn').click()
        # Wait for confirmation message
        confirmation = WebDriverWait(driver, 10).until(
            EC.visibility_of_element_located((By.ID, 'confirmation'))
        )
        # Assert confirmation text
        self.assertIn('Thank you', confirmation.text)

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

if __name__ == '__main__':
    unittest.main()
Execution Trace - 9 Steps
StepActionSystem StateAssertionResult
1Test starts and Chrome browser opensBrowser is open at 'https://example.com/contact' page with the form visible-PASS
2Waits for the 'name' input field to be present using WebDriverWait'name' input field is present and ready for inputPresence of element with ID 'name'PASS
3Fills 'name' field with 'Alice''name' field contains text 'Alice'-PASS
4Fills 'email' field with 'alice@example.com''email' field contains text 'alice@example.com'-PASS
5Fills 'message' field with 'Hello, this is a test message.''message' textarea contains the test message-PASS
6Clicks the submit button with ID 'submit-btn'Form is submitted, page processes the submission-PASS
7Waits for confirmation message with ID 'confirmation' to be visibleConfirmation message is visible on the pageVisibility of element with ID 'confirmation'PASS
8Checks that confirmation text contains 'Thank you'Confirmation message text includes 'Thank you'Assert 'Thank you' in confirmation.textPASS
9Test ends and browser closesBrowser is closed-PASS
Failure Scenario
Failing Condition: The confirmation message does not appear after form submission
Execution Trace Quiz - 3 Questions
Test your understanding
What does the test verify after submitting the form?
AThat a confirmation message with 'Thank you' appears
BThat the form fields are cleared
CThat the page URL changes
DThat the submit button is disabled
Key Result
Testing form handling is essential because forms are the main way users interact with web apps to submit data, so verifying input and submission ensures the app works correctly.