Authentication testing in Testing Fundamentals - Build an Automation Script
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 TestAuthentication(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_and_invalid_login(self): driver = self.driver wait = self.wait # Valid login email_input = wait.until(EC.visibility_of_element_located((By.ID, 'email'))) email_input.clear() email_input.send_keys('validuser@example.com') password_input = driver.find_element(By.ID, 'password') password_input.clear() password_input.send_keys('ValidPass123') login_button = driver.find_element(By.ID, 'login-button') login_button.click() # Wait for dashboard URL wait.until(EC.url_contains('/dashboard')) self.assertIn('/dashboard', driver.current_url, 'Dashboard URL not loaded after valid login') # Log out to return to login page logout_button = wait.until(EC.element_to_be_clickable((By.ID, 'logout-button'))) logout_button.click() wait.until(EC.url_contains('/login')) # Invalid login email_input = wait.until(EC.visibility_of_element_located((By.ID, 'email'))) email_input.clear() email_input.send_keys('invaliduser@example.com') password_input = driver.find_element(By.ID, 'password') password_input.clear() password_input.send_keys('WrongPass') login_button = driver.find_element(By.ID, 'login-button') login_button.click() # Verify error message error_message = wait.until(EC.visibility_of_element_located((By.ID, 'error-message'))) self.assertEqual(error_message.text, 'Invalid email or password', 'Error message text mismatch') 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.
In test_valid_and_invalid_login, we first enter valid credentials and click login. We wait explicitly for the dashboard URL to load and assert the URL contains '/dashboard'.
Then we log out by clicking the logout button and wait to return to the login page.
Next, we enter invalid credentials and click login. We wait for the error message element to appear and assert its text matches the expected error.
The tearDown method closes the browser after the test finishes.
Explicit waits ensure the test waits for elements or page changes before interacting or asserting, making the test stable.
Using IDs for locators keeps selectors simple and reliable.
Now add data-driven testing with 3 different sets of credentials: one valid, two invalid.
Practice
authentication testing in software testing?Solution
Step 1: Understand authentication testing purpose
Authentication testing focuses on verifying user identity before granting access.Step 2: Compare options with this purpose
Only To verify that only valid users can access the system matches this goal; others relate to different testing areas.Final Answer:
To verify that only valid users can access the system -> Option AQuick Check:
Authentication testing = verify valid users [OK]
- Confusing authentication with performance testing
- Thinking authentication tests UI elements
- Mixing authentication with database testing
Solution
Step 1: Identify valid test steps for authentication
Testing authentication requires entering valid credentials and verifying access.Step 2: Evaluate options for correctness
Enter valid username and password, then check for successful login correctly describes this; others do not test authentication properly.Final Answer:
Enter valid username and password, then check for successful login -> Option AQuick Check:
Valid credentials test = successful login [OK]
- Ignoring input fields and clicking randomly
- Testing only UI elements without input
- Confusing visual checks with authentication tests
Input: username='user1', password='wrongpass'
Expected: Login fails with error message 'Invalid credentials'
What will be the test result if the system allows login with these credentials?
Solution
Step 1: Analyze expected vs actual behavior
The test expects login failure with an error message for wrong password.Step 2: Compare actual system behavior
If login succeeds with wrong password, it contradicts expected behavior, causing test failure.Final Answer:
Test fails because login should not succeed -> Option DQuick Check:
Wrong password login = test fail [OK]
- Assuming any login success means pass
- Ignoring expected error messages
- Confusing test pass/fail logic
def test_login():
username = 'admin'
password = 'admin123'
result = login(username, password)
assert result == FalseWhat is the error in this test?
Solution
Step 1: Understand test intent and data
Username and password appear valid, so login should succeed (True).Step 2: Check assertion correctness
Assertion expects False (failure), which contradicts valid credentials, causing test error.Final Answer:
The assertion expects login to fail but credentials are valid -> Option CQuick Check:
Valid credentials expect True, not False [OK]
- Asserting wrong expected result
- Assuming variables undefined without checking
- Confusing function call syntax
Solution
Step 1: Identify feature to test
The feature locks account after 3 failed login attempts.Step 2: Select test case matching feature
Attempt login 3 times with wrong password, then check if account is locked tests exactly this by failing login 3 times and checking lock status.Final Answer:
Attempt login 3 times with wrong password, then check if account is locked -> Option BQuick Check:
3 failed attempts = account lock test [OK]
- Testing unrelated features like password reset
- Using correct password instead of failed attempts
- Ignoring the number of failed attempts
Ready to prove it?
Pass the test and this pattern moves unknown
