This project demonstrates the integration of Python Selenium testing with Jenkins for Continuous Integration (CI) and Continuous Deployment (CD). The following steps outline how to set up and automate Selenium-based tests using Jenkins and GitHub.
- Create a folder named
Jenkins_Project
where all project files will reside. - Inside
Jenkins_Project
, create a subfolder namedtarget
. - Inside the
target
folder, create two subfolders:allure-reports
andallure-results
.
- Set up a new Python project in PyCharm inside the
Jenkins_Project
directory.
- Ensure that the
target
,allure-reports
, andallure-results
folders are present for Allure reporting and test results.
Download the ChromeDriver: ChromeDriver Download
Download the GeckoDriver: GeckoDriver Download
Create a Python file named Test_Selenium_Jenkins_Integration.py
and include the following code:
import time
import pytest
from selenium import webdriver
from selenium.webdriver.common.by import By
from webdriver_manager.firefox import GeckoDriverManager
from selenium.webdriver.firefox.service import Service
def setup_function():
global driver
# Setup WebDriver
path = Service(GeckoDriverManager().install())
driver = webdriver.Firefox(service=path)
driver.get("https://www.coursera.org/?authMode=login")
driver.maximize_window()
def teardown_function():
driver.quit()
def my_cred():
return [
('python16@gmail.com', 'python@123'),
('selenium19@gmail.com', 'sel@123'),
('pytsel@gmail.com', 'pytsel@123')
]
@pytest.mark.parametrize("username, password", my_cred())
def test_login(username, password):
print("My pytest Login")
driver.find_element(By.NAME, 'email').send_keys(username)
time.sleep(10)
driver.find_element(By.NAME, 'password').send_keys(password)
time.sleep(10)
Create a pytest.ini
file and add the following configuration:
[pytest]
addopts = -n3
markers =
phase1
phase2
Download the Jenkins: Download Jenkins
Place the downloaded jenkins.war in the Jenkins_Project folder.
Download the JDK 21 (Java): Download JDK-21_JAVA
Install JDK 21, follow the installation wizard, keeping the default path.
- Navigate to
C:\Program Files\Java\jdk-21
and copy the path. - Open "
Environment Variables
" and create a new variable. - Variable Name:
JAVA_HOME
- Variable Value:
C:\Program Files\Java\jdk-21
- Add the JDK bin directory to the system Path.
- Verify Java Installation by running the following command in the terminal:
java -version
In the Jenkins_Project
folder, open a terminal and run the following command:
java -jar jenkins.war
Access Jenkins Web Interface: https://YOUR_LOCALHOST_IPv4_ADDRESS:8080/
Put your localhost Ipv4 Address.
Use the generated password to proceed with the initial Jenkins setup.
In Jenkins, click on "New Item
", name it Selenium_Python_Jenkins
, and select "Freestyle project
".
Go to "Manage Jenkins
" > "Manage Plugins
".
- Allure (for reporting)
- ShiningPanda (for Python support)
Under the project configuration
, select "Use custom workspace
" and point it to your Jenkins_Project
folder.
Under Build Steps, choose "Execute Windows batch command
" and enter the following commands:
call ./Scripts/activate.bat
pytest -v -s Test_Selenium_Jenkins_Integration.py --alluredir=./target/allure-results
Under Post-build Actions
, select "Allure Report
" and set the following:
Results directory: target/allure-results
Report output directory: target/allure-reports
Create a public GitHub repository named Selenium_Project
.
Open Git Bash and run the following commands:
git init
git add .
git commit -m "Initial commit"
git branch -M master
git remote add origin https://github.com/YourUsername/Selenium_Project.git
git push -u origin master
Set up your Git username
and email
:
git config --global user.name "Your Name"
git config --global user.email "your_email@example.com"
Optionally, save credentials for future use:
git config --global credential.helper store
Create a new Jenkins job named GitHub_Selenium_Project
and select "Freestyle Project
".
Under Source Code Management
, select Git
and paste your GitHub repository URL:
https://github.com/YourUsername/Selenium_Project.git
Add Jenkins credentials for GitHub access.
Add a new Build Step: "Execute Windows batch command
" and enter the following commands:
python -m venv env
call ./Scripts/activate.bat
pip install -r requirements.txt
pytest -v -s Test_Selenium_Jenkins_Integration.py --alluredir=./target/allure-results
Add Allure Report under "Post-build Actions
".
Save and apply the configuration.
Configure Poll SCM with the following cron expression to trigger builds on code changes:
* * * * *
Make changes to the test code, such as modifying credentials or the test case.
Commit the changes using Git Bash:
git init
git add .
git commit -m "Updated credentials"
git push -u origin master
Jenkins will automatically trigger the build due to the SCM polling configuration, running the tests and generating the Allure report.
This project demonstrates the full CI/CD pipeline using Selenium
, Jenkins
, and GitHub
, from test automation to integration with Jenkins for continuous testing and deployment. By setting up this pipeline, you ensure automated testing and reporting on every code change, significantly improving software quality and deployment efficiency.