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.