Attaching file in SoapUI with groovy

纵然是瞬间 提交于 2019-12-03 08:53:26

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

Thaven, thank you for your answer. It helped. I will attach my full groovy script as I spent some time to fully assembled your parts, but anyhow all tributes goes to you.

Please note that:

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

are the test step properties defined inside the test case. Filename: my_sample_filename.xml and FileNamePath: C:\samples\my_sample_filename.xml accordingly.

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 soap request:

<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>
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!