Thaven Thaven - 1 month ago 49
Groovy Question

Attaching file in SoapUI with groovy

I'm creating some tests in SoapUI. SOAP request, that i want to test has attachment. When I'm setting it manualy, everything is ok:
Designer view
part of raw request

But in my case, i need to set attachment dynamically. I'm trying to made it by properties to hold file path, and groovy script to set attachment. but it's not work at all:

// get request
def request = testRunner.testCase.getTestStepByName( "UploadRoutingCodes" ).testRequest

// clear existing attachments
for( a in request.attachments ) {
request.removeAttachment( a )

// get file to attach
//def fileName = context.expand( '${Source of data#PathToXRC File data name }' )
def fileName = context.expand( '${#TestCase#XRC_file_name}' )
def filePath = context.expand( '${#Project#XRC_files_path}' ) "file: " + filePath + fileName
def file = new File(filePath + fileName )
if ( file == null) {
log.error "bad filename"
// attach and set properties
def attachment = request.attachFile( file, true )
attachment.contentType = "application/octet-stream"
def list = fileName.tokenize("\\");

After run this script, request look like this:
Designer view
part of raw request

Documentation to SoapUI is not helpful at all.
So, my question is: what i'm doing wrong?


I found the answer:

def holder2 = groovyUtils.getXmlHolder( "UploadRoutingCodes#Request" ) // Get Request body
def startDate2 = holder2.setNodeValue( "//blac:FileByteStream","cid:"+list.last()); //Set "link" to attachment in request body
holder2.updateProperty() //and update

attachment.setPart(list.last()); //set attachment