0
0
Selenium Pythontesting~7 mins

Test class using page objects in Selenium Python

Choose your learning style9 modes available
Introduction

Using page objects helps keep your test code clean and easy to understand by separating page details from test steps.

When you want to test a website with many pages or elements.
When you want to reuse code for interacting with the same page in multiple tests.
When you want to make your tests easier to maintain if the website changes.
When you want to write clear and simple test cases without repeating locator details.
Syntax
Selenium Python
from selenium.webdriver.common.by import By
import unittest
from selenium import webdriver

class PageName:
    def __init__(self, driver):
        self.driver = driver
        self.element_locator = (By.ID, 'element_id')

    def action_method(self):
        element = self.driver.find_element(*self.element_locator)
        element.click()


class TestClass(unittest.TestCase):
    def setUp(self):
        self.driver = webdriver.Chrome()
        self.page = PageName(self.driver)

    def test_something(self):
        self.driver.get('https://example.com')
        self.page.action_method()
        self.assertTrue(True)

    def tearDown(self):
        self.driver.quit()

Page classes hold locators and methods to interact with page elements.

Test classes use page objects to perform actions and assertions.

Examples
This page object represents a login page with a method to perform login.
Selenium Python
from selenium.webdriver.common.by import By

class LoginPage:
    def __init__(self, driver):
        self.driver = driver
        self.username_input = (By.NAME, 'username')
        self.password_input = (By.NAME, 'password')
        self.login_button = (By.ID, 'loginBtn')

    def login(self, username, password):
        self.driver.find_element(*self.username_input).send_keys(username)
        self.driver.find_element(*self.password_input).send_keys(password)
        self.driver.find_element(*self.login_button).click()
This test class uses the LoginPage object to test a valid login scenario.
Selenium Python
import unittest
from selenium import webdriver

class TestLogin(unittest.TestCase):
    def setUp(self):
        self.driver = webdriver.Chrome()
        self.login_page = LoginPage(self.driver)

    def test_valid_login(self):
        self.driver.get('https://example.com/login')
        self.login_page.login('user1', 'pass123')
        self.assertIn('Dashboard', self.driver.title)

    def tearDown(self):
        self.driver.quit()
Sample Program

This test opens Google, searches for 'Selenium testing', and checks the page title contains the search term.

Selenium Python
import unittest
from selenium import webdriver
from selenium.webdriver.common.by import By

class HomePage:
    def __init__(self, driver):
        self.driver = driver
        self.search_box = (By.NAME, 'q')
        self.search_button = (By.NAME, 'btnK')

    def search(self, text):
        self.driver.find_element(*self.search_box).send_keys(text)
        self.driver.find_element(*self.search_button).click()

class TestGoogleSearch(unittest.TestCase):
    def setUp(self):
        self.driver = webdriver.Chrome()
        self.home_page = HomePage(self.driver)

    def test_search(self):
        self.driver.get('https://www.google.com')
        self.home_page.search('Selenium testing')
        self.assertIn('Selenium testing', self.driver.title)

    def tearDown(self):
        self.driver.quit()

if __name__ == '__main__':
    unittest.main()
OutputSuccess
Important Notes

Use clear and unique locators to avoid flaky tests.

Keep page object methods focused on one action for clarity.

Always quit the browser in tearDown to free resources.

Summary

Page objects separate page details from test logic.

Test classes use page objects to perform actions and checks.

This approach makes tests easier to read and maintain.