Bird
Raised Fist0
Selenium Pythontesting~20 mins

Test class using page objects in Selenium Python - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Page Object Pro
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate
2:00remaining
Output of a Selenium test using Page Object
What will be the output of this test run if the login page loads correctly and the username field is present?
Selenium Python
from selenium.webdriver.common.by import By
from selenium.webdriver.remote.webdriver import WebDriver

class LoginPage:
    def __init__(self, driver: WebDriver):
        self.driver = driver
        self.username_input = (By.ID, 'username')

    def is_username_displayed(self):
        return self.driver.find_element(*self.username_input).is_displayed()


class TestLogin:
    def test_username_field(self, driver):
        page = LoginPage(driver)
        assert page.is_username_displayed() is True
        print('Test Passed')
ATest Passed
BAssertionError
CSyntaxError
DNoSuchElementException
Attempts:
2 left
💡 Hint
Think about what happens if the element is found and is visible.
❓ locator
intermediate
1:30remaining
Best locator strategy in Page Object
Which locator is the best practice to use in a Page Object for a login button with id 'loginBtn'?
ABy.ID, 'loginBtn'
BBy.XPATH, "//button[text()='Login']"
CBy.CSS_SELECTOR, 'button.login-button'
DBy.CLASS_NAME, 'btn'
Attempts:
2 left
💡 Hint
IDs are unique and fast to locate.
❓ assertion
advanced
1:30remaining
Correct assertion for page title in test class
Which assertion correctly verifies the page title is exactly 'Dashboard' in a Selenium test class?
Selenium Python
def test_page_title(self, driver):
    driver.get('https://example.com/dashboard')
Aassert driver.title != 'Dashboard'
Bassert 'Dashboard' in driver.title
Cassert driver.title == 'Dashboard'
Dassert driver.title is 'Dashboard'
Attempts:
2 left
💡 Hint
Use equality operator for exact match.
🔧 Debug
advanced
2:00remaining
Identify error in test class using Page Object
What error will this test raise when run?
Selenium Python
from selenium.webdriver.common.by import By

class LoginPage:
    def __init__(self, driver):
        self.driver = driver
        self.password_input = (By.ID, 'password')

    def enter_password(self, pwd):
        self.driver.find_element(self.password_input).send_keys(pwd)

class TestLogin:
    def test_password_entry(self, driver):
        page = LoginPage(driver)
        page.enter_password('secret')
ANoSuchElementException
BTypeError: find_element() takes 2 positional arguments but 1 was given
CAttributeError: 'tuple' object has no attribute 'send_keys'
DNo error, test passes
Attempts:
2 left
💡 Hint
Check how find_element is called with tuple argument.
❓ framework
expert
3:00remaining
Test class setup and teardown with Page Object
Which test class structure correctly uses setup and teardown methods with a Page Object in pytest style?
A
import unittest
from selenium import webdriver

class TestLogin(unittest.TestCase):
    def setup(self):
        self.driver = webdriver.Chrome()
        self.page = LoginPage(self.driver)

    def teardown(self):
        self.driver.quit()

    def test_login(self):
        self.page.open()
        self.assertTrue(self.page.is_loaded())
B
import pytest
from selenium import webdriver

class TestLogin:
    def setup(self):
        self.driver = webdriver.Chrome()
        self.page = LoginPage(self.driver)

    def teardown(self):
        self.driver.quit()

    def test_login(self):
        self.page.open()
        assert self.page.is_loaded()
C
import unittest
from selenium import webdriver

class TestLogin(unittest.TestCase):
    def setUp(self):
        self.driver = webdriver.Chrome()
        self.page = LoginPage(self.driver)

    def tearDown(self):
        self.driver.quit()

    def test_login(self):
        self.page.open()
        self.assertTrue(self.page.is_loaded())
D
import pytest
from selenium import webdriver

class TestLogin:
    def setup_method(self):
        self.driver = webdriver.Chrome()
        self.page = LoginPage(self.driver)

    def teardown_method(self):
        self.driver.quit()

    def test_login(self):
        self.page.open()
        assert self.page.is_loaded()
Attempts:
2 left
💡 Hint
pytest uses setup_method and teardown_method for per-test setup/teardown.

Practice

