0
0
Selenium Pythontesting~8 mins

Time.sleep vs proper waits in Selenium Python - Framework Approaches Compared

Choose your learning style9 modes available
Framework Mode - Time.sleep vs proper waits
Folder Structure for Selenium Python Framework
selenium-python-framework/
├── tests/
│   ├── test_login.py
│   └── test_checkout.py
├── pages/
│   ├── base_page.py
│   ├── login_page.py
│   └── checkout_page.py
├── utils/
│   ├── wait_utils.py
│   └── logger.py
├── config/
│   ├── config.yaml
│   └── credentials.yaml
├── conftest.py
└── requirements.txt
Test Framework Layers
  • Driver Layer: Manages browser setup and teardown (in conftest.py using pytest fixtures).
  • Page Objects: Classes representing web pages with locators and methods (pages/ folder).
  • Tests: Test cases using pytest in tests/ folder.
  • Utilities: Helper functions like explicit wait wrappers in utils/wait_utils.py.
  • Configuration: Environment settings, URLs, credentials in config/ folder.
Configuration Patterns
  • Use config.yaml to store environment URLs and browser options.
  • Store sensitive data like usernames and passwords in credentials.yaml, not in code.
  • Load configs in conftest.py to pass to tests and page objects.
  • Allow switching browsers and environments via command line or config file.
Test Reporting and CI/CD Integration
  • Use pytest built-in reporting with --junitxml=report.xml for CI tools.
  • Integrate with CI/CD pipelines (GitHub Actions, Jenkins) to run tests on code push.
  • Generate HTML reports using plugins like pytest-html for easy reading.
  • Log wait failures and exceptions clearly for debugging.
Best Practices for Waits in Selenium Python
  1. Avoid time.sleep(): It pauses test for fixed time, slowing tests and causing flakiness.
  2. Use Explicit Waits: Wait only as long as needed for specific conditions (element visible, clickable).
  3. Centralize Wait Logic: Put wait helper functions in utils/wait_utils.py for reuse.
  4. Use Expected Conditions: Use Selenium's WebDriverWait with expected conditions for reliability.
  5. Fail Fast: Set reasonable timeout values to avoid long test delays.
Self Check Question

Where in this framework structure would you add a new explicit wait helper function to wait for an element to be clickable?

Key Result
Use explicit waits with reusable helper functions instead of fixed time sleeps for reliable Selenium Python tests.