0
0
Selenium Pythontesting~8 mins

Jenkins integration in Selenium Python - Framework Patterns

Choose your learning style9 modes available
Framework Mode - Jenkins integration
Folder Structure
selenium-python-project/
├── tests/
│   ├── test_login.py
│   └── test_checkout.py
├── pages/
│   ├── login_page.py
│   └── checkout_page.py
├── utils/
│   ├── driver_factory.py
│   └── helpers.py
├── config/
│   ├── config.yaml
│   └── credentials.yaml
├── reports/
│   └── latest_report.html
├── requirements.txt
├── pytest.ini
└── Jenkinsfile
  
Test Framework Layers
  • Driver Layer: utils/driver_factory.py manages browser setup and teardown.
  • Page Objects: pages/ contains page classes representing UI elements and actions.
  • Test Cases: tests/ holds test scripts using pytest and page objects.
  • Utilities: utils/helpers.py includes reusable functions like waits and assertions.
  • Configuration: config/ stores environment settings and credentials in YAML files.
  • Reports: reports/ saves test execution reports for review.
  • CI/CD Integration: Jenkinsfile defines Jenkins pipeline steps to run tests automatically.
Configuration Patterns

Use YAML files in config/ to separate environment variables, browser choices, and credentials.

Example config.yaml content:

environment: staging
browser: chrome
base_url: https://staging.example.com
  

Example credentials.yaml content:

username: testuser
password: securepass123
  

Load these configs in tests or driver factory to customize test runs.

Jenkinsfile uses parameters to select environment and browser dynamically:

pipeline {
    agent any
    parameters {
        choice(name: 'ENV', choices: ['staging', 'production'], description: 'Select environment')
        choice(name: 'BROWSER', choices: ['chrome', 'firefox'], description: 'Select browser')
    }
    stages {
        stage('Run Tests') {
            steps {
                sh 'pytest --env=${ENV} --browser=${BROWSER} --html=reports/latest_report.html'
            }
        }
    }
    post {
        always {
            publishHTML(target: [
                reportDir: 'reports',
                reportFiles: 'latest_report.html',
                reportName: 'Test Report'
            ])
        }
    }
}
  
Test Reporting and CI/CD Integration
  • Use pytest-html plugin to generate readable HTML reports saved in reports/.
  • Jenkins pipeline (Jenkinsfile) runs tests on each commit or schedule.
  • Reports are published in Jenkins UI using publishHTML plugin for easy access.
  • Failures cause Jenkins build to fail, alerting the team immediately.
  • Parameters in Jenkins allow flexible test runs without changing code.
Best Practices
  1. Separate Config from Code: Keep environment and credentials outside code for security and flexibility.
  2. Use Parameterized Jenkins Pipelines: Allow choosing environment and browser at runtime.
  3. Generate and Archive Reports: Always produce HTML reports and archive them in Jenkins for traceability.
  4. Fail Fast: Configure Jenkins to fail builds on test failures to catch issues early.
  5. Use Virtual Environments: Manage Python dependencies with virtualenv or similar to avoid conflicts.
Self Check

Where in this folder structure would you add a new page object for the "User Profile" page?

Key Result
Organize Selenium Python tests with clear layers, config files, and Jenkins pipeline for automated runs and reporting.