Kobielak Kobielak - 3 months ago 12
Groovy Question

Multiple values from same txt file

I've been using this script to load values separated by new line from files to send requests with different values.

def size
File valueFile = new File("C:\\values\\myValueFile.txt")
File valueFile2 = new File("C:\\values\\myValueFile2.txt")
List lines = valueFile.readLines()
List lines2 = valueFile2.readLines()
size = lines.size.toInteger()
def myProps = testRunner.testCase.getTestStepByName("MyProperties")

for( counter in 0..size-1)
{
tempValue = lines[counter]
tempValue2 = lines2[counter]
myProps.setPropertyValue("Value", tempValue)
myProps.setPropertyValue("Value2", tempValue2)
log.info tempValue
log.info tempValue2
testRunner.runTestStepByName("updateBusinessTrip")
}


How to make it load values from same file separated by ";"?
txt file would look like that:

Value1;Value2
Value1.1;Value2.1
Value1.2;Value2.2

Answer

If I got you...:

Option 1:

tempValue = lines[counter].split(/;/)
myProps.setPropertyValue("Value", tempValues[0])
myProps.setPropertyValue("Value2", tempValue[1])

or Option 2:

(tempValue, tempValue2) = lines[counter].tokenize(';')
myProps.setPropertyValue("Value", tempValues)
myProps.setPropertyValue("Value2", tempValue2)

or another one:

File valueFile = new File("C:\\values\\myValueFile.txt")
def myProps = testRunner.testCase.getTestStepByName("MyProperties") 
valueFile.splitEachLine(/;/) { items ->
    myProps.setPropertyValue("Value", items[0])
    myProps.setPropertyValue("Value2", items[1])
    log.info tempValue
    log.info tempValue2
    testRunner.runTestStepByName("updateBusinessTrip")
}