Forum Discussion
Not sure what exactly the further.
Is that you need to ignore the last line (Rows=1) which is not actual data?
Then you change it from:
if (index) {
to:
if (index && (lines.size() != index+1)) {
- richie9 years agoCommunity Hero
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
- groovyguy9 years agoCommunity Hero
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
- nmrao9 years agoCommunity Hero
Usually I try before providing the code snippet. Looks this case I was lazy to do so as split would do the job with |, but it requires escaping. Sorry about it.
So here is the change code, this would also take care of additional row in the last line:
def lines = new File('/tmp/data.txt').readLines() lines.eachWithIndex { line, index -> if (index && (lines.size()-1 != index)) { // handles extra unwanted last line def data = line.split('\\|')*.trim() //CustRefID field's index 0 log.info data[0] } }
Now, it appears that you wanted to do the further processing with each CustRefID? In that case
Hope you can alter the step sequence as below
DataSource - DirectoryType (fileContents property)
POST /path/synchronous-upload <-- this POST publishes the fileContents propertyGroovyStep <-- to extract the CustRefID value from the fileContents property
for each CustRefID
save CustRefID at test case level
run step from with in groovy step ==> GET /path/contact?CustRefID=${#TestCase#CustRefID}