0
0
Selenium Pythontesting~8 mins

XPath axes (parent, child, sibling) in Selenium Python - Framework Patterns

Choose your learning style9 modes available
Framework Mode - XPath axes (parent, child, sibling)
Folder Structure
selenium_python_project/
├── src/
│   ├── pages/
│   │   ├── __init__.py
│   │   ├── base_page.py
│   │   └── sample_page.py
│   ├── tests/
│   │   ├── __init__.py
│   │   └── test_sample_page.py
│   ├── utils/
│   │   ├── __init__.py
│   │   └── wait_utils.py
│   └── config/
│       ├── __init__.py
│       └── config.py
├── requirements.txt
└── pytest.ini
  
Test Framework Layers
  • Driver Layer: Manages Selenium WebDriver setup and teardown (in utils or base_page.py).
  • Page Objects: Classes representing web pages, using XPath axes (parent, child, sibling) in locators to find elements precisely.
  • Tests: Test scripts using pytest that call page object methods to perform actions and assertions.
  • Utilities: Helper functions like explicit waits to handle dynamic page elements.
  • Configuration: Stores environment settings, browser options, and credentials.
Configuration Patterns

Use a config.py file to manage environment variables and browser settings.

# config/config.py
class Config:
    BASE_URL = "https://example.com"
    BROWSER = "chrome"
    IMPLICIT_WAIT = 10
    EXPLICIT_WAIT = 15

# You can extend this to load from environment variables or .env files for flexibility.
  
Test Reporting and CI/CD Integration
  • Use pytest with pytest-html plugin to generate HTML reports.
  • Integrate tests in CI/CD pipelines (GitHub Actions, Jenkins) to run on code push.
  • Reports show pass/fail status, screenshots on failure, and logs.
# Example pytest command with HTML report
pytest --html=reports/report.html --self-contained-html
  
Framework Design Principles
  1. Use XPath axes carefully: Use parent::, child::, and following-sibling:: axes to locate elements relative to known elements for stable locators.
  2. Keep locators readable: Avoid overly complex XPath expressions; prefer clear and maintainable ones.
  3. Page Object Model: Encapsulate XPath locators and actions inside page classes to separate test logic from UI details.
  4. Explicit waits: Use waits to handle dynamic content before accessing elements found by XPath axes.
  5. Config-driven tests: Manage environment and browser settings centrally for easy changes.
Self Check

Where in this folder structure would you add a new page object class for a login page that uses XPath axes to locate elements?

Key Result
Organize Selenium Python tests using Page Object Model with XPath axes locators, explicit waits, and config-driven settings.