What if you could fix one place and save hours of test fixes?
Why Test class using page objects in Selenium Python? - Purpose & Use Cases
Imagine testing a website by manually writing code that finds buttons and fields everywhere in your test scripts.
Every time the page changes, you must update all those scattered locators.
This manual way is slow and confusing.
Tests break easily when the page changes.
It's hard to find where to fix problems because locators are all over the place.
Using page objects means creating a single place for each page's elements and actions.
Tests become cleaner and easier to maintain.
When the page changes, update only the page object, not every test.
driver.find_element(By.ID, 'username').send_keys('user') driver.find_element(By.ID, 'password').send_keys('1234') driver.find_element(By.ID, 'login').click()
login_page.enter_username('user') login_page.enter_password('1234') login_page.click_login()
It enables writing clear, reusable tests that stay stable even when the website changes.
Imagine testing a shopping site where the login page changes often.
With page objects, you fix locators once in the login page class, and all tests keep working smoothly.
Manual locator use spreads code and causes fragile tests.
Page objects centralize page details for easy updates.
Tests become simpler, cleaner, and more reliable.
Practice
Solution
Step 1: Understand page object role
Page objects hold locators and page actions, separating them from test logic.Step 2: Recognize test class role
Test classes use page objects to perform actions and assertions, improving readability and maintenance.Final Answer:
To separate page details from test logic for easier maintenance -> Option AQuick Check:
Page objects separate page details = C [OK]
- Thinking tests should contain locators directly
- Believing page objects speed up test execution
- Confusing page objects with test runners
Solution
Step 1: Check class and constructor syntax
class LoginPage: def __init__(self, driver): self.driver = driver self.username_input = driver.find_element(By.ID, 'username') correctly defines a class with __init__ accepting driver and assigns driver to self.driver.Step 2: Verify locator usage
class LoginPage: def __init__(self, driver): self.driver = driver self.username_input = driver.find_element(By.ID, 'username') uses driver.find_element with By.ID and locator string, which is correct Selenium syntax.Final Answer:
Correct page object class with driver parameter and locator using By.ID -> Option CQuick Check:
Class with __init__(driver) and By.ID locator = A [OK]
- Missing driver parameter in __init__
- Using driver.find_element without By selector
- Defining page object as a function instead of class
class LoginPage:
def __init__(self, driver):
self.driver = driver
def get_title(self):
return self.driver.title
class TestLogin:
def test_title(self, driver):
page = LoginPage(driver)
print(page.get_title())
# Assume driver.title = 'Login Page'Solution
Step 1: Understand get_title method
get_title returns driver.title, which is 'Login Page' as given.Step 2: Check test_title method output
test_title creates LoginPage instance and prints get_title result, so it prints 'Login Page'.Final Answer:
Login Page -> Option BQuick Check:
page.get_title() returns 'Login Page' [OK]
- Confusing method return with attribute name
- Assuming print outputs None
- Thinking driver is undefined inside test method
class LoginPage:
def __init__(self, driver):
self.driver = driver
def enter_username(self, username):
self.driver.find_element(By.ID, 'username').send_keys(username)
class TestLogin:
def test_enter_username(self, driver):
page = LoginPage()
page.enter_username('user1')Solution
Step 1: Check LoginPage instantiation
LoginPage requires driver argument in __init__, but called without arguments in test_enter_username.Step 2: Verify other code parts
send_keys usage and By.ID locator are correct; test_enter_username has self parameter as method of class.Final Answer:
LoginPage is instantiated without driver argument -> Option AQuick Check:
Missing driver argument in LoginPage() call = B [OK]
- Forgetting to pass driver to page object constructor
- Assuming send_keys needs different syntax
- Confusing locator syntax errors
Solution
Step 1: Understand page object responsibility
Page objects should provide methods that check element states like visibility and clickability.Step 2: Understand test class responsibility
Test class uses page object methods to assert expected conditions, keeping test logic separate from page details.Final Answer:
Page object has method is_login_button_clickable() returning True/False; test class asserts this method's result -> Option DQuick Check:
Page object checks state, test asserts result = D [OK]
- Mixing locator logic inside test class
- Not returning boolean from page object methods
- Ignoring visibility checks in tests
Ready to prove it?
Pass the test and this pattern moves unknown
