🏗 Selenium Automation Framework Concepts in Python
Automation frameworks provide structure, scalability, reusability, and maintainability to Selenium projects. Professional automation is not about writing scripts — it is about building frameworks.
📦 Page Object Model (POM)
POM is a design pattern where each web page is represented as a class and each element is defined as a variable.
class LoginPage:
def __init__(self, driver):
self.driver = driver
self.username = (By.ID, "user")
self.password = (By.ID, "pass")
self.login_btn = (By.ID, "login")
def login(self, u, p):
self.driver.find_element(*self.username).send_keys(u)
self.driver.find_element(*self.password).send_keys(p)
self.driver.find_element(*self.login_btn).click()
📊 Data Driven Framework
Test data is separated from test scripts and stored in external files.
- Excel
- CSV
- JSON
- Database
import pandas as pd
data = pd.read_excel("data.xlsx")
for i in data.index:
username = data['username'][i]
password = data['password'][i]
---
🧠 Keyword Driven Framework
Actions are driven by keywords instead of code logic.
- OPEN_BROWSER
- CLICK
- ENTER_TEXT
- VERIFY_TEXT
IF keyword == "CLICK":
click(element)
---
🔀 Hybrid Framework
Combination of POM + Data Driven + Keyword Driven framework.
🗂 Project Folder Structure
project/ │ ├── tests/ ├── pages/ ├── utils/ ├── data/ ├── config/ ├── logs/ ├── reports/ ├── screenshots/ └── main.py---
⚙ Config File Handling
import configparser
config = configparser.ConfigParser()
config.read("config.ini")
url = config.get("app", "url")
browser = config.get("app", "browser")
---
📝 Logging in Selenium
import logging
logging.basicConfig(
filename="logs/test.log",
level=logging.INFO,
format="%(asctime)s - %(levelname)s - %(message)s"
)
logging.info("Test Started")
---
📄 Report Generation
Common reporting tools:
- Allure Reports
- HTMLTestRunner
- PyTest HTML Reports
pytest --html=report.html---
📸 Screenshot Capture on Failure
driver.save_screenshot("screenshots/failure.png")
- Always use POM
- Separate test logic and test data
- Implement logging
- Generate reports
- Use hybrid framework for enterprise projects
- Maintain clean folder structure
Framework design is what makes Selenium automation professional and enterprise-ready. POM + Hybrid Framework + Logging + Reporting + CI/CD = Industry-standard automation architecture.
No comments:
Post a Comment