Running Selenium in Jenkins
Running Selenium in Jenkins means integrating Selenium automation tests with Jenkins so that browser-based tests can be executed automatically as part of a Continuous Integration and Continuous Delivery (CI/CD) process. Instead of running Selenium tests manually on a developer or tester's machine, Jenkins can automatically obtain the latest source code, build the project, execute the Selenium test suite, generate test reports, and make the results available to the team.
Jenkins is an automation server commonly used to automate build, test, and delivery workflows. Jenkins Pipeline allows these workflows to be defined as code, commonly through a Jenkinsfile stored with the project's source code. :contentReference[oaicite:0]{index=0}
In Selenium automation, Jenkins can execute TestNG, JUnit, Maven, Gradle, or other supported test frameworks. A typical workflow is: source code checkout → dependency installation → Selenium test execution → report generation → publishing test results.
Course Resource: Selenium Training | Register for Course Demo
1. What is Jenkins?
Jenkins is an open-source automation server used to automate software development activities such as building applications, running tests, and supporting delivery workflows.
For Selenium testers, Jenkins is particularly useful because automated browser tests can be triggered whenever new code is committed or according to a scheduled execution strategy.
- Automates test execution.
- Supports Continuous Integration.
- Supports Continuous Delivery pipelines.
- Can execute Maven and Gradle projects.
- Can integrate with Git repositories.
- Can publish test reports.
- Can execute Selenium tests on Jenkins agents.
- Can run tests on different environments.
- Can support parallel and distributed execution.
2. What is Selenium?
Selenium is a browser automation framework used to automate web applications. Selenium WebDriver allows automation code to interact with browsers such as Chrome, Firefox, and Edge.
A Selenium test can perform actions such as:
- Opening a browser.
- Navigating to a URL.
- Entering username and password.
- Clicking buttons.
- Searching for products.
- Validating page content.
- Submitting forms.
- Taking screenshots.
- Validating expected results.
3. Why Run Selenium in Jenkins?
Running Selenium tests through Jenkins allows automated tests to become part of the software delivery process.
- Tests can run automatically after code changes.
- Tests can run on a fixed schedule.
- Test execution does not depend on a tester manually starting the suite.
- Results can be stored with Jenkins builds.
- Failures can be detected early.
- Reports can be published automatically.
- Tests can run on dedicated Jenkins agents.
- Different browser and environment combinations can be automated.
4. Selenium + Jenkins Architecture
Developer
|
v
Git Repository
|
v
Jenkins
|
v
Checkout Source Code
|
v
Build Project
|
v
Install Dependencies
|
v
Execute Selenium Tests
|
v
Browser
|
v
Web Application
|
v
Assertions
|
v
Test Reports
|
v
Jenkins Build Result
5. Selenium Jenkins Execution Flow
A typical Selenium Jenkins execution follows these steps:
- Developer pushes code to a source-control repository.
- Jenkins detects or is triggered for the change.
- Jenkins checks out the source code.
- Jenkins prepares the build environment.
- Project dependencies are downloaded.
- Selenium tests are executed.
- The browser automation runs on the configured Jenkins agent.
- Assertions determine test results.
- Test reports are generated.
- Jenkins publishes or archives the results.
6. Prerequisites for Running Selenium in Jenkins
Before running Selenium tests through Jenkins, the execution environment should have the required tools and dependencies.
| Requirement | Purpose |
| Jenkins | Automation and CI/CD server |
| Java | Required for Java-based Selenium/TestNG projects and Jenkins itself |
| Git | Source-code management |
| Maven/Gradle | Build and dependency management |
| Selenium | Browser automation |
| Chrome/Firefox/Edge | Browser execution |
| TestNG/JUnit | Test execution framework |
| Jenkins Agent | Machine where builds and tests execute |
7. Jenkins Controller and Agent
Jenkins can distribute work between a controller and one or more agents. An agent provides an execution environment where Jenkins can run builds and tests.
Jenkins Controller
|
+-------------------+
| |
v v
Jenkins Agent 1 Jenkins Agent 2
| |
v v
Chrome Tests Firefox Tests
| |
+---------+---------+
|
v
Test Reports
For Selenium automation, agents are especially useful when different machines need different browsers, operating systems, or execution configurations.
8. Installing Jenkins
Jenkins can be installed using supported installation methods for the target operating system or can be run using containerized environments.
After installation, Jenkins provides a web interface where jobs and pipelines can be configured.
The exact installation process depends on the operating system and Jenkins deployment method, so the Java runtime and Jenkins version should be selected according to the current Jenkins documentation.
9. Creating a Jenkins Job
A Jenkins job defines what Jenkins should execute.
A basic Jenkins workflow can be created as a Pipeline project. Jenkins documentation supports defining pipelines through the Jenkins UI or through a Jenkinsfile stored in source control. Jenkins recommends keeping Pipeline definitions in source control for maintainability. :contentReference[oaicite:1]{index=1}
Typical steps are:
- Open Jenkins.
- Select New Item.
- Enter a project name.
- Select Pipeline.
- Configure the required source-control and build settings.
- Add the Pipeline script or configure Pipeline from SCM.
- Save the job.
- Run the build.
10. Jenkins Pipeline
Jenkins Pipeline allows the build and test workflow to be defined as code. Pipeline supports both Declarative and Scripted approaches. A Pipeline can be stored in a Jenkinsfile and loaded from source control. :contentReference[oaicite:2]{index=2}
A simple Declarative Pipeline looks like:
pipeline {
agent any
stages {
stage('Build') {
steps {
echo 'Building project'
}
}
stage('Test') {
steps {
echo 'Running tests'
}
}
}
}
11. What is a Jenkinsfile?
A Jenkinsfile is a text file that defines a Jenkins Pipeline. It can be stored alongside the application source code in a Git repository.
This approach is commonly known as Pipeline as Code.
Project
|
|-- src
|-- pom.xml
|-- testng.xml
|-- Jenkinsfile
|-- README.md
Keeping the Jenkinsfile with the source code allows the pipeline configuration to be version-controlled along with the automation framework.
12. Selenium Maven Project
A Maven-based Selenium project commonly contains the Selenium and test framework dependencies in pom.xml.
<dependencies>
<dependency>
<groupId>org.seleniumhq.selenium</groupId>
<artifactId>selenium-java</artifactId>
<version>LATEST_VERSION</version>
</dependency>
<dependency>
<groupId>org.testng</groupId>
<artifactId>testng</artifactId>
<version>LATEST_VERSION</version>
<scope>test</scope>
</dependency>
</dependencies>
In a real project, use versions that are explicitly selected and managed by the project rather than relying on a moving version label.
13. Running Selenium Tests with Maven
Maven can be used to build the project and execute tests.
mvn clean test
The clean phase removes previous build output, while test runs the configured tests.
14. Running Maven Tests from Jenkins
Jenkins can execute Maven commands as part of a Pipeline stage.
pipeline {
agent any
stages {
stage('Run Selenium Tests') {
steps {
sh 'mvn clean test'
}
}
}
}
For a Windows Jenkins agent, the command can be executed using bat:
pipeline {
agent any
stages {
stage('Run Selenium Tests') {
steps {
bat 'mvn clean test'
}
}
}
}
15. Linux vs Windows Jenkins Commands
| Environment | Jenkins Step |
| Linux/macOS | sh 'mvn clean test' |
| Windows | bat 'mvn clean test' |
| PowerShell environment | PowerShell command execution |
The command should match the operating system and shell available on the Jenkins agent.
16. Selenium Test Example
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.testng.Assert;
import org.testng.annotations.AfterMethod;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.Test;
public class LoginTest {
private WebDriver driver;
@BeforeMethod
public void setup() {
driver = new ChromeDriver();
driver.manage().window().maximize();
}
@Test
public void loginTest() {
driver.get("https://example.com/login");
driver.findElement(By.id("username"))
.sendKeys("testuser");
driver.findElement(By.id("password"))
.sendKeys("password");
driver.findElement(By.id("loginButton"))
.click();
Assert.assertTrue(
driver.getTitle().contains("Dashboard")
);
}
@AfterMethod
public void tearDown() {
if (driver != null) {
driver.quit();
}
}
}
17. TestNG with Jenkins
TestNG is frequently used with Selenium Java frameworks. Jenkins can invoke the Maven test lifecycle, which in turn executes the configured TestNG tests.
Jenkins
|
v
Maven
|
v
TestNG
|
v
Selenium WebDriver
|
v
Browser
|
v
Application
18. TestNG XML Suite
A TestNG XML file can define the tests that should be executed.
<!DOCTYPE suite SYSTEM "https://testng.org/testng-1.0.dtd">
<suite name="Selenium Suite">
<test name="Login Tests">
<classes>
<class name="tests.LoginTest"/>
</classes>
</test>
</suite>
The Maven configuration can be used to connect this suite to the project execution.
19. Jenkins Pipeline for TestNG
pipeline {
agent any
stages {
stage('Checkout') {
steps {
checkout scm
}
}
stage('Run TestNG Tests') {
steps {
sh 'mvn clean test'
}
}
}
}
20. Checkout Source Code from Git
Jenkins can obtain the Selenium framework from a Git repository before executing tests.
pipeline {
agent any
stages {
stage('Checkout') {
steps {
git branch: 'main',
url: 'https://github.com/example/selenium-framework.git'
}
}
stage('Test') {
steps {
sh 'mvn clean test'
}
}
}
}
The repository URL and branch should be replaced with the actual project configuration.
21. Complete Basic Selenium Jenkinsfile
pipeline {
agent any
stages {
stage('Checkout') {
steps {
checkout scm
}
}
stage('Build') {
steps {
sh 'mvn clean compile'
}
}
stage('Run Selenium Tests') {
steps {
sh 'mvn test'
}
}
}
post {
always {
echo 'Test execution completed'
}
success {
echo 'Selenium tests passed'
}
failure {
echo 'Selenium tests failed'
}
}
}
22. Jenkins Pipeline Stages
A Selenium CI pipeline can be divided into logical stages.
| Stage | Purpose |
| Checkout | Get source code |
| Build | Compile and prepare the project |
| Dependencies | Resolve project dependencies |
| Test | Execute Selenium tests |
| Report | Publish or archive results |
| Notification | Inform the team about results |
23. Headless Browser Execution
Jenkins agents may not have a graphical desktop environment. In such cases, browsers can often be configured to run in headless mode.
ChromeOptions options = new ChromeOptions();
options.addArguments("--headless=new");
options.addArguments("--window-size=1920,1080");
WebDriver driver = new ChromeDriver(options);
Headless execution allows browser automation to run without displaying the browser window on the Jenkins machine.
24. Why Headless Mode is Useful in Jenkins?
- Useful on machines without a graphical desktop.
- Suitable for CI environments.
- Can reduce dependencies on desktop sessions.
- Supports automated browser execution.
- Works well for unattended test execution.
25. WebDriver Management in Jenkins
Selenium browser automation requires a compatible browser and driver setup. Modern Selenium versions can manage driver binaries through Selenium Manager in many common configurations, but the Jenkins agent still needs a suitable browser/runtime environment.
For stable CI execution, teams should explicitly manage browser versions, operating-system dependencies, and test-environment compatibility.
26. Browser Configuration through Environment Variables
Instead of hard-coding the browser in Selenium code, Jenkins can provide an environment variable.
String browser = System.getenv("BROWSER");
if (browser == null || browser.isBlank()) {
browser = "chrome";
}
System.out.println("Browser: " + browser);
The framework can then use the value to initialize the required browser.
27. Parameterizing Selenium Tests in Jenkins
Jenkins parameters can be used to select values such as browser, environment, test suite, or execution mode.
pipeline {
agent any
parameters {
choice(
name: 'BROWSER',
choices: ['chrome', 'firefox', 'edge'],
description: 'Select browser'
)
choice(
name: 'ENVIRONMENT',
choices: ['qa', 'staging'],
description: 'Select environment'
)
}
stages {
stage('Run Tests') {
steps {
sh "mvn clean test -Dbrowser=${params.BROWSER} -Denv=${params.ENVIRONMENT}"
}
}
}
}
28. Passing Maven System Properties
Jenkins can pass system properties to Maven.
mvn clean test -Dbrowser=chrome -Denv=qa
Java code can retrieve these values using:
String browser = System.getProperty("browser");
String environment = System.getProperty("env");
System.out.println(browser);
System.out.println(environment);
29. Selenium Environment Configuration
Different environments may have different URLs.
String environment = System.getProperty("env");
String url;
if ("qa".equalsIgnoreCase(environment)) {
url = "https://qa.example.com";
} else if ("staging".equalsIgnoreCase(environment)) {
url = "https://staging.example.com";
} else {
url = "https://example.com";
}
driver.get(url);
30. Running Different Test Suites
Jenkins can be configured to execute different test suites depending on the build parameter or pipeline configuration.
mvn clean test -DsuiteXmlFile=testng-regression.xml
Other examples may include:
mvn clean test -DsuiteXmlFile=testng-smoke.xml
mvn clean test -DsuiteXmlFile=testng-regression.xml
mvn clean test -DsuiteXmlFile=testng-sanity.xml
31. Smoke Test Execution in Jenkins
A smoke suite contains a smaller set of tests used to quickly verify whether the main application functionality is working.
pipeline {
agent any
stages {
stage('Smoke Tests') {
steps {
sh 'mvn clean test -DsuiteXmlFile=testng-smoke.xml'
}
}
}
}
32. Regression Test Execution in Jenkins
Regression suites contain broader test coverage and can be executed after successful builds or according to the team's CI strategy.
pipeline {
agent any
stages {
stage('Regression Tests') {
steps {
sh 'mvn clean test -DsuiteXmlFile=testng-regression.xml'
}
}
}
}
33. Selenium Test Reports in Jenkins
After Selenium tests execute, the framework should generate machine-readable and human-readable results. Jenkins can then publish or archive those results using suitable reporting integrations.
Jenkins provides Pipeline-compatible steps through plugins, including steps for publishing certain Selenium HTML reports and standard test-result formats. :contentReference[oaicite:3]{index=3}
34. JUnit/TestNG XML Reports
Many Java test frameworks generate XML test results. Jenkins can consume supported test-result XML files and display test trends and failures.
target/
|
|-- surefire-reports/
| |-- TEST-LoginTest.xml
| |-- TEST-SearchTest.xml
| |-- TEST-CheckoutTest.xml
| |-- emailable-report.html
35. Publishing Test Results
A Jenkins Pipeline can publish test-result XML files when the appropriate Jenkins test-result functionality is available.
post {
always {
junit 'target/surefire-reports/*.xml'
}
}
This allows test execution results to remain visible in the Jenkins build information.
36. Publishing HTML Reports
If the automation framework produces an HTML report, Jenkins can publish it through a suitable HTML reporting plugin or archive it as a build artifact.
post {
always {
archiveArtifacts artifacts: 'test-output/**',
allowEmptyArchive: true
}
}
The exact publishing step depends on the reporting plugin and report format installed in the Jenkins environment.
37. Screenshots on Test Failure
Screenshots are extremely useful when Selenium tests fail in Jenkins because the tester may not have direct access to the browser session.
public void takeScreenshot(String fileName) {
File source = ((TakesScreenshot) driver)
.getScreenshotAs(OutputType.FILE);
Path destination = Paths.get(
"screenshots",
fileName + ".png"
);
try {
Files.createDirectories(destination.getParent());
Files.copy(
source.toPath(),
destination,
StandardCopyOption.REPLACE_EXISTING
);
} catch (IOException e) {
throw new RuntimeException(e);
}
}
38. Archiving Selenium Screenshots
After execution, screenshots can be archived by Jenkins so that they remain available with the build.
post {
always {
archiveArtifacts artifacts: 'screenshots/**/*.png',
allowEmptyArchive: true
}
}
39. Failure Handling
Jenkins should clearly identify whether the Selenium test stage succeeded or failed.
post {
success {
echo 'All tests completed successfully'
}
failure {
echo 'One or more tests failed'
}
always {
echo 'Publishing test artifacts'
}
}
40. Jenkins Console Output
The Jenkins console output shows the commands and messages produced during the build.
[Pipeline] stage
[Pipeline] { (Run Selenium Tests)
[Pipeline] sh
+ mvn clean test
...
Tests run: 20, Failures: 1, Errors: 0
...
[Pipeline] }
Finished: FAILURE
Console output is useful for troubleshooting build, dependency, browser, driver, and test-execution problems.
41. Scheduled Selenium Execution
Jenkins can run Selenium suites on a schedule. Scheduled execution is useful for nightly regression testing or periodic validation.
A Jenkins cron trigger can be configured according to the team's required schedule.
pipeline {
agent any
triggers {
cron('H 2 * * *')
}
stages {
stage('Regression') {
steps {
sh 'mvn clean test'
}
}
}
}
42. Triggering Tests After Code Changes
A Selenium pipeline can be triggered after source-code changes through the configured source-control integration, webhook, or polling strategy.
Developer
|
v
Git Commit
|
v
Repository
|
v
Jenkins Trigger
|
v
Selenium Pipeline
|
v
Test Result
43. Jenkins and GitHub
A common Selenium CI workflow uses GitHub or another Git-based repository as the source-control system. Jenkins checks out the project and executes its pipeline.
GitHub
|
v
Jenkins
|
v
Checkout
|
v
Maven
|
v
Selenium
|
v
Reports
The exact webhook and credential configuration depends on the Jenkins and Git hosting setup.
44. Pipeline from SCM
Jenkins can load a Jenkinsfile directly from source control. The Jenkins documentation describes the Pipeline script from SCM approach, where Jenkins checks out the repository and loads the specified Jenkinsfile. :contentReference[oaicite:4]{index=4}
This provides several advantages:
- Pipeline configuration is version controlled.
- Changes to the pipeline can be reviewed with code changes.
- The automation workflow stays close to the project.
- Teams can reproduce pipeline configurations.
45. Jenkins Credentials
Automation pipelines may need credentials for source-control repositories, cloud platforms, databases, test environments, or other protected systems.
Credentials should be managed through Jenkins' credential facilities or an approved secret-management solution rather than placing passwords or tokens directly inside the Jenkinsfile.
pipeline {
agent any
stages {
stage('Secure Step') {
steps {
withCredentials([
usernamePassword(
credentialsId: 'test-environment',
usernameVariable: 'TEST_USER',
passwordVariable: 'TEST_PASSWORD'
)
]) {
sh 'echo "Running secure test step"'
}
}
}
}
}
Sensitive values should not be printed into Jenkins console logs.
46. Selenium Grid with Jenkins
Selenium Grid can be used when browser tests need to execute remotely across different environments. Jenkins can orchestrate test execution while Selenium Grid or another supported remote-browser infrastructure provides browser sessions.
Jenkins
|
v
Selenium Test Suite
|
v
Remote WebDriver
|
+------------------+
| |
v v
Chrome Node Firefox Node
| |
+--------+---------+
|
v
Web Application
Jenkins also has a Selenium plugin that can integrate Jenkins infrastructure with Selenium Grid, although teams should review the current plugin status and security advisories before choosing it. :contentReference[oaicite:5]{index=5}
47. RemoteWebDriver Example
URL gridUrl = new URL(
"http://selenium-grid:4444"
);
WebDriver driver = new RemoteWebDriver(
gridUrl,
new ChromeOptions()
);
The actual Grid URL, authentication, capabilities, and deployment model depend on the test infrastructure.
48. Cross-Browser Testing in Jenkins
Jenkins can execute Selenium tests against multiple browsers by using build parameters, matrix-style execution, separate stages, or a Selenium Grid infrastructure.
Browser Selection
|
+----------+----------+
| | |
v v v
Chrome Firefox Edge
| | |
+----------+----------+
|
v
Test Results
49. Cross-Browser Pipeline Example
pipeline {
agent any
stages {
stage('Chrome Tests') {
steps {
sh 'mvn clean test -Dbrowser=chrome'
}
}
stage('Firefox Tests') {
steps {
sh 'mvn clean test -Dbrowser=firefox'
}
}
stage('Edge Tests') {
steps {
sh 'mvn clean test -Dbrowser=edge'
}
}
}
}
50. Parallel Browser Execution
When the framework and infrastructure are designed for parallel execution, browser tests can be executed concurrently.
pipeline {
agent any
stages {
stage('Cross Browser Tests') {
parallel {
stage('Chrome') {
steps {
sh 'mvn test -Dbrowser=chrome'
}
}
stage('Firefox') {
steps {
sh 'mvn test -Dbrowser=firefox'
}
}
stage('Edge') {
steps {
sh 'mvn test -Dbrowser=edge'
}
}
}
}
}
}
Parallel Selenium execution requires isolated WebDriver sessions, appropriate resources, and thread-safe test code.
51. Selenium with Page Object Model in Jenkins
The Page Object Model can be combined with Jenkins without changing the basic POM architecture.
Jenkins
|
v
TestNG Test
|
v
Page Object
|
v
WebDriver
|
v
Browser
|
v
Application
Jenkins executes the test framework, while POM continues to organize Selenium page interaction logic.
52. Selenium Test Data in Jenkins
Test data can be supplied through configuration files, environment variables, Maven properties, Jenkins parameters, databases, or other approved data sources.
Jenkins Parameter
|
v
Maven Property
|
v
Java Test Framework
|
v
Data Provider
|
v
Selenium Test
53. Running Data-Driven Selenium Tests
TestNG Data Providers can execute the same Selenium test with multiple data sets.
@DataProvider(name = "users")
public Object[][] users() {
return new Object[][] {
{"admin", "admin123"},
{"manager", "manager123"},
{"employee", "employee123"}
};
}
@Test(dataProvider = "users")
public void loginTest(String username, String password) {
System.out.println(username);
}
Jenkins can execute the complete data-driven suite using Maven or another configured build command.
54. Selenium Test Reports and Build History
One of Jenkins' useful features is that build results can be retained and compared over time. Teams can use build history to investigate when failures started occurring and whether subsequent builds continue to fail.
Build #101 - SUCCESS
Build #102 - SUCCESS
Build #103 - FAILURE
Build #104 - FAILURE
Build #105 - SUCCESS
55. Notifications After Selenium Execution
Jenkins can be integrated with notification mechanisms so that teams can be informed when a build or test stage completes.
Common notification approaches include:
- Email notifications.
- Chat integrations.
- Webhook-based notifications.
- Issue-tracking integrations.
- Custom notification scripts.
The exact implementation depends on the plugins and external services configured in Jenkins.
56. Handling Test Failures in CI
A Selenium test failure should normally make the relevant CI stage visible as failed while still allowing diagnostic artifacts such as screenshots and reports to be collected.
Test Failure
|
+--------------------+
| |
v v
Build Status Diagnostics
| |
v +-- Screenshot
FAILURE +-- Logs
+-- Reports
57. Retry Failed Selenium Tests
Retries can sometimes be useful for diagnosing transient infrastructure problems, but they should not be used to hide genuine application failures.
Retry Strategy
|
v
Test Failure
|
v
Retry
|
+--------+
| |
v v
Pass Fail
| |
v v
Success Investigate
A good framework should distinguish genuine product failures from temporary infrastructure or environment problems.
58. Jenkins Workspace
A Jenkins workspace is the directory where Jenkins performs operations such as checking out source code, building the project, and generating test artifacts.
Jenkins Workspace
|
|-- src
|-- pom.xml
|-- testng.xml
|-- screenshots
|-- target
| |-- surefire-reports
| |-- test-output
| |-- screenshots
59. Cleaning the Workspace
Old files can sometimes affect test execution. A pipeline can clean the workspace before or after execution when appropriate.
pipeline {
agent any
stages {
stage('Clean Workspace') {
steps {
deleteDir()
}
}
stage('Checkout') {
steps {
checkout scm
}
}
stage('Test') {
steps {
sh 'mvn clean test'
}
}
}
}
60. Jenkins Environment Variables
Jenkins provides environment variables that can be useful for identifying the build, workspace, job, and other execution information.
pipeline {
agent any
stages {
stage('Environment') {
steps {
echo "Build: ${env.BUILD_NUMBER}"
echo "Job: ${env.JOB_NAME}"
echo "Workspace: ${env.WORKSPACE}"
}
}
}
}
61. Browser and Environment Matrix
Large Selenium frameworks may need to validate combinations of browsers and environments.
| Environment | Chrome | Firefox | Edge |
| QA | Yes | Yes | Yes |
| Staging | Yes | Yes | Optional |
| Production Smoke | Yes | Optional | Optional |
The exact matrix should be based on the project's testing requirements and available infrastructure.
62. Jenkins and Docker for Selenium
Docker can be used to create consistent execution environments for Jenkins agents and browser automation infrastructure.
Jenkins
|
v
Docker Agent
|
+----------------+
| |
v v
Test Container Browser/Grid
| |
+--------+-------+
|
v
Test Results
Containerized execution can improve environment consistency, although browser containers, drivers, display requirements, networking, and resource limits must be configured correctly.
63. Jenkins Agent for Selenium
A dedicated Jenkins agent can be configured with the software required for Selenium execution.
Selenium Jenkins Agent
|
|-- Java
|-- Maven
|-- Git
|-- Chrome
|-- Firefox
|-- Edge
|-- Selenium Framework
|-- Test Data
Keeping the required software consistent on agents reduces environment-related failures.
64. Common Selenium Jenkins Failures
| Problem | Possible Cause |
| Browser not starting | Browser or runtime configuration problem |
| Driver error | Browser/driver compatibility or environment issue |
| Element not found | Locator, timing, or application issue |
| Session creation failure | Browser/driver/Grid configuration issue |
| Maven command failure | Java/Maven/project configuration problem |
| Tests not discovered | TestNG/Maven configuration problem |
| Report missing | Incorrect report path or publishing configuration |
| Permission error | Jenkins agent or filesystem permissions |
| Network error | Agent/application/network configuration |
65. Troubleshooting Selenium in Jenkins
- Check Jenkins console output.
- Verify Java version and configuration.
- Verify Maven installation.
- Verify the browser is installed.
- Verify browser execution permissions.
- Check Selenium and browser compatibility.
- Check the Jenkins workspace.
- Check application URL accessibility from the Jenkins agent.
- Check test configuration.
- Check report and screenshot paths.
- Check credentials and environment variables.
- Check Selenium Grid connectivity if remote execution is used.
66. Common Mistakes
- Running GUI-dependent browsers on agents without configuring headless or display support.
- Hard-coding environment-specific URLs.
- Hard-coding passwords and API keys.
- Sharing WebDriver instances between parallel tests.
- Ignoring screenshots and logs for failures.
- Using unstable test data.
- Not cleaning stale test artifacts.
- Not publishing test results.
- Using outdated Jenkins plugins without reviewing compatibility and security information.
- Assuming a local Selenium environment will behave exactly like a Jenkins agent.
67. Best Practices for Running Selenium in Jenkins
- Keep the Jenkinsfile in source control.
- Use a dedicated and reproducible test environment.
- Use Page Object Model for maintainable Selenium code.
- Use explicit waits instead of unnecessary fixed sleeps.
- Keep credentials in secure credential management.
- Parameterize browser and environment configuration.
- Generate and publish test reports.
- Capture screenshots for important failures.
- Keep tests independent where possible.
- Use parallel execution only when the framework is thread-safe.
- Keep Jenkins agents properly configured.
- Monitor browser and dependency compatibility.
- Review plugins and integrations for current maintenance and security status.
- Keep the CI pipeline simple and understandable.
68. Complete Practical Selenium Jenkins Project
A practical Selenium Jenkins project can be organized as follows:
selenium-automation
|
|-- src
| |-- test
| |-- java
| |-- tests
| | |-- LoginTest.java
| | |-- SearchTest.java
| | |-- CheckoutTest.java
| |
| |-- pages
| | |-- LoginPage.java
| | |-- SearchPage.java
| | |-- CheckoutPage.java
| |
| |-- utilities
| |-- DriverFactory.java
| |-- ConfigReader.java
| |-- ScreenshotUtil.java
|
|-- testng.xml
|-- pom.xml
|-- Jenkinsfile
|-- README.md
69. Complete Practical Jenkinsfile
pipeline {
agent any
parameters {
choice(
name: 'BROWSER',
choices: ['chrome', 'firefox', 'edge'],
description: 'Select browser'
)
choice(
name: 'ENVIRONMENT',
choices: ['qa', 'staging'],
description: 'Select environment'
)
}
stages {
stage('Checkout') {
steps {
checkout scm
}
}
stage('Build') {
steps {
sh 'mvn clean compile'
}
}
stage('Run Selenium Tests') {
steps {
sh "mvn test -Dbrowser=${params.BROWSER} -Denv=${params.ENVIRONMENT}"
}
}
}
post {
always {
junit 'target/surefire-reports/*.xml'
archiveArtifacts(
artifacts: 'screenshots/**/*.png',
allowEmptyArchive: true
)
}
success {
echo 'Selenium execution completed successfully'
}
failure {
echo 'Selenium execution failed'
}
}
}
70. Real-World Selenium Jenkins Architecture
Git Repository
|
v
Jenkins Pipeline
|
+-------------+-------------+
| |
v v
Build Project Environment Setup
| |
+-------------+-------------+
|
v
Test Execution
|
v
TestNG / JUnit
|
v
Selenium WebDriver
|
+-------------+-------------+
| | |
v v v
Chrome Firefox Edge
| | |
+-------------+-------------+
|
v
Web Application
|
v
Assertions
|
+-------------+-------------+
| | |
v v v
Reports Screenshots Logs
| | |
+-------------+-------------+
|
v
Jenkins Build Result
71. Jenkins Pipeline with Report Collection
pipeline {
agent any
stages {
stage('Checkout') {
steps {
checkout scm
}
}
stage('Execute Tests') {
steps {
sh 'mvn clean test'
}
}
}
post {
always {
junit 'target/surefire-reports/*.xml'
archiveArtifacts(
artifacts: 'target/**',
allowEmptyArchive: true
)
}
}
}
This pattern ensures that diagnostic files can be collected even when the test stage fails.
72. Selenium Jenkins CI/CD Flow
Code Commit
|
v
Jenkins Trigger
|
v
Checkout
|
v
Build
|
v
Dependency Resolution
|
v
Test Environment
|
v
Selenium Tests
|
v
TestNG
|
v
Assertions
|
+----------------+
| |
v v
PASS FAIL
| |
+--------+-------+
|
v
Reports / Logs
|
v
Jenkins Result
|
v
Team Notification
73. Advantages of Running Selenium in Jenkins
- Automation: Tests can execute automatically.
- Continuous Integration: Selenium tests can become part of the CI workflow.
- Early Feedback: Failures can be detected soon after changes are introduced.
- Centralized Execution: Test execution can be managed from Jenkins.
- Reporting: Build results and test artifacts can be collected.
- Scheduling: Regression suites can be executed periodically.
- Cross-Browser Testing: Multiple browsers can be configured.
- Scalability: Jenkins agents and remote browser infrastructure can support larger suites.
- CI/CD Integration: Tests can be integrated with broader software delivery pipelines.
74. Limitations and Challenges
- Jenkins requires administration and maintenance.
- Selenium tests require a stable execution environment.
- Browser versions can introduce compatibility issues.
- GUI-dependent execution can require additional configuration.
- Parallel execution increases infrastructure requirements.
- Flaky tests can reduce confidence in pipeline results.
- Remote browser infrastructure requires network configuration.
- Incorrect credentials or permissions can stop a pipeline.
- Plugin compatibility should be monitored.
75. Jenkins vs Manual Selenium Execution
| Manual Execution | Jenkins Execution |
| Tester starts tests manually | Tests can be triggered automatically |
| Usually runs on tester machine | Runs on configured Jenkins agents |
| Results may need manual collection | Results can be published automatically |
| Limited scheduling | Scheduled execution is possible |
| Less suitable for continuous integration | Designed to integrate with CI workflows |
| Environment may differ between testers | Execution environment can be standardized |
76. Jenkins and Selenium Interview Questions
1. What is Jenkins?
Jenkins is an automation server used to automate activities such as building, testing, and delivering software.
2. Why is Jenkins used with Selenium?
Jenkins can automatically execute Selenium test suites as part of CI/CD workflows.
3. What is a Jenkinsfile?
A Jenkinsfile is a file containing Jenkins Pipeline code that defines the automated workflow.
4. What is Jenkins Pipeline?
Jenkins Pipeline is a collection of plugins and DSL features that allows delivery workflows to be defined as code. :contentReference[oaicite:6]{index=6}
5. How can Selenium tests be executed in Jenkins?
They can be executed through Maven, Gradle, direct test commands, or other build mechanisms configured in the Jenkins Pipeline.
6. What command is commonly used for a Maven Selenium project?
mvn clean test
7. What is a Jenkins agent?
A Jenkins agent is an execution machine where Jenkins can run builds and tests.
8. What is headless browser execution?
Headless execution runs a browser without displaying its graphical user interface.
9. Why is headless mode useful in Jenkins?
It is useful when the Jenkins execution environment does not provide a graphical desktop session.
10. How can browser selection be parameterized?
Jenkins parameters and Maven system properties can be used to pass the browser value to the Selenium framework.
11. How can Selenium reports be published?
Supported Jenkins test-result steps, HTML report plugins, and archived artifacts can be used depending on the report format.
12. Why are screenshots useful in Jenkins?
Screenshots provide visual evidence of the browser state when a test fails.
13. Can Jenkins run Selenium tests in parallel?
Yes. Jenkins can run parallel pipeline branches or distribute work across agents, provided the Selenium framework and infrastructure are designed for concurrency.
14. Can Jenkins run tests on Selenium Grid?
Yes. Selenium tests can connect to remote browser infrastructure such as Selenium Grid.
15. What is CI?
Continuous Integration is a development practice where changes are integrated frequently and automated validation is performed.
16. What is CD?
Continuous Delivery or Continuous Deployment refers to automated processes that move software through later delivery or deployment stages according to the team's workflow.
17. How can Jenkins trigger Selenium tests?
Triggers can include source-control events, scheduled builds, manual execution, upstream jobs, and other Jenkins-supported mechanisms.
18. Why should credentials not be hard-coded?
Hard-coded credentials can expose sensitive information. Jenkins credentials or an approved secret-management solution should be used instead.
19. What should be checked when Selenium works locally but fails in Jenkins?
Check browser availability, Java/Maven configuration, environment variables, filesystem permissions, network access, browser-driver compatibility, display/headless configuration, and Jenkins-agent differences.
20. What is the benefit of storing Jenkinsfile in Git?
It allows the pipeline configuration to be version-controlled, reviewed, and maintained together with the automation project.
77. Quick Reference Table
| Concept | Description |
| Jenkins | Automation server for CI/CD workflows |
| Selenium | Browser automation framework |
| Jenkinsfile | Pipeline definition stored as code |
| Pipeline | Automated sequence of build/test/delivery stages |
| Agent | Machine that executes Jenkins work |
| Maven | Build and dependency management tool |
| TestNG | Java testing framework |
| Headless Browser | Browser execution without a visible UI |
| RemoteWebDriver | Connects Selenium tests to a remote browser |
| Jenkins Parameter | Allows runtime configuration values |
| JUnit XML | Machine-readable test-result format supported by Jenkins |
| Artifact | File produced or collected during a build |
| Selenium Grid | Infrastructure for remote/distributed browser execution |
78. Learning Roadmap for Running Selenium in Jenkins
- Learn Selenium WebDriver fundamentals.
- Learn Java programming for Selenium.
- Learn TestNG or JUnit.
- Learn Maven.
- Build a Selenium automation framework.
- Learn Page Object Model.
- Learn Git and source-control workflows.
- Install and understand Jenkins.
- Learn Jenkins jobs and Pipeline.
- Learn Jenkinsfile syntax.
- Connect Jenkins with Git.
- Run Maven Selenium tests from Jenkins.
- Configure browser and environment parameters.
- Generate and publish reports.
- Capture screenshots and logs.
- Learn Jenkins agents.
- Learn Selenium Grid and remote execution.
- Configure parallel execution.
- Integrate Selenium with CI/CD.
- Build a complete production-style automation pipeline.
79. Practical Exercises
- Create a basic Selenium Maven project.
- Create a LoginTest using Selenium WebDriver.
- Run the test locally using Maven.
- Create a Jenkins Pipeline project.
- Connect Jenkins to a Git repository.
- Create a Jenkinsfile.
- Execute
mvn clean test through Jenkins.
- Configure Chrome headless execution.
- Add browser selection as a Jenkins parameter.
- Add environment selection as a Jenkins parameter.
- Publish test-result XML files.
- Archive Selenium screenshots.
- Run a smoke test suite.
- Run a regression suite.
- Configure scheduled execution.
- Configure cross-browser execution.
- Connect tests to Selenium Grid.
- Configure parallel execution.
- Add failure notifications.
- Build a complete Selenium CI/CD pipeline.
80. Final Practical Example
Suppose a team has a Selenium framework containing login, search, checkout, and registration tests. The source code is stored in Git, and Jenkins is configured to execute the tests whenever the project is updated.
Developer Push
|
v
Git Repository
|
v
Jenkins Trigger
|
v
Checkout Source
|
v
Maven Build
|
v
TestNG
|
v
Selenium WebDriver
|
+-------------------+
| | |
v v v
Login Search Checkout
| | |
+---------+---------+
|
v
Assertions
|
+--------+--------+
| |
v v
Reports Screenshots
| |
+--------+--------+
|
v
Jenkins Result
This architecture allows the team to repeatedly execute the same Selenium automation framework through a controlled CI environment.
81. Summary
Running Selenium in Jenkins is an important skill for automation testers because it connects browser automation with continuous integration and delivery workflows.
Jenkins can obtain Selenium source code from a repository, prepare the project, execute Maven or Gradle commands, run TestNG or JUnit tests, collect screenshots and reports, and expose the results through build history and pipeline output.
A strong Selenium Jenkins implementation normally combines Selenium WebDriver + Java + TestNG/JUnit + Maven/Gradle + Git + Jenkins + Page Object Model + reporting + CI/CD.
For larger automation projects, Jenkins agents, headless browsers, Selenium Grid, parameterized execution, parallel testing, secure credentials, and reusable framework components can be added as the project grows.
Jenkins Pipeline supports defining these workflows as code, while Jenkins' Pipeline ecosystem provides many plugin-based steps for integrating reports and other tools. :contentReference[oaicite:7]{index=7}
82. Course Resources
Learn more about Selenium automation, TestNG, Page Object Model, Jenkins, and software testing:
Final Takeaway: Jenkins transforms Selenium automation from a manually executed test activity into an automated CI workflow. By combining source control, Jenkins Pipeline, Maven, TestNG, Selenium WebDriver, reporting, screenshots, and controlled execution environments, teams can repeatedly validate web applications and receive consistent feedback from automated test runs.