Test Overview
This test checks how well a simple login form works by using a well-planned test design. It verifies that the login succeeds with correct data and fails with wrong data, showing how good test design saves time and effort.
This test checks how well a simple login form works by using a well-planned test design. It verifies that the login succeeds with correct data and fails with wrong data, showing how good test design saves time and effort.
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 TestLogin(unittest.TestCase): def setUp(self): self.driver = webdriver.Chrome() self.driver.get('https://example.com/login') def test_login(self): driver = self.driver # Test data from designed test cases test_cases = [ {'username': 'user1', 'password': 'pass1', 'should_pass': True}, {'username': 'user1', 'password': 'wrongpass', 'should_pass': False} ] for case in test_cases: username_input = WebDriverWait(driver, 10).until( EC.presence_of_element_located((By.ID, 'username')) ) password_input = driver.find_element(By.ID, 'password') login_button = driver.find_element(By.ID, 'login-btn') username_input.clear() username_input.send_keys(case['username']) password_input.clear() password_input.send_keys(case['password']) login_button.click() if case['should_pass']: welcome_msg = WebDriverWait(driver, 10).until( EC.presence_of_element_located((By.ID, 'welcome-msg')) ) self.assertIn('Welcome', welcome_msg.text) else: error_msg = WebDriverWait(driver, 10).until( EC.presence_of_element_located((By.ID, 'error-msg')) ) self.assertIn('Invalid', error_msg.text) def tearDown(self): self.driver.quit() if __name__ == '__main__': unittest.main()
| Step | Action | System State | Assertion | Result |
|---|---|---|---|---|
| 1 | Test starts and opens Chrome browser | Browser opens at https://example.com/login showing login form with username, password fields and login button | - | PASS |
| 2 | Find username, password input fields and login button | Login form elements are present and interactable | Check presence of username input field | PASS |
| 3 | Enter username 'user1' and password 'pass1', then click login | Login form filled with valid credentials and login button clicked | - | PASS |
| 4 | Wait for welcome message to appear | Page shows welcome message with text containing 'Welcome' | Verify welcome message text contains 'Welcome' | PASS |
| 5 | Clear inputs, enter username 'user1' and wrong password 'wrongpass', then click login | Login form filled with invalid credentials and login button clicked | - | PASS |
| 6 | Wait for error message to appear | Page shows error message with text containing 'Invalid' | Verify error message text contains 'Invalid' | PASS |
| 7 | Close browser and end test | Browser closed | - | PASS |