0
0
Selenium Pythontesting~15 mins

Page factory pattern in Selenium Python - Build an Automation Script

Choose your learning style9 modes available
Login functionality test using Page Factory pattern
Preconditions (3)
Step 1: Open the browser and navigate to 'https://example.com/login'
Step 2: Locate the username input field and enter 'testuser'
Step 3: Locate the password input field and enter 'Test@1234'
Step 4: Locate and click the login button
Step 5: Wait for the dashboard page to load
Step 6: Verify that the URL is 'https://example.com/dashboard'
Step 7: Verify that a welcome message with text 'Welcome, testuser!' is displayed
✅ Expected Result: User is successfully logged in and redirected to the dashboard page with a welcome message displayed
Automation Requirements - Selenium with Python
Assertions Needed:
Verify current URL is 'https://example.com/dashboard'
Verify welcome message text is 'Welcome, testuser!'
Best Practices:
Use Page Factory pattern with @find_element decorators
Use explicit waits to wait for elements or page load
Separate page object class from test class
Use meaningful locator strategies (By.ID, By.NAME, By.CSS_SELECTOR)
Avoid hardcoded sleeps
Automated Solution
Selenium Python
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
from selenium.webdriver.chrome.service import Service
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.support.page_factory import PageFactory
import unittest

class LoginPage:
    def __init__(self, driver):
        self.driver = driver
        self.wait = WebDriverWait(driver, 10)
        self.username = (By.ID, 'username')
        self.password = (By.ID, 'password')
        self.login_button = (By.ID, 'loginBtn')

    def enter_username(self, username):
        self.wait.until(EC.visibility_of_element_located(self.username)).send_keys(username)

    def enter_password(self, password):
        self.wait.until(EC.visibility_of_element_located(self.password)).send_keys(password)

    def click_login(self):
        self.wait.until(EC.element_to_be_clickable(self.login_button)).click()

class DashboardPage:
    def __init__(self, driver):
        self.driver = driver
        self.wait = WebDriverWait(driver, 10)
        self.welcome_message = (By.ID, 'welcomeMsg')

    def get_welcome_text(self):
        element = self.wait.until(EC.visibility_of_element_located(self.welcome_message))
        return element.text

class TestLogin(unittest.TestCase):
    def setUp(self):
        options = Options()
        options.add_argument('--headless')
        self.driver = webdriver.Chrome(options=options)
        self.driver.get('https://example.com/login')

    def test_login_success(self):
        login_page = LoginPage(self.driver)
        login_page.enter_username('testuser')
        login_page.enter_password('Test@1234')
        login_page.click_login()

        # Verify URL
        WebDriverWait(self.driver, 10).until(EC.url_to_be('https://example.com/dashboard'))
        self.assertEqual(self.driver.current_url, 'https://example.com/dashboard')

        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()

The code uses Selenium WebDriver with Python's unittest framework.

We define two page classes: LoginPage and DashboardPage. Each class holds locators and methods to interact with page elements.

In LoginPage, methods enter username, password, and click login button using explicit waits to ensure elements are ready.

In DashboardPage, we wait for the welcome message to appear and get its text.

The test class TestLogin opens the browser in headless mode, navigates to login page, performs login steps, then verifies the URL and welcome message text.

Explicit waits avoid timing issues. Locators use IDs for reliability. The Page Factory pattern is followed by separating page logic from test logic, making code clean and maintainable.

Common Mistakes - 4 Pitfalls
Using time.sleep() instead of explicit waits
Locating elements inside test methods instead of page classes
Using brittle locators like absolute XPaths
Not quitting the browser after tests
Bonus Challenge

Now add data-driven testing with 3 different username and password combinations to verify login success or failure.

Show Hint