Attaching a file to SoapUI with groovy

I am creating some tests in SoapUI. The SOAP request I want to test has an attachment. When I configure it manually, everything is fine: Designer viewpart of raw request

But in my case, I need to install the application dynamically. I am trying to do this by the properties for storing the file path and the groovy script for setting the attachment. but it does 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}' ) log.info "file: " + filePath + fileName def file = new File(filePath + fileName ) if ( file == null) { log.error "bad filename" } else { // attach and set properties def attachment = request.attachFile( file, true ) attachment.contentType = "application/octet-stream" def list = fileName.tokenize("\\"); attachment.setPart(list.last()) } 

After running this script, the request looks like this: Designer viewpart of raw request

The SoapUI documentation does not help at all. So my question is: what am I doing wrong?

+4
source share
2 answers

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 
+3
source

Twain, thanks for your reply. That helped. I will attach my full groovy script, as I have spent some time completely assembling your parts, but somehow the tribute goes to you.

Note:

  //FileNamePath def fileNamePath = testCase.getTestStepAt(testRunner.testCase.getTestStepIndexByName("FileNameProperties")).getProperty("FileNamePath") //FileName def fileName = testCase.getTestStepAt(testRunner.testCase.getTestStepIndexByName("FileNameProperties")).getProperty("FileName") 

- These are the properties of the test step defined within the test case. File name: my_sample_filename.xml and FileNamePath: C: \ samples \ my_sample_filename.xml, respectively.

 import groovy.xml.MarkupBuilder import org.custommonkey.xmlunit.* import java.util.Random import java.security.MessageDigest import java.nio.file.* def groovyUtils = new com.eviware.soapui.support.GroovyUtils(context) def projectPath = groovyUtils.projectPath log.info projectPath def project = testRunner.testCase.testSuite.project log.info "Project: " + project.name def myTestSuite = testRunner.testCase.testSuite; log.info "TestSuite: " + myTestSuite.name def testCase = testRunner.testCase log.info "TestCase: " + testCase.name def testStepUploadDataAfterCheck = testCase.getTestStepByName("UploadDataAfterCheck") def request= testStepUploadDataAfterCheck.testRequest log.info "TestStep: " + testStepUploadDataAfterCheck.name // clear existing attachments for( a in request.attachments ) { request.removeAttachment( a ) } //FileNamePath def fileNamePath = testCase.getTestStepAt(testRunner.testCase.getTestStepIndexByName("FileNameProperties")).getProperty("FileNamePath") //FileName def fileName = testCase.getTestStepAt(testRunner.testCase.getTestStepIndexByName("FileNameProperties")).getProperty("FileName") // get file to attach log.info "file to attach: " + fileNamePath.getValue() def file = new File(fileNamePath.getValue() ) if ( file == null) { log.error "bad filename" } else { // attach and set properties def attachment = request.attachFile( file, true ) attachment.contentType = "application/octet-stream" attachment.setPart(fileName.getValue()) def holder2 = groovyUtils.getXmlHolder( "UploadDataAfterCheck#Request" ) // Get Request body holder2.setNodeValue( "//upl:UploadDataAfterCheckRequest/uploadedData","cid:"+fileName.getValue()); //Set "link" to attachment in request body holder2.updateProperty() //and update log.info "file attached succesfully" } 

And here is my request for soap:

 <soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:upl="http://www.acer.europa.eu/aris/upload"> <soapenv:Header/> <soapenv:Body> <upl:UploadDataAfterCheckRequest> <uploadedData>cid:my_sample_filename.xml</uploadedData> </upl:UploadDataAfterCheckRequest> </soapenv:Body> </soapenv:Envelope> 
+2
source

Source: https://habr.com/ru/post/1393789/


All Articles