How to Integrate Selenium with Python Logging for Better Test Reports

How to Integrate Selenium with Python Logging for Better Test Reports

Integrating Selenium with Python’s built-in logging module is a best practice for producing detailed and structured test reports. Logging helps you monitor test execution, capture key actions, and diagnose failures without relying solely on console output or manual inspection.


✅ Why Use Logging in Selenium Tests?

Better debugging of test failures


Track test flow and actions (clicks, inputs, navigation)


Save logs to file for audit or CI/CD reports


Different log levels for better filtering


๐Ÿงฐ Step-by-Step: Integrate Selenium with Python Logging

๐Ÿ”น Step 1: Import Required Modules

python

Copy

Edit

import logging

from selenium import webdriver

from selenium.webdriver.common.by import By

๐Ÿ”น Step 2: Configure Logging

python

Copy

Edit

logging.basicConfig(

    filename='test_log.log',     # Log file name

    filemode='w',                # Overwrite on each run

    format='%(asctime)s - %(levelname)s - %(message)s',

    level=logging.INFO           # Log level

)

๐Ÿ’ก You can use logging.DEBUG for more detailed output.


๐Ÿ”น Step 3: Use Logging in Your Selenium Tests

python

Copy

Edit

def test_google_search():

    logging.info("Starting the test: test_google_search")


    try:

        driver = webdriver.Chrome()

        logging.info("Launched Chrome browser")


        driver.get("https://www.google.com")

        logging.info("Navigated to Google")


        search_box = driver.find_element(By.NAME, "q")

        search_box.send_keys("Selenium with Python logging")

        logging.info("Entered search query")


        search_box.submit()

        logging.info("Submitted the search")


        assert "Selenium" in driver.title

        logging.info("Test passed: Title contains 'Selenium'")


    except Exception as e:

        logging.error("Test failed: %s", str(e))

        raise


    finally:

        driver.quit()

        logging.info("Browser closed")

๐Ÿงช Sample Output (test_log.log)

pgsql

Copy

Edit

2025-06-11 14:35:22,305 - INFO - Starting the test: test_google_search

2025-06-11 14:35:23,105 - INFO - Launched Chrome browser

2025-06-11 14:35:24,122 - INFO - Navigated to Google

2025-06-11 14:35:25,134 - INFO - Entered search query

2025-06-11 14:35:25,541 - INFO - Submitted the search

2025-06-11 14:35:26,980 - INFO - Test passed: Title contains 'Selenium'

2025-06-11 14:35:27,001 - INFO - Browser closed

๐Ÿงฉ Tips for Better Logging Integration

Feature Benefit

logger = logging.getLogger(__name__) Use module-level logging for complex projects

logging.debug() Use for verbose, internal info

logging.warning() Highlight unexpected but non-breaking issues

logging.error() For exceptions and test failures

Use pytest + --log-cli-level Real-time log output in terminal


๐Ÿ›  Bonus: Use pytest with Logging

Install pytest if needed:


bash

Copy

Edit

pip install pytest

Run with logging:


bash

Copy

Edit

pytest test_script.py --log-cli-level=INFO

This shows logs live in the terminal while still saving them to file.


✅ Summary

Task Code

Set up logger logging.basicConfig(...)

Log test steps logging.info("Step description")

Handle exceptions logging.error("Error", exc_info=True)

Save logs Use filename='logfile.log'

Learn Selenium Python Training in Hyderabad

Read More

Automating Social Media Login and Posting Using Selenium

How to Handle Dynamic Web Elements in Selenium with Python

Visit Our IHUB Talent Training Institute in Hyderabad

Get Directions


Comments

Popular posts from this blog

How to Install and Set Up Selenium in Python (Step-by-Step)

Feeling Stuck in Manual Testing? Here’s Why You Should Learn Automation Testing

A Beginner's Guide to ETL Testing: What You Need to Know