0
0
Selenium Pythontesting~8 mins

Typing text (send_keys) in Selenium Python - Framework Patterns

Choose your learning style9 modes available
Framework Mode - Typing text (send_keys)
Folder Structure
my_selenium_project/
├── src/
│   ├── pages/
│   │   └── login_page.py
│   ├── tests/
│   │   └── test_login.py
│   ├── utils/
│   │   └── helpers.py
│   └── config/
│       └── config.yaml
├── requirements.txt
└── pytest.ini

This structure keeps code organized by purpose: pages for page objects, tests for test cases, utils for helpers, and config for settings.

Test Framework Layers
  • Driver Layer: Manages browser setup and teardown using Selenium WebDriver.
  • Page Objects: Classes representing web pages with methods to interact, e.g., typing text using send_keys().
  • Tests: Test functions or classes that use page objects to perform actions and assertions.
  • Utilities: Helper functions like waiting for elements or reading config files.
  • Configuration: Stores environment variables, URLs, credentials, and browser options.
Configuration Patterns
  • Use a config.yaml file to store URLs, user credentials, and browser choices.
  • Load config in tests or fixtures to keep sensitive data separate from code.
  • Use environment variables or command-line options to switch between test environments (e.g., dev, staging).
  • Example snippet to load config in Python:
    import yaml
    with open('src/config/config.yaml') as f:
        config = yaml.safe_load(f)
    
Test Reporting and CI/CD Integration
  • Use pytest with plugins like pytest-html to generate readable test reports.
  • Reports show which tests passed or failed, including screenshots on failure.
  • Integrate tests into CI/CD pipelines (GitHub Actions, Jenkins) to run tests automatically on code changes.
  • Configure test runs to use different browsers or environments via config files or command-line options.
Best Practices
  • Use Page Object Model: Keep typing actions inside page object methods to reuse and maintain easily.
  • Explicit Waits: Wait for input fields to be visible and enabled before typing to avoid flaky tests.
  • Clear Before Typing: Clear input fields before sending keys to avoid leftover text.
  • Use Constants for Locators: Store element locators as constants or variables in page objects for easy updates.
  • Keep Tests Simple: Tests should call page object methods, not directly use Selenium commands.
Self Check

Where in this framework structure would you add a new method to type text into the search box on the homepage?

Key Result
Organize Selenium tests with clear layers: driver setup, page objects for typing actions, tests for validation, utilities for helpers, and config for environments.