Decode base64 string to an image(pdf or png) using groovy

This is community board for the SoapUI Community. Here the members can exchange experiences and help each other improve their soapUI testing.

Decode base64 string to an image(pdf or png) using groovy

Postby RohitBBorse » 20 May 2011 06:15

Hi,

I wanted to decode base64 string to an image(pdf or png)

Originally, image type (pdf, png) is getting encoded into base64 file in xml response as below
<ns1:Label>
<ns1:Parts>
<ns1:DocumentPartSequenceNumber>1</ns1:DocumentPartSequenceNumber>
<ns1:Image>HOJBAAAElFTkSuQmCC.....</ns1:Image>
</ns1:Parts>
</ns1:Label>

I am trying to decode value from Image body to pdf or png.

Please suggest a groovy script for this.


Rohit
RohitBBorse
User
 
Posts: 20
Joined: 18 May 2011 13:49
Location: India

Re: Decode base64 string to an image(pdf or png) using groov

Postby Finan » 20 May 2011 08:50

I do not know how to switch the output thats base64 encode to a img, but I assume its an encoded bytestream, so I've written the following script that should do the trick.
The script is added to a new testCase that has the encoded String as testCase property. (So it can be run as runTestCase testStep).
The filename contains the path you want to write to relative to the projectpath. (Path should also include the extension)

Code: Select all
def groovyUtils = new com.eviware.soapui.support.GroovyUtils( context );
import org.apache.commons.codec.binary.Base64;
Base64 coder = new Base64();

def filename = context.expand( '${#TestCase#filename}' )
def encodedString = context.expand( '${#TestCase#encodedString}' );
def decoded = encodedString.decodeBase64();
def f = new File(groovyUtils.projectPath + filename);
f.delete();
f << decoded

log.info(groovyUtils.projectPath + filename);
Finan
Wizard
Wizard
 
Posts: 127
Joined: 06 Dec 2010 16:18

Re: Decode base64 string to an image(pdf or png) using groov

Postby RohitBBorse » 20 May 2011 11:42

Thanks Finan :)

But I am still not able to switch into desired output i.e. pdf or png image type.

I have my code as code below which saves the encodeed string as base64 extension into D drive. I then needs to extract the file from this base64 manually and view the image output.

Could you please look into the below code and suggest how I can make it to the expected final one.

----------- Code is as below ---------------
teststep=context.testCase.getTestStepAt(context.getCurrentStepIndex()).getLabel()

Image='$'+"{"+teststep+"#Response#//ns1:Label/ns1:Parts/ns1:Image}"
Label=context.expand(Image)

//Specify the location of the label to be saved as below.
def File = new PrintWriter ("D:/"+teststep+".b64")

File.println(Label)
File.flush()
File.close()
-------------------------------

Thanks in advance
Rohit
RohitBBorse
User
 
Posts: 20
Joined: 18 May 2011 13:49
Location: India

Re: Decode base64 string to an image(pdf or png) using groov

Postby SmartBear Support » 20 May 2011 13:52

Rohit,

No more cross-posting (posting the same topic in multiple forums) please. For more information, see the forum rules.

/Henrik
eviware
SmartBear Support
Administrator
Administrator
 
Posts: 8688
Joined: 16 Feb 2009 10:53

Re: Decode base64 string to an image(pdf or png) using groov

Postby RohitBBorse » 20 May 2011 15:08

Hi Henrik,

Sorry ! It happened by mistake. Ahead to this a due care will be taken.


Thanks
Rohit
RohitBBorse
User
 
Posts: 20
Joined: 18 May 2011 13:49
Location: India

Re: Decode base64 string to an image(pdf or png) using groov

Postby RohitBBorse » 23 May 2011 14:42

Hi,

I am able to switch the base64 string to image using Base64 class and swing builder as below:

Code: Select all
import groovy.swing.SwingBuilder
import javax.swing.ImageIcon
import groovy.swing.SwingBuilder
import org.apache.commons.codec.binary.Base64

teststep=context.testCase.getTestStepAt(context.getCurrentStepIndex()).getLabel()
Image='$'+"{"+teststep+"#Response#//ns1:Label/ns1:Parts/ns1:Image}"
Label=context.expand(Image)
b64 = new Base64()
swing = new groovy.swing.SwingBuilder()
i1 = swing.label(icon:new ImageIcon(b64.decode(Label.getBytes())))

frame = swing.frame(title:"LABEL IMAGE", defaultCloseOperation:javax.swing.WindowConstants.DISPOSE_ON_CLOSE) {
  panel(){
    widget(i1)
  }
}
frame.pack()
frame.show()


However, I wanted the frame to be saved in local repository rather being displayed for the time being.

Secondly, I again need help for generating images if there exists more than 1 image parts in an xml response as below:
Code: Select all
<ns1:Label>
   <ns1:Name>Label01</ns1:Parts>
   <ns1:Parts>
      <ns1:DocumentPartSequenceNumber>1</ns1:DocumentPartSequenceNumber>
      <ns1:Image>HOJBAAAElFTkSuQmCC.....</ns1:Image>
   </ns1:Parts>
</ns1:Label>
<ns1:Label>
   <ns1:Name>Label02</ns1:Parts>
   <ns1:Parts>
      <ns1:DocumentPartSequenceNumber>1</ns1:DocumentPartSequenceNumber>
      <ns1:Image>dfdJBAAAElFTkSuQmCC.....</ns1:Image>
   </ns1:Parts>
</ns1:Label>


I am working on the same. Please to-time help me with groovy script for the above two issues.


Thanks
Rohit Borse
RohitBBorse
User
 
Posts: 20
Joined: 18 May 2011 13:49
Location: India

Re: Decode base64 string to an image(pdf or png) using groov

Postby RohitBBorse » 26 May 2011 12:20

Hi,

Its again me here.

A groovy script for genearting image from base64 string has been created. We can save this image in the desired local repository. Now there need to address the second issue.


Thanks
Rohit Borse
RohitBBorse
User
 
Posts: 20
Joined: 18 May 2011 13:49
Location: India


Return to SoapUI Community Board