0
0
Selenium Pythontesting~5 mins

Page factory pattern in Selenium Python - Cheat Sheet & Quick Revision

Choose your learning style9 modes available
Recall & Review
beginner
What is the Page Factory pattern in Selenium?
Page Factory is a design pattern in Selenium that helps initialize web elements easily using annotations, making tests cleaner and easier to maintain.
Click to reveal answer
beginner
How do you declare a web element using Page Factory in Python?
You define locators as class attributes using tuples with By, and use the @property decorator to lazily locate and return elements when first accessed.
Click to reveal answer
intermediate
Why is Page Factory pattern preferred over traditional element locating?
Because it reduces code duplication, improves readability, and initializes elements only when they are used, which can improve test performance.
Click to reveal answer
intermediate
What is the role of lazy initialization in Page Factory?
It initializes web elements lazily when accessed so they can be used directly without calling find_element repeatedly each time.
Click to reveal answer
beginner
Give a simple example of a Page Factory class in Selenium Python.
from selenium.webdriver.common.by import By

class LoginPage:
    username_loc = (By.ID, 'username')
    password_loc = (By.ID, 'password')
    login_button_loc = (By.ID, 'login')

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

    @property
    def username(self):
        return self.driver.find_element(*self.username_loc)

    @property
    def password(self):
        return self.driver.find_element(*self.password_loc)

    @property
    def login_button(self):
        return self.driver.find_element(*self.login_button_loc)

    def login(self, user, pwd):
        self.username.send_keys(user)
        self.password.send_keys(pwd)
        self.login_button.click()
Click to reveal answer
What does the Page Factory pattern help with in Selenium?
AWriting SQL queries
BManaging browser cookies
CCreating database connections
DInitializing web elements efficiently
Which is used to lazily initialize elements in Page Factory (Python)?
A@property
Bstart_elements
Cload_elements
Dcreate_elements
In Page Factory, when are web elements initialized?
AOnly when they are used
BAt the start of the test suite
CAfter the test finishes
DBefore the browser opens
Which of these is NOT a benefit of using Page Factory?
AAutomatic element initialization
BFaster test execution by skipping element checks
CImproved code readability
DReduced code duplication
What is the main purpose of locators in Page Factory?
ATo generate reports
BTo store test data
CTo find web elements on the page
DTo manage browser sessions
Explain the Page Factory pattern and how it improves Selenium test code.
Think about how you find elements in Selenium and how Page Factory changes that.
You got /3 concepts.
    Describe how you would implement a login page using the Page Factory pattern in Selenium Python.
    Imagine the steps a user takes to log in and how to automate them.
    You got /3 concepts.