Creating out First project to Run Selenium script in Jenkins

Monil Joshi
8 min readFeb 11, 2022

--

In previous article we saw Jenkins installation process and major feature Jenkins have. You can read that article here

Let’s get started with Jenkins

in this article we will see how can we execute out automation code using Jenkins. First we need to create a Jenkins Job (a project in Jenkins). Then configure our code in the Jobs for execution. Later we schedule that job so that we do not have to trigger it manually.

Here we are creating Free Style project. Follow below steps to create a project.

  1. Login to Jenkins.
  2. Click on New Item.
  3. You will have to provide name to your project. Lets name it as Selenium_First_Test
  4. Now you have to select project type. As we are creating freestyle project click on Freestyle Project.
  5. Click on OK.

6. You will land on configuration page.

7. You can see several tabs or you can just scroll down to move around all the options. We will see all of them one by one.

  • General : These are general settings respective to your project like discard older builds, Add parameters to the project, Disable same project.
Options in General Tab
  • Source code management: You can setup source code management tool. It could be Git, SVN. You can integrate version control with the Jenkins project. If you are not using any version control and your code is in same machine you can select None.

NEED TO ADD SCREENSHOT

  • Build Trigger: You can achieve full automation using this option. Here you can set up script trigger points and also setup schedular to auto trigger your execution. In this case no need of manual intervention needed to execute automation scripts. You can schedule your job as per your convenience like monthly, weekly, daily, hourly. I will be writing separate article to cover this
Options on Build Triggers Tab
  • Build Environment: This will help you to setup environment for your project. Options present here are self explanatory like
    Delete workspace before build start: it will allow to pick fresh build from mentioned code base
    Abort build if stuck
    Add timestamp to the Console Output
Options in Build Environment Tab
  • Build: This is the most used option for free style project. At least I have used it in all of my project. Here you can write commands which triggers the script execution. Click on Add Build step and you can see all the options like Execute Window Batch command, Execute Shell, Invoke gradle script. We will be using one of these option to execute our script
Options in Build Tab
  • Post build action: This option allows user to perform post execution actions like publishing reports, cleanup activities on current build. Some of the options are publish report, Send Email notification. We will be using these options for our script execution
Options in Add Post-Build action Tab

Once you click on Apply and save. You will see your project in list of View

List View after creation of Jenkins Project

If you want to edit configurations click on the above project. You will re-direct inside the project configurations.

Project Settings

Before editing configuration you need few things from IDE source code management.

  1. Code base path: My code base is present in my local machine and not on any version control. So I have to configure code base path in the Jenkins from where Job will take out latest code.
  2. While running Job in IntelliJ we configure the execution, right click inside class and Run the test. But here we are asking some third party tool to execute the test. So we will have to provide such configuration in the Jenkins job that it will reach to the code and execute the code for us.
  3. As I am working on gradle project, I need to execute gradle code from command line. So now I have to setup my project in such a way that I can run my code through gradle command.
  4. I need to create a task in build.gradle which is can be execute through gradle command and executes my test cases. This is the task that I have created
task smoketest(type: Test){
useTestNG(){
useDefaultListeners = true
}

Explanation of Task:

  1. task keyword tells gradle that next block code will be task
  2. smoketest is nothing but the task name. You can give any name here.
  3. type : test. All JVM testing revolves around a single task type:Test. This runs a collection of test cases using any supported test library like Junit or TestNG. In our project we using TestNG as supported library. As we are using TestNG we will have mentioned it separately like useTestNG(). It will scan the project given in above directory and execute all the methods comes under @Test annotation.
  4. Now you can execute your test from terminal using below command
./gradlew clean smoketest

Click on Configuration to edit configuration. My code base is in my local machine where Jenkins is installed. So I will have to give path till my workspace for Jenkins reach the code. I have create gradle project so I will have to give path till my project directory in which build.gradle is present.

So In General options click on Advance. Select Use Custom workspace option. Provide the path till your project directory where build.gradle is present

Next is to Select Source Code Management. As Out code resides in local directory we have to select None here.

Ignore next configurations as we do not need them right now. Come to build settings. Click on Drop down. Now you have to choose the option from which you are hitting a gradle command from IntelliJ’s terminal to execute suite.

./gradlew clean smoketest

To execute above command from Jenkins.

  1. Click on Configure inside project
  2. Click on Build tab
  3. Click on Add build steps
  4. Click on Execute Shell
  5. Provide above command inside it. This is same command which triggers execution from terminal in Intellij.

If you are triggering this Jenkins job in Window machine execute shell script will fail. In this you can follow below steps

  1. Remove Execute Shell from Build
  2. Click on Add build steps
  3. Click on Invoke Gradle Script
  4. You Either invoke Gradle or use Gradle wrapper
  5. I am using gradle wrapper here
  6. Wrapper location is optional here
  7. You can provide task name under task text field
  8. You can apply the changes and Click on Save

Next step is to get the reports. You can find reports inside WorkSpace. Question here is what is workspace. It has the same meaning in the context of Jenkins which we have in context of IDE.

When we trigger any Jenkins job, it fetch the code from where ask us to fetch be it from Custom workspace we have provided or from Version control repository and create workspace inside Jenkins directory we have set while installing Jenkins.

This workspace is reflecting in Jenkins Job as well. You will get workspace option in left hand side which consist of complete project structure that got checked out in Jenkins job.

Workspace option

As it replicate same project structure as your project in IDE you can open workspace and go inside till report. General path is

build/reports/tests/<task_name>

But this is very tedious way to get the report. You can fetch this report and keep it in the list of main options on left side panel.

Here we are using a HTML plugin to publish our report. To add this plugin.

  1. Click on Dashboard
  2. Click on Manage Jenkins
  3. Search and Click on Manage Plugins
  4. You will get different tabs for the plugins.
  5. If you have already installed this plugin this will be Available under Installed Tab
  6. But for the first time you will have install it.
  7. So Click on Available
  8. Search for HTML

9. Check the plugin

10. As soon as you checked the plugin all the buttons at the buttons will get enabled

11. Select “Install without restart”

12. Now you can see Installation progress for the plugin

Plugin Installation in Progress
Plugin Installation Successful

13. Once it gets installed successfully. You can click on Manage plugins and now this plugin is get listed under Installed tab.

Plugin get listed in Installed Tab

14. Now click on Dashboard and Select your Jenkins Job

15. Click on Configure

16. Now go to Post-Build Action

17. Click on Add Post-Build Action

Plugin is visible on Add post-build action

18. Select HTML Publish Report Plugin.

Select Plugin

19. Click on Add

20. Add project path till report html file. Here we have file name called index.html

21. Provide html file name in Index Page field.

22. Give the Report Title you want to reflect on main Job screen. I kept it default as HTML Report

23. Click on Apply and Save

Publish your report

24. Now when you execute this Job you will get on more option HTML report

HTML Report option Visible

Click on HTML report. It will not be in proper format as it is a Zip file.

HTML report Zip file

Click on top right corner to download zip file and open report in browser. Report will contain coverage for package with clickable link. Click on it to check class for the. Class also has clickable link which consist of result based on Test

Clickable link with Package name
Clickable link with Class name
Report with Test name

In the next article we will see how to configure version control to your Jenkins Job

Happy Learning..!!!

--

--

Monil Joshi

I am having an 8+ years of experience in software testing. Working with Web, Mobile and API technologies. Visit my GIT https://github.com/monilj