Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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 viewpart 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 viewpart of raw request

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

like image 746
Thaven Avatar asked Jan 30 '12 15:01

Thaven


People also ask

How to add Groovy script to a SoapUI Pro Test?

Here is how Groovy script can be added to a test: Step #1. In SoapUI Pro create a SOAP project with valid WSDL document. Under the project, create a test suite with the desired name. Inside the test suite, add groovy script test step as shown below: Step #2. Enter the name of the step in the dialogue that comes up as below and Clicks OK Step #3.

What are SoapUI attachments and how to use them?

These attachments are an older mechanism of attaching files to SOAP messages (MTOM is recognized as a standard nowadays). Nevertheless, SoapUI supports this mechanism to help you test existing web services that might use this technology. Add a file to the Attachments tab as it was described above

How do I add files to my SoapUI project?

Simply drag a file from a file manager (like Windows Explorer) to the Attachments tab. SoapUI will prompt if the file should be cached in the request or not. If you select to cache the file, SoapUI will store a compressed file copy in the project and will use it in simulated requests.

What language is SoapUI written in?

The SoapUI application is written in Groovy. We will add scripts in our tests written in Groovy (although Java syntax is valid for Groovy as well). SoapUI is a test case constructor that allows you to build tests from current API queries.


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
like image 138
Thaven Avatar answered Oct 02 '22 09:10

Thaven


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>
like image 39
Gico Avatar answered Oct 02 '22 10:10

Gico