gpt4 book ai didi

groovy - 如何将 Groovy 放在集中式 Groovy 库中并从任何脚本访问该类

转载 作者:行者123 更新时间:2023-12-04 18:02:20 26 4
gpt4 key购买 nike

我有下面的 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())
}

我在库中创建了如下代码:
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 static 方法中要提到什么。我是这个过程的新手,还没有这样做。有人可以指导我吗!我已经阅读了关于 Ready API 的文档!网站。但我不是很清楚。

最佳答案

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

希望您已经知道在哪里复制上述类(class)。请注意,这也有包名称。所以把它复制到正确的目录中。
我在这里有关于你的包名的建议,它太长了,你可以把它改成 com.linos.readyapi.util 之类的东西。 .当然,取决于你。

现在,您可以通过 Groovy Script 使用/调用上述类或其方法。不同soapui项目中测试用例的测试步骤:

Groovy 脚本步骤
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)

关于groovy - 如何将 Groovy 放在集中式 Groovy 库中并从任何脚本访问该类,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43254590/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com