Thursday, 20 December 2012

SoapUI & Groovy Script - Tips

I've been working on a SoapUI project for the past few months.
I'm novice to SoapUI & web services.. Here are few tips which I think would be helpful for learners on this area.
 SoapUI is used to mimic the web server responses. Based on the request you can have a response.
 In SoapUI there are many ways you can dispatch a response. Scripting is most versatile and complex way to do that.

Usually we can do the scripting using groovy. But you may handle the same using your java code as well. However using Groovy & SoapUI APIs will reduce the coding much.

Groovy Scripting

There are few inbuilt variables available in SoapUI. They are log, mockRequest, context.

1) To define a variable

 def reqHolder = new XmlSlurper().parseText(mockRequest.requestContent);
def groovyUtils = new com.eviware.soapui.support.GroovyUtils(context);
def holderRequest = groovyUtils.getXmlHolder(mockRequest.requestContent);
def responseLOB = reqHolder.Body.VerificationRequest.LOB;


2) How to declare a name space with a request

 holderRequest.declareNamespace('ns', 'http://www.xyz.xx.ic/verificationprocess/2');

3) To get a value from node.
 
 def idValue = holderRequest.getNodeValue("//ns:ID");

 getNodeValue() and [] are  for same purpose. But the latter is for returning array of values.

4) To see the number of items with same node, we can use getNodeValues().

def idValue = holderRequest.getNodeValue("//ns:ID");
def allIDFiles = holderRequest.getNodeValues("//ns:ID");


and then check the size of allIDFiles variable.

if (allIDFiles.length > 1) {
}


 5) To iterate through a list of files stored in your local direcrtory and compare the name of the file with the file name in the request using groovy script.

new File( folderName ).eachFile() { file ->
                if( file.name =~ /.xml/ ) {                   
                    if (file.name == "$responseValueWithFileName") {
                        log.info "Match Found for ID : "+"$responseValue";
                        actual = "$folderName"+"\\"+"$responseValueWithFileName"
                        context.content = new File("$actual").text;                                                              
                    }
                 }
             }






















Disclaimer:The information given in this blog are based on my coding experience. I am not assuring that the solutions provided here would be suitable for what you are looking :-)Just don't want to dirty my hands...

1 comment:

swathi chowdary said...

Hi its nice... do you have any sample project? how you are generating reports. can you please share it