Forum Discussion

Apoorva6's avatar
Apoorva6
Frequent Contributor
4 years ago

How to get resource path in groovy soapui open source?

import groovy.xml.MarkupBuilder
import org.custommonkey.xmlunit.*
import java.util.Random  
import java.security.MessageDigest
import java.nio.file.*
import java.text.SimpleDateFormat
/*def date = new Date()
trnsID1 = new SimpleDateFormat("yyyymmdd")
trnsID = new SimpleDateFormat("ddMMhhss")
trnsID2 = new SimpleDateFormat("MMhhss")
def fileNM = 'JWCC_'+trnsID1.format(date)+'_DataUsage_'+trnsID.format(date)+'_'+trnsID2.format(date)+'_001.dat'
log.info('FileName is ' +fileNM) */

String uri = new String(context.getProperty("requestUri").toString()) 
log.info ('uri is ' +uri)
//log.info submit.request.getPath()
//submit.request.setPath("/product/api/v4.0/foo")
//log.info submit.request.getPath()

    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("Request 1")
    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("Properties1")).getProperty("FileNamePath")
    //FileName
    
    def fileName = testCase.getTestStepAt(testRunner.testCase.getTestStepIndexByName("Properties1")).getProperty("FileName")
    
    // get file to attach
    log.info "file to attach: " + fileNamePath.getValue()
    log.info (fileName)
    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( "Request 1#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 (fileName)
    }

5 Replies

    • Apoorva6's avatar
      Apoorva6
      Frequent Contributor

      Himanshu,

       

      In my test case, i have Groovy script which i mentioned above followed by my rest request . In request, file name that need to be attached is a part of resource path like below.

       

      /drservice/JPR/Ref/JW_20200910_000000.dat

       

      where JW_20200910_000000.dat is my file name. There are many such files in my input folder which should be passed to groovy and then to resource path dynamically so that request can be submitted. I need help on getting the resource path and setting file there and submit request.

      • HimanshuTayal's avatar
        HimanshuTayal
        Community Hero

        Apoorva6 :

         

        You can split the path with "/" and save the last array into any variable and use further in you code where every you want to save.

        Hope this will help. 🙂