Last Updated On
In the previous tutorial, I discussed the Jenkins pipeline. This tutorial will discuss the steps to create the Jenkins pipeline for Selenium tests. This is an important step in CI/CD.
Table of Contents
Prerequisite
Jenkins was installed and started on the computer.
Implementation Steps
Step 1: Create a new pipeline project
- Give the Name of the project – Selenium_PipelineDemo.
- Click on the pipeline project.
- Click on the OK button.
![](https://qaautomation.expert/wp-content/uploads/2023/01/image-10.png?w=1200)
In the General section, enter the project description in the Description box.
Step 2: Scroll down to Pipeline
From the Definition field, choose the “Pipeline script from SCM” option. This option instructs Jenkins to obtain your Pipeline from Source Control Management (SCM), which will be your locally cloned Git repository.
From the SCM field, choose Git.
The “Repositories” section contains the “Repository URL” and “Credentials“.
In the Repository URL field, specify the directory path of the GitLab/GitHub project.
In the Credentials field, specify the username and password needed to log in to GitLab/GitHub.
In this case, I have the project present in GitLab and using it.
![](https://qaautomation.expert/wp-content/uploads/2023/01/image-11.png?w=1200)
Step 3: Create Jenkinsfile
Create and save a new text file with the name Jenkinsfile at the root of the project in the GitLab repository. Here, we are using the Selenium project with TestNG. To learn more about the Integration of Selenium with TestNG, please refer to this tutorial – Integration of Selenium and TestNG.
![](https://qaautomation.expert/wp-content/uploads/2023/01/image-12.png?w=1200)
For this tutorial, we are using Declarative syntax. The sample example is given below:
Here, I have used emailable-report.html, you can also use index.html and that report will be published.
pipeline {
agent any
stages {
stage('Test') {
steps {
bat "mvn -D clean test"
}
post {
// If Maven was able to run the tests, even if some of the test
// failed, record the test results and archive the jar file.
success {
publishHTML([
allowMissing: false,
alwaysLinkToLastBuild: false,
keepAll: false,
reportDir: 'target/surefire-reports/',
reportFiles: 'emailable-report.html',
reportName: 'HTML Report',
reportTitles: '',
useWrapperFileDirectly: true])
}
}
}
}
}
Step 4: Specify branches to build a section under Repositories
- Branch Specifier – */master (This is my main branch)
- ScriptPath – Jenkinsfile
![](https://qaautomation.expert/wp-content/uploads/2023/01/image-13.png?w=1200)
Click on the Apply and Save buttons.
We have created a new Maven project “Selenium_PipelineDemo” with the configuration to run the Selenium Test with TestNG.
![](https://qaautomation.expert/wp-content/uploads/2023/01/image-18.png?w=989)
Step 5: Execute the tests
Let’s execute it now by clicking on the “Build Now” button.
![](https://qaautomation.expert/wp-content/uploads/2023/01/image-19.png?w=284)
Right-click on Build Number (here in my case it is #11) and click on Console Output to see the result.
![](https://qaautomation.expert/wp-content/uploads/2023/01/image-20.png?w=286)
Below is the test execution summary.
![](https://qaautomation.expert/wp-content/uploads/2023/01/image-21.png?w=914)
Step 6: Pipeline Steps
Once the execution is completed, and we want to see the Pipeline Steps, click on the Pipeline Steps mentioned on the left side of the page.
![](https://qaautomation.expert/wp-content/uploads/2023/01/image-24.png?w=1200)
Step 7: View the Report
Once the execution is completed, go back to “Selenium_PipelineDemo”. We can see below that the HTML Report is generated.
![](https://qaautomation.expert/wp-content/uploads/2023/01/image-23.png?w=911)
We could see a link to view ‘HTML Reports’. Click on the HTML Reports. It displays the emailable-report.html Report.
![](https://qaautomation.expert/wp-content/uploads/2023/01/image-22.png?w=1200)
Tip: If you don’t see the Report UI intact, then you need to configure a simple groovy script. For that, go to Dashboard–>Manage Jenkins–>Script Console and add the script as:
System.setProperty("hudson.model.DirectoryBrowserSupport.CSP","")
![](https://qaautomation.expert/wp-content/uploads/2022/12/image-97.png?w=729)
How to get the pipeline syntax?
If you don’t know the syntax for the pipeline, there is an option “Pipeline Syntax” in the UI as shown below.
![](https://qaautomation.expert/wp-content/uploads/2023/01/image-14.png?w=1200)
Select any of the suitable options. In my case, I have used Publish HTML reports.
![](https://qaautomation.expert/wp-content/uploads/2023/01/image-15.png?w=963)
Specify the name of the Report in the Index page[s]and Report Title such as HTML Report and click on the “Generate Pipeline Script”.
![](https://qaautomation.expert/wp-content/uploads/2023/01/image-16.png?w=1200)
Below is the sample syntax.
System.setProperty("hudson.model.DirectoryBrowserSupport.CSP","")
![](https://qaautomation.expert/wp-content/uploads/2023/01/image-17.png?w=993)
We are done! Congratulations on making it through this tutorial and hope you found it useful! Happy Learning!!
can you share the repo
LikeLike
I can’t provide access to my GitLab repo. But I have the same code in my public GitHub repo, but will generate an Allure Report. If you want, you can change the Allure Report part with the TestNG Report part and it should work. – https://github.com/vibssingh/Selenium-TestNG
LikeLike