How to host Groovy in a centralized Groovy library and access this class from any script

I have a Groovy script below, which I need to place in the Groovy centralized library, and then access the class specified in 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 created the code in the Library as shown 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 say in the def static method. I am new to this process and have not done it yet. Can someone please guide me! I read the Ready API documentation! Web site. But I do not understand.

+4
source share
2 answers

ReadyAPI Script .

, ReadyAPI groovy script Script, groovy classes.

, script, , .

, groovy script SoapUI. , . , context, log .

, . file, property step name ..

groovy, script. . .

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())
        } 
    }
}

, , . , . . , , - com.linos.readyapi.util. , .

/ Groovy Script soapui:

Groovy script

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)
+4

! ? , groovy

D:\GroovyLib\\Linos\readyapi\Util\\propertyvalidation

soapui, ,

> > SoapUiPro > Script

api,

> > ! API > Script

,

PropertyValidation classObject = new PropertyValidation()
classObject.propertystepvalidation()
//you might need to pass some parameters which are declared/initiated outside this class
+1

Source: https://habr.com/ru/post/1674200/


All Articles