Wednesday, March 9, 2016

Useful Groovy Scripts for SoapUI

Basics of Groovy Script…

1.       To Print in the console
log.info(“Print Groovy”)

OutPut :- Print Groovy



2.       To get Test Case Name
Type 1 - log.info(testRunner.testCase.name)
Type 2 - log.info(context.getTestCase().name)
Type 3 - log.info(context.testCase.name)

OutPut :- Test Case 1



3.       To get Test Suite Name
Type 1 - log.info(context.testCase.testSuite.name)
Type 2 - log.info(context.getTestCase().getTestSuite().name)
Type 3 - log.info(testRunner.testCase.testSuite.name)

OutPut :- Test Suite 1



4.       To get Project Name
Type 1 - log.info(context.testCase.project.name)
Type 2 - log.info(context.testCase.testSuite.project.name)
Type 3 - log.info(context.getTestCase().getProject().name)
Type 4 - log.info(context.getTestCase().getTestSuite().getProject().name)
Type 5 - log.info(testRunner.testCase.project.name)
Type 6 - log.info(testRunner.getTestCase().getProject().name)
Type 7 - log.info(testRunner.testCase.testSuite.project.name)
Type 8 - log.info(testRunner.getTestCase().getTestSuite().getProject().name)

OutPut :- tempconvert (P.S. Same output for all above query)



5.       To get Test Case Count and Test Case Name
def testCases = context.testCase.testSuite.getTestCaseList()
log.info("Number Test Cases Present :- "+testCases.size())
testCases.each  {
        log.info("Test Cases Name :- "+it.name)
}

OutPut :- Number Test Cases Present :- 2
                           Test Cases Name :- TestCase 1
   Test Cases Name :- TestCase 2



6.       To get Test Suite Count and Test Suite Name
def testSuites = context.testCase.project.getTestSuiteList()
log.info("Number Test Suites Present :- "+testSuites.size())
testSuites.each  {
        log.info("Test Suite Name :- "+it.name)
}

OutPut :- Number Test Suites Present :- 2
                           Test Suite Name :- TestSuite 1
   Test Suite Name :- TestSuite 2



7.       To get Project Count and Project Name
def getProjectsList = testRunner.testCase.testSuite.project.workspace.getProjectList()
log.info("Number of Projects :- " +getProjectsList.size)
for(i in 0..getProjectsList.size-1 ){
        log.info("Project name :- "+getProjectsList.getAt(i).name)
}

        OutPut :- Number of Projects :- 5
   Project name :- Project1
   Project name :- Project2
   Project name :- Project3
   Project name :- Project4
   Project name :- Project5



8.       To get count of blank spaces
def val = "Groovy Scripting Language is used for SOAPUI"
def sval = val.replaceAll(" ","")
def sCount =  val.size() - sval.size()
 log.info("Count of Blank (' ') spaces is = $sCount")

OutPut :- Count of Blank(‘ ‘) spaces is = 6



9.       To get count of SAME Char repeated
def val = "Groovy"
def sval = val.replaceAll("o","")
def sCount =  val.size() - sval.size()
 log.info("The Char 'o' Repeats = $sCount Times")

        OutPut :- The Char 'o' Repeats = 2 Times



10.   To set project property values

testRunner.testCase.testSuite.project.setPropertyValue("ProjectProp"," ProjectProp Value")
context.getTestCase().getTestSuite().getProject().setPropertyValue("ProjectProp", " ProjectProp Value")



11.   To get project property values

def projPropValue = testRunner.testCase.testSuite.project.getPropertyValue("ProjectProp")
log.info(projPropValue)

def projPropValue1 = context.getTestCase().getTestSuite().getProject().getPropertyValue("ProjectProp")
log.info(projPropValue1)

OutPut :- ProjectProp Value 1
           ProjectProp Value 2




12.   To set TestSuite property values

testRunner.testCase.testSuite.setPropertyValue("TestSuiteProp","TestSuite Property Value")
context.getTestCase().getTestSuite().setPropertyValue("TestSuiteProp","TestSuite Property Value")




13.   To get TestSuite property values

def testSuitePropValue = testRunner.testCase.testSuite.getPropertyValue("TestSuiteProp")
log.info(testSuitePropValue)

def testSuitePropValue1 = context.getTestCase().getTestSuite().getPropertyValue("TestSuiteProp")
log.info(testSuitePropValue)