(1/5)
1. What is the main purpose of using a page object in Selenium test classes?
easy
A. To separate page details from test logic for easier maintenance
B. To write all test steps inside the test class directly
C. To avoid using locators in tests
D. To run tests faster by skipping page loading

Solution

  1. Step 1: Understand page object role

    Page objects hold locators and page actions, separating them from test logic.
  2. Step 2: Recognize test class role

    Test classes use page objects to perform actions and assertions, improving readability and maintenance.
  3. Final Answer:

    To separate page details from test logic for easier maintenance -> Option A
  4. Quick Check:

    Page objects separate page details = C [OK]
Hint: Page objects keep page info separate from tests [OK]
Common Mistakes:
  • Thinking tests should contain locators directly
  • Believing page objects speed up test execution
  • Confusing page objects with test runners
2. Which of the following is the correct way to define a page object class in Python using Selenium?
easy
A. class LoginPage: def __init__(self): self.username_input = driver.find_element(By.ID, 'username')
B. def LoginPage(driver): username_input = driver.find_element(By.ID, 'username')
C. class LoginPage: def __init__(self, driver): self.driver = driver self.username_input = driver.find_element(By.ID, 'username')
D. class LoginPage: def __init__(self, driver): self.username_input = driver.find_element('username')

Solution

  1. 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.
  2. 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.
  3. Final Answer:

    Correct page object class with driver parameter and locator using By.ID -> Option C
  4. Quick Check:

    Class with __init__(driver) and By.ID locator = A [OK]
Hint: Page object classes need __init__(self, driver) and proper locators [OK]
Common Mistakes:
  • Missing driver parameter in __init__
  • Using driver.find_element without By selector
  • Defining page object as a function instead of class
3. Given the following test class using a page object, what will be printed if the username input is found and the page title is 'Login Page'?
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'
medium
A. None
B. Login Page
C. driver.title
D. Error: driver not defined

Solution

  1. Step 1: Understand get_title method

    get_title returns driver.title, which is 'Login Page' as given.
  2. Step 2: Check test_title method output

    test_title creates LoginPage instance and prints get_title result, so it prints 'Login Page'.
  3. Final Answer:

    Login Page -> Option B
  4. Quick Check:

    page.get_title() returns 'Login Page' [OK]
Hint: get_title returns driver.title, prints page title string [OK]
Common Mistakes:
  • Confusing method return with attribute name
  • Assuming print outputs None
  • Thinking driver is undefined inside test method
4. Identify the error in this test class using a page object:
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')
medium
A. LoginPage is instantiated without driver argument
B. send_keys method is called incorrectly
C. By.ID locator is invalid
D. test_enter_username method missing self parameter

Solution

  1. Step 1: Check LoginPage instantiation

    LoginPage requires driver argument in __init__, but called without arguments in test_enter_username.
  2. 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.
  3. Final Answer:

    LoginPage is instantiated without driver argument -> Option A
  4. Quick Check:

    Missing driver argument in LoginPage() call = B [OK]
Hint: Check constructor arguments when creating page object instances [OK]
Common Mistakes:
  • Forgetting to pass driver to page object constructor
  • Assuming send_keys needs different syntax
  • Confusing locator syntax errors
5. You want to write a test class that uses a page object to check if a login button is visible and clickable. Which approach correctly combines page object and test class methods?
hard
A. Test class calls page object method that returns the button WebElement but does not check visibility
B. Test class directly uses driver.find_element to check button visibility, ignoring page object
C. Page object only stores locators; test class performs all actions and assertions
D. Page object has method is_login_button_clickable() returning True/False; test class asserts this method's result

Solution

  1. Step 1: Understand page object responsibility

    Page objects should provide methods that check element states like visibility and clickability.
  2. Step 2: Understand test class responsibility

    Test class uses page object methods to assert expected conditions, keeping test logic separate from page details.
  3. Final Answer:

    Page object has method is_login_button_clickable() returning True/False; test class asserts this method's result -> Option D
  4. Quick Check:

    Page object checks state, test asserts result = D [OK]
Hint: Page object checks element state; test asserts result [OK]
Common Mistakes:
  • Mixing locator logic inside test class
  • Not returning boolean from page object methods
  • Ignoring visibility checks in tests
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard