Optional variables Map Value

The Elvis operator was not enough to meet your requirement.


I have made another solution. Please have a look at my demo project at

I created a Groovy class at "Keywords/my/Params.groovy":

package my

/**
 * Keywords/my/Params.groovy
 * 
 */
public class Params extends LinkedHashMap<String, Object> {

	private static final Map<String, Object> defaultValues =
	[
		"foo": 0,
		"bar": "default_bar",
		"baz": false,

	]

	Params(Map<String, Object> values) {
		super();
		this.putAll(defaultValues);
		this.putAll(values);
	}
}

I made a Test Case script "Test Cases/caller":

// Test Cases/caller

import static com.kms.katalon.core.testcase.TestCaseFactory.findTestCase

import com.kms.katalon.core.webui.keyword.WebUiBuiltInKeywords as WebUI

import my.Params

Map params = (Map)new Params(["bar": "Hello, world!"])

WebUI.callTestCase(findTestCase("callee"), ["params": params])

I made one more Test Case script "Test Cases/callee":

// Test Cases/callee

println "params.foo=" + params.foo
println "params.bar=" + params.bar
println "params.baz=" + params.baz

When I execute the caller, I got the following output in the Console:

2022-04-04 18:38:03.701 INFO  c.k.katalon.core.main.TestCaseExecutor   - CALL Test Cases/callee
params.foo=0
params.bar=Hello, world!
params.baz=false

Please find that the params.foo is assigned with a default value 0, the params.baz is assigned with a defautl value false.

And the params.bar has a value Hello, world! which is passed by the caller script runtime.

Your requirement requires this level of Groovy programming. The Manual mode is no longer enough for you.

Thank you for the solution that you offer.
Seems like too ‘overkill’ for simple test.

If I had to add more section default value, lets say :

default_for_func1:
{"no" : "1", "foo": "bar", "john" : "doe}

default_for_func2:
{"city": "Jakarta", "phone": 15000, "isNeedCall": false}

default_for_func3:
{"isExpensive": false, "isRetired" : false}

Do I have to create class of param for each default function? So in total there are 3 class of param

Bit of twist is required.

Keywords/my/ParamsFactory.groovy:

package my

/**
 * Keywords/my/ParamsFactory.groovy
 * 
 */
public class ParamsFactory extends LinkedHashMap<String, Object> {

	ParamsFactory(Map<String, Object> defaultValues) {
		super();
		this.putAll(defaultValues);
	}

	Map<String, Object> createParams(Map<String, Object> runtimeParams) {
		this.putAll(runtimeParams);
		return this;
	}
}

Test Cases/caller

// Test Cases/caller

import static com.kms.katalon.core.testcase.TestCaseFactory.findTestCase

import com.kms.katalon.core.webui.keyword.WebUiBuiltInKeywords as WebUI

import my.ParamsFactory

ParamsFactory func1ParamFactory = new ParamsFactory(["no":"1","foo":"bar","john":"doe"])
WebUI.callTestCase(findTestCase("func1"), ["params": func1ParamFactory.createParams(["no":"007"])])

ParamsFactory func2ParamFactory = new ParamsFactory(["city": "Jakarta", "phone": 15000, "isNeedCall": false]);
WebUI.callTestCase(findTestCase("func2"), ["params": func2ParamFactory.createParams(["city":"Soeul"])])

ParamsFactory func3ParamFactory = new ParamsFactory(["isExpensive": false, "isRetired" : false]);
WebUI.callTestCase(findTestCase("func3"), ["params": func3ParamFactory.createParams(["isExpensive":true])])

Test Cases/func1

// Test Cases/func1

println "params.no=" + params.no
println "params.foo=" + params.foo
println "params.john=" + params.john

Test Cases/func2

// Test Cases/func2

println "params.city=" + params.city
println "params.phone=" + params.phone
println "params.isNeedCall=" + params.isNeedCall

Test Cases/func3

// Test Cases/func3

println "params.isExpensive=" + params.isExpensive
println "params.isRetired=" + params.isRetired

Output when executed caller

2022-04-05 13:22:09.946 INFO  c.k.katalon.core.main.TestCaseExecutor   - CALL Test Cases/func1
params.no=007
params.foo=bar
params.john=doe
2022-04-05 13:22:09.986 INFO  c.k.katalon.core.main.TestCaseExecutor   - END CALL Test Cases/func1
2022-04-05 13:22:09.987 INFO  c.k.katalon.core.main.TestCaseExecutor   - --------------------
2022-04-05 13:22:10.069 INFO  c.k.katalon.core.main.TestCaseExecutor   - --------------------
2022-04-05 13:22:10.070 INFO  c.k.katalon.core.main.TestCaseExecutor   - CALL Test Cases/func2
params.city=Soeul
params.phone=15000
params.isNeedCall=false
2022-04-05 13:22:10.100 INFO  c.k.katalon.core.main.TestCaseExecutor   - END CALL Test Cases/func2
2022-04-05 13:22:10.100 INFO  c.k.katalon.core.main.TestCaseExecutor   - --------------------
2022-04-05 13:22:10.169 INFO  c.k.katalon.core.main.TestCaseExecutor   - --------------------
2022-04-05 13:22:10.170 INFO  c.k.katalon.core.main.TestCaseExecutor   - CALL Test Cases/func3
params.isExpensive=true
params.isRetired=false
2022-04-05 13:22:10.198 INFO  c.k.katalon.core.main.TestCaseExecutor   - END CALL Test Cases/func3

1 Like

If you think so, the easiest solution would be enumerating all required param values without expecting defaults.

1 Like