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.username_input = (By.ID, 'username')
self.password_input = (By.ID, 'password')
self.login_button = (By.ID, 'loginBtn')
def enter_username(self, username):
WebDriverWait(self.driver, 10).until(EC.visibility_of_element_located(self.username_input)).send_keys(username)
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 get_welcome_text(self):
return WebDriverWait(self.driver, 10).until(EC.visibility_of_element_located(self.welcome_message)).text
class TestIntegrationLoginDashboard(unittest.TestCase):
def setUp(self):
self.driver = webdriver.Chrome()
self.driver.get('https://example.com/login')
def test_login_and_dashboard_integration(self):
login_page = LoginPage(self.driver)
login_page.enter_username('testuser')
login_page.enter_password('Test@1234')
login_page.click_login()
# Verify URL changed to dashboard
WebDriverWait(self.driver, 10).until(EC.url_contains('/dashboard'))
self.assertIn('/dashboard', self.driver.current_url)
dashboard_page = DashboardPage(self.driver)
welcome_text = dashboard_page.get_welcome_text()
self.assertEqual(welcome_text, 'Welcome, testuser!')
def tearDown(self):
self.driver.quit()
if __name__ == '__main__':
unittest.main()This test uses Selenium with Python's unittest framework to automate the integration test.
We define two page objects: LoginPage and DashboardPage. This keeps code organized and easy to maintain.
In test_login_and_dashboard_integration, we open the login page, enter username and password, and click login.
We then wait until the URL contains '/dashboard' to confirm navigation.
Finally, we verify the welcome message text matches the expected string.
Explicit waits ensure elements are ready before interacting, avoiding flaky tests.
Setup and teardown methods open and close the browser cleanly for each test run.