What is the main purpose of a Page class in Selenium testing?
A Page class represents a web page in the application. It contains locators and methods to interact with elements on that page, helping to organize test code and improve maintainability.
Click to reveal answer
beginner
In a Page class, where should you store the locators for web elements?
Locators should be stored as class variables or constants at the top of the Page class. This keeps them organized and easy to update if the web page changes.
Click to reveal answer
intermediate
Why should interaction methods in a Page class avoid assertions?
Interaction methods should only perform actions like clicking or typing. Assertions belong in test scripts to keep Page classes reusable and focused on page behavior.
Click to reveal answer
beginner
How does using a Page class improve test code?
It makes tests easier to read and maintain by separating page details from test logic. Changes in the UI require updates only in the Page class, not in every test.
Click to reveal answer
beginner
What is a typical structure of a Page class in Selenium Python?
A typical Page class has: 1) locators as variables, 2) an __init__ method to receive the WebDriver, 3) methods to interact with page elements (click, enter text, get text).
Click to reveal answer
What should a Page class constructor (__init__) usually accept?
ATest report object
BTest data values
CAssertion methods
DA WebDriver instance
✗ Incorrect
The Page class constructor typically accepts a WebDriver instance to interact with the browser.
Where should you put the code to click a button in the Page class?
AInside a method dedicated to clicking that button
BDirectly in the test script
CIn the constructor
DIn the assertion method
✗ Incorrect
Click actions belong in dedicated methods inside the Page class to keep tests clean and reusable.
Why is it better to keep locators as class variables in a Page class?
ASo they can be reused and easily updated
BTo make the class longer
CTo avoid using WebDriver
DTo run tests faster
✗ Incorrect
Storing locators as class variables helps reuse and simplifies maintenance when page elements change.
Which of these is NOT a responsibility of a Page class?
AInteracting with page elements
BLocating web elements
CPerforming assertions on test results
DProviding methods for page actions
✗ Incorrect
Assertions should be done in test scripts, not in Page classes.
What is the main benefit of using the Page Object Model?
ARemoves the need for locators
BImproves test code readability and maintenance
CMakes tests run faster
DAutomatically generates test reports
✗ Incorrect
The Page Object Model organizes code to make tests easier to read and maintain.
Explain the structure and key components of a Page class in Selenium Python.
Think about how you organize code to represent a web page.
You got /4 concepts.
Describe why using a Page class helps improve test automation projects.
Consider how changes in the web page affect your tests.
You got /4 concepts.
Practice
(1/5)
1. What is the main purpose of a page class in Selenium Python testing?
easy
A. To write assertions for test results
B. To store test data for multiple test cases
C. To execute the browser driver directly
D. To group web elements and actions for a single web page
Solution
Step 1: Understand the role of page classes
Page classes organize locators and methods related to one web page, making tests cleaner.
Step 2: Identify the correct purpose
Grouping elements and actions for a page matches the page class concept.
Final Answer:
To group web elements and actions for a single web page -> Option D
Quick Check:
Page class = group elements and actions [OK]
Hint: Page classes group elements and actions per page [OK]
Common Mistakes:
Confusing page class with test data storage
Thinking page class runs the browser driver
Mixing page class with assertion logic
2. Which of the following is the correct way to define a locator inside a Selenium page class in Python?
easy
A. submit_button = (By.ID, 'submit')
B. submit_button = 'By.ID, submit'
C. submit_button = By.ID('submit')
D. submit_button = By('ID', 'submit')
Solution
Step 1: Recall locator tuple syntax
Locators use tuples like (By.ID, 'value') to specify how to find elements.
Step 2: Check each option
submit_button = (By.ID, 'submit') correctly uses a tuple with By.ID and string 'submit'. Others have syntax errors or wrong formats.
Final Answer:
submit_button = (By.ID, 'submit') -> Option A
Quick Check:
Locator = (By.METHOD, 'value') tuple [OK]
Hint: Use tuple (By.METHOD, 'value') for locators [OK]
Common Mistakes:
Using string instead of tuple for locator
Calling By.ID as a function
Incorrect tuple structure
3. Given the following page class snippet, what will page.get_title() return if the browser's current page title is "Welcome Page"?
A. Missing return statement in enter_username method
B. send_keys should be called on driver, not element
C. find_element expects two arguments, fix by unpacking locator: *self.username_input
D. Locator should be a string, not a tuple
Solution
Step 1: Check find_element usage
find_element expects two arguments: method and locator string, not a tuple as one argument.
Step 2: Fix by unpacking tuple
Use *self.username_input to unpack tuple into two arguments.
Final Answer:
find_element expects two arguments, fix by unpacking locator: *self.username_input -> Option C
Quick Check:
Use * to unpack locator tuple in find_element [OK]
Hint: Unpack locator tuple with * in find_element [OK]
Common Mistakes:
Passing locator tuple as single argument
Calling send_keys on driver instead of element
Thinking return is required for action methods
5. You want to create a page class for a login page with username and password fields and a login button. Which design best follows the page class structure and good practices?
class LoginPage:
def __init__(self, driver):
self.driver = driver
self.username = (By.ID, 'user')
self.password = (By.ID, 'pass')
self.login_btn = (By.ID, 'login')
def enter_username(self, name):
self.driver.find_element(*self.username).send_keys(name)
def enter_password(self, pwd):
self.driver.find_element(*self.password).send_keys(pwd)
def click_login(self):
self.driver.find_element(*self.login_btn).click() stores locators as tuples in __init__, which is best practice for reuse and clarity.
Step 2: Check method structure
class LoginPage:
def __init__(self, driver):
self.driver = driver
self.username = (By.ID, 'user')
self.password = (By.ID, 'pass')
self.login_btn = (By.ID, 'login')
def enter_username(self, name):
self.driver.find_element(*self.username).send_keys(name)
def enter_password(self, pwd):
self.driver.find_element(*self.password).send_keys(pwd)
def click_login(self):
self.driver.find_element(*self.login_btn).click() has separate methods for each action, improving readability and maintainability.
Step 3: Compare other options
class LoginPage:
def __init__(self, driver):
self.driver = driver
def login(self, user, pwd):
self.driver.find_element(By.ID, 'user').send_keys(user)
self.driver.find_element(By.ID, 'pass').send_keys(pwd)
self.driver.find_element(By.ID, 'login').click() repeats locators in methods, class LoginPage:
def __init__(self, driver):
self.driver = driver
self.username = 'user'
self.password = 'pass'
self.login_btn = 'login'
def login(self, user, pwd):
self.driver.find_element(By.ID, self.username).send_keys(user)
self.driver.find_element(By.ID, self.password).send_keys(pwd)
self.driver.find_element(By.ID, self.login_btn).click() stores locators as strings only, class LoginPage:
def __init__(self, driver):
self.driver = driver
def enter_username(self, name):
self.driver.find_element(By.ID, 'user').send_keys(name)
def enter_password(self, pwd):
self.driver.find_element(By.ID, 'pass').send_keys(pwd)
def click_login(self):
self.driver.find_element(By.ID, 'login').click() does not store locators as variables.
Final Answer:
Option A follows page class structure and good practices best -> Option A
Quick Check:
Store locators once, use methods for actions [OK]
Hint: Store locators as tuples, use methods for actions [OK]
Common Mistakes:
Repeating locators inside methods
Storing locators as plain strings
Not separating actions into methods
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.