0
0
Selenium Pythontesting~10 mins

Test class using page objects in Selenium Python - Test Execution Trace

Choose your learning style9 modes available
Test Overview

This test opens a login page, enters username and password using page objects, clicks login, and verifies successful login by checking the welcome message.

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).clear()
        self.driver.find_element(*self.username_input).send_keys(username)

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

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

class HomePage:
    def __init__(self, driver):
        self.driver = driver
        self.welcome_message = (By.ID, "welcomeMsg")

    def get_welcome_text(self):
        return self.driver.find_element(*self.welcome_message).text

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

    def test_valid_login(self):
        self.login_page.enter_username("testuser")
        self.login_page.enter_password("securepass")
        self.login_page.click_login()
        WebDriverWait(self.driver, 10).until(
            EC.presence_of_element_located(self.home_page.welcome_message)
        )
        welcome_text = self.home_page.get_welcome_text()
        self.assertEqual(welcome_text, "Welcome, testuser!")

    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 showing login form with username, password fields and login button-PASS
2Find username input field and enter 'testuser'Username field contains 'testuser'-PASS
3Find password input field and enter 'securepass'Password field contains 'securepass'-PASS
4Find and click the login buttonLogin button clicked, page starts loading home page-PASS
5Wait up to 10 seconds for welcome message element to appearWelcome message element is present on the pageCheck presence of element located by ID 'welcomeMsg'PASS
6Get text from welcome message elementWelcome message text is 'Welcome, testuser!'Assert that welcome message text equals 'Welcome, testuser!'PASS
7Close browser and end testBrowser closed-PASS
Failure Scenario
Failing Condition: Welcome message element does not appear within 10 seconds after login
Execution Trace Quiz - 3 Questions
Test your understanding
What does the test verify after clicking the login button?
AThat the username field is cleared
BThat the login button is disabled
CThat the welcome message with text 'Welcome, testuser!' appears
DThat the password is visible on screen
Key Result
Using page objects separates page details from test logic, making tests easier to read and maintain.