This guide will walk you through the complete process of setting up Jenkins to run Selenium tests using the PyTest framework. This guide will cover everything from setting up the environment to configuring Jenkins to run your tests automatically.
Jenkins Setup for PyTest + Selenium Automation Testing
1. Prerequisites
Before setting up Jenkins to run PyTest and Selenium tests, make sure you have the following:
- Jenkins installed on your server or local machine
- Git repository with your PyTest + Selenium test scripts (public or private).
- Python installed on your System or Jenkins machine.
- Web Browser (e.g., Chrome, Firefox) and corresponding WebDriver installed.
If you don’t have Jenkins, follow these steps to install it on Ubuntu:
2. Install Web Browsers and WebDrivers
Selenium requires a browser and a WebDriver to interact with it. In this guide, we will use Google Chrome and ChromeDriver, but you can use other browsers like Firefox with their respective drivers.
Install Google Chrome (Ubuntu):
- Update package lists
- Install Google Chrome
$ sudo apt update
$ sudo apt install google-chrome-stable
Install ChromeDriver:
- Download the ChromeDriver that matches your installed Chrome version from ChromeDriver.
- Extract the downloaded file and move it to
/usr/local/bin
(or any directory in your PATH): - Ensure ChromeDriver is executable:
sudo chmod +x /usr/local/bin/chromedriver
$ unzip chromedriver-linux64.zip #(replace your zip file name here)
$ sudo mv chromedriver /usr/local/bin/
$ sudo chmod +x /usr/local/bin/chromedriver
$ chromedriver --version
Now Selenium should be able to interact with Chrome on your Jenkins machine.
3. Install Python and PyTest with Selenium
On the Jenkins machine, you need Python and the necessary libraries.
Install Python:
Install Python 3 on your Ubuntu system:
sudo apt install python3 python3-pip
Install PyTest and Selenium:
- Create a virtual environment (recommended to avoid dependency issues):
python3 -m venv myenv
- Activate the virtual environment:
source myenv/bin/activate
- Install the required libraries:
pip install pytest selenium pytest-selenium
$ python3 -m venv myenv
$source myenv/bin/activate
$pip install pytest selenium pytest-selenium
You’re now ready to run Selenium tests using PyTest under the Virtual Environment!
4. Set Up Git in Jenkins (For Private Repositories)
If your project is hosted in a private Git repository, you will need to configure Git credentials in Jenkins to clone the repository.
Step 1: Configure Git Credentials in Jenkins
- Go to the Jenkins Dashboard → Manage Jenkins → Manage Credentials → (select the domain or click “(global)”) → Add Credentials.
- Select Kind: Username with password:
- Username: Enter your Git username.
- Password: Enter your Git password (or personal access token if using services like GitHub).
- Click OK to save the credentials.
How to Create Git Access Token Click Here
Step 2: Add Git Repository to Jenkins Job
- In your Jenkins job configuration, scroll down to Source Code Management.
- Select Git.
- In the Repository URL field, add the Git repository URL (e.g.,
https://github.com/your-user/your-repo.git
). - In the Credentials dropdown, select the credentials you just added.
- In the Branch Section Give Your Branch Name
Jenkins will now be able to access your private Git repository and pull the test scripts for execution.
5. Create Jenkins Job for PyTest + Selenium
Step 1: Create a New Freestyle Job
- In Jenkins, go to the Dashboard, click New Item.
- Enter a job name (e.g.,
Selenium_PyTest_Automation
), select Freestyle project, and click OK.
Step 2: Configure Source Code Management
- In the job configuration page, scroll to Source Code Management.
- Select Git, enter your repository URL, and select the credentials (if private).
- Jenkins will now clone your repository before running the tests.
Step 3: Configure Build Environment
In the Build section, we will set up the steps to install dependencies and run the tests.
- Add a Build Step → Select Execute Shell (for Linux/Mac) or Execute Windows Batch Command (if using Windows). For Ubuntu/Linux:
- Activate the virtual environment:
source venv/bin/activate
Install any dependencies (if you use arequirements.txt
file):pip install -r requirements.txt
Run your PyTest tests:pytest --maxfail=1 --disable-warnings -q
- Activate the virtual environment:
# Your Execute Shell format will look like this
#!/bin/bash
# Update package list and install Google Chrome
echo "Installing Google Chrome..."
sudo apt update
sudo apt install -y google-chrome-stable
# Install necessary dependencies for headless Chrome
echo "Installing necessary dependencies..."
sudo apt-get install -y \
libgdk-pixbuf2.0-0 \
libgtk-3-0 \
xvfb
echo "Installing Python dependencies..."
pip install --upgrade pip
pip install webdriver-manager pytest selenium
echo "Making sure chromedriver is executable..."
sudo chmod +x /usr/bin/chromedriver
# Activate virtual environment if it exists
if [ -d "myenv" ]; then
echo "Activating virtual environment..."
source myenv/bin/activate
else
echo "No virtual environment found. Proceeding without it."
fi
# Run the test with Xvfb to simulate display (for headless mode)
echo "Running tests using Xvfb..."
xvfb-run pytest tests
# Deactivate virtual environment if it was activated
if [ -d "myenv" ]; then
echo "Deactivating virtual environment..."
deactivate
fi
# Done
echo "Test execution complete."
- Save and Apply your configuration.
6. Set Up Email Notifications
It’s useful to get notified when tests pass or fail. Jenkins can send email notifications for the status of the build.
Step 1: Configure Email Notifications
- Scroll down to Post-build Actions in your Jenkins job configuration.
- Click Add post-build action, select Editable Email Notification.
- In the Recipients field, enter the email addresses to receive notifications (e.g.,
youremail@example.com
). - In the Subject and Content sections, you can customize the email body using Jenkins variables like
${BUILD_STATUS}
,${BUILD_URL}
, etc.
Step 2: Configure SMTP Settings
- Go to Manage Jenkins → Configure System.
- Scroll to E-mail Notification, and configure the SMTP server (e.g., Gmail, or your organization’s SMTP server).
- Test the email configuration by sending a test message.
Once your above configuration is done. Click on save button.. and click on Build Now
Your Output will Look Like this..
7. Triggering the Job Automatically
You can configure Jenkins to trigger tests automatically whenever changes are pushed to the repository.
Step 1: Set Up GitHub Webhooks (for GitHub repositories)
- In your GitHub repository, go to Settings → Webhooks → Add webhook.
- Set the payload URL to
http://<your_jenkins_url>/github-webhook/
and select the events you want to trigger the job on (e.g., Push events). - This will allow Jenkins to automatically trigger the job when changes are pushed to the repository.
For more details How to Add Webhook in Jenkins Click Here
Step 2: Schedule Regular Builds (Optional)
- In the Build Triggers section, select Build periodically.
- Use a cron-style schedule to trigger the job regularly (e.g., nightly):
H 2 * * *
# This will trigger the job at 2 AM every day.
8. Troubleshooting
Common Issues:
- WebDriver issues: Make sure the correct version of ChromeDriver or GeckoDriver is installed for the version of the browser you’re using.
- Git credentials: If Jenkins can’t access a private repository, make sure the correct credentials are set in both the job and the global Jenkins configuration.
- Permissions: If you see permission issues when running the tests, check the file and directory permissions on the Jenkins machine.
- Refer Below Tutorial For More Information…
Conclusion
By following these steps, you should have a fully automated testing pipeline in Jenkins that uses Selenium and PyTest for web application testing. This setup will help you automatically run tests every time changes are pushed to your repository, with notifications and detailed logs to track the results.
- Dependency Track – End To End CI/CD Pipeline - November 29, 2024
- Dependency-track Jenkins Integration - November 27, 2024
- Jenkins Setup for PyTest + Selenium Automation Testing - November 27, 2024