0
0
Selenium Pythontesting~3 mins

Why Page class structure in Selenium Python? - Purpose & Use Cases

Choose your learning style9 modes available
The Big Idea

Discover how organizing your test code by pages can save hours of frustration and bugs!

The Scenario

Imagine testing a website by clicking buttons and filling forms manually every time you want to check if it works.

You write separate code for each page, repeating similar steps everywhere.

The Problem

This manual way is slow and tiring.

Every small change in the website means you must find and fix many places in your code.

It's easy to make mistakes and hard to keep track of what each part does.

The Solution

Using a Page class structure, you create one class for each page of the website.

Each class holds all the buttons, fields, and actions for that page in one place.

This makes your code clean, easy to update, and simple to understand.

Before vs After
Before
driver.find_element(By.ID, 'login').click()
driver.find_element(By.ID, 'username').send_keys('user')
driver.find_element(By.ID, 'password').send_keys('pass')
driver.find_element(By.ID, 'submit').click()
After
class LoginPage:
    def __init__(self, driver):
        self.driver = driver
    def login(self, user, pwd):
        self.driver.find_element(By.ID, 'login').click()
        self.driver.find_element(By.ID, 'username').send_keys(user)
        self.driver.find_element(By.ID, 'password').send_keys(pwd)
        self.driver.find_element(By.ID, 'submit').click()
What It Enables

It enables writing tests that are easy to read, maintain, and reuse across many test cases.

Real Life Example

When a website changes its login button ID, you only update it once inside the LoginPage class instead of searching through all your test scripts.

Key Takeaways

Manual test code is repetitive and hard to maintain.

Page class structure groups page elements and actions logically.

This approach saves time and reduces errors when website changes.