0
0
Selenium Pythontesting~8 mins

XPath with text() in Selenium Python - Framework Patterns

Choose your learning style9 modes available
Framework Mode - XPath with text()
Folder Structure
selenium_project/
├── tests/
│   ├── test_login.py
│   ├── test_search.py
│   └── test_navigation.py
├── pages/
│   ├── base_page.py
│   ├── login_page.py
│   └── search_page.py
├── utils/
│   ├── driver_factory.py
│   └── wait_utils.py
├── config/
│   ├── config.yaml
│   └── credentials.yaml
├── reports/
│   └── latest_report.html
├── conftest.py
└── pytest.ini
    
Test Framework Layers
  • Driver Layer: Manages browser setup and teardown (e.g., driver_factory.py).
  • Page Objects: Classes representing web pages with locators and actions. Use XPath with text() to locate elements by visible text (e.g., login_page.py).
  • Tests: Test scripts using pytest that call page object methods to perform actions and assertions.
  • Utilities: Helper functions like explicit waits to handle dynamic content.
  • Configuration: YAML files for environment URLs, browser types, and credentials.
Configuration Patterns

Use config.yaml to store environment URLs and browser preferences. Use credentials.yaml for sensitive data like usernames and passwords.

Example config.yaml snippet:

environment:
  base_url: "https://example.com"
browser: "chrome"
    

Load these configs in conftest.py to parameterize tests and driver setup.

Test Reporting and CI/CD Integration
  • Use pytest with pytest-html plugin to generate HTML reports saved in reports/.
  • Integrate with CI/CD pipelines (e.g., GitHub Actions, Jenkins) to run tests on each commit and publish reports.
  • Failing tests highlight XPath locator issues, especially when text() values change.
Best Practices
  • Use XPath text() only when the visible text is stable and unique on the page.
  • Prefer explicit waits for elements located by XPath with text() to avoid flaky tests.
  • Keep XPath expressions simple and readable, e.g., //button[text()='Submit'].
  • Store XPath locators in page object classes to centralize maintenance.
  • Validate XPath locators in browser DevTools before adding to the framework.
Self Check

Where would you add a new XPath locator using text() for a "Logout" button in this framework structure?

Key Result
Organize Selenium Python tests with page objects using XPath text() locators centralized in page classes for maintainability.