Introduction
As Selenium automation projects grow, many test scripts begin to perform the same actions repeatedly, such as entering text, clicking buttons, waiting for elements, or retrieving text. Writing the same code in multiple test cases leads to duplication, making the framework difficult to maintain.
Reusable Methods solve this problem by moving commonly used Selenium operations into helper functions that can be called from any test script. Instead of rewriting the same logic, automation engineers simply call reusable methods whenever needed.
Using reusable methods makes automation scripts shorter, cleaner, easier to understand, and much easier to maintain.
In this tutorial, you’ll learn how to create reusable helper methods in Selenium with Python and understand how they improve automation framework design.
What are Reusable Methods?
Reusable Methods are functions that perform common automation tasks and can be called from multiple test cases.
Instead of writing the same Selenium code repeatedly, the logic is written once and reused whenever required.
Examples include methods for:
Entering text
Clicking buttons
Waiting for elements
Reading text
Selecting dropdown values
Taking screenshots
Switching windows
Why Use Reusable Methods?
Reusable methods provide several benefits:
Reduce duplicate code.
Improve code readability.
Simplify framework maintenance.
Promote code reusability.
Make test scripts shorter and cleaner.
Ensure consistent Selenium operations across the framework.
Example
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
def type_text(driver, locator, text, timeout=10):
element = WebDriverWait(driver, timeout).until(
EC.visibility_of_element_located(locator)
)
element.clear()
element.send_keys(text)
def click(driver, locator, timeout=10):
WebDriverWait(driver, timeout).until(
EC.element_to_be_clickable(locator)
).click()
def get_text(driver, locator, timeout=10):
return WebDriverWait(driver, timeout).until(
EC.visibility_of_element_located(locator)
).text
def test_reusable_methods():
driver = webdriver.Chrome()
try:
driver.get("https://www.testmuai.com/selenium-playground/simple-form-demo")
type_text(driver, (By.ID, "user-message"), "Reusable Methods")
click(driver, (By.ID, "showInput"))
assert get_text(driver, (By.ID, "message")) == "Reusable Methods"
finally:
driver.quit()
Understanding the Code
Import Required Modules
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
The required Selenium modules are imported.
webdriverlaunches and controls the browser.Byis used to locate web elements.WebDriverWaitperforms explicit waits.expected_conditionsprovides predefined waiting conditions.
Create the type_text() Method
def type_text(driver, locator, text, timeout=10):
element = WebDriverWait(driver, timeout).until(
EC.visibility_of_element_located(locator)
)
element.clear()
element.send_keys(text)
This reusable method:
Waits until the element becomes visible.
Clears any existing text.
Enters the new text.
Instead of repeating these three steps in every test, they are written once and reused.
Create the click() Method
def click(driver, locator, timeout=10):
WebDriverWait(driver, timeout).until(
EC.element_to_be_clickable(locator)
).click()
This helper method waits until an element becomes clickable before clicking it.
Using this method prevents failures caused by elements not being ready for interaction.
Create the get_text() Method
def get_text(driver, locator, timeout=10):
return WebDriverWait(driver, timeout).until(
EC.visibility_of_element_located(locator)
).text
This method waits for the element to become visible and then returns its text.
It can be reused whenever text needs to be validated.
Create the Test Function
def test_reusable_methods():
The test function demonstrates how reusable methods simplify Selenium automation.
Launch the Browser
driver = webdriver.Chrome()
A new Chrome browser session is created.
Open the Webpage
driver.get("https://www.testmuai.com/selenium-playground/simple-form-demo")
The browser navigates to the Selenium Playground Simple Form Demo page.
Enter Text Using the Reusable Method
type_text(driver, (By.ID, "user-message"), "Reusable Methods")
Instead of writing Selenium code to locate the element, wait for it, clear it, and enter text, the test simply calls the reusable type_text() method.
This makes the test much cleaner.
Click the Button Using the Reusable Method
click(driver, (By.ID, "showInput"))
The reusable click() method waits until the button becomes clickable and then performs the click operation.
Verify the Result Using the Reusable Method
assert get_text(driver, (By.ID, "message")) == "Reusable Methods"
The reusable get_text() method retrieves the displayed message, and the assertion verifies that it matches the expected value.
Close the Browser
finally:
driver.quit()
The browser is closed after test execution.
Using driver.quit() inside the finally block ensures that browser resources are released properly.
Common Reusable Methods in Selenium Frameworks
Most automation frameworks contain reusable methods such as:
click()type_text()get_text()wait_for_element()select_dropdown()take_screenshot()scroll_to_element()switch_to_window()accept_alert()hover_over_element()
These helper methods reduce duplicate Selenium code throughout the project.
Practical Example
Suppose an application has 300 test cases that require entering text into input fields.
Instead of writing find_element(), clear(), and send_keys() in every test, a single type_text() method is created and reused. If the text entry process ever changes, only one method needs to be updated instead of hundreds of test cases.
Automation Testing Example
Consider an online shopping application.
Many test cases require clicking the Add to Cart button. Rather than writing the click logic repeatedly, the framework uses a reusable click() method. This ensures consistent behavior and reduces maintenance effort.
Real-World Example
Reusable methods are commonly used in:
Banking applications
Healthcare systems
E-commerce platforms
CRM applications
ERP systems
Government portals
SaaS products
Enterprise Selenium automation frameworks
Advantages of Reusable Methods
Reduce duplicate code.
Improve code readability.
Simplify framework maintenance.
Promote code reuse.
Improve consistency across test scripts.
Make test cases shorter and easier to understand.
Increase framework scalability.
Common Mistakes Beginners Make
Writing Duplicate Selenium Code
Avoid copying the same Selenium operations into multiple test cases.
Move common logic into reusable helper methods.
Creating Large Utility Methods
Keep each reusable method focused on one specific task.
For example:
click()type_text()get_text()
instead of creating one method that performs many unrelated actions.
Not Using Explicit Waits
Reusable methods should include appropriate waits where necessary to improve test stability.
Hardcoding Values Inside Utility Methods
Design reusable methods to accept parameters such as the locator, text, and timeout so they can be used with different elements.
Ignoring Method Names
Use descriptive method names that clearly indicate their purpose.
Best Practices
Create reusable methods for frequently performed Selenium actions.
Keep each method focused on a single responsibility.
Use descriptive method names such as
click(),type_text(), andget_text().Include explicit waits inside reusable methods whenever appropriate.
Pass values such as locators and text as parameters instead of hardcoding them.
Store reusable methods inside utility classes or helper modules.
Reuse the same helper methods across all test cases to keep the framework consistent.
Conclusion
Reusable Methods are an essential part of every professional Selenium automation framework. They eliminate duplicate code, improve readability, simplify maintenance, and make test scripts easier to understand. By moving common Selenium operations into reusable helper methods, automation engineers can build scalable, maintainable, and professional-quality automation frameworks.
Frequently Asked Questions (FAQs)
What are Reusable Methods?
Reusable Methods are helper functions that perform common Selenium operations and can be called from multiple test cases.
Why should reusable methods be used?
They reduce duplicate code, improve readability, simplify maintenance, and promote code reuse.
Where should reusable methods be stored?
Reusable methods are typically stored inside utility classes or helper modules so they can be accessed throughout the framework.
Why do reusable methods often include explicit waits?
Including explicit waits makes Selenium interactions more reliable by ensuring that elements are ready before performing actions.
Which Selenium operations are commonly converted into reusable methods?
Operations such as clicking elements, entering text, retrieving text, taking screenshots, selecting dropdown values, waiting for elements, and handling alerts are commonly implemented as reusable methods.
Key Takeaways
Reusable Methodseliminate duplicate Selenium code.They improve readability and simplify framework maintenance.
Helper methods such as
click(),type_text(), andget_text()make test scripts shorter and cleaner.Passing parameters makes reusable methods flexible and reusable across different test cases.
Combining reusable methods with explicit waits creates more reliable and maintainable Selenium automation frameworks.
