Pair 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 TestLogin(unittest.TestCase): def setUp(self): self.driver = webdriver.Chrome() self.driver.get('https://example.com/login') self.wait = WebDriverWait(self.driver, 10) def login(self, email, password): email_field = self.wait.until(EC.presence_of_element_located((By.ID, 'email'))) password_field = self.driver.find_element(By.ID, 'password') login_button = self.driver.find_element(By.ID, 'loginBtn') email_field.clear() email_field.send_keys(email) password_field.clear() password_field.send_keys(password) login_button.click() def test_valid_login(self): self.login('user@example.com', 'Password123') self.wait.until(EC.url_contains('/dashboard')) self.assertIn('/dashboard', self.driver.current_url) def test_invalid_email_format(self): self.login('userexample.com', 'Password123') error = self.wait.until(EC.visibility_of_element_located((By.ID, 'emailError'))) self.assertEqual(error.text, 'Please enter a valid email address.') def test_empty_password(self): self.login('user@example.com', '') error = self.wait.until(EC.visibility_of_element_located((By.ID, 'passwordError'))) self.assertEqual(error.text, 'Password cannot be empty.') def test_wrong_password(self): self.login('user@example.com', 'WrongPass') error = self.wait.until(EC.visibility_of_element_located((By.ID, 'loginError'))) self.assertEqual(error.text, 'Incorrect email or password.') def tearDown(self): self.driver.quit() if __name__ == '__main__': unittest.main()
This test script uses Selenium with Python's unittest framework to automate the login tests.
setUp: Opens the browser and navigates to the login page before each test.
login method: Enters email and password, then clicks login.
test_valid_login: Checks that after valid login, URL contains '/dashboard'.
test_invalid_email_format: Checks error message for invalid email format.
test_empty_password: Checks error message when password is empty.
test_wrong_password: Checks error message for wrong password.
tearDown: Closes the browser after each test.
Explicit waits ensure elements are ready before interaction. Assertions verify expected results. Locators use IDs for clarity and maintainability.
Now add data-driven testing with 3 different sets of login credentials (valid, invalid email, wrong password).