0
0
Testing Fundamentalstesting~15 mins

Why test design drives efficiency in Testing Fundamentals - Automation Benefits in Action

Choose your learning style9 modes available
Verify login functionality with valid credentials
Preconditions (2)
Step 1: Enter 'user@example.com' in the email input field
Step 2: Enter 'Password123' in the password input field
Step 3: Click the 'Login' button
Step 4: Wait for the dashboard page to load
✅ 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 current URL contains '/dashboard'
Verify welcome message element is visible and contains text 'Welcome'
Best Practices:
Use explicit waits to wait for elements or page load
Use descriptive and maintainable locators (e.g., By.ID or By.CSS_SELECTOR)
Organize code with setup and teardown methods
Keep test steps clear and simple
Automated Solution
Testing Fundamentals
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 TestLogin(unittest.TestCase):
    def setUp(self):
        self.driver = webdriver.Chrome()
        self.driver.get('https://example.com/login')
        self.wait = WebDriverWait(self.driver, 10)

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

        email_input = 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, 'login-button')
        login_button.click()

        wait.until(EC.url_contains('/dashboard'))
        self.assertIn('/dashboard', driver.current_url)

        welcome_message = wait.until(EC.visibility_of_element_located((By.ID, 'welcome-msg')))
        self.assertIn('Welcome', 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 manual test case.

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

test_valid_login() performs the steps: enters email and password, clicks login, waits for the dashboard URL, and verifies the welcome message.

Explicit waits ensure the test waits only as long as needed for elements or URL changes, improving reliability.

Locators use By.ID for clarity and maintainability.

tearDown() closes the browser after the test.

This structure keeps the test clear, efficient, and easy to maintain, showing how good test design drives efficiency.

Common Mistakes - 3 Pitfalls
Using time.sleep() instead of explicit waits
Using brittle XPath locators that rely on full element paths
Not verifying the expected page or element after login
Bonus Challenge

Now add data-driven testing with 3 different sets of valid credentials

Show Hint