Verify login functionality with valid credentials
Preconditions (2)
✅ Expected Result: User is redirected to the dashboard page and sees a welcome message
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 LoginPage: def __init__(self, driver): self.driver = driver self.email_input = (By.ID, 'email') self.password_input = (By.ID, 'password') self.login_button = (By.ID, 'loginBtn') def enter_email(self, email): WebDriverWait(self.driver, 10).until(EC.visibility_of_element_located(self.email_input)).send_keys(email) def enter_password(self, password): WebDriverWait(self.driver, 10).until(EC.visibility_of_element_located(self.password_input)).send_keys(password) def click_login(self): WebDriverWait(self.driver, 10).until(EC.element_to_be_clickable(self.login_button)).click() class DashboardPage: def __init__(self, driver): self.driver = driver self.welcome_message = (By.ID, 'welcomeMsg') def is_welcome_message_displayed(self): return WebDriverWait(self.driver, 10).until(EC.visibility_of_element_located(self.welcome_message)).is_displayed() class TestLogin(unittest.TestCase): def setUp(self): self.driver = webdriver.Chrome() self.driver.get('https://example.com/login') def test_valid_login(self): login_page = LoginPage(self.driver) login_page.enter_email('user@example.com') login_page.enter_password('Password123') login_page.click_login() WebDriverWait(self.driver, 10).until(EC.url_contains('/dashboard')) self.assertIn('/dashboard', self.driver.current_url) dashboard_page = DashboardPage(self.driver) self.assertTrue(dashboard_page.is_welcome_message_displayed()) def tearDown(self): self.driver.quit() if __name__ == '__main__': unittest.main()
This test script uses Selenium with Python and unittest framework.
We define two page classes: LoginPage and DashboardPage to organize locators and actions, following the Page Object Model.
In test_valid_login, we enter the email and password, click login, then wait for the dashboard URL and check the welcome message is visible.
Explicit waits ensure elements are ready before interacting, avoiding flaky tests.
The setUp and tearDown methods open and close the browser cleanly for each test.
Now add data-driven testing with 3 different sets of valid credentials
Enter username 'user1' and password 'pass123', what is the expected result?Click the 'Submit' button without entering any data with expected result: Form submits successfully.Pass the test and this pattern moves unknown