Verify login functionality with valid credentials
Preconditions (2)
✅ Expected Result: User is redirected to the dashboard page with URL containing '/dashboard'
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.
Now add data-driven testing with 3 different sets of valid credentials
Steps: 1. Enter username 'user1'
2. Enter password 'pass123'
3. Click 'Submit'
Expected: User is logged in and dashboard appears
Actual: Error message 'Invalid credentials'
Steps: 1. Open app
2. Click 'Settings'
Expected: Settings page opens
Actual: Settings page opens
Steps: 1. Add item to cart
2. Proceed to checkout
Expected: Total price updates correctly
Actual: Total price remains unchanged
Pass the test and this pattern moves unknown