Long time ago that I was here (under a different name :-) ). Glad to be back though ;-)
So, my question:
We want to send a file as Query, cached, multipart/form-data.
After sending we need to adapt, e.g. the vinnumber, save the file and send again with REST request.
For the moment I have this:
import groovy.xml.XmlParser
import groovy.xml.XmlUtil
// 1 Generate VIN
def chars = "ABCDEFGHJKLMNPRSTUVWXYZ0123456789"
def random = new Random()
def generatedVin = (1..17).collect { chars[random.nextInt(chars.length())] }.join()
log.info "Generated VIN: " + generatedVin
// 2 Get file path from test case level -->wll fetch it from folder project
def filePath = context.expand('${#TestCase#file}')
def file2 = new File(filePath)
log.info "File path: " + filePath
// 3 Parse XML
def xml = new XmlParser().parse(file)
// 4 Update VIN
xml.Body.VehicleTransportOrder.Line.Vehicle.VehicleID[0].value.generatedVin
// 5 Write updated XML
def writer = new FileWriter(file2)
XmlUtil.serialize(xml, writer)
writer.close()
log.info "VIN replaced successfully"
The original file came with the composite testproject as everybody needs to be able to reach that file. We don't know if this would be better put in a folder because we will have different files from different clients. But this is what we came up to.
The file is read correctly, the vinnumber = generated, but the new number can't be put in that attached file anymore.
Anybody an idea on what should be the best steps now?
If the VIN is the only value that changes, you could avoid parsing and serializing the entire XML and simply perform a string replacement on a placeholder, replacing VIN with the generated value before sending the request. This is often simpler and more maintainable.
Thank you for your response. As this could be an option, I've decided to do this differently. I wanted to be able to use the objects from ReadyAPI together with groovyscript. How I did it:
A first groovyscript: to lookup the files in a local 'resource' folder. A lot of those files have different extensions.
2nd step is use of Data Source. I gave a name to the property 'currentFile' and use this groovy:
def fileList =
testRunner.testCase.getPropertyValue(
"fileList"
)
def index =
testRunner.testCase.getPropertyValue(
"fileIndex"
) as Integer
def files =
fileList.split("\\|")
if(index >= files.size()) {
log.info "NO MORE FILES"
return false
}
result["currentFile"] =
files[index]
log.info(
"CURRENT FILE = " +
files[index]
)
The 3rd step is the REST request where I've attached the original file, saved on project level , as multipart/form-data
Followed by another groovyscript that will check which type of file we need to tackle, change the random vinnumber and saved it in a local folder to be fetched again in a next step