Forum Discussion

rajpenumalli's avatar
rajpenumalli
Occasional Contributor
7 years ago

How to Upload a CSV file Via Groovy Script using SOAPUI Open Source ?

Hi all,

Can anybody share me the sample script to  Upload a CSV/Excel/txt file Via Groovy Script using SOAPUI Open Source version.

 

Thanks,

Rajendra

1 Reply

  • where are you wanting to upload a file to? I've used scripts to do similar things but they are specific to where I am loading them to.

     

    Here is some groovy for loading a file to a service that uploads files. It calls the REST API and loads the details to the "Attachment" tab on the service request.

     

    // encode file
    ResultsPath = context.testCase.getPropertyValue("resultFilePath")
    def inputFile = new File(ResultsPath)
    String encoded = inputFile.bytes.encodeBase64().toString()
    testRunner.testCase.setPropertyValue("EncodedFileRequest", "$encoded" )

     

    // get request
    def request = testRunner.testCase.getTestStepByName( "AttachmentContentRequest" ).testRequest

     

    // clear existing attachments
    for( a in request.attachments ) {
    request.removeAttachment( a )
    }

     

    // get file to attach
    ResultsPath = context.testCase.getPropertyValue("resultFilePath")
    def file = new File(ResultsPath)
    if ( file == null) {
    log.info "bad filename"
    }
    else
    {


    // attach and set properties
    def attachment = request.attachFile( file, true )
    attachment.contentType = "base64Binary"
    attachment.setPart( "Message" )
    }