πŸ’‘ If you like this website, please share it with your friends and network! πŸš€
Back to All Scenarios
Scenario 83 of 100
CI/CD
Intermediate

Running Selenium Automation Suites in Jenkins Pipelines

πŸ—οΈScenario Overview

Running Selenium Automation Suites in Jenkins Pipelines

Key Takeaways & Cheat Sheet

  • βœ“Maintain a clean Maven pom.xml configuration file
  • βœ“Define a standard Jenkinsfile using pipeline stages
  • βœ“Enable headless execution mode for target browser runners
  • βœ“Archive Extent and TestNG reports using Jenkins artifacts features

Short Direct Answer

To run tests in Jenkins, define a declarative `Jenkinsfile` that pulls the code, installs dependencies, and runs the Maven test suite using `mvn test`. Ensure your tests run in headless mode to work on non-GUI CI environments, and use Jenkins post-build actions to archive test reports.

⚠️ Senior Warning (Red Flag)

Do not run desktop browser GUI environments inside standard headless Jenkins servers. Without a virtual display frame buffer (like Xvfb) or headless arguments, your execution will throw a WebDriverException.

πŸ’‘ STAR Deep Dive Explanation & Pro Tip

Archiving report artifacts ensures that your visual execution reports and screenshots are preserved and accessible from the Jenkins build page.

SeleniumAutomation.java
Selenium 4 + Java
// βœ… Standard declarative Jenkinsfile pipeline
pipeline {
    agent any
    stages {
        stage('Checkout') {
            steps { checkout scm }
        }
        stage('Execute Tests') {
            steps {
                // Run Maven suite passing headless system property flags
                sh 'mvn test -Dheadless=true -Dbrowser=chrome'
            }
        }
    }
    post {
        always {
            // Archive HTML Extent reports and TestNG logs
            archiveArtifacts artifacts: 'target/*.html, target/surefire-reports/**/*', onlyIfSuccessful: false
            junit 'target/surefire-reports/**/*.xml'
        }
    }
}