0
0
Testing Fundamentalstesting~15 mins

Use case testing in Testing Fundamentals - Build an Automation Script

Choose your learning style9 modes available
Verify user login use case
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' and a welcome message 'Welcome, user!' is displayed
Automation Requirements - Selenium with Python
Assertions Needed:
Verify current URL contains '/dashboard'
Verify welcome message text is 'Welcome, user!'
Best Practices:
Use explicit waits to wait for elements to be visible or clickable
Use descriptive locators such as By.ID or By.CSS_SELECTOR
Organize code with setup and teardown methods
Use assertions from unittest or pytest frameworks
Automated Solution
Testing Fundamentals
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 TestUserLogin(unittest.TestCase):
    def setUp(self):
        self.driver = webdriver.Chrome()
        self.driver.get('https://example.com/login')
        self.wait = WebDriverWait(self.driver, 10)

    def test_user_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'))
        self.assertIn('/dashboard', driver.current_url, 'URL does not contain /dashboard after login')

        welcome_message = wait.until(EC.visibility_of_element_located((By.ID, 'welcome-msg')))
        self.assertEqual(welcome_message.text, 'Welcome, user!', 'Welcome message text is incorrect')

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

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

The setUp method opens the browser and navigates to the login page before each test.

We use WebDriverWait with explicit waits to wait for the email and password fields to be visible before interacting with them. This avoids errors if the page loads slowly.

We locate elements by their id attributes, which is a reliable and fast locator strategy.

After entering credentials, we wait for the login button to be clickable and then click it.

We wait until the URL contains '/dashboard' to confirm navigation succeeded.

Finally, we check that the welcome message text matches exactly 'Welcome, user!'.

The tearDown method closes the browser after the test finishes.

This structure keeps tests clean, readable, and reliable.

Common Mistakes - 4 Pitfalls
Using time.sleep() instead of explicit waits
Using brittle XPath locators with absolute paths
Not clearing input fields before typing
Not verifying the correct page after login
Bonus Challenge

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

Show Hint