0
0
Selenium Pythontesting~15 mins

Why test frameworks structure execution in Selenium Python - Automation Benefits in Action

Choose your learning style9 modes available
Verify login functionality with structured test execution
Preconditions (2)
Step 1: Open the login page URL
Step 2: Enter 'user@example.com' in the email input field with id 'email'
Step 3: Enter 'Password123' in the password input field with id 'password'
Step 4: Click the login button with id 'loginBtn'
Step 5: Wait for the dashboard page to load
Step 6: Verify the URL is 'https://example.com/dashboard'
Step 7: Verify the welcome message with id 'welcomeMsg' contains 'Welcome, user!'
✅ Expected Result: User is successfully logged in and redirected to the dashboard page with the welcome message displayed.
Automation Requirements - Selenium with Python unittest
Assertions Needed:
Assert current URL is 'https://example.com/dashboard'
Assert welcome message text contains 'Welcome, user!'
Best Practices:
Use unittest framework to structure test setup, execution, and teardown
Use explicit waits to wait for elements or page load
Use clear and maintainable locators (by ID)
Separate setup and cleanup code in setUp and tearDown methods
Automated Solution
Selenium Python
import unittest
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

class TestLogin(unittest.TestCase):
    def setUp(self):
        self.driver = webdriver.Chrome()
        self.driver.maximize_window()
        self.wait = WebDriverWait(self.driver, 10)

    def test_login_success(self):
        driver = self.driver
        driver.get('https://example.com/login')

        email_input = self.wait.until(EC.visibility_of_element_located((By.ID, 'email')))
        email_input.send_keys('user@example.com')

        password_input = driver.find_element(By.ID, 'password')
        password_input.send_keys('Password123')

        login_button = driver.find_element(By.ID, 'loginBtn')
        login_button.click()

        # Wait for dashboard URL
        self.wait.until(EC.url_to_be('https://example.com/dashboard'))
        self.assertEqual(driver.current_url, 'https://example.com/dashboard')

        # Verify welcome message
        welcome_msg = self.wait.until(EC.visibility_of_element_located((By.ID, 'welcomeMsg')))
        self.assertIn('Welcome, user!', welcome_msg.text)

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

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

This test script uses the unittest framework to structure the test execution clearly. The setUp method opens the browser and prepares the wait object. The test method test_login_success performs the login steps exactly as described in the manual test case.

Explicit waits ensure the script waits for elements to appear or the URL to change before proceeding. This avoids timing issues.

Assertions check that the URL is correct and the welcome message is displayed, confirming successful login.

The tearDown method closes the browser after the test, keeping tests isolated and clean.

This structure helps keep tests organized, easy to read, and reliable, which is why test frameworks structure execution this way.

Common Mistakes - 3 Pitfalls
Not using explicit waits and relying on fixed sleep times
Mixing test logic and setup/cleanup code in one method
Using brittle locators like absolute XPaths
Bonus Challenge

Now add data-driven testing with 3 different sets of login credentials (valid and invalid) to verify login behavior.

Show Hint