0
0
Selenium Pythontesting~8 mins

JavaScript executor basics in Selenium Python - Framework Patterns

Choose your learning style9 modes available
Framework Mode - JavaScript executor basics
Folder Structure
selenium_python_project/
├── tests/
│   ├── test_login.py
│   ├── test_javascript_executor.py  <-- Tests using JavaScript executor
│   └── __init__.py
├── pages/
│   ├── base_page.py  <-- Contains JavaScript executor helper methods
│   ├── login_page.py
│   └── __init__.py
├── utils/
│   ├── driver_factory.py  <-- WebDriver setup and teardown
│   ├── config.py  <-- Environment and browser configs
│   └── __init__.py
├── reports/
│   └── (test reports output here)
├── conftest.py  <-- Pytest fixtures for setup
└── requirements.txt
  
Test Framework Layers
  • Driver Layer: Manages WebDriver instances (in utils/driver_factory.py).
  • Page Objects: Classes representing pages, with methods using JavaScript executor for advanced actions (pages/base_page.py).
  • Tests: Test scripts that call page object methods, including JavaScript executor usage (tests/test_javascript_executor.py).
  • Utilities: Helper functions and config management (utils/config.py).
  • Configuration: Environment settings, browser options, credentials (utils/config.py).
Configuration Patterns

Use a config.py file to store environment URLs, browser choices, and credentials. Use environment variables or command-line options to switch between test environments (e.g., dev, staging, prod).

Example snippet in utils/config.py:

import os

ENV = os.getenv('TEST_ENV', 'dev')
BROWSER = os.getenv('BROWSER', 'chrome')

CONFIG = {
    'dev': {'url': 'https://dev.example.com'},
    'staging': {'url': 'https://staging.example.com'},
    'prod': {'url': 'https://example.com'}
}

BASE_URL = CONFIG[ENV]['url']

Pass browser choice to driver factory to create correct WebDriver.

Test Reporting and CI/CD Integration
  • Use pytest with pytest-html plugin to generate HTML reports.
  • Reports saved in reports/ folder for easy access.
  • Integrate tests in CI/CD pipelines (GitHub Actions, Jenkins) to run on code push.
  • Fail fast on JavaScript executor errors to catch UI issues early.
Best Practices
  • Encapsulate JavaScript calls: Put JavaScript executor code inside page object methods to keep tests clean.
  • Use explicit waits: Wait for elements or conditions before running JavaScript to avoid flaky tests.
  • Keep JavaScript simple: Use JavaScript executor only when Selenium commands cannot perform the action.
  • Handle exceptions: Catch and log JavaScript execution errors for easier debugging.
  • Reuse driver instance: Pass the same WebDriver to page objects to maintain session and context.
Self Check

Where in this folder structure would you add a new method that uses JavaScript executor to scroll to an element?

Key Result
Organize Selenium Python tests with clear layers, encapsulate JavaScript executor calls in page objects, and manage configs for flexible, maintainable automation.