0
0
Selenium Pythontesting~10 mins

Why POM organizes test code in Selenium Python - Test Execution Impact

Choose your learning style9 modes available
Test Overview

This test opens a login page, enters username and password using Page Object Model (POM), clicks login, and verifies successful login message. It shows how POM organizes test code by separating page actions from test logic.

Test Code - Selenium
Selenium Python
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
import unittest

class LoginPage:
    def __init__(self, driver):
        self.driver = driver
        self.username_input = (By.ID, "username")
        self.password_input = (By.ID, "password")
        self.login_button = (By.ID, "loginBtn")

    def enter_username(self, username):
        self.driver.find_element(*self.username_input).send_keys(username)

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

    def click_login(self):
        self.driver.find_element(*self.login_button).click()

class TestLogin(unittest.TestCase):
    def setUp(self):
        self.driver = webdriver.Chrome()
        self.driver.get("https://example.com/login")
        self.login_page = LoginPage(self.driver)

    def test_valid_login(self):
        self.login_page.enter_username("user1")
        self.login_page.enter_password("pass123")
        self.login_page.click_login()
        WebDriverWait(self.driver, 10).until(
            EC.presence_of_element_located((By.ID, "welcomeMsg"))
        )
        welcome_text = self.driver.find_element(By.ID, "welcomeMsg").text
        self.assertEqual(welcome_text, "Welcome user1!")

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

if __name__ == "__main__":
    unittest.main()
Execution Trace - 7 Steps
StepActionSystem StateAssertionResult
1Test starts and Chrome browser opensBrowser window is open at https://example.com/login page-PASS
2Test calls enter_username method to find username input and types 'user1'Username input field contains text 'user1'-PASS
3Test calls enter_password method to find password input and types 'pass123'Password input field contains text 'pass123'-PASS
4Test calls click_login method to find and click login buttonLogin button clicked, page starts loading next screen-PASS
5Test waits up to 10 seconds for element with ID 'welcomeMsg' to appearWelcome message element is present on pageVerify welcome message text equals 'Welcome user1!'PASS
6Test asserts that welcome message text is exactly 'Welcome user1!'Welcome message text is 'Welcome user1!'assertEqual passesPASS
7Test ends and browser closesBrowser window closed-PASS
Failure Scenario
Failing Condition: If the login button ID changes or welcome message does not appear
Execution Trace Quiz - 3 Questions
Test your understanding
What does the Page Object Model (POM) help organize in this test?
AAutomatically generates test data
BRuns tests faster by skipping waits
CSeparates page element locators and actions from test logic
DReplaces the need for assertions
Key Result
Using POM keeps test code clean and easy to maintain by putting page details in one place, so tests focus on verifying behavior.