0
0
Selenium Pythontesting~8 mins

Select by value, text, index in Selenium Python - Framework Patterns

Choose your learning style9 modes available
Framework Mode - Select by value, text, index
Folder Structure
selenium-python-project/
├── tests/
│   ├── test_select_dropdown.py
│   └── __init__.py
├── pages/
│   ├── base_page.py
│   └── dropdown_page.py
├── utils/
│   ├── selenium_helpers.py
│   └── __init__.py
├── config/
│   ├── config.yaml
│   └── __init__.py
├── conftest.py
└── requirements.txt
    
Test Framework Layers
  • Driver Layer: Manages browser setup and teardown (in conftest.py using pytest fixtures).
  • Page Objects: Encapsulate page elements and actions (e.g., dropdown_page.py has methods to select dropdown options by value, text, or index).
  • Tests: Test scripts that use page objects to perform actions and assertions (e.g., test_select_dropdown.py).
  • Utilities: Helper functions for Selenium actions, waits, or common utilities (selenium_helpers.py).
  • Configuration: Environment settings like URLs, browser types, credentials stored in config/config.yaml.
Configuration Patterns

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

# config/config.yaml
base_url: "https://example.com"
browser: "chrome"

Load config in conftest.py to initialize WebDriver accordingly.

import yaml
import pytest
from selenium import webdriver

@pytest.fixture(scope="session")
def config():
    with open("config/config.yaml") as f:
        return yaml.safe_load(f)

@pytest.fixture(scope="function")
def driver(config):
    if config["browser"] == "chrome":
        driver = webdriver.Chrome()
    elif config["browser"] == "firefox":
        driver = webdriver.Firefox()
    else:
        raise ValueError("Unsupported browser")
    driver.implicitly_wait(10)
    yield driver
    driver.quit()
Test Reporting and CI/CD Integration
  • Use pytest with pytest-html plugin to generate HTML reports.
  • Run tests in CI pipelines (GitHub Actions, Jenkins) with commands like pytest --html=report.html.
  • Reports show pass/fail status of dropdown selection tests and assertion results.
  • Logs and screenshots on failure can be added via pytest hooks for better debugging.
Best Practices
  • Use Page Object Model: Keep selectors and actions in page classes to separate test logic from UI details.
  • Explicit Waits: Use Selenium waits to handle dynamic dropdown loading instead of fixed sleeps.
  • Data-Driven Testing: Test selecting dropdown options by different methods (value, text, index) with parameterized tests.
  • Clear Naming: Name methods like select_by_value, select_by_text, select_by_index for readability.
  • Configurable Browsers: Allow running tests on multiple browsers via config files.
Self Check

Where in this folder structure would you add a new method to select a dropdown option by partial text match?

Key Result
Use Page Object Model with config-driven WebDriver setup and pytest for clean, maintainable Selenium Python tests.