Extract specific string value from whole string contained within a Datasource Property
Hi to all,
I have a flat | delimited file (see below for format) that I need to parse for a specific value and extract it to a property so I can use it later in my test
file contents as below:
CustRefID|title|firstname|lastname|addressline1|addressline2|addressline3|postcode ID0000001|MR|RICH|JONES|5 WHATEVER WAY|HODGE HILL|BIRMINGHAM|B36 9LB Rows=1
I'm extracting the file contents using the Datasource - Directory type (to minimise the groovyscript) and assigning the file contents to a fileContents property. I am using the fileContents property later in a POST request (by posting the whole string in the fileContents property) to the webservice.
HOWEVER - I need to parse the fileContents property for a particular value (my CustRefID attribute value) within the file, so I can add in a GET request after my POST to query the webservice to check the POST was successful - I need the CustRefID value that was submitted in the POST. I understand I can do this from the response probably more easily than extracting it from the testdata, however, there are reasons why I'm not allowed to do that, so I MUST extract the value from the testdata.
So I'm expecting my testcase to look something like the following:
Directory (Datasource) - sourcing files of particular type from the directory. I have a fileContents property setup to hold the file contents Groovy step - (to parse the fileContents property to extract the CustRefId value) REST POST step - (posting the fileContents property to the web service) REST GET step- (querying the webservice (using the CustRefID value) to ensure the POST was successful) Datasource Loop step - (looping round for the next file in my Directory Datasource)
I should highlight that I need to preserve the header record and the footer record in my REST requests, which is one of the reasons I'm going for the above approach despite loads of help from Rao, Radford, msiadak, and IgorG.
The below is courtesy of Rao from another post I made - I'm hoping its fairly straightforward to alter the code below
def lines = new File('data.txt').readLines() // need to change this line to read the contents of the fileContents property??? lines.eachWithIndex { line, index -> if (index) { def data = line.split('|')*.trim() log.info data[0] //need to alter this SOMEHOW to pick out the CustRefID value } }
I'm pretty sure the line.split will do it - but my reading is only taking me so far at the moment.
I can see the above is reporting the first character on lines 2 and 3 of the file - I've tried playing with the script myself but I'm struggling. Could anyone point me in the right direction?
Essentially - I need to parse the fileContents property (created in the Directory - Datasource object) to pull out the CustRefID value.
I've attached my project file in case this helps clarify.
As always - many thanks to all/anyone who can help - I do appreciate that you are helping me doing my job for me!
richie