Use case testing in Testing Fundamentals - Build an Automation Script
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.
Now add data-driven testing with 3 different sets of valid user credentials