Hello everybody, while running your automation jobs in Jenkins, you sometimes need to pass some parameters to your scripts. Those parameters can be url, browser name or even the credentials of the test user. This tutorial will tell you to create a parametrized Jenkins job.
Firstly, I assume you have Jenkins ready in your environment. In case you also want to install Jenkins from scratch you can check below articles.
When creating a Jenkins job, you should check “This project is parametrized” checkbox
You might want your user to be able to select one value among many others. That can be applied to Browsers. You should choose Choice Parameter on the Add Parameter menu. Then you should give a name and fill the values.
You might want your user to change the test environment url or some other value that you in the test. So the user should provide a string value. You should add String Parameter on the Add Parameter menu.
You might want to send a boolean parameter to your test by choosing the Boolean Parameter on the menu. This time you just have to provide a name to it. As the value can be true or false. Jenkins doesn’t allow you to enter a value :)
In case you need to pass a password parameter to your script, you should use this parameter type as it will be represented by starts and value will stay hidden from all users.
There are many other Parameters that you can provide to your Jenkins Job. Those can be
- Password Parameter In case you use that one your inout will be represented by starts and hidden from all users.
- Multi Line String Parameter
- Git Parameter
- String Parameter
Then how you will pass all those parameters into your Maven or Gradle command.
How to Use Those Parameters
I’ll show you how to use those parameters in a Gradle build. Same principles apply for both Maven or any other build tool that you might use. You should create a Build Step then choose Invoke Gradle Script. Then you should put the Gradle command and parameters with below structure.
test -DbrowserName="$browser" -DtestUrl="$url"
Then you should read that value in the source code. Here’s a small example with Java.
String testUrl = System.getProperty("testUrl");
Happy Testing with Jenkins!
Canberk Akduygu is a Test Lead working on Test Automation and Performance Testing in Amsterdam at Qardio.