Integrating Selenium with Continuous Integration (CI) Tools

by | Python

Table of Contents

Setting Up Jenkins for Selenium Automated Tests with Python

Prerequisites

Jenkins installed on your machine or server.
Python and necessary Selenium libraries installed.
Git installed if not already.

Step-by-Step Guide

1. Install Selenium and WebDriver

pip install selenium

2. Configure Jenkins

Open Jenkins dashboard.
Click on New Item.
Enter an item name, select Freestyle project, and click OK.

3. Set Up Git Repository

Under Source Code Management, select Git.
Enter the repository URL and credentials if required.

4. Add Build Step

Under Build, click Add build step.
Select Execute shell (Linux/macOS) or Execute Windows batch command (Windows).

For Linux/macOS:

# Navigate to the directory containing your tests
cd /path/to/your/tests

# Run your tests
python -m unittest discover -s ./tests

For Windows:

:: Navigate to the directory containing your tests
cd /path/to/your/tests

:: Run your tests
python -m unittest discover -s ./tests

5. Schedule Build Triggers

Under Build Triggers, select Poll SCM.
Enter the schedule (* * * * * for every minute).

6. Save and Build

Save the configuration.
Click Build Now to manually trigger a build and run your Selenium tests.

Setting Up GitLab CI for Selenium Automated Tests with Python

Prerequisites

Python and necessary Selenium libraries installed.
GitLab repository with .gitlab-ci.yml file.

1. Install Selenium and WebDriver

pip install selenium

2. Create .gitlab-ci.yml in your project root

image: python:latest

before_script:
  - pip install selenium

stages:
  - test

test_job:
  stage: test
  script:
    - cd /path/to/your/tests
    - python -m unittest discover -s ./tests

3. Commit and Push

git add .gitlab-ci.yml
git commit -m "Add GitLab CI configuration for Selenium tests"
git push origin main

4. Run Pipeline

Go to your GitLab project.
Navigate to CI/CD > Pipelines to see the running 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:

pip install selenium

2. Download WebDriver

For Chrome:

Download the WebDriver for your specific Chrome version.
Extract the executable to a known path on your system.

3. Setting Up Python Code

Create a Python script test_script.py:

from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.common.keys import Keys
import time

# Path to WebDriver executable
driver_path = '/path/to/chromedriver'

# Initialize WebDriver
driver = webdriver.Chrome(executable_path=driver_path)

# Open a website
driver.get('https://www.example.com')

# Perform actions
search_box = driver.find_element(By.NAME, 'q')
search_box.send_keys('Selenium')
search_box.send_keys(Keys.RETURN)

# Wait for results
time.sleep(5)

# Close the browser
driver.quit()

4. Integrating with CI Tools (Short Overview)

Jenkins

Configure your Jenkins job to use a Python environment where Selenium is installed.
Add a step to run your test_script.py.

GitLab CI

Use a .gitlab-ci.yml file to automate the pipeline.
stages:
  - test

test_job:
  stage: test
  script:
    - pip install selenium
    - python test_script.py

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:

/my_project
  /tests
    test_sample.py
  /drivers
    chromedriver (or appropriate WebDriver)
  requirements.txt

File: requirements.txt

selenium==3.141.0
pytest==6.2.4

File: tests/test_sample.py

import pytest
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.common.keys import Keys

# Fixture to initialize and close the WebDriver
@pytest.fixture
def setup_driver():
    driver = webdriver.Chrome(executable_path='../drivers/chromedriver')
    yield driver
    driver.quit()

# Test function
def test_google_search(setup_driver):
    driver = setup_driver
    driver.get("https://www.google.com")

    search_box = driver.find_element(By.NAME, "q")
    search_box.send_keys("Selenium Python")
    search_box.send_keys(Keys.RETURN)

    assert "Selenium Python" in driver.title

CI Integration

Jenkins: Jenkinsfile

pipeline {
    agent any

    stages {
        stage('Install Dependencies') {
            steps {
                sh 'pip install -r requirements.txt'
            }
        }

        stage('Run Tests') {
            steps {
                sh 'pytest tests/'
            }
        }
    }

    post {
        always {
            junit 'tests/test-results.xml'
        }
    }
}

GitLab CI: .gitlab-ci.yml

image: python:3.8

services:
  - selenium/standalone-chrome

variables:
  DISPLAY_SIZE: 1920x1080

before_script:
  - pip install -r requirements.txt

stages:
  - test

test:
  script:
    - pytest tests/ --junit-xml=tests/test-results.xml
artifacts:
  when: always
  reports:
    junit: tests/test-results.xml

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:

