Forum Discussion

HEATConsultant's avatar
HEATConsultant
Occasional Contributor
6 years ago

How to load a file as Base64 in a loop using source data property as filename

I have a data load that uses XLS as a source and SOAP as a destination.  

 

The test steps are:

 

  1. Data Source
  2. Create Record - Post SOAP Request
  3. Add Attachment  - Post SOAP Request 
    Note:  2 post requests are needed as there are two different post requests for the related record and attachment.
  4. Datasource Loop

 

The above works fine and the records are created and attachment added where the attachment is a property (column) in the XLS file and contains base64 data.

 

What I would like to be able to do instead is load a file at run time by using a column in the xls file to specify the file name to load and convert.  

 

for example the source column would contain the value  C:\Temp\MyPic.jpg and when posting the soap request SoapUI would load the file C:\Temp\MyPic.jpg, convert it, and pass it as Base64 data in the Soap Properties.

1 Reply

  • Radford's avatar
    Radford
    Super Contributor

    You should be able to do this with a Groovy Script Test Step, using code something like:

     

    // You may have to escape backslashes, or store the file path and name as a URI
    def yourFilename = 'Obtain filename from data source'
    
    def file = new File(yourFilename)
    
    def base64encodedData = file.getBytes().encodeBase64().toString()
    log.info('Base64 data = ' + base64encodedData)
    
    def soapRequestData = 'Build your SOAP request with Base64 data here'
    
    testRunner.getTestCase().getTestStepByName('SOAP Request').setPropertyValue('Request', soapRequestData)

    If you are unsure how to get the filename from the data source via Groovy, you can use the point and click Get Data GUI method.