首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >如何将Groovy放在集中的Groovy库中,并从任何脚本访问该类

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

Stack Overflow用户
提问于 2017-04-06 11:59:00
回答 2查看 1.5K关注 0票数 5

我有下面的Groovy脚本,我需要将它放在集中的Groovy中,然后从就绪API项目Path:中的任何脚本中访问Groovy中提到的类

代码语言:javascript
复制
//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())
} 

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

代码语言:javascript
复制
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())
} 

我不知道在def静态方法中应该提到什么。我对这个过程还不熟悉,还没有做过。有人能指点我吗!我已经阅读了现成API的文档!网站。但我不太清楚。

EN

Stack Overflow用户

发布于 2017-04-06 22:30:49

阿!你指的是一个实用图书馆?假设您已经将groovy库文件放置在此路径中

D:\GroovyLib\com\Linos\readyapi\util\property\propertyvalidation

如果您使用的是soapui,那么将上面的路径设置为下面字段的值,

File>preferences>SoapUiPro>Script库

如果您使用的是就绪api,

File>preferences>Ready!API>Script库

然后先初始化类来调用您的方法

代码语言:javascript
复制
PropertyValidation classObject = new PropertyValidation()
classObject.propertystepvalidation()
//you might need to pass some parameters which are declared/initiated outside this class
票数 1
EN
查看全部 2 条回答
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/43254590

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档