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.
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.
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()
| Step | Action | System State | Assertion | Result |
|---|---|---|---|---|
| 1 | Test starts and Chrome browser opens | Browser is open at 'https://example.com/contact' page with the form visible | - | PASS |
| 2 | Waits for the 'name' input field to be present using WebDriverWait | 'name' input field is present and ready for input | Presence of element with ID 'name' | PASS |
| 3 | Fills 'name' field with 'Alice' | 'name' field contains text 'Alice' | - | PASS |
| 4 | Fills 'email' field with 'alice@example.com' | 'email' field contains text 'alice@example.com' | - | PASS |
| 5 | Fills 'message' field with 'Hello, this is a test message.' | 'message' textarea contains the test message | - | PASS |
| 6 | Clicks the submit button with ID 'submit-btn' | Form is submitted, page processes the submission | - | PASS |
| 7 | Waits for confirmation message with ID 'confirmation' to be visible | Confirmation message is visible on the page | Visibility of element with ID 'confirmation' | PASS |
| 8 | Checks that confirmation text contains 'Thank you' | Confirmation message text includes 'Thank you' | Assert 'Thank you' in confirmation.text | PASS |
| 9 | Test ends and browser closes | Browser is closed | - | PASS |