Test Overview
This test checks if the login page properly blocks access when wrong credentials are used, protecting user accounts from unauthorized access.
This test checks if the login page properly blocks access when wrong credentials are used, protecting user accounts from unauthorized access.
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 TestLoginSecurity(unittest.TestCase): def setUp(self): self.driver = webdriver.Chrome() self.driver.get('https://example.com/login') def test_invalid_login_blocks_access(self): driver = self.driver # Wait for username field WebDriverWait(driver, 10).until(EC.presence_of_element_located((By.ID, 'username'))) username_input = driver.find_element(By.ID, 'username') password_input = driver.find_element(By.ID, 'password') login_button = driver.find_element(By.ID, 'login-btn') # Enter invalid credentials username_input.send_keys('wronguser') password_input.send_keys('wrongpass') login_button.click() # Wait for error message error_message = WebDriverWait(driver, 10).until( EC.visibility_of_element_located((By.ID, 'error-msg')) ) # Verify error message text self.assertEqual(error_message.text, 'Invalid username or password.') 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 window opens at 'https://example.com/login' showing login form with username, password fields and login button | - | PASS |
| 2 | Waits for username input field to be present | Login page fully loaded with input fields visible | Username input field is present | PASS |
| 3 | Finds username, password fields and login button | All required elements located on the page | Elements found by ID selectors | PASS |
| 4 | Enters invalid username and password | Input fields filled with 'wronguser' and 'wrongpass' | - | PASS |
| 5 | Clicks login button | Login form submitted, page processes login | - | PASS |
| 6 | Waits for error message to appear | Error message element visible with text 'Invalid username or password.' | Error message is visible | PASS |
| 7 | Checks error message text matches expected | Error message text is exactly 'Invalid username or password.' | Error message text equals expected string | PASS |
| 8 | Test ends and browser closes | Browser window closed | - | PASS |