Forum Discussion
8 Replies
- testerAOccasional ContributorI was able to read one value, namely AreaCode, from the DataSource. How do I read and input multiple values from a txt file? I need to read and input Prefix and Line Number too.
#1 Read the DataSource - GroovyScript-SaveDataSource
#2 Properties-Looper
#3 a groovy script looper - GroovyScript-DataLoop
#1 GroovyScript-SaveDataSource
/*
@Description : Data Source to read .txt file and pass the value to corresponding property.
@GroovyTestStepName : "GroovyScript-SaveDataSource"
*/
import com.eviware.soapui.support.XmlHolder
def myTestCase = context.testCase
def counter,next,previous,size
File tickerEnumFile = new File("C:/CTS/TestFiles/Save_AreaCode.txt")
List lines = tickerEnumFile.readLines()
size = lines.size.toInteger()
propTestStep = myTestCase.getTestStepByName("Properties-Looper") // get the Property TestStep
propTestStep.setPropertyValue("Total", size.toString())
counter = propTestStep.getPropertyValue("Count").toString()
counter= counter.toInteger()
next = (counter > size-2? 0: counter+1)
tempValue = lines[counter]
propTestStep.setPropertyValue("Value", tempValue)
propTestStep.setPropertyValue("Count", next.toString())
next++
log.info "Reading line : ${(counter+1)} / $lines.size"
propTestStep.setPropertyValue("Next", next.toString())
log.info "Value '$tempValue' -- updated in $propTestStep.name"
if (counter == size-1)
{
propTestStep.setPropertyValue("StopLoop", "T")
log.info "Setting the stoploop property now..."
}
else if (counter==0)
{
def runner = new com.eviware.soapui.impl.wsdl.testcase.WsdlTestCaseRunner(testRunner.testCase, null)
propTestStep.setPropertyValue("StopLoop", "F")
}
else
{
propTestStep.setPropertyValue("StopLoop", "F")
}
#2 Properties-Looper
Total 9
Value 304
Count 0
Next 1
StopLoop T
#3 GroovyScript-DataLoop
def myTestCase = context.testCase
def runner
propTestStep = myTestCase.getTestStepByName("Properties-Looper") // get the Property TestStep
endLoop = propTestStep.getPropertyValue("StopLoop").toString()
if (endLoop.toString() == "T" || endLoop.toString()=="True" || endLoop.toString()=="true")
{
log.info ("Exit Groovy Data Source Looper")
assert true
}
else
{
testRunner.gotoStepByName("GroovyScript-SaveDataSource") //setStartStep
}
Step that needs to be parameterize
<cus:CallerID AreaCode='${Properties-Looper#Value}' LineNumber="2013" Prefix="208"/>
Please provide me a way to read and input values for LineNumber and Prefix - pflaumengeistContributorRegarding your first post, reading would look something like this
import java.io.File
import jxl.*
import jxl.write.*
//=====================================================================
base_xml = '<cusCallerID AreaCode="404" LineNumber="2013" Prefix="208"/>'
//read excelfile
wb = Workbook.getWorkbook(new File('e:/values.xls'))
workbook = Workbook.createWorkbook(new File('e:/values.xls'), wb)
sheet = workbook.getSheet(0)
(1 .. sheet.getRows()).each {
row ->
xml = new XmlParser().parseText(base_xml)
log.info 'new xml'
xml.@"${sheet.getCell(0, 0).getContents()}" = sheet.getCell(row, 0).getContents()
xml.@"${sheet.getCell(0, 1).getContents()}" = sheet.getCell(row, 1).getContents()
xml.@"${sheet.getCell(0, 2).getContents()}" = sheet.getCell(row, 2).getContents()
w = new StringWriter()
p = new XmlNodePrinter(new PrintWriter(w))
p.preserveWhitespace = true;
p.print(xml)
//do stuff here
log.info w.toString()
}
workbook.close()
wb.close()
If your excel file looks like this:
col:0 col:1 col:2
row:0 AreaCode LineNumber Prefix
row:1 10 1 a
row:2 20 2 b
and your xml file looks something like this:
<cusCallerID AreaCode="404" LineNumber="2013" Prefix="208"/> - pflaumengeistContributorreading from a text file could look something like this
import java.io.File
base_xml = '<cusCallerID AreaCode="1" LineNumber="2013" Prefix="208"/>'
project = testRunner.testCase.testSuite.project
linecount = 0
new File('e:/values.txt').eachLine {
line ->
line.split(';').each {
values ->
project.setPropertyValue(linecount.toString() + '#' + values.split('=')[0], values.split('=')[1])
}
linecount++
}
(0 .. (project.getPropertyCount()/3) - 1).each {
counter ->
xml = new XmlParser().parseText(base_xml)
log.info 'new xml'
xml.@AreaCode = project.getPropertyValue(counter + '#' + 'AreaCode')
xml.@LineNumber = project.getPropertyValue(counter + '#' + 'LineNumber')
xml.@Prefix = project.getPropertyValue(counter + '#' + 'Prefix')
w = new StringWriter()
p = new XmlNodePrinter(new PrintWriter(w))
p.preserveWhitespace = true;
p.print(xml)
//do stuff here
log.info w.toString()
}
if your text file looks like this:
AreaCode=100;LineNumber=1;Prefix=a
LineNumber=2;Prefix=b;AreaCode=200
AreaCode=300;LineNumber=3;Prefix=c
this will set the properties at your prject level, properties would look something like this
name value
0#AreaCode 100
0#LineNumber 1
0#Prefix a
1#AreaCode 200
1#LineNumber 2
1#Prefix b
2#AreaCode 300
2#LineNumber 3
2#Prefix c - testerAOccasional ContributorI am able to get value in the "Value"
e.g Value = 202;704;8235
How do I split this "Value" to
three property values
I need
areacode = 202
prefix = 704
linenumber = 8235
I tried this codepropTestStep.setPropertyValue("Value", tempValue)
def (areacode, prefix,linenumber) = "Value".split(";")
My data text file
202;704;8235
979;557;6114
310;533;7810
940;594;7006
903;327;5261
Http log shows
Thu Mar 07 18:58:24 EST 2013:DEBUG:>> " <cus:CallerID AreaCode="Value" LineNumber="Value" Prefix="Value"/>[\n]" - pflaumengeistContributor
import java.io.File
base_xml = '<cusCallerID AreaCode="1" LineNumber="2013" Prefix="208"/>'
project = testRunner.testCase.testSuite.project
val = ''
new File('e:/values.txt').eachLine {
line ->
val += line + ','
}
project.setPropertyValue('value', val)
project.getPropertyValue('value').split(',').each {
values ->
xml = new XmlParser().parseText(base_xml)
log.info 'new xml'
xml.@AreaCode = values.split(';')[0]
xml.@LineNumber = values.split(';')[1]
xml.@Prefix = values.split(';')[2]
w = new StringWriter()
p = new XmlNodePrinter(new PrintWriter(w))
p.preserveWhitespace = true;
p.print(xml)
//do stuff here
log.info w.toString()
}
creates this
Fri Mar 08 18:06:24 CET 2013:INFO:new xml
Fri Mar 08 18:06:24 CET 2013:INFO:<cusCallerID AreaCode="202" LineNumber="704" Prefix="8235"/>
Fri Mar 08 18:06:24 CET 2013:INFO:new xml
Fri Mar 08 18:06:24 CET 2013:INFO:<cusCallerID AreaCode="979" LineNumber="557" Prefix="6114"/>
Fri Mar 08 18:06:24 CET 2013:INFO:new xml
Fri Mar 08 18:06:24 CET 2013:INFO:<cusCallerID AreaCode="310" LineNumber="533" Prefix="7810"/>
Fri Mar 08 18:06:24 CET 2013:INFO:new xml
Fri Mar 08 18:06:24 CET 2013:INFO:<cusCallerID AreaCode="940" LineNumber="594" Prefix="7006"/>
Fri Mar 08 18:06:24 CET 2013:INFO:new xml
Fri Mar 08 18:06:24 CET 2013:INFO:<cusCallerID AreaCode="903" LineNumber="327" Prefix="5261"/> - pflaumengeistContributorAnd try to change
propTestStep.setPropertyValue("Value", tempValue)
def (areacode, prefix,linenumber) = "Value".split(";")
topropTestStep.setPropertyValue("Value", tempValue)
def (areacode, prefix,linenumber) = propTestStep.getPropertyValue("Value").split(";") - testerAOccasional Contributorpflaumengeist, thank you so much.
I was able to read the three values from the text file: AreaCode, Prefix and LineNumber from one file
Now I need to read another set of values from another text file: ContactAreaCode, ContactPrefix and LineNumber
The loop I used to from first input file, TestFilesSave/Save_Ph.txt is included in the code section
How do I add another input file, TestFilesContact/Contact_Ph.txt with ContactAreaCode, ContactPrefix and LineNumber
Note:
/*
@Description : Data Source to read .txt file and pass the value to corresponding property.
@GroovyTestStepName : "GroovyScript-SaveDataSource"
*/
import com.eviware.soapui.support.XmlHolder
def myTestCase = context.testCase
def counter,next,previous,size
File tickerEnumFile = new File("C:/CTS/TestFilesSave/Save_Ph.txt") //make sure input.txt file already exists and contains different set of values sepearted by new line (CR).
"sleep(100000000000000000000000000000000)"
List lines = tickerEnumFile.readLines()
size = lines.size.toInteger()
propTestStep = myTestCase.getTestStepByName("Properties-Looper") // get the Property TestStep
propTestStep.setPropertyValue("Total", size.toString())
counter = propTestStep.getPropertyValue("Count").toString()
counter= counter.toInteger()
next = (counter > size-2? 0: counter+1)
tempValue = lines[counter]
propTestStep.setPropertyValue("Phone", tempValue)
def(AreaCode,Prefix,LineNumber) = propTestStep.getPropertyValue("Phone").split(";")
propTestStep.setPropertyValue("AreaCode", AreaCode)
propTestStep.setPropertyValue("Prefix", Prefix)
propTestStep.setPropertyValue("LineNumber", LineNumber)
propTestStep.setPropertyValue("Count", next.toString())
next++
log.info "Reading line : ${(counter+1)} / $lines.size"
propTestStep.setPropertyValue("Next", next.toString())
log.info "Phone '$tempValue' -- updated in $propTestStep.name"
if (counter == size)
{
"sleep(100000000000000000000000000000000)"
propTestStep.setPropertyValue("StopLoop", "T")
log.info "Setting the stoploop property now..."
}
else if (counter==0)
{
"sleep(100000000000000000000000000000000)"
def runner = new com.eviware.soapui.impl.wsdl.testcase.WsdlTestCaseRunner(testRunner.testCase, null)
propTestStep.setPropertyValue("StopLoop", "F")
}
else
{
"sleep(100000000000000000000000000000000)"
propTestStep.setPropertyValue("StopLoop", "F")
}
Properties-Looper : Attached file
I need to read from a total of 6 different phone numbers, stored in six different text files
TestFilesSave/Save_Ph.txt
TestFilesContact/Contact_Ph.txt
TestFilesContact/Mobile_Ph.txt
TestFilesContact/Service_Ph.txt
TestFilesContact/Work_Ph.txt
TestFilesContact/Callback_Ph.txt - testerAOccasional ContributorI was able to loop through the other test files, using this code. Thank you for your help.
/*
@Description : Data Source Looper responsible for looping a specific teststep.
@GroovyTestStepName : "GroovyScript-SaveDataSource"
*/
def myTestCase = context.testCase
def runner
propTestStep = myTestCase.getTestStepByName("Properties-Looper") // get the Property TestStep
endLoop = propTestStep.getPropertyValue("StopLoop").toString()
if (endLoop.toString() == "T" || endLoop.toString()=="True" || endLoop.toString()=="true")
{
"sleep(100000000000000000000000000000000)"
log.info ("Exit Groovy Data Source Looper")
assert true
}
else
{
"sleep(100000000000000000000000000000000)"
testRunner.gotoStepByName("GroovyScript-DataSource_Save")
testRunner.gotoStepByName("GroovyScript-DataSource_Contact")
testRunner.gotoStepByName("GroovyScript-DataSource_Mobile")
testRunner.gotoStepByName("GroovyScript-DataSource_Service")
testRunner.gotoStepByName("GroovyScript-DataSource_Work")
testRunner.gotoStepByName("GroovyScript-DataSource_Callback")
//setStartStep
}