Run your Selenium Script pushed to GIT repository using Jenkins Job

Monil Joshi
4 min readMar 9, 2022

--

In the previous article, we saw how to create your first Jenkins job to execute the a selenium script through Jenkins. You can read that article Here

Creating out First project to Run Selenium script in Jenkins

Please go through above article as we will be using same Job in this article to re-use configuration like reporting.

In this project we are picking up a code present on local machine by providing Custom path. In general practice that might not be the case. If there are multiple people are there in the team code usually be on some version control be it SVN or GIT.

In this article I will be showing how to run our script if it is pushed to git.

Pre-requisite

  1. You must have github account and code must be pushed

2. You must have complete path of you code.

Now configure the Jenkins Job.

  1. Login to Jenkins with Valid Credentials.
  2. I will be updating my existing job I have created in last article mention above.
  3. If you are using same Job then you will have to uncheck Use custom workspace you have selected
  4. Here I will be using my code repository for “execution of test case using Selenium 4”
  5. Here you will find git repository https://github.com/monilj/Selenium4.0
  6. Open you job
  7. Click on Configure.
  8. Select Git as Source Code Management

9. Provide exact path of repository in repository URL as I have provided this https://github.com/monilj/Selenium4.0

10. As you are doing it for first time, you will have to add credentials over here

  1. Click on Add
  2. Click on Jenkins

3. Fill the form for Add credential

11. You will have to provide branch name on which your code base or Script present. By default this edit field is pre-filled with value */master. You have to provide exact branch name over here. As my code is present on Master branch only I will keep this field as it is

12. Go to Build

13. Add build step

14. Add Execute Shell where you have to provide command to execute gradle task

15. Make sure you have created task in build.gradle. You can check previous article which will help you to create a task

16. I am adding below command

./gradlew clean smoketest

Here smoketest is task name in build.gradle

17. If you are triggering test from windows instead of going for Execute Shell, Select “Invoke Gradle script

18. Select Use Gradle wrapper

19. Provide Task name

20. Save the Configuration

21. Click on Build now.

22. Observe the execution

23. Your execution might fail with error

*/jenkins8379183576289814159.sh: line 2: ./gradlew: Permission denied
Build step 'Execute shell' marked build as failure

24. In this case you will have to change the access and Try to set the execution flag on your gradlew file. We will take help from command chmod.

25. We will send one more command in Add Execute Shell as

chmod +x gradlew

26. Your Execute Shell command will look like this

27. Apply and Save the changes

28. Again click on Build Now

29. Your test execution will start

30. Click on triggered job and check console logs

31. Check the job status. Now it will run without any failure

While executing, Jenkins job will checkout recent changes from repository. Make some changes in your code/script and push it to github repository. After that, whenever Jenkins job gets triggered these changes will get reflected in execution report

See the screenshot below Jenkins console logs. It will checkout code up to the latest/recent commit

Try this out and let me know if you are facing any problems.

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