OutPut :- TestSuite Property Value
           TestSuite Property Value 




14.   To set TestCase property values

testRunner.testCase.setPropertyValue("TestCaseProp", "TestCase Property Value")
context.getTestCase().setPropertyValue("TestCaseProp", "TestCase Property Value")




15.   To get TestCase property values

def testCasePropValue = testRunner.testCase.getPropertyValue("TestCaseProp")
log.info(testCasePropValue)

def testCasePropValue2 = context.getTestCase().getPropertyValue("TestCaseProp")
log.info(testCasePropValue2)

OutPut :- TestCase Property Value
           TestCase Property Value




 ******************* Sample Request XML **************************
Note :-  Here I have taken sample request xml from “SendSMSToIndia” service (http://www.webservicex.net/SendSMS.asmx)
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:web="http://www.webserviceX.NET">
   <soapenv:Header/>
   <soapenv:Body>
      <web:SendSMSToIndia>
         <web:MobileNumber>9879879785465</web:MobileNumber>
         <web:FromEmailAddress>mail@email.com</web:FromEmailAddress>
         <web:Message>Hi Hello!!!!!</web:Message>
      </web:SendSMSToIndia>
   </soapenv:Body>
</soapenv:Envelope>
************************************************************************


16.   To get Request Content
                        def groovyUtils = new com.eviware.soapui.support.GroovyUtils(context)
def reqHolder = groovyUtils.getXmlHolder("OperationName#Request")

                    reqHolder holds all the request XML content.
      
     Note :- “reqHolder” we will use same variable in our next scripts.




17.   To get or Read request XML tag/Element value
Consider above sample request xml, in that we have three fields 1. MobileNumber, 2. FormEmailAddress and 3. Message.

To get value from each filed use below code.
Syntax :- holder.getNodevalue(“XPATH of xml element name”)
log.info(reqHolder.getNodeValue(“//*:MobileNumber”))
log.info(reqHolder.getNodeValue(“//*:FromEmailAddress”))
log.info(reqHolder.getNodeValue(“//*:Message”))

OutPut :-     9879879785465
                        mail@email.com
Hi Hello!!!!!




18.   To Set values to Request XML tag/element

Syntax :- holder.setNodevalue(“XPATH of xml element name”,”String value”)
reqHolder.setNodeValue(“//*:MobileNumber”, ”2487987646”)
reqHolder.setNodeValue(“//*:FromEmailAddress”, ”email@mailer.com”)
reqHolder.setNodeValue(“//*:Message”, ”Hello World”)
reqHolder.updateProperty ()       // this line of code will save the request xml.

19.   To Get count of xml tag/element

Syntax :- holder[Count(“xml element xpath”)]
log.info(reqHolder["count(//*:MobileNumber)"])
log.info(reqHolder["count(//*:FromEmailAddress)"])
log.info(reqHolder["count(//*:Message)"])

OutPut :- 1
          1
     1






20.   Reading response
******************* Sample Response XML *************************
Note :-  Here I have taken sample response xml from “SendSMSToIndia” service (http://www.webservicex.net/SendSMS.asmx)
<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
   <soap:Body>
      <SendSMSToIndiaResponse xmlns="http://www.webserviceX.NET">
         <SendSMSToIndiaResult>
            <FromEmailAddress>mail@gmail.com</FromEmailAddress>
            <MobileNumber>9879879785465</MobileNumber>
            <Provider>Not Covered</Provider>
            <State>Not Covered</State>
            <Status>Not Covered</Status>
         </SendSMSToIndiaResult>
      </SendSMSToIndiaResponse>
   </soap:Body>
</soap:Envelope>*********************************************************

Note :- Here we will use same groovyutills class object as we are already created (groovyUtils) object above in at section 16.
def resHolder = groovyUtils.getXmlHolder("OperationName#Response")

resHolder holds all the response XML content.

Note :- “resHolder” we will use same variable in our next scripts.


To read or get value from response xml
Syntax :- holder.getNodevalue(“XPATH of xml element name”)
log.info(resHolder.getNodeValue(“//*:FromEmailAddress”))
log.info(resHolder.getNodeValue(“//*:MobileNumber”))
log.info(resHolder.getNodeValue(“//*:Provider”))
log.info(resHolder.getNodeValue(“//*:State”))
log.info(resHolder.getNodeValue(“//*:Status”))

OutPut :- mail@gmail.com
                9879879785465
    Not Covered
    Not Covered
    Not Covered
  
21.   If… else condition.

Syntax :-  If(condition){
                                      …….
     }
       else{
        …….
}

If(“Groovy”.equals(“Groovy”)){
  log.info(“Pass”)
}else{
  log.info(“Fail”)
}
if(“Groovy”.equals(“Groovi”)){
  log.info(“Pass”)
}else{
  log.info(“Fail”)
}
OutPut :- Pass
OutPut :- Fail
If(10==10){
  log.info(“Pass”)
}else{
  log.info(“Fail”)
}
if(5==8){
  log.info(“Pass”)
}else{
  log.info(“Fail”)
}
OutPut :- Pass
OutPut :- Fail

22.   If …. else if…else condition

Syntax :-  If(condition){
                        …….
     }
       else if{
        …….
}
       else if{
        …….
}
       else{
        …….
}

If(“Groovy”.equals(“Groovy”)){
  log.info(“Groovy”)
}else if(“SoapUI”.equals(“SoapUI”)){
  log.info(“SoapUI”)
}
else{
  log.info(“Fail”)
}
If(“Groovy”.equals(“Groov”)){
  log.info(“Groovy”)
}else if(“SoapUI”.equals(“Soap”)){
  log.info(“SoapUI”)
}
else{
  log.info(“Condition Not Met”)
}
OutPut :- Groovy or SoapUI
OutPut :- Condition Not Met
If(25==25){
  log.info(25)
}else if(48==48){
  log.info(48)
}
else{
  log.info(“Fail”)
}
If(25==50){
  log.info(25)
}else if(48==84){
  log.info(48)
}
else{
  log.info(“Condition Not Met”)
}
OutPut :- 25 or 48
OutPut :- Condition Not Met


23.   For loop
Syntax :-  for(initialization ; condition ; statement){
                        Statement block
}

for(int i = 1; i<=5;i++){
        log.info(i)
}

  OutPut :- 1
     2
     3
     4
     5


24.   How to remove attributes present in XMLtag element
For example:- To configure or set values to element or tag which has ‘nil’ attributes.
Follow the below steps to remove the ‘nil’ attribute from xml tag/element and provide values to it.

Consider sample request xml tag which ‘nil’ attribute as below,
  <sendSMSMessage xsi:nil="true" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"/>

        def sendSMSMessageNode = reqHolder.getDomNode("//*:sendSMSMessage ")                         
        sendSMSMessageNode.attributes.removeNamedItem("xsi:nil")
        sendSMSMessageNode.attributes.removeNamedItem("xmlns:xsi")   
        reqHolder.setNodeValue("//*:sendSMSMessage ", “Hi Hello!!!!!!” )

Here sendSMSMessageNode.attributes.removeNamedItem(“….”) removes attributes present in xml tag.




                         That’s all Folks.


8 comments:

  1. Excellent blog Sandeep .Very useful for beginners to start writing groovy scripts

    Thanks,
    Santosh

    ReplyDelete
  2. Hi Sandeep,

    Could you post some example for the REST service?

    Thanks
    Amit

    ReplyDelete
    Replies
    1. This comment has been removed by the author.

      Delete
  3. Hi Sunny ,
    Can you please post How to customize soapUI reports in soapui open source

    ReplyDelete
    Replies
    1. Hello Jhansi,

      Thanks your time to reading my blog.

      Please refer the below link for one of my post is on soapui reports in Excel file.

      https://lgsofttest.blogspot.no/2014/06/datadriven-testing-in-soapui-open.html


      Let me know if any more info needed.


      Regards,
      Sandeep S S

      Delete
  4. Thanks for the reply ,Really your blog is Excellent and please keep posting new concepts on soapui..
    Could you please post on Service Orchestration and Choreography .How to test Service orchestration and Choreography testing it using SoapUI .Where we use these two concepts ?? On what basis we identify which scenario is Service Orchestration or Choreography ?? Could you please post some example for these concepts ??

    ReplyDelete
  5. Hi Sandeep,

    Thanks for sharing the knowledge. Kindly let me know where can I study more about Groovy in SOAPUI??

    ReplyDelete
  6. Hi Sandeep,

    Very useful information, Keep going

    Can you also provide article on below
    1. Execute test cases from excel based on Yes/No flag
    2. Driver file to kick off test cases

    ReplyDelete