Setting Up Jenkins for Selenium Automated Tests with Python
Prerequisites
Step-by-Step Guide
1. Install Selenium and WebDriver
2. Configure Jenkins
New Item
.Freestyle project
, and click OK
.3. Set Up Git Repository
Source Code Management
, select Git
.4. Add Build Step
Build
, click Add build step
.Execute shell
(Linux/macOS) or Execute Windows batch command
(Windows).For Linux/macOS:
For Windows:
5. Schedule Build Triggers
Build Triggers
, select Poll SCM
.* * * * *
for every minute).6. Save and Build
Build Now
to manually trigger a build and run your Selenium tests.Setting Up GitLab CI for Selenium Automated Tests with Python
Prerequisites
.gitlab-ci.yml
file.1. Install Selenium and WebDriver
2. Create .gitlab-ci.yml
in your project root
3. Commit and Push
4. Run Pipeline
Conclusion
You now have practical setups for running Selenium automated tests with both Jenkins and GitLab CI using Python.
Installing Selenium and WebDriver
1. Install Selenium
In your terminal or command prompt, run:
2. Download WebDriver
For Chrome:
3. Setting Up Python Code
Create a Python script test_script.py
:
4. Integrating with CI Tools (Short Overview)
Jenkins
test_script.py
.GitLab CI
.gitlab-ci.yml
file to automate the pipeline.Now you have the implementation ready for practical use, focusing on Selenium and WebDriver installations, with a generic guide for CI tool integration.
Practical Implementation of Selenium Test Scripts in Python
Sample Selenium Test Script
Directory Structure
Assume the project structure:
File: requirements.txt
File: tests/test_sample.py
CI Integration
Jenkins: Jenkinsfile
GitLab CI: .gitlab-ci.yml
Use the above configurations to integrate and execute your Selenium tests with Jenkins or GitLab CI.
Configuring Jenkins Pipeline for Selenium Automated Tests in Python
Jenkinsfile
Create a Jenkinsfile
in the root of your repository:
Configuring GitLab CI Pipeline for Selenium Automated Tests in Python
.gitlab-ci.yml
Create a .gitlab-ci.yml
file in the root of your repository:
These configurations will allow you to run Selenium automated tests using Jenkins or GitLab CI pipelines effectively with Python.
Integrate Selenium Tests into CI Pipeline
Jenkins
Jenkinsfile
GitLab CI
.gitlab-ci.yml
Assume the structure of your repository is:
Execute and Monitor the CI Pipeline
1. Configure the Jenkins or GitLab CI Job to Execute Tests
Jenkins:
GitLab CI:
.gitlab-ci.yml
:
2. Trigger the Pipeline
Jenkins:
GitLab CI:
3. Monitor the Pipeline Execution
Jenkins:
GitLab CI:
4. Monitor and Maintain Test Health
Common Configurations:
Conclusion
These steps provide a practical outline to execute and monitor a CI pipeline with Jenkins or GitLab CI integrated with Selenium tests using Python. This implementation can be readily used to maintain and observe your CI/CD pipeline.
Part 7: Generate and Analyze Test Reports
Generate Test Reports
Selenium Test Script with Report Generation:
Analyze Test Reports
Jenkins/GitLab CI Configuration:
Example Script for Jenkins Freestyle Project:
Add build step “Execute shell”:
Post-build action “Publish HTML reports”:
reports
index.html
Practical Code for GitLab CI Integration:
tests/
directory with your Selenium test scripts.reports/
directory for HTMLTestRunner output.Running Tests and Generating Reports
Ensure HtmlTestRunner
is installed:
Optimize and Maintain the CI System
Script to Optimize Jenkins CI System
Script to Optimize GitLab CI System
CRON Script to Maintain Jenkins CI System
Generic Optimization Tips (Pseudocode)
These implementation instructions can be directly applied to optimize and maintain your CI system in real practice.