Introduction
Naming conventions are standardized rules for naming variables, functions, classes, files, constants, and test cases. Following consistent naming conventions makes automation code easier to read, understand, maintain, and debug.
In Selenium automation projects, good naming conventions help:
-
Improve code readability
-
Reduce confusion
-
Simplify maintenance
-
Improve collaboration among team members
-
Make frameworks more scalable
Python follows naming guidelines defined in PEP 8, the official Python style guide.
Why Naming Conventions Matter
Consider the following example:
Poor Naming
a = "admin"
b = "password123"
def t():
pass
It is difficult to understand the purpose of the variables and function.
Good Naming
username = "admin"
password = "password123"
def login_user():
pass
The code is now self-explanatory.
Python Naming Convention Overview
| Element | Convention | Example |
|---|---|---|
| Variable | snake_case | user_name |
| Function | snake_case | login_user() |
| Method | snake_case | click_login_button() |
| Class | PascalCase | LoginPage |
| Constant | UPPER_CASE | BASE_URL |
| Module/File | snake_case | login_page.py |
| Package | lowercase | pages |
| Private Member | _single_leading_underscore | _password |
| Strongly Private Member | __double_leading_underscore | __salary |
Variable Naming
Variables should clearly describe the data they store.
Good Example
username = "admin"
browser_name = "Chrome"
login_status = True
Bad Example
a = "admin"
x = "Chrome"
z = True
Variable Naming Rules
Use snake_case
first_name = "John"
last_name = "Smith"
Avoid CamelCase
firstName = "John"
Avoid Single Character Names
u = "admin"
Use:
user_name = "admin"
Function Naming
Functions should describe the action being performed.
Good Example
def login_user():
pass
def verify_home_page():
pass
Bad Example
def test():
pass
def login():
pass
Too generic.
Function Naming Guidelines
Use:
verb + object
Examples:
def launch_browser():
pass
def click_login_button():
pass
def verify_page_title():
pass
def capture_screenshot():
pass
Method Naming
Methods inside classes follow the same snake_case convention.
Example
class LoginPage:
def enter_username(self):
pass
def enter_password(self):
pass
def click_login_button(self):
pass
Class Naming
Classes should use PascalCase.
Correct
class LoginPage:
pass
class HomePage:
pass
class DatabaseUtils:
pass
Incorrect
class loginpage:
pass
class login_Page:
pass
Selenium Page Object Naming
Recommended
class LoginPage:
pass
class DashboardPage:
pass
class RegistrationPage:
pass
Each class represents a page.
Constant Naming
Constants should be written in uppercase.
Example
BASE_URL = "https://example.com"
TIMEOUT = 10
SCREENSHOT_PATH = "screenshots/"
Incorrect
baseUrl = "https://example.com"
timeout = 10
Module/File Naming
Python files should use snake_case.
Good Examples
login_page.py
home_page.py
database_utils.py
webdriver_factory.py
Bad Examples
LoginPage.py
HomePage.py
DatabaseUtils.py
Package Naming
Packages should be lowercase.
Example
pages
utilities
tests
reports
config
Avoid
Pages
Utilities
TestCases
Test Case Naming
Test methods should clearly describe the expected behavior.
Good Examples
def test_user_can_login():
pass
def test_user_can_logout():
pass
def test_invalid_password_error_message():
pass
Bad Examples
def test1():
pass
def login_test():
pass
Selenium Locator Naming
Recommended
username_input
password_input
login_button
logout_link
search_box
Example
username_input = (
By.ID,
"username"
)
login_button = (
By.ID,
"login"
)
Naming Configuration Variables
Good Example
browser_name = "chrome"
execution_environment = "qa"
api_base_url = "https://api.example.com"
Naming Utility Classes
Examples
class BrowserUtils:
pass
class WaitUtils:
pass
class ExcelReader:
pass
class DatabaseHelper:
pass
Names should clearly indicate functionality.
Naming Exceptions
Custom exceptions should end with “Error” or “Exception”.
Example
class LoginError(Exception):
pass
class DatabaseConnectionError(Exception):
pass
Private Members
Use a single underscore.
Example
class Employee:
def __init__(self):
self._salary = 50000
Indicates internal use.
Strongly Private Members
Use double underscores.
Example
class Employee:
def __init__(self):
self.__salary = 50000
Triggers name mangling.
Real-World Selenium Framework Example
Folder Structure
project/
│
├── pages/
│ ├── login_page.py
│ ├── dashboard_page.py
│
├── tests/
│ ├── test_login.py
│ ├── test_dashboard.py
│
├── utilities/
│ ├── wait_utils.py
│ ├── browser_utils.py
│
├── config/
│ └── config.py
Example Page Object
class LoginPage:
username_input = (
By.ID,
"username"
)
password_input = (
By.ID,
"password"
)
login_button = (
By.ID,
"login"
)
def enter_username(
self,
username
):
pass
def enter_password(
self,
password
):
pass
def click_login_button(
self
):
pass
All names clearly describe their purpose.
Common Mistakes Beginners Make
Using Generic Names
Bad
a = "admin"
b = "password"
Good
username = "admin"
password = "password"
Mixing Naming Styles
Bad
userName
user_name
UserName
Use one style consistently.
Good
user_name
Using Long Names
Bad
current_logged_in_user_name_for_dashboard_page
Better
logged_in_user
Using Abbreviations
Bad
usr_nm
pwd
Good
username
password
Best Practices
Be Descriptive
customer_email
instead of:
email
Keep Names Consistent
Use the same naming style throughout the framework.
Follow PEP 8
-
Variables → snake_case
-
Functions → snake_case
-
Classes → PascalCase
-
Constants → UPPER_CASE
Avoid Unnecessary Abbreviations
browser_name
instead of:
br_nm
Name Tests by Business Scenario
test_user_can_add_product_to_cart()
instead of:
test_cart()
Benefits of Good Naming Conventions
-
Better readability
-
Easier debugging
-
Faster onboarding of new team members
-
Improved maintainability
-
Professional code quality
-
Easier framework scalability
Conclusion
Naming conventions are one of the simplest yet most important coding standards in Python automation frameworks. Well-named variables, functions, classes, files, and test cases make code self-documenting and significantly reduce maintenance effort.
By following PEP 8 naming standards and using meaningful, descriptive names, Selenium automation engineers can create cleaner, more maintainable, and highly scalable test automation frameworks.
Frequently Asked Questions (FAQs)
Which naming style should variables use?
user_name = "admin"
Use snake_case.
Which naming style should classes use?
class LoginPage:
pass
Use PascalCase.
How should constants be named?
BASE_URL = "https://example.com"
Use UPPER_CASE.
How should Python files be named?
login_page.py
Use snake_case.
What naming convention should Selenium test methods follow?
def test_user_can_login():
pass
Use descriptive test names that explain the business scenario.
Key Takeaways
-
Follow PEP 8 naming standards.
-
Use snake_case for variables, functions, methods, and file names.
-
Use PascalCase for classes.
-
Use UPPER_CASE for constants.
-
Use descriptive and meaningful names.
-
Avoid abbreviations and generic names.
-
Name Selenium page objects clearly.
-
Name test cases based on business scenarios.
-
Keep naming consistent across the framework.
-
Good naming conventions improve readability and maintainability.
