0
0
Selenium Pythontesting~15 mins

Base page class in Selenium Python - Build an Automation Script

Choose your learning style9 modes available
Create and use a Base Page class for Selenium tests
Preconditions (3)
Step 1: Create a BasePage class that accepts a WebDriver instance in its constructor
Step 2: Add a method 'open_url' that navigates the browser to a given URL
Step 3: Add a method 'find_element' that takes a locator tuple and returns the web element
Step 4: Add a method 'click' that clicks on an element found by a locator
Step 5: Add a method 'enter_text' that sends text to an element found by a locator
Step 6: Create a test script that uses the BasePage class to open the sample page
Step 7: Use the BasePage methods to find an input field, enter text, and click a button
Step 8: Verify that the expected page or element is displayed after the actions
✅ Expected Result: The test script runs without errors, opens the page, interacts with elements using BasePage methods, and verifies the expected outcome successfully.
Automation Requirements - selenium with unittest
Assertions Needed:
Verify the page URL after navigation
Verify that an element is present after interaction
Best Practices:
Use explicit waits to wait for elements
Use By locators for element identification
Keep BasePage methods generic and reusable
Use unittest framework for test structure
Automated Solution
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 BasePage:
    def __init__(self, driver):
        self.driver = driver
        self.wait = WebDriverWait(driver, 10)

    def open_url(self, url: str):
        self.driver.get(url)

    def find_element(self, locator: tuple):
        return self.wait.until(EC.presence_of_element_located(locator))

    def click(self, locator: tuple):
        element = self.find_element(locator)
        element.click()

    def enter_text(self, locator: tuple, text: str):
        element = self.find_element(locator)
        element.clear()
        element.send_keys(text)

class TestSamplePage(unittest.TestCase):
    def setUp(self):
        self.driver = webdriver.Chrome()
        self.base_page = BasePage(self.driver)

    def test_open_and_interact(self):
        url = "https://example.com/login"
        self.base_page.open_url(url)
        self.assertEqual(self.driver.current_url, url)

        username_locator = (By.ID, "username")
        password_locator = (By.ID, "password")
        login_button_locator = (By.ID, "loginBtn")

        self.base_page.enter_text(username_locator, "testuser")
        self.base_page.enter_text(password_locator, "password123")
        self.base_page.click(login_button_locator)

        dashboard_locator = (By.ID, "dashboard")
        dashboard_element = self.base_page.find_element(dashboard_locator)
        self.assertTrue(dashboard_element.is_displayed())

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

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

The BasePage class is designed to hold common browser actions. It takes a driver and creates a WebDriverWait for explicit waits.

The open_url method navigates to a URL.

The find_element method waits until the element is present and returns it.

The click and enter_text methods use find_element to interact with elements safely.

The TestSamplePage class uses Python's unittest framework. It sets up the Chrome driver and BasePage instance.

The test opens a login page URL, verifies the URL, enters username and password, clicks login, then verifies the dashboard is displayed.

Finally, tearDown quits the browser to clean up.

This structure keeps code clean, reusable, and easy to maintain.

Common Mistakes - 4 Pitfalls
Using time.sleep() instead of explicit waits
Hardcoding locators inside test methods instead of BasePage
Not clearing input fields before sending keys
Not quitting the driver after tests
Bonus Challenge

Now add data-driven testing with 3 different username and password combinations.

Show Hint