0
0
Selenium Pythontesting~15 mins

Test class using page objects in Selenium Python - Build an Automation Script

Choose your learning style9 modes available
Login functionality test using page objects
Preconditions (2)
Step 1: Open the browser and navigate to 'https://example.com/login'
Step 2: Enter 'testuser' in the username input field
Step 3: Enter 'Test@1234' in the password input field
Step 4: Click the login button
Step 5: Wait for the dashboard page to load
✅ Expected Result: User is successfully logged in and the dashboard page URL contains '/dashboard'
Automation Requirements - Selenium with Python unittest
Assertions Needed:
Verify the current URL contains '/dashboard' after login
Verify the login button is clickable before clicking
Verify username and password fields are present before input
Best Practices:
Use Page Object Model to separate page elements and actions
Use explicit waits to wait for elements to be interactable
Use unittest framework for test structure and assertions
Keep locators in the page object class using By selectors
Automated Solution
Selenium Python
import unittest
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

class LoginPage:
    URL = "https://example.com/login"
    USERNAME_INPUT = (By.ID, "username")
    PASSWORD_INPUT = (By.ID, "password")
    LOGIN_BUTTON = (By.ID, "loginBtn")

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

    def load(self):
        self.driver.get(self.URL)

    def enter_username(self, username):
        WebDriverWait(self.driver, 10).until(
            EC.visibility_of_element_located(self.USERNAME_INPUT)
        ).send_keys(username)

    def enter_password(self, password):
        WebDriverWait(self.driver, 10).until(
            EC.visibility_of_element_located(self.PASSWORD_INPUT)
        ).send_keys(password)

    def click_login(self):
        login_button = WebDriverWait(self.driver, 10).until(
            EC.element_to_be_clickable(self.LOGIN_BUTTON)
        )
        login_button.click()

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

    def test_valid_login(self):
        self.login_page.load()
        self.login_page.enter_username("testuser")
        self.login_page.enter_password("Test@1234")
        self.login_page.click_login()

        WebDriverWait(self.driver, 10).until(
            EC.url_contains("/dashboard")
        )
        current_url = self.driver.current_url
        self.assertIn("/dashboard", current_url, "Dashboard URL not found after login")

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

if __name__ == "__main__":
    unittest.main()

This test uses the Page Object Model to keep the login page elements and actions in the LoginPage class. This makes the test code cleaner and easier to maintain.

The LoginPage class defines locators as tuples using By.ID for better readability and maintainability.

Explicit waits (WebDriverWait) ensure the test waits for elements to be visible or clickable before interacting, avoiding flaky tests.

The test class TestLogin uses Python's unittest framework for structure and assertions.

Setup and teardown methods open and close the browser for each test, ensuring a clean state.

The test verifies the URL contains '/dashboard' after login to confirm success.

Common Mistakes - 3 Pitfalls
Using hardcoded sleep instead of explicit waits
Locating elements directly in the test instead of using page objects
Not verifying elements are present before interacting
Bonus Challenge

Now add data-driven testing with 3 different sets of username and password inputs to test multiple login scenarios.

Show Hint