如何将Groovy放在集中的Groovy库中,并从任何脚本访问该类

Har*_*rry 5 groovy soapui ready-api

我有下面的Groovy脚本,我需要将它放在集中的Groovy库中,然后从我的Ready API项目路径中的任何脚本访问Groovy中提到的类 :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())
} 
Run Code Online (Sandbox Code Playgroud)

我在库中创建了如下代码:

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())
} 
Run Code Online (Sandbox Code Playgroud)

我不确定在def静态方法中要提到什么.我是这个过程的新手,还没有这样做.有人可以指导我!我已经阅读了Ready API上的文档!网站.但我不清楚这一点.

Rao*_*Rao 6

ReadyAPI允许用户创建库并将它们放在Script目录下并根据需要重用它们.

请注意,ReadyAPI不允许在Script目录中使用groovy脚本,而应该是Groovy类.

看起来你试图将一个脚本(从之前的一个问题回答)转换为课程.

这里有一些由SoapUI在Groovy Script中提供的变量.所以,那些需要传递给库类.例如,context, log很常见.

并且可能还有一些与您的方法相关的参数.在这种情况下,你需要通过file, property step name等.

这是从脚本转换而来的Groovy类.并且该方法可以是非静态的或静态的.但我选择非静态.

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())
        } 
    }
}
Run Code Online (Sandbox Code Playgroud)

希望你已经知道在哪里复制上面的课程.请注意,这也有包名称.所以将它复制到正确的目录中.我在这里建议你的包名很长,你可以把它改成类似的东西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)
Run Code Online (Sandbox Code Playgroud)