0
0
Selenium Pythontesting~5 mins

Base page class in Selenium Python

Choose your learning style9 modes available
Introduction

A base page class helps keep your test code clean and organized by putting common web page actions in one place.

When you test many web pages that share similar actions like clicking or typing.
When you want to avoid repeating the same code in multiple test files.
When you want to make your tests easier to update if the website changes.
When you want to keep your test code simple and easy to read.
When you want to add common waits or checks for all pages in one place.
Syntax
Selenium Python
class BasePage:
    def __init__(self, driver):
        self.driver = driver

    def find_element(self, locator):
        return self.driver.find_element(*locator)

    def click(self, locator):
        element = self.find_element(locator)
        element.click()

    def type_text(self, locator, text):
        element = self.find_element(locator)
        element.clear()
        element.send_keys(text)

The driver is the browser control object from Selenium.

Locators are usually tuples like (By.ID, 'element_id').

Examples
This example shows how to store the driver and find an element using a locator tuple.
Selenium Python
from selenium.webdriver.common.by import By

class BasePage:
    def __init__(self, driver):
        self.driver = driver

    def find_element(self, locator):
        return self.driver.find_element(*locator)
This method clicks on the element found by the locator.
Selenium Python
def click(self, locator):
    element = self.find_element(locator)
    element.click()
This method clears any existing text and types new text into an input field.
Selenium Python
def type_text(self, locator, text):
    element = self.find_element(locator)
    element.clear()
    element.send_keys(text)
Sample Program

This script opens a browser, navigates to example.com, and tries to type and click using the base page methods. It prints if the test passed or failed.

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

class BasePage:
    def __init__(self, driver):
        self.driver = driver

    def find_element(self, locator):
        return self.driver.find_element(*locator)

    def click(self, locator):
        element = self.find_element(locator)
        element.click()

    def type_text(self, locator, text):
        element = self.find_element(locator)
        element.clear()
        element.send_keys(text)

# Example usage:
if __name__ == '__main__':
    driver = webdriver.Chrome()
    driver.get('https://www.example.com')

    base_page = BasePage(driver)

    # Example locators (these won't work on example.com, just for demo)
    search_box = (By.NAME, 'q')
    search_button = (By.NAME, 'btnK')

    try:
        base_page.type_text(search_box, 'Selenium')
        base_page.click(search_button)
        print('Test passed: Typed text and clicked button')
    except Exception as e:
        print(f'Test failed: {e}')
    finally:
        time.sleep(2)
        driver.quit()
OutputSuccess
Important Notes

Always pass the Selenium driver to the base page class when creating it.

Use locator tuples like (By.ID, 'id_value') for better readability and maintainability.

Handle exceptions in your tests to know if an element was not found or an action failed.

Summary

A base page class groups common web page actions to avoid repeating code.

It makes tests easier to write, read, and update.

Use locator tuples and pass the driver to the base page class.