Optional Field Entry

Hello Team,

I have a programming question. I have a scenario in which a field is optional and not necessary to be filled but I want to have my test cases in such a way that whenever test case encounters an even number it should fill out the field if it encounters an odd number it should not fill out that field.

I tried to create a script here :

def optionalfield = org.apache.commons.lang.RandomStringUtils.randomNumeric(1)

if (optionalfield is even) {

WebUI.sendKeys(findTestObject(‘7_Payments_Page/iOrganizationField’), findTestData(‘companydata’).getValue(
2, 16))

I am not able to figure out how to let the script know if the number “optionalfield” is odd or even. What should be the snippet

Java and Groovy have the modulus operator “%”.

int optionalfield = (int)org.apache.commons.lang.RandomStringUtils.randomNumeric(1)
if ( (optionalfield % 2) == 0 ) {

Some people call this the"remainder operator" because it returns the remainder for division. So if optionalfield divided by two has a remainder of zero, then the number is even.


Thanks @Harold_Owen I will give it a try.

Hello @Harold_Owen

When I try the solution I am getting below error which looks to me that I need to import a java library to my script before or maybe I am wrong.

03-01-2019 11:40:51 AM if (company_decide % 2 == 0)

Elapsed time: 0.022s

if (company_decide % 2 == 0) FAILED.


groovy.lang.MissingMethodException: No signature of method: java.lang.String.mod() is applicable for argument types: (java.lang.Integer) values: [2]

Possible solutions: drop(int), any(), find(), use([Ljava.lang.Object;), any(groovy.lang.Closure), is(java.lang.Object)

at TC to simulate a disputed transaction.run(TC to simulate a disputed transaction:80)

at com.kms.katalon.core.main.ScriptEngine.run(ScriptEngine.java:194)

at com.kms.katalon.core.main.ScriptEngine.runScriptAsRawText(ScriptEngine.java:119)

at com.kms.katalon.core.main.TestCaseExecutor.runScript(TestCaseExecutor.java:328)

at com.kms.katalon.core.main.TestCaseExecutor.doExecute(TestCaseExecutor.java:319)

at com.kms.katalon.core.main.TestCaseExecutor.processExecutionPhase(TestCaseExecutor.java:298)

at com.kms.katalon.core.main.TestCaseExecutor.accessMainPhase(TestCaseExecutor.java:290)

at com.kms.katalon.core.main.TestCaseExecutor.execute(TestCaseExecutor.java:224)

at com.kms.katalon.core.main.TestCaseMain.runTestCase(TestCaseMain.java:106)

at com.kms.katalon.core.main.TestCaseMain.runTestCase(TestCaseMain.java:97)

at com.kms.katalon.core.main.TestCaseMain$runTestCase$0.call(Unknown Source)

at TempTestCase1551469223068.run(TempTestCase1551469223068.groovy:22)


The Script I have is :

def company_decide = org.apache.commons.lang.RandomStringUtils.randomNumeric(1)

if ((company_decide % 2) == 0) {

WebUI.sendKeys(findTestObject('5_Customer_Information_Page/Field_Company_Information'), findTestData('Test_Data_Generator/Company_Data').getValue(
	1, 1))


@Russ_Thomas It worked, Thanks

1 Like