0
0
Selenium Pythontesting~8 mins

Checking element state (displayed, enabled, selected) in Selenium Python - Framework Patterns

Choose your learning style9 modes available
Framework Mode - Checking element state (displayed, enabled, selected)
Folder Structure
my_selenium_project/
├── src/
│   ├── pages/
│   │   └── login_page.py
│   ├── tests/
│   │   └── test_login.py
│   ├── utils/
│   │   └── element_state_helpers.py
│   └── config/
│       └── config.py
├── 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 with methods to interact with elements and check their states (displayed, enabled, selected).
  • Tests: Test scripts using page objects to perform actions and assert element states.
  • Utilities: Helper functions for common element state checks to keep code clean and reusable.
  • Configuration: Settings for environment URLs, browser options, and credentials.
Configuration Patterns
  • Use a config.py file to store environment URLs, browser types, and credentials as variables or dictionaries.
  • Use command-line options or environment variables to select browser type (e.g., Chrome, Firefox) at runtime.
  • Keep sensitive data like passwords out of code by using environment variables or secure vaults.
  • Example snippet in config.py:
    BASE_URL = "https://example.com"
    BROWSER = "chrome"
    USER_CREDENTIALS = {"username": "testuser", "password": "secret"}
Test Reporting and CI/CD Integration
  • Use pytest with plugins like pytest-html to generate readable HTML test reports showing pass/fail results.
  • Configure CI/CD pipelines (GitHub Actions, Jenkins) to run tests on each code push and publish reports.
  • Include screenshots on failure to help debug element state issues.
  • Example pytest command for report:
    pytest --html=reports/test_report.html --self-contained-html
Best Practices
  • Use Explicit Waits: Wait for elements to be in the expected state (visible, enabled) before interacting to avoid flaky tests.
  • Encapsulate State Checks: Put element state checks (is_displayed, is_enabled, is_selected) inside page object methods for reuse and clarity.
  • Clear Naming: Name methods clearly, e.g., is_login_button_enabled(), so tests read like plain English.
  • Keep Tests Independent: Each test should set up its own state and not rely on previous tests for element states.
  • Use Assertions Properly: Assert element states explicitly to catch failures early and provide clear error messages.
Self Check

Where in this folder structure would you add a new method to check if a "Remember Me" checkbox is selected on the login page?

Key Result
Organize Selenium Python tests with clear layers: driver setup, page objects with element state methods, tests, utilities, and config for clean, reliable checks of displayed, enabled, and selected states.