2012-01-30 48 views
4

我在SoapUI中創建了一些測試。 SOAP請求,我想測試的附件。當我manualy設置它,一切都很好: Designer view part of raw request使用groovy在SoapUI中附加文件

但於我而言,我需要動態設置附件。我試圖通過屬性來保存文件路徑,並使用groovy腳本來設置附件。但它不是在所有的工作:

// 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()) 
} 

運行之後,該腳本,要求是這樣的: Designer view part of raw request

文檔到了SoapUI是沒有幫助的。 所以,我的問題是:我做錯了什麼?

回答

3

我找到了答案:

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 
2

Thaven,感謝您的回答。它有幫助。因爲我花了一些時間來完整地組裝你的零件,所以我會附上我的全部常規腳本,但無論如何,所有的貢品都會發給你。

請注意:

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

是測試用例內定義的測試步驟的性質。 文件名:my_sample_filename.xml相應地FileNamePath:C:\ samples \ my_sample_filename.xml

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" 
    } 

這裏是我的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>