0
0
Selenium Pythontesting~10 mins

Submitting forms in Selenium Python - Test Execution Trace

Choose your learning style9 modes available
Test Overview

This test opens a web page with a form, fills in the input fields, submits the form, and verifies that the submission was successful by checking the confirmation message.

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 TestFormSubmission(unittest.TestCase):
    def setUp(self):
        self.driver = webdriver.Chrome()
        self.driver.get('https://example.com/contact')

    def test_submit_form(self):
        driver = self.driver
        # Find the name input and enter text
        name_input = WebDriverWait(driver, 10).until(
            EC.presence_of_element_located((By.ID, 'name'))
        )
        name_input.send_keys('Alice')

        # Find the email input and enter text
        email_input = driver.find_element(By.ID, 'email')
        email_input.send_keys('alice@example.com')

        # Find the message textarea and enter text
        message_input = driver.find_element(By.ID, 'message')
        message_input.send_keys('Hello, this is a test message.')

        # Submit the form by clicking the submit button
        submit_button = driver.find_element(By.ID, 'submit-btn')
        submit_button.click()

        # Wait for confirmation message to appear
        confirmation = WebDriverWait(driver, 10).until(
            EC.visibility_of_element_located((By.ID, 'confirmation'))
        )

        # Verify the confirmation text
        self.assertEqual(confirmation.text, 'Thank you for your message!')

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

if __name__ == '__main__':
    unittest.main()
Execution Trace - 12 Steps
StepActionSystem StateAssertionResult
1Test starts and Chrome browser opensBrowser window is open, ready to load the page-PASS
2Navigates to 'https://example.com/contact'Contact page with form is loadedPage loaded with form elements presentPASS
3Finds the name input field by ID 'name'Name input field is located and ready for inputElement with ID 'name' is presentPASS
4Enters 'Alice' into the name input fieldName input field contains text 'Alice'Input field value is 'Alice'PASS
5Finds the email input field by ID 'email'Email input field is located and ready for inputElement with ID 'email' is presentPASS
6Enters 'alice@example.com' into the email input fieldEmail input field contains text 'alice@example.com'Input field value is 'alice@example.com'PASS
7Finds the message textarea by ID 'message'Message textarea is located and ready for inputElement with ID 'message' is presentPASS
8Enters 'Hello, this is a test message.' into the message textareaMessage textarea contains the test messageTextarea value matches the entered messagePASS
9Finds the submit button by ID 'submit-btn' and clicks itForm is submitted, page processes submissionSubmit button is clickable and clickedPASS
10Waits for confirmation message with ID 'confirmation' to appearConfirmation message is visible on the pageElement with ID 'confirmation' is visiblePASS
11Checks that confirmation text equals 'Thank you for your message!'Confirmation text is displayed correctlyText matches expected confirmation messagePASS
12Test ends and browser closesBrowser window 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 action does the test perform to submit the form?
APresses the Enter key in the message textarea
BClicks the submit button with ID 'submit-btn'
CCalls a JavaScript submit function directly
DRefreshes the page after filling inputs
Key Result
Always wait explicitly for elements that appear after actions like form submission to avoid flaky tests.