pipeline {
    agent any

    stages {
        stage('Checkout') {
            steps {
                // Checkout source code
                git 'https://your-repo-url.git'
            }
        }

        stage('Set Up Python Environment') {
            steps {
                // Set up Python environment
                sh 'python3 -m venv venv'
                sh '. venv/bin/activate'
                sh 'pip install -r requirements.txt'
            }
        }

        stage('Run Tests') {
            steps {
                // Run Selenium tests
                sh '. venv/bin/activate && pytest tests/'
            }
        }

        stage('Post Steps') {
            steps {
                // Clean up after testing
                sh 'deactivate'
            }
        }
    }

    post {
        always {
            // Store test results
            junit 'reports/*.xml'
        }
    }
}

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:

stages:
  - test

variables:
  PIP_CACHE_DIR: "$CI_PROJECT_DIR/.cache/pip"

cache:
  paths:
    - .cache/pip

test:
  stage: test
  image: python:3.8
  before_script:
    - python -m venv venv
    - source venv/bin/activate
    - pip install -r requirements.txt
  script:
    - pytest tests/
  artifacts:
    when: always
    reports:
      junit:
        - Report.xml

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

pipeline {
    agent any
    stages {
        stage('Checkout') {
            steps {
                git 'https://your-repository-url.git'
            }
        }
        stage('Install Dependencies') {
            steps {
                sh 'pip install -r requirements.txt'
            }
        }
        stage('Run Selenium Tests') {
            steps {
                sh 'pytest path/to/your_selenium_tests'
            }
        }
    }
    post {
        always {
            junit 'path/to/selenium_test_results/*.xml'
        }
    }
}

GitLab CI

.gitlab-ci.yml

stages:
  - test

cache:
  paths:
    - .cache/pip

before_script:
  - pip install -r requirements.txt

selenium_tests:
  stage: test
  script:
    - pytest --junitxml=path/to/selenium_test_results.xml path/to/your_selenium_tests
  artifacts:
    reports:
      junit: path/to/selenium_test_results.xml
    paths:
      - path/to/selenium_test_results.xml

Assume the structure of your repository is:

??? Jenkinsfile
??? .gitlab-ci.yml
??? requirements.txt
??? path
    ??? to
        ??? your_selenium_tests
            ??? test_script.py

Execute and Monitor the CI Pipeline

1. Configure the Jenkins or GitLab CI Job to Execute Tests

Jenkins:

Create a new Jenkins job:
pipeline {
    agent any
    stages {
        stage('Checkout') {
            steps {
                git 'https://your-repo-url.git'
            }
        }
        stage('Install Dependencies') {
            steps {
                sh 'pip install -r requirements.txt'
            }
        }
        stage('Run Tests') {
            steps {
                sh 'pytest --junitxml=report.xml'
            }
        }
    }
    post {
        always {
            junit 'report.xml'
        }
    }
}

GitLab CI:

Add to your .gitlab-ci.yml:
stages:
  - test

test_job:
  stage: test
  script:
    - pip install -r requirements.txt
    - pytest --junitxml=report.xml
  artifacts:
    reports:
      junit: report.xml

2. Trigger the Pipeline

Jenkins:

Manual Trigger: Go to Jenkins job dashboard and click on “Build Now”.
Automated Trigger: Ensure you have set up a webhook from your Git repository to automatically trigger the build on code push.

GitLab CI:

Manual Trigger: Go to GitLab project, navigate to CI/CD > Pipelines, and click on “Run pipeline”.
Automated Trigger: By default, pipelines run automatically on push.

3. Monitor the Pipeline Execution

Jenkins:

Console Output: Navigate to the Jenkins job, and click on the build number to see console output.
Test Results: After the test run, view the “Test Result” link available on the build dashboard.
Notification: Configure “Post-build Actions” to send an email notification on build completion.

GitLab CI:

Pipeline Status: Go to CI/CD > Pipelines, and click on the pipeline ID to see detailed job logs.
Test Report: Access the “Tests” tab in the pipeline view for test results.
Notification: Set up GitLab Notifications in User Settings to receive emails on pipeline status.

4. Monitor and Maintain Test Health

Common Configurations:

Test Flakiness: Identify and fix flaky tests by analyzing test logs and trends.
Resource Utilization: Monitor server/load usage of Jenkins/GitLab runners and scale resources if necessary.
Regular Maintenance: Regularly update dependencies and clean up old artifacts to ensure optimal performance.

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:

import unittest
from selenium import webdriver
from selenium.webdriver.common.by import By
import HtmlTestRunner

class TestExample(unittest.TestCase):

    def setUp(self):
        self.driver = webdriver.Chrome()
        self.driver.implicitly_wait(10)

    def test_title(self):
        self.driver.get("http://example.com")
        self.assertEqual("Example Domain", self.driver.title)

    def test_example_element(self):
        self.driver.get("http://example.com")
        element = self.driver.find_element(By.TAG_NAME, 'h1')
        self.assertEqual(element.text, "Example Domain")

    def tearDown(self):
        self.driver.quit()

