0
0
Testing Fundamentalstesting~15 mins

Test case components (steps, expected, actual) in Testing Fundamentals - Build an Automation Script

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
✅ Expected Result: User is redirected to the dashboard page with URL containing '/dashboard'
Automation Requirements - Selenium with Python
Assertions Needed:
Verify current URL contains '/dashboard' after login
Verify login button is clickable before clicking
Best Practices:
Use explicit waits to wait for elements to be clickable or visible
Use By.ID or By.NAME locators instead of XPath for better maintainability
Structure code with setup and teardown methods for browser management
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.clear()
        email_input.send_keys('user@example.com')

        password_input = wait.until(EC.visibility_of_element_located((By.ID, 'password')))
        password_input.clear()
        password_input.send_keys('Password123')

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

        wait.until(EC.url_contains('/dashboard'))
        current_url = driver.current_url
        self.assertIn('/dashboard', current_url, 'User was not redirected to dashboard')

    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() waits for the email and password fields to be visible, enters the given credentials, waits for the login button to be clickable, and clicks it.

Then it waits until the URL contains '/dashboard' to confirm successful login and asserts this condition.

tearDown() closes the browser after the test.

Explicit waits ensure the test waits for elements properly, avoiding flaky failures.

Common Mistakes - 3 Pitfalls
{'mistake': 'Using time.sleep() instead of explicit waits', 'why_bad': 'It causes tests to wait unnecessarily long or fail if the element loads slower or faster than expected.', 'correct_approach': "Use Selenium's explicit waits like WebDriverWait with expected_conditions."}
Using brittle XPath locators like absolute paths
Not clearing input fields before sending keys
Bonus Challenge

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

Show Hint