Automation Testing with Selenium & Python using PyTest Framework

Selenium with Python is one of the most popular combinations for automating web application testing. Using the PyTest framework, testers can write modular, scalable, and maintainable test scripts that can be integrated with CI/CD pipelines using Jenkins or AzureDevOps.
Why PyTest with Selenium?
Ease of use: PyTest provides simple syntax for writing tests and supports fixtures for setup and teardown.
Modular structure: Encourages reusable test code.
Reporting & Integration: Compatible with Jenkins, and other CI/CD tools.
In pytest, every test file, every test method starts with test_”nameOfFile”
Basic Framework Structure
| Folder/File | Purpose |
tests/ | Contains the actual test cases that call methods from the page classes (e.g. test_login.py) |
pages/ | Implements the Page Object Model. Each web page is represented by a class, which includes locators and methods for interacting with elements on that page.(e.g., login_page.py) |
data/ | Holds external test data, such as JSON files for user credentials or browser config. |
reports/ | Stores test reports, which we generate using tools like pytest-html |
conftest.py | This contains configuration files like conftest.py for setting up and tearing down browsers using PyTest fixtures. |
Example: Login Page Test
1. Page Object (pages/login_page.py)
from selenium.webdriver.common.by import By
class LoginPage:
def __init__(self, driver):
self.driver = driver
self.username = (By.ID, "username")
self.password = (By.ID, "password")
self.login_btn = (By.ID, "loginBtn")
def login(self, user, pwd):
self.driver.find_element(*self.username).send_keys(user)
self.driver.find_element(*self.password).send_keys(pwd)
self.driver.find_element(*self.login_btn).click()
2. Test Script (tests/test_login.py)
import pytest
from pages.login_page import LoginPage
def test_valid_login(driver):
login = LoginPage(driver)
login.login("testuser", "password123")
assert "Dashboard" in driver.title
3. PyTest Fixture (conftest.py)
import pytest
from selenium import webdriver
@pytest.fixture
// fixture initializes the Chrome driver
//before the test starts and quits the browser after the test is complete.
def driver():
# Setup: open Chrome browser
driver = webdriver.Chrome()
driver.get("https://example.com/login")
yield driver # <-- hands control to the test
# Teardown: close browser after test
driver.quit()
To automate login functionality using Selenium with Python, I follow the Page Object Model structure. I create:
A 'pages' folder containing a LoginPage class. This includes locators (e.g., username, password fields, login button) and action methods like enter_username(), enter_password(), and click_login().
A 'tests' folder with test_login.py where I call the methods from the LoginPage class and verify login success.
A 'config' or 'conftest.py' file for browser setup using PyTest fixtures.
A 'data' folder where I keep test input data like username and password in a JSON file.
For reporting, I generate HTML reports using pytest-html plugin.
This modular structure makes the login test reusable, maintainable, and easy to expand later.”
How to Execute :
Use Pytest as the test runner
Tests can be executed with pytest --html=reports/report.html for detailed reports in PyCharm
Integrate with Jenkins:
We’ve also integrated this framework with Jenkins CI to run the tests automatically on code commits or scheduled jobs.
