Minimize scripts using For Loop Array

Hello Team,

I just want to minimize my script using for loop or any. Here are the scripts

//GLOBAl Timeout
int timeout = 10

def inputField = findTestObject('VitalSign/VitalSign_tab/temperature_editText')
Mobile.tap(inputField, timeout)
Mobile.sendKeys(inputField, '10')

def inputField1 = findTestObject('VitalSign/VitalSign_tab/pulseHeartRate_editText')
Mobile.tap(inputField1, timeout)
Mobile.sendKeys(inputField1, '11')

def inputField3 = findTestObject('VitalSign/VitalSign_tab/bpRightArm1_editText')
Mobile.tap(inputField3, timeout)
Mobile.sendKeys(inputField3, '13')

def inputField5 = findTestObject('VitalSign/VitalSign_tab/bpLeftArm1_edittext')
Mobile.tap(inputField5, timeout)
Mobile.sendKeys(inputField5, '15')

def inputField7 = findTestObject('VitalSign/VitalSign_tab/02Saturation_editText')
Mobile.tap(inputField7, timeout)
Mobile.sendKeys(inputField7, '17')

def inputField8 = findTestObject('VitalSign/VitalSign_tab/02Saturation_lpm_editText')
Mobile.tap(inputField8, timeout)
Mobile.sendKeys(inputField8, '18')

def inputField9 = findTestObject('VitalSign/VitalSign_tab/02SatAT')
Mobile.tap(inputField9, timeout)
Mobile.sendKeys(inputField9, '19')
                                                                             
def inputField10 = findTestObject('VitalSign/VitalSign_tab/bloodGlocuse_editText')
Mobile.tap(inputField10, timeout)
Mobile.sendKeys(inputField10, '20')
 
def inputField12 = findTestObject('VitalSign/VitalSign_tab/bpRightLeg1_editText')
Mobile.tap(inputField12, timeout)
Mobile.sendKeys(inputField12, '22')
 
def inputField13 = findTestObject('VitalSign/VitalSign_tab/bpRightLeg2_editText')
Mobile.tap(inputField13, timeout)
Mobile.sendKeys(inputField13, '23')
 
def inputField14 = findTestObject('VitalSign/VitalSign_tab/bpLeftLeg1_editText')
Mobile.tap(inputField14, timeout)
Mobile.sendKeys(inputField14, '24')
 
def inputField15 = findTestObject('VitalSign/VitalSign_tab/bpLeftLeg2_editText')
Mobile.tap(inputField15, timeout)
Mobile.sendKeys(inputField15, '25')

I just tried this scipt but not working

inputField1 = 'VitalSign/VitalSign_tab/temperature_editText'
inputField2 = 'VitalSign/VitalSign_tab/pulseHeartRate_editText'
inputField3 = 'VitalSign/VitalSign_tab/bpRightArm1_editText'
def list = [inputField1, inputField2, inputField3]
for (def item : list) {
	WebUI.verifyElementPresent(findTestObject(item), 0)
	Mobile.tap(item, timeout)
	Mobile.sendKeys(item, '10')
}

Can you help me?

Thank You for the response

Hi

It’s because you try to tap on a String.
Try

for (def item : list) {
        TestObject inputField = findTestObject(item)
	WebUI.verifyElementPresent(inputField, 1)
	Mobile.tap(inputField, timeout)
	Mobile.sendKeys(inputField, '10')
}

you can use also list.each and pass a closure containing your iteration, to make it more ‘groovy-ish’ :wink:
no more need for ’ for’ or ‘def’ in iteration

I am new in katalon, can you give an example code? Thank You :slight_smile: @Ibus

Hello @HeleneB

I tried the scripts but it is not working

What is the error ?

the groovy way:

timeout = 10
count = 10

def inputFields = ['temperature_editText', 'pulseHeartRate_editText', 'bpRightArm1_editText']

inputFields.each {
    to = findTestObject("VitalSign/VitalSign_tab/$it", 0)
    WebUI.verifyElementPresent(to)
    Mobile.tap(it, timeout)
    Mobile.sendKeys(it, count.toString())
    count++
}

ref: http://docs.groovy-lang.org/next/html/documentation/working-with-collections.html

Hi @heavenmojica ,

KABABAYAN! :smiley:

It cannot be just a few lines since you really have a lot of different objects. What we can do is to just minimize the repeating ‘taps’ and ‘sendkeys’. I don’t have an exp in mobile testing but I think the code will be almost the same as automating web that’s why I have this idea. . .

try this one out… I hope you find this simple and easy for you to understand…

/**
first, declare all your objects. When you try to print it, it will give you a string output in the console like this:
TestObject - 'VitalSign/VitalSign_tab/temperature_editText'
which is why it is not working because it includes the 'TestObject - ' keyword instead of just passing the whole testobject path. When calling the test object we need to eliminate the extra keyword to get the exact testobject path.
**/

//since it will just return a string output, just remove the 'findTestObject' keyword to eliminate the 'TestObject - '
String input1 = 'VitalSign/VitalSign_tab/temperature_editText'
String input2 = 'VitalSign/VitalSign_tab/pulseHeartRate_editText'
String input3 = 'VitalSign/VitalSign_tab/bpRightArm1_editText'

//I'm not quite familiar with groovy so let's do it in java.
//create a java list then put all your objects in the list.
java.util.List<String> testObjectContainer = [input1, input2, input3]

int input = 10

//do your loop
for (int index = 0; index < testObjectContainer.size(); index++)
{
     //this is where the 'findTestObject' will take place
     WebUI.verifyElementPresent(findTestObject(testObjectContainer[index]), 5)
     Mobile.tap(findTestObject(testObjectContainer[index]))
     Mobile.sendKeys(findTestObject(testObjectContainer[index]), input + index)
}

Hope that helps. . . :slight_smile:

Hello @HeleneB,

This is the error

Reason:
com.kms.katalon.core.exception.StepFailedException: Unable to verify object 'Object Repository/VitalSign/VitalSign_tab/temperature_editText' is present
	at com.kms.katalon.core.webui.keyword.internal.WebUIKeywordMain.stepFailed(WebUIKeywordMain.groovy:64)

Hi kababayan sir @Arnel

Thank you, I will try that scripts in mobile :slight_smile:

Hi sir @Ibus

Thank you, I will try this scripts also :slight_smile: