0
0
Selenium Pythontesting~8 mins

Page title and URL retrieval in Selenium Python - Framework Patterns

Choose your learning style9 modes available
Framework Mode - Page title and URL retrieval
Folder Structure
selenium-python-project/
├── tests/
│   ├── test_page_info.py          # Test cases for page title and URL retrieval
├── pages/
│   ├── base_page.py                # Base page with common methods
│   ├── home_page.py                # Example page object
├── utils/
│   ├── driver_factory.py           # WebDriver setup and teardown
├── config/
│   ├── config.yaml                 # Environment and browser configs
├── reports/
│   ├── test_report.html            # Generated test reports
├── requirements.txt               # Python dependencies
└── pytest.ini                     # Pytest configuration
    
Test Framework Layers
  • Driver Layer: utils/driver_factory.py manages WebDriver setup and cleanup.
  • Page Objects: pages/base_page.py has common methods like get_title() and get_url(). Specific pages like home_page.py extend it.
  • Tests: tests/test_page_info.py contains test functions that use page objects to verify title and URL.
  • Utilities: Helpers for waits, logging, or config parsing if needed.
  • Configuration: config/config.yaml stores environment URLs, browser types, and credentials.
Configuration Patterns

Use a YAML file (config/config.yaml) to store environment details and browser preferences.

# config/config.yaml
base_url: "https://example.com"
browser: "chrome"
headless: true
credentials:
  username: "testuser"
  password: "password123"
    

Load config in driver_factory.py and tests to initialize WebDriver and navigate to correct URLs.

Test Reporting and CI/CD Integration
  • Use pytest with pytest-html plugin to generate readable HTML reports in reports/.
  • Integrate with CI tools like GitHub Actions or Jenkins to run tests on each commit.
  • Failing tests for title or URL mismatches will be clearly reported with screenshots (if implemented).
  • Reports include pass/fail status and test duration for each test case.
Framework Design Principles
  1. Page Object Model: Encapsulate page title and URL retrieval in base page methods to reuse across tests.
  2. Explicit Waits: Wait for page load or elements before retrieving title or URL to avoid flaky tests.
  3. Config Driven: Use external config files for environment and browser settings to keep tests flexible.
  4. Clear Assertions: Assert page title and URL exactly to catch navigation issues early.
  5. Clean Setup/Teardown: Use fixtures to initialize and quit WebDriver cleanly for each test.
Self Check Question

Where in this folder structure would you add a new method to retrieve the page title for a login page?

Key Result
Use Page Object Model with config-driven WebDriver setup to retrieve and verify page title and URL reliably.