if __name__ == "__main__":
    unittest.main(testRunner=HtmlTestRunner.HTMLTestRunner(output='reports'))

Analyze Test Reports

Jenkins/GitLab CI Configuration:

# .gitlab-ci.yml or Jenkins pipeline configuration
stages:
  - test

test:
  stage: test
  script:
    - python -m unittest discover -s tests -p '*_test.py'
  artifacts:
    paths:
      - reports
    when: always

Example Script for Jenkins Freestyle Project:

Add build step “Execute shell”:

python -m unittest discover -s tests -p '*_test.py'

Post-build action “Publish HTML reports”:

HTML directory to archive: reports
Index page: index.html

Practical Code for GitLab CI Integration:

Ensure the runner has access to Chrome, WebDriver and necessary libraries.
Your project structure should include:
tests/ directory with your Selenium test scripts.
reports/ directory for HTMLTestRunner output.

Running Tests and Generating Reports

# Command line to execute tests and generate reports
python -m unittest discover -s tests -p '*_test.py'

Ensure HtmlTestRunner is installed:

pip install html-testRunner

Optimize and Maintain the CI System

Script to Optimize Jenkins CI System

pipeline {
    agent any

    environment {
        // Optimize resource allocation
        JAVA_OPTS = '-Xmx1g'
    }

    stages {
        stage('Clean Up Workspace') {
            steps {
                deleteDir()
            }
        }
        stage('Checkout Code') {
            steps {
                git 'https://github.com/your-repository.git'
            }
        }
        stage('Build and Test') {
            steps {
                // Use parallel stages to reduce build time
                parallel {
                    stage('Build') {
                        steps {
                            sh 'make build'
                        }
                    }
                    stage('Run Selenium Tests') {
                        steps {
                            sh 'python -m pytest tests/'
                        }
                    }
                }
            }
        }
        stage('Post Build Actions') {
            steps {
                // Archive build artifacts
                archiveArtifacts artifacts: '**/target/*.jar', allowEmptyArchive: true

                // Publish test results
                junit '**/target/test-*.xml'
            }
        }
        stage('Clean Up') {
            steps {
                // Efficient workspace cleanup to maintain disk space
                deleteDir()
            }
        }
    }

    post {
        always {
            // Notify team members about build status
            mail to: 'team@example.com',
                 subject: "Build ${currentBuild.fullDisplayName} completed",
                 body: "Check build logs at ${env.BUILD_URL}"
        }
    }
}

Script to Optimize GitLab CI System

stages:
  - cleanup
  - build
  - test
  - post

variables:
  JAVA_OPTS: "-Xmx1g"

cleanup_workspace:
  stage: cleanup
  script:
    - rm -rf *

build_job:
  stage: build
  script:
    - make build
  artifacts:
    paths:
      - target/*.jar

test_job:
  stage: test
  script:
    - python -m pytest tests/
  artifacts:
    reports:
      junit: target/test-*.xml

post_build_actions:
  stage: post
  script:
    - echo "Build completed"
  when: always
  after_script:
    # Notify team about the completion status
    - if [[ "$CI_JOB_STATUS" == "success" ]]; then echo "Build succeeded"; else echo "Build failed"; fi
    - # Use your preferred notification service here
    - curl -X POST -H 'Content-type: application/json' --data '{"text":"Build completed"}' https://hooks.slack.com/services/T00000000/B00000000/XXXXXXXXXXXXXXXXXXXXXXXX
    - rm -rf *

default:
  before_script:
    - echo "Setting up the environment"
  after_script:
    - echo "Cleaning up"

CRON Script to Maintain Jenkins CI System

node {
    stage('Clean Up Old Builds') {
        steps {
            script {
                // Allow only the last 10 builds to be kept
                def job = Jenkins.instance.getItem('your-job-name')
                job.builds.findAll {
                    it.number < job.builds[-10].number
                }.each {
                    it.delete()
                }
            }
        }
    }

    stage('Clean Up Old Workspace') {
        steps {
            script {
                // Clean up old workspace data periodically
                def workspacePath = '/var/jenkins_home/workspace/your-job-name'
                sh "find ${workspacePath} -mtime +7 -exec rm -rf {} \;"
            }
        }
    }
}

Generic Optimization Tips (Pseudocode)

1. Allocate resources optimally:
   Adjust memory and CPU usage based on your server and job needs.

2. Use parallel stages where possible:
   Parallelize jobs to reduce the overall build time.

3. Clean up workspaces:
   Remove unnecessary files after jobs complete.

4. Limit the number of old builds to be kept:
   Retain only a specific number of recent builds to save space.

5. Monitor and analyze logs:
   Regularly check CI server logs for any issues or optimizations.

6. Automate notifications:
   Notify team members of job completion using email or instant messaging.

These implementation instructions can be directly applied to optimize and maintain your CI system in real practice.

Related Posts