0
0
Selenium Pythontesting~8 mins

XPath with attributes in Selenium Python - Framework Patterns

Choose your learning style9 modes available
Framework Mode - XPath with attributes
Folder Structure
selenium_python_project/
├── src/
│   ├── pages/
│   │   └── login_page.py
│   ├── tests/
│   │   └── test_login.py
│   ├── utils/
│   │   └── helpers.py
│   └── config/
│       └── config.yaml
├── drivers/
│   └── chromedriver.exe
├── reports/
│   └── test_report.html
├── requirements.txt
└── pytest.ini
  
Test Framework Layers
  • Driver Layer: Manages browser drivers and WebDriver setup (e.g., ChromeDriver).
  • Page Objects: Classes representing web pages. Use XPath with attributes to locate elements, e.g., self.driver.find_element(By.XPATH, "//input[@id='username']").
  • Tests: Test cases using pytest that call page object methods to perform actions and assertions.
  • Utilities: Helper functions for waits, logging, and common actions.
  • Configuration: Environment settings, browser options, and credentials stored in config files.
Configuration Patterns

Use a YAML config file (src/config/config.yaml) to store environment URLs, browser choice, and user credentials.

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

Load config in tests or page objects to keep data separate from code.

Use pytest command line options or environment variables to switch browsers or environments.

Test Reporting and CI/CD Integration
  • Use pytest with pytest-html plugin to generate HTML test reports saved in reports/.
  • Integrate with CI/CD pipelines (e.g., GitHub Actions, Jenkins) to run tests on code push and publish reports.
  • Use explicit waits in tests to reduce flaky failures caused by timing issues.
Framework Design Principles
  1. Use XPath with attributes for precise element location: For example, //button[@type='submit'] targets the submit button clearly.
  2. Keep locators in page objects: Centralize XPath expressions to ease maintenance.
  3. Use explicit waits: Wait for elements located by XPath to be visible or clickable before interacting.
  4. Separate test data from code: Use config files for URLs and credentials.
  5. Write clear, small test methods: Each test should verify one behavior using page object methods.
Self Check

Where in this folder structure would you add a new XPath locator for a "Search" input field on the homepage?

Key Result
Organize Selenium Python tests using page objects with XPath attribute locators, config files for data, and pytest for execution and reporting.