Forum Discussion
Hey Rao,
I'm sorry - I'm not being clear enough in my descriptions.
I already have a datasource with a filecontents property (which holds the whole file contents). What I need is ANOTHER test step (I'm guessing using groovy) to pick out the CustRefID attribute value from the file, as I have a GET step that uses the CustRefID in the GET request to confirm the POST was successful.
I am expecting my TestCase to look like the following
DataSource - DirectoryType (fileContents property)
GroovyStep <-- to extract the CustRefID value from the fileContents property POST /path/synchronous-upload <-- this POST publishes the fileContents property GET /path/contact?CustRefID=${#TestCase#CustRefID}
I can't rely on the POST response providing the CustRefID, to extract to pass onto the GET request, so I need to extract it (I think) from the fileContents property
The file contents are as follows:
CustRefID|title|firstname|lastname|addressline1|addressline2|addressline3|postcode ID0000001|MR|RICH|JONES|5 WHATEVER WAY|HODGE HILL|BIRMINGHAM|B36 9LB Rows=1
Your original script was as follows:
def lines = new File('D:\\Donor Marketing\\SoapUI\\nongroovyscriptfile\\New Text Document.dat').readLines() lines.eachWithIndex { line, index -> if (index) { def data = line.split('|')*.trim() log.info data[0] } }
When I ran your original script - an 'Information' dialogue appears with the whole file contents displayed, and the info in the log output reports the following:
Tue May 23 10:46:56 BST 2017:INFO:I Tue May 23 10:46:56 BST 2017:INFO:R
As you can see - it appears the script is logging the first character of every line after the header row.
I am hoping I can change your script to capture the whole value of 'ID000001' so I can pass it to the GET request later in my test case.
In summary - I'm hoping you can help editing your script so that it parses the fileContents property and captures the 'ID0000001'
I did edit your script to the following:
def lines = new File('D:\\Donor Marketing\\SoapUI\\nongroovyscriptfile\\New Text Document.dat').readLines() lines.eachWithIndex { line, index -> if (index) { def data = line.split('|')*.trim() log.info data[0] + data[1] + data[2] + data[3] + data[4] + data[5] + data[6] + data[7] + data[8] } }
and the logging was as follows:
Tue May 23 10:46:56 BST 2017:INFO:ID0000001 Tue May 23 10:46:56 BST 2017:INFO:Rows=1nullnullnull
I only need to capture the ID0000001 value - so that I can pass it to the GET Request.
I hope I've been clear - at this point, I need to edit the script to parse the fileContents property (rather than from the directory as the script currently is doing (because I'm going to use a Datasource of Directory type with lots of files to cycle through) and to ONLY capture the ID0000001 value.
Many, many thanks to all, and especially Rao,
richie
Try changing
def data = line.split('|')*.trim()
to
def data = line.tokenize('|');
That may work better for harvesting the first item out of every column.
- richie9 years agoCommunity Hero
def lines = new File('D:\\Donor Marketing\\SoapUI\\nongroovyscriptfile\\New Text Document.dat').readLines() lines.eachWithIndex { line, index -> if (index) { def data = line.tokenize('|'); log.info data[0] } }
ok - that's definitely progress!
I've altered the file content (by removing the footer record)
CustRefID|title|firstname|lastname|addressline1|addressline2|addressline3|postcode ID0000001|MR|RICH|JONES|5 WHATEVER WAY|HODGE HILL|BIRMINGHAM|B36 9LB
I'm going to hard code the footer record into the request resulting in the output of @Rao's script with msiadak's update to generate the following when executing the script.
Tue May 23 10:46:56 BST 2017:INFO:ID0000001
I'm almost there!
Does anyone know how to capture the results of the log.info data request to place into a property? I need to now pass this value (the 'ID0000001' value) into a property and am unsure how to do this...
Many thanks to all (especially Rao & msiadak) for all their help - I don't want you guys to think I don't appreciate everything you're doing to help me - you're helping me keep my job.....rent is one bill you really need to make sure you keep paying!
richie
- groovyguy9 years agoCommunity Hero
Typically the log.info(data[0]) is to show that you're getting the correct value. From there, it's a matter of putting that into a property.
I usually use a property test step in the same test case, and use groovy to transfer the property there.
In this instance, assuming the Properties Test Step is in the same test case, you can do:
context.testCase.testStep["Properties"].setPropertyValue("EnterPropertyName", data[0]);
That'll give you a property that you can reference in the Properties test step.
- richie9 years agoCommunity Hero
I'm sorry - I don't understand.
I'm guessing you are saying add in a 'Properties' Test Step (with the CustRefID property created) - but when I added the line of code to the existing script - I got an error saying 'no such property:teststep for class:' response
my hierarchy after your answer is as follows:
Datasource(Directorytype) - fileContents property Groovy step (see below for code) Properties step (entitled 'Properties') (CustRefID property created, blank value)
I changed my code after your answer to the following:
def lines = new File('D:\\Donor Marketing\\SoapUI\\nongroovyscriptfile\\New Text Document.dat').readLines() lines.eachWithIndex { line, index -> if (index) { def data = line.tokenize('|'); log.info data[0] } } context.testCase.testStep["Properties"].setPropertyValue("CustRefID", data[0]);
The Properties step is entitled 'Properties'. The property within the 'Properties' step is entitled 'CustRefID' (which is the attribute that is returned by the log.info data[0] command above.
I'm sorry that I don't understand - can you provide an extra hint as to what I'm doing wrong?
Many thanks,
richie
- groovyguy9 years agoCommunity Hero
richie, Try the below script. I forgot the "s" on context.testCase.testSteps. Also, will you need to harvest multiple ID values from the same text file?
def lines = new File('D:\\Donor Marketing\\SoapUI\\nongroovyscriptfile\\New Text Document.dat').readLines() lines.eachWithIndex { line, index -> if (index) { def data = line.tokenize('|'); log.info data[0]; context.testCase.testSteps["Properties"].setPropertyValue("CustRefID", data[0]); } }