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

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:

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):

  1. Update package lists
  2. Install Google Chrome
$ sudo apt update
$ sudo apt install google-chrome-stable

Install ChromeDriver:

  1. Download the ChromeDriver that matches your installed Chrome version from ChromeDriver.
  2. Extract the downloaded file and move it to /usr/local/bin (or any directory in your PATH):
  3. 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
Jenkins Automation Testing
Jenkins Setup for PyTest + Selenium Automation Testing

Now Selenium should be able to interact with Chrome on your Jenkins machine.

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:

  1. Create a virtual environment (recommended to avoid dependency issues): python3 -m venv myenv
  2. Activate the virtual environment: source myenv/bin/activate
  3. 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!

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

  1. Go to the Jenkins DashboardManage JenkinsManage Credentials(select the domain or click “(global)”)Add Credentials.
  2. Select Kind: Username with password:
    • Username: Enter your Git username.
    • Password: Enter your Git password (or personal access token if using services like GitHub).
  3. Click OK to save the credentials.

How to Create Git Access Token Click Here

Step 2: Add Git Repository to Jenkins Job

  1. In your Jenkins job configuration, scroll down to Source Code Management.
  2. Select Git.
  3. In the Repository URL field, add the Git repository URL (e.g., https://github.com/your-user/your-repo.git).
  4. In the Credentials dropdown, select the credentials you just added.
  5. 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.

Step 1: Create a New Freestyle Job

  1. In Jenkins, go to the Dashboard, click New Item.
  2. Enter a job name (e.g., Selenium_PyTest_Automation), select Freestyle project, and click OK.

Step 2: Configure Source Code Management

  1. In the job configuration page, scroll to Source Code Management.
  2. Select Git, enter your repository URL, and select the credentials (if private).
  3. 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 a requirements.txt file): pip install -r requirements.txt Run your PyTest tests: pytest --maxfail=1 --disable-warnings -q
# 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.

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

  1. Scroll down to Post-build Actions in your Jenkins job configuration.
  2. Click Add post-build action, select Editable Email Notification.
  3. In the Recipients field, enter the email addresses to receive notifications (e.g., youremail@example.com).
  4. 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

  1. Go to Manage JenkinsConfigure System.
  2. Scroll to E-mail Notification, and configure the SMTP server (e.g., Gmail, or your organization’s SMTP server).
  3. 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..

You can configure Jenkins to trigger tests automatically whenever changes are pushed to the repository.

Step 1: Set Up GitHub Webhooks (for GitHub repositories)

  1. In your GitHub repository, go to SettingsWebhooksAdd webhook.
  2. 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).
  3. 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)

  1. In the Build Triggers section, select Build periodically.
  2. 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.

Common Issues:

  1. WebDriver issues: Make sure the correct version of ChromeDriver or GeckoDriver is installed for the version of the browser you’re using.
  2. 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.
  3. Permissions: If you see permission issues when running the tests, check the file and directory permissions on the Jenkins machine.
  4. 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.

Mahesh Wabale
Latest posts by Mahesh Wabale (see all)

Leave a Comment