Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to place the Groovy in centralized Groovy Library and access that class from any script

I have the below Groovy script which i need to place it in centralized Groovy Library and then access the class mentioned in the Groovy from any script in my Ready API Project Path: D:\GroovyLib\com\Linos\readyapi\util\property\propertyvalidation

//Change the name of the Properties test step below
def step = context.testCase.testSteps['Properties']

//Parse the xml like you have in your script
def parsedXml = new XmlSlurper().parse(file)

//Get the all the error details from the response as map
def errorDetails = parsedXml.'**'.findAll { it.name() == 'IntegrationServiceErrorCode'}.inject([:]){map, entry ->    map[entry.ErrorCode.text()] = entry.Description.text(); map    }
log.info "Error details from response  : ${errorDetails}"

def failureMessage = new StringBuffer()

//Loop thru properties of Property step and check against the response
step.properties.keySet().each { key ->
   if (errorDetails.containsKey(key)) {
       step.properties[key]?.value == errorDetails[key] ?:  failureMessage.append("Response error code discription mismatch. expected [${step.properties[key]?.value}] vs actual [${errorDetails[key]}]")
   } else {
       failureMessage.append("Response does not have error code ${key}")
   }
}
if (failureMessage.toString()) {
  throw new Error(failureMessage.toString())
} 

I have created the code in Library as below:

package com.Linos.readyapi.util.property.propertyvalidation
import com.eviware.soapui.support.GroovyUtils
import groovy.lang.GroovyObject
import groovy.sql.Sql

class PropertyValidation
{
    def static propertystepvalidation()
{
//Change the name of the Properties test step below
def step = context.testCase.testSteps['Properties']

//Parse the xml like you have in your script
def parsedXml = new XmlSlurper().parse(file)

//Get the all the error details from the response as map
def errorDetails = parsedXml.'**'.findAll { it.name() == 'IntegrationServiceErrorCode'}.inject([:]){map, entry ->    map[entry.ErrorCode.text()] = entry.Description.text(); map    }
log.info "Error details from response  : ${errorDetails}"

def failureMessage = new StringBuffer()

//Loop thru properties of Property step and check against the response
step.properties.keySet().each { key ->
   if (errorDetails.containsKey(key)) {
       step.properties[key]?.value == errorDetails[key] ?:  failureMessage.append("Response error code discription mismatch. expected [${step.properties[key]?.value}] vs actual [${errorDetails[key]}]")
   } else {
       failureMessage.append("Response does not have error code ${key}")
   }
}
if (failureMessage.toString()) {
  throw new Error(failureMessage.toString())
} 

I am not sure what to mention in the def static method. I am new to this process and haven't done that yet. Can someone please guide me! I've read the documentation on Ready API! website. But I'm not clear on that.

like image 553
Kalaiselvan Avatar asked Apr 06 '17 11:04

Kalaiselvan


People also ask

How do I run a Groovy script?

To run a Groovy script, from the context menu in the editor, select Run 'name' Ctrl+Shift+F10 .

Where can Groovy script be used in soap?

Scripts can be used at the following places in SoapUI: As part of a TestCase with the Groovy Script TestStep, allowing your tests to perform virtually any desired functionality. Before and after running a TestCase or TestSuite for initializing and cleaning up before or after running your tests.


1 Answers

ReadyAPI allows users to create libraries and put them under Script directory and reuse them as needed.

Please note that, ReadyAPI does not allow to have a groovy script in the Script directory, instead it should be Groovy classes.

Looks you were trying to convert a script, answered from one of the previous questions, to class.

Here there are certain variables available in Groovy Script by SoapUI. So, those needs to passed to library class. For instance, context, log are common.

And there can be some more parameters related to your method as well. In this case, you need to pass the file, property step name etc.

Here is the Groovy Class which isconverted from the script. And the method can be non-static or static. But I go with non-static.

package com.Linos.readyapi.util.property.propertyvalidation

class PropertyValidator {

    def context
    def log

    def validate(String stepName, File file) {
        //Change the name of the Properties test step below
        def step = context.testCase.testSteps[stepName]

        //Parse the xml like you have in your script
        def parsedXml = new XmlSlurper().parse(file)

        //Get the all the error details from the response as map
        def errorDetails = parsedXml.'**'.findAll { it.name() == 'IntegrationServiceErrorCode'}.inject([:]){map, entry ->    map[entry.ErrorCode.text()] = entry.Description.text(); map    }
        log.info "Error details from response  : ${errorDetails}"

        def failureMessage = new StringBuffer()

        //Loop thru properties of Property step and check against the response
        step.properties.keySet().each { key ->
            if (errorDetails.containsKey(key)) {
                step.properties[key]?.value == errorDetails[key] ?:  failureMessage.append("Response error code discription mismatch. expected [${step.properties[key]?.value}] vs actual [${errorDetails[key]}]")
            } else {
                failureMessage.append("Response does not have error code ${key}")
            }
        }
        if (failureMessage.toString()) {
            throw new Error(failureMessage.toString())
        } 
    }
}

Hope you already aware where to copy the above class. Note that this also has package name. So copy it in the right directory. I have suggestion here regarding your package name which is so long, you can change it to something like com.linos.readyapi.util. Of course, upto you.

Now here is how you can use / call the above class or its methods from a Groovy Script test step of a test case in the different soapui projects:

Groovy Script step

import com.Linos.readyapi.util.property.propertyvalidation.PropertyValidator

def properties = [context:context, log:log] as PropertyValidator
//You need to have the file object here
properties.validate('Properties', file)
like image 113
Rao Avatar answered Nov 15 '22 13:11

Rao