0
0
Selenium Pythontesting~8 mins

Browser console log capture in Selenium Python - Framework Patterns

Choose your learning style9 modes available
Framework Mode - Browser console log capture
Folder Structure
selenium-python-project/
├── src/
│   ├── pages/
│   │   └── login_page.py
│   ├── tests/
│   │   └── test_login.py
│   ├── utils/
│   │   └── log_capture.py
│   └── config/
│       └── config.yaml
├── requirements.txt
└── pytest.ini
Test Framework Layers
  • Driver Layer: Manages Selenium WebDriver setup and teardown, including browser options to enable log capturing.
  • Page Objects: Classes representing web pages, encapsulating locators and actions.
  • Tests: Test scripts using pytest that perform actions and assertions, and call log capture utilities.
  • Utilities: Helper functions for capturing and processing browser console logs.
  • Configuration: Environment settings like browser type, URLs, and log preferences stored in config files.
Configuration Patterns

Use a config.yaml file to store environment variables such as:

  • Browser type (e.g., Chrome)
  • Base URL
  • Log level preferences

Load these settings in test setup to configure WebDriver options, including enabling loggingPrefs for capturing browser console logs.

Example snippet to enable console log capture in Chrome options:

from selenium import webdriver
from selenium.webdriver.chrome.service import Service
from selenium.webdriver.chrome.options import Options

options = Options()
options.set_capability('goog:loggingPrefs', {'browser': 'ALL'})
driver = webdriver.Chrome(service=Service(), options=options)
Test Reporting and CI/CD Integration
  • Capture browser console logs during test execution and attach them to test reports.
  • Use pytest hooks or fixtures to collect logs after each test.
  • Integrate with CI/CD pipelines (e.g., GitHub Actions, Jenkins) to run tests and archive logs for debugging.
  • Generate HTML or XML reports that include console log summaries for failed tests.
Best Practices
  1. Enable only necessary log levels to avoid large log files and improve performance.
  2. Use utility functions to abstract log retrieval and filtering for reuse.
  3. Capture logs immediately after test steps to correlate logs with actions.
  4. Store logs per test case to simplify debugging and traceability.
  5. Integrate log capture with test failure handling to automatically save logs on errors.
Self Check

Where in this folder structure would you add a new utility function to filter and save browser console logs?

Key Result
Organize Selenium Python tests with clear layers and utilities to capture and report browser console logs effectively.