Base page class in Selenium Python - Build an Automation Script
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.
Now add data-driven testing with 3 different username and password combinations.
Practice
BasePage class in Selenium Python testing?Solution
Step 1: Understand the role of BasePage
The BasePage class is designed to hold common actions and locators shared by multiple page classes.Step 2: Compare with other options
Storing test data, browser settings, or generating reports are not responsibilities of BasePage.Final Answer:
To group common web page actions and locators for reuse -> Option AQuick Check:
BasePage groups common actions [OK]
- Confusing BasePage with test data storage
- Thinking BasePage manages browser setup
- Assuming BasePage creates reports
Solution
Step 1: Recall locator tuple syntax
Locator tuples use the format (By.<METHOD>, 'value'), e.g., (By.ID, 'submit').Step 2: Check each option
submit_button = (By.ID, 'submit') matches the tuple format. The other options use wrong data structures or invalid syntax.Final Answer:
submit_button = (By.ID, 'submit') -> Option AQuick Check:
Locator tuple = (By.METHOD, 'value') [OK]
- Using lists or dicts instead of tuples for locators
- Calling By.ID as a function
- Missing parentheses around locator tuple
page.get_title() return if the browser's current page title is "Welcome Page"?
class BasePage:
def __init__(self, driver):
self.driver = driver
def get_title(self):
return self.driver.title
page = BasePage(driver)Solution
Step 1: Understand get_title method
The method returnsself.driver.title, which is the current page title string.Step 2: Check given title
The browser's current page title is "Welcome Page", so the method returns that string.Final Answer:
"Welcome Page" -> Option DQuick Check:
get_title returns driver.title = "Welcome Page" [OK]
- Thinking it returns the driver object
- Assuming no return means None
- Confusing method with attribute
class BasePage:
def __init__(self, driver):
self.driver = driver
def click_element(locator):
element = self.driver.find_element(*locator)
element.click()Solution
Step 1: Check method parameters
Instance methods must have 'self' as the first parameter. Here, click_element lacks 'self'.Step 2: Understand impact
Without 'self', calling click_element will cause a TypeError because 'self.driver' is undefined.Final Answer:
Missing 'self' parameter in click_element method -> Option BQuick Check:
Instance methods need 'self' first [OK]
- Forgetting 'self' in instance methods
- Confusing find_element with find_elements
- Misunderstanding locator types
- Using method call without parentheses
is_element_visible that returns True if an element is visible and False if not, without raising exceptions. Which is the best way to implement it?Solution
Step 1: Understand element visibility check
Usingfind_elementraises NoSuchElementException if element not found, so we must handle it.Step 2: Use try-except for safe check
Try to find element and checkis_displayed(). If exception occurs, return False safely.Final Answer:
Use try-except to catch NoSuchElementException and return False if caught -> Option CQuick Check:
Try-except handles missing element safely [OK]
- Not handling exceptions causing test failure
- Assuming find_elements always returns visible elements
- Searching page_source is unreliable and slow
Ready to prove it?
Pass the test and this pattern moves unknown
