Creating out First project to Run Selenium script in Jenkins
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.
- Login to Jenkins.
- Click on New Item.
- You will have to provide name to your project. Lets name it as Selenium_First_Test
- Now you have to select project type. As we are creating freestyle project click on Freestyle Project.
- 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.
- 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
- 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
- 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
- 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
Once you click on Apply and save. You will see your project in list of View
If you want to edit configurations click on the above project. You will re-direct inside the project configurations.
Before editing configuration you need few things from IDE source code management.
- 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.
- 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.
- 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.
- 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:
- task keyword tells gradle that next block code will be task
- smoketest is nothing but the task name. You can give any name here.
- 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.
- 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.
- Click on Configure inside project
- Click on Build tab
- Click on Add build steps
- Click on Execute Shell
- 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
- Remove Execute Shell from Build
- Click on Add build steps
- Click on Invoke Gradle Script
- You Either invoke Gradle or use Gradle wrapper
- I am using gradle wrapper here
- Wrapper location is optional here
- You can provide task name under task text field
- 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.
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.
- Click on Dashboard
- Click on Manage Jenkins
- Search and Click on Manage Plugins
- You will get different tabs for the plugins.
- If you have already installed this plugin this will be Available under Installed Tab
- But for the first time you will have install it.
- So Click on Available
- 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
13. Once it gets installed successfully. You can click on Manage plugins and now this plugin is get listed under 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
18. Select HTML Publish Report 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
24. Now when you execute this Job you will get on more option HTML report
Click on HTML report. It will not be in proper format as it is a 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
In the next article we will see how to configure version control to your Jenkins Job
Happy Learning..!!!