Introduction
As automation projects grow, certain user actions are performed repeatedly across multiple test cases. For example, logging in to an application, searching for a product, submitting a form, or displaying a message may be required in dozens of tests.
Writing the same sequence of Selenium commands repeatedly leads to duplicate code, makes the framework difficult to maintain, and increases the chances of errors.
Reusable Components solve this problem by grouping frequently used actions into reusable classes or methods. Instead of repeating the same Selenium steps in every test case, the test simply calls the reusable component whenever that functionality is needed.
In this tutorial, you’ll learn what Reusable Components are, why they are used in Selenium frameworks, how to create them, and how they help build clean, scalable, and maintainable automation frameworks.
What are Reusable Components?
Reusable Components are classes or methods that encapsulate a sequence of commonly performed actions into a single reusable unit.
Rather than interacting with individual web elements every time, a test calls the reusable component, which performs all the required steps internally.
For example, instead of writing code to:
Locate the message textbox
Enter text
Click the Show Message button
Read the displayed message
every time, all these actions can be combined into one reusable method.
For example:
Test Case
│
▼
Reusable Component
│
┌─────────┼─────────┐
▼ ▼ ▼
Enter Text Click Button Read Result
│
▼
Return Output
This approach keeps test scripts short, readable, and easier to maintain.
Why Use Reusable Components?
Using Reusable Components provides several benefits:
Eliminates duplicate automation code.
Improves code reusability.
Makes test scripts cleaner and easier to read.
Simplifies framework maintenance.
Encourages modular framework design.
Reduces development time.
Makes updates easier when application workflows change.
How to Create Reusable Components
Reusable Components can be created whenever the same sequence of actions is performed repeatedly across different test cases.
1. Identify Repeated Actions
The first step is to identify actions that are frequently repeated.
For example:
Login
Logout
Search Product
Fill Registration Form
Display Message
Upload File
These actions are good candidates for reusable components.
2. Create a Separate Component Class
Create a dedicated class that contains the repeated workflow.
Example:
class LoginComponent:
def login(self, username, password):
...
The component should perform the complete workflow instead of exposing individual Selenium commands.
3. Reuse the Component
Instead of writing Selenium code repeatedly, create an object of the component and call its methods.
Example:
component = LoginComponent(driver)
component.login("admin", "admin123")
This makes the test script much shorter and easier to understand.
Which Components Should Be Reusable?
Some commonly reused components include:
Login Component
Search Component
Navigation Component
Header Component
Footer Component
Menu Component
Message Component
Date Picker Component
File Upload Component
Create reusable components whenever the same workflow appears in multiple test cases.
Example
from selenium import webdriver
from selenium.webdriver.common.by import By
# Topic: 45. Framework Components - Reusable Components
# Practice site: https://www.testmuai.com/selenium-playground/simple-form-demo
# Run: pytest -s 45_examples/test_06_reusable_components.py
#
# Reusable components encapsulate repeating flows like "enter and show message".
class MessageComponent:
def __init__(self, driver):
self.driver = driver
def show(self, text):
self.driver.find_element(By.ID, "user-message").clear()
self.driver.find_element(By.ID, "user-message").send_keys(text)
self.driver.find_element(By.ID, "showInput").click()
return self.driver.find_element(By.ID, "message").text
def test_reusable_components():
driver = webdriver.Chrome()
try:
driver.get("https://www.testmuai.com/selenium-playground/simple-form-demo")
component = MessageComponent(driver)
assert component.show("Reusable") == "Reusable"
finally:
driver.quit()
Understanding the Code
Import Required Libraries
from selenium import webdriver
from selenium.webdriver.common.by import By
The webdriver module is imported to launch the browser, while the By class is used to locate web elements.
Create the Reusable Component
class MessageComponent:
A class named MessageComponent is created.
Instead of placing the repeated Selenium steps directly inside the test case, they are grouped into this reusable component.
Initialize the Driver
def __init__(self, driver):
self.driver = driver
The constructor receives the WebDriver instance and stores it as an instance variable so that all methods in the component can use the same browser session.
Create the show() Method
def show(self, text):
The show() method accepts a message as input.
Rather than requiring the test case to perform multiple Selenium operations, this single method performs the complete workflow.
Clear the Textbox
self.driver.find_element(By.ID, "user-message").clear()
The existing text in the message textbox is removed before entering new data.
This ensures that previously entered text does not affect the current test.
Enter the Message
self.driver.find_element(By.ID, "user-message").send_keys(text)
The text passed to the show() method is entered into the message textbox.
Click the Show Message Button
self.driver.find_element(By.ID, "showInput").click()
The button is clicked to display the entered message.
Return the Displayed Message
return self.driver.find_element(By.ID, "message").text
The displayed message is retrieved from the webpage and returned to the calling test.
Returning the value allows the test case to perform assertions easily.
Create the Browser
driver = webdriver.Chrome()
A new Chrome browser session is launched.
Open the Practice Website
driver.get("https://www.testmuai.com/selenium-playground/simple-form-demo")
The browser navigates to the Selenium Playground Simple Form Demo page.
Create the Component Object
component = MessageComponent(driver)
An object of the reusable component is created.
The test can now use the component’s methods without writing individual Selenium commands.
Execute the Reusable Workflow
component.show("Reusable")
Calling the show() method automatically:
Clears the textbox.
Enters the message.
Clicks the button.
Reads the displayed message.
Returns the displayed text.
The test only calls one method instead of writing several Selenium statements.
Verify the Result
assert component.show("Reusable") == "Reusable"
The returned message is compared with the expected value.
If both values match, the test passes successfully.
Close the Browser
driver.quit()
The browser session is closed after the test execution.
Practical Example
Suppose an e-commerce website requires users to log in before accessing their account.
Instead of writing the complete login steps in every test case, a LoginComponent performs the entire login process.
Whenever authentication is required, the test simply calls the component, reducing duplicate code and improving readability.
Automation Testing Example
Consider an online banking application where multiple test cases require users to transfer funds.
Rather than repeatedly locating textboxes, entering account details, selecting beneficiaries, and clicking the transfer button, a reusable FundTransferComponent encapsulates the complete workflow.
Every test requiring fund transfer simply calls the component, making the automation framework cleaner and easier to maintain.
Real-World Example
Reusable Components are commonly used in automation frameworks developed for:
Banking Applications
E-commerce Websites
Healthcare Systems
CRM Applications
ERP Systems
Insurance Portals
Travel and Booking Applications
Enterprise Automation Frameworks
Professional Selenium frameworks often include reusable components for login, navigation, search, forms, menus, file uploads, and common dialogs.
Advantages of Using Reusable Components
Eliminates duplicate workflow code.
Improves code reusability.
Simplifies framework maintenance.
Keeps test scripts short and readable.
Promotes modular framework design.
Reduces development time.
Improves framework scalability.
Encourages consistent implementation of common workflows.
Common Mistakes Beginners Make
Writing the Same Workflow Repeatedly
Avoid repeating the same Selenium steps in multiple test cases.
Move repeated workflows into reusable components.
Creating Very Large Components
Do not place unrelated actions inside one component.
Each component should focus on a specific feature or workflow.
Mixing Business Logic with Utility Methods
Reusable Components should represent complete user workflows.
Generic helper methods should remain inside Utility Classes.
Not Returning Useful Values
Whenever appropriate, return useful information such as displayed text or status values.
This makes assertions simpler and keeps test cases cleaner.
Best Practices
Create reusable components for frequently repeated workflows.
Keep each component focused on one responsibility.
Give component classes meaningful names.
Return useful results from component methods.
Reuse components across multiple test cases.
Combine reusable components with the Page Object Model (POM).
Keep component methods simple and easy to understand.
Conclusion
Reusable Components are an important part of a Selenium automation framework because they encapsulate frequently used workflows into reusable classes or methods. By reducing duplicate code and separating common user actions from test scripts, reusable components improve readability, maintainability, and scalability. They help create clean automation frameworks that are easier to extend and maintain as projects grow.
Frequently Asked Questions (FAQs)
What are Reusable Components?
Reusable Components are classes or methods that encapsulate commonly performed user workflows into reusable units.
Why are Reusable Components used?
They eliminate duplicate code, improve maintainability, and make test scripts easier to read.
What is the difference between a Utility Class and a Reusable Component?
A Utility Class provides generic helper methods such as waits, screenshots, or configuration handling.
A Reusable Component represents a complete business workflow, such as login, search, or form submission.
Can Reusable Components be used with the Page Object Model?
Yes.
Reusable Components are often integrated with the Page Object Model to create modular and maintainable automation frameworks.
Are Reusable Components used in professional Selenium frameworks?
Yes.
Most enterprise automation frameworks use reusable components for common workflows such as login, navigation, search, shopping cart operations, and form handling.
Key Takeaways
Reusable Componentsencapsulate frequently used workflows into reusable classes or methods.They reduce duplicate code and improve framework maintainability.
They keep test scripts shorter and easier to understand.
Common reusable components include login, search, navigation, and form handling.
Reusable Components are widely used in professional Selenium automation frameworks to build scalable and maintainable test suites.
