0
0
Selenium Pythontesting~15 mins

Why form handling is common in testing in Selenium Python - Automation Benefits in Action

Choose your learning style9 modes available
Test user login form submission
Preconditions (2)
Step 1: Locate the username input field and enter 'testuser'
Step 2: Locate the password input field and enter 'Test@1234'
Step 3: Locate and click the login button
Step 4: Wait for the page to load after submission
✅ Expected Result: User is redirected to the dashboard page with URL containing '/dashboard' and a welcome message is displayed
Automation Requirements - Selenium with Python
Assertions Needed:
Verify the current URL contains '/dashboard'
Verify the welcome message element is visible and contains text 'Welcome, testuser'
Best Practices:
Use explicit waits to wait for elements to be visible or clickable
Use By.ID or By.NAME locators for form fields for better reliability
Avoid hardcoded sleeps
Structure code with setup and teardown methods for browser management
Automated Solution
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 TestLoginForm(unittest.TestCase):
    def setUp(self):
        self.driver = webdriver.Chrome()
        self.driver.get('https://example.com/login')
        self.wait = WebDriverWait(self.driver, 10)

    def test_login_form_submission(self):
        driver = self.driver
        wait = self.wait

        # Enter username
        username_field = wait.until(EC.visibility_of_element_located((By.ID, 'username')))
        username_field.clear()
        username_field.send_keys('testuser')

        # Enter password
        password_field = wait.until(EC.visibility_of_element_located((By.ID, 'password')))
        password_field.clear()
        password_field.send_keys('Test@1234')

        # Click login button
        login_button = wait.until(EC.element_to_be_clickable((By.ID, 'loginBtn')))
        login_button.click()

        # Wait for dashboard URL
        wait.until(EC.url_contains('/dashboard'))

        # Assert URL contains '/dashboard'
        self.assertIn('/dashboard', driver.current_url)

        # Assert welcome message is visible and correct
        welcome_message = wait.until(EC.visibility_of_element_located((By.ID, 'welcomeMsg')))
        self.assertIn('Welcome, testuser', welcome_message.text)

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

if __name__ == '__main__':
    unittest.main()

This test script uses Selenium with Python's unittest framework to automate the login form submission.

setUp() opens the browser and navigates to the login page.

In test_login_form_submission(), it waits explicitly for each form field to be visible before entering data. This avoids timing issues.

The login button is clicked after waiting for it to be clickable.

After submission, the script waits for the URL to contain '/dashboard' to confirm navigation.

Assertions check that the URL is correct and the welcome message is displayed with the expected text.

tearDown() closes the browser after the test.

This structure ensures reliable, maintainable automation following best practices.

Common Mistakes - 4 Pitfalls
Using time.sleep() instead of explicit waits
Using brittle XPath locators like absolute paths
Not clearing input fields before sending keys
Not verifying the page changed after form submission
Bonus Challenge

Now add data-driven testing with 3 different sets of username and password inputs

Show Hint