使用 groovy 在 SoapUI 中附加文件

发布于 2025-01-01 06:43:36 字数 1253 浏览 1 评论 0原文

我正在 SoapUI 中创建一些测试。我想测试的 SOAP 请求有附件。当我手动设置时,一切正常: 设计器视图 part of raw request

但就我而言,我需要动态设置附件。我试图通过属性来保存文件路径,并通过常规脚本来设置附件。但它根本不起作用:

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

运行此脚本后,请求如下所示: 设计器视图 part of raw request

SoapUI 的文档根本没有帮助。 所以,我的问题是:我做错了什么?

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}' )

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 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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

七堇年 2025-01-08 06:43:36

我找到了答案:

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

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
傲娇萝莉攻 2025-01-08 06:43:36

塞文,谢谢你的回答。这有帮助。我将附上我完整的绝妙脚本,因为我花了一些时间来完全组装你的部件,但无论如何,所有的敬意都归于你。

请注意:

    //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文件名路径: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"
    }

这是我的肥皂要求:

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

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>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文