如何将 Groovy 放在集中式 Groovy 库中并从任何脚本访问 class
How to place the Groovy in centralized Groovy Library and access that class from any script
我有下面的 Groovy 脚本,我需要将它放在集中的 Groovy 库中,然后从中的任何脚本访问 Groovy 中提到的 class我的 Ready API 项目
路径: 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 静态方法中提及什么。我是这个过程的新手,还没有做过。有人可以指导我吗!我已阅读有关 Ready API 的文档!网站。但是我不清楚。
啊!你是说一个实用程序库?
假设您已将 groovy 库文件放在此路径中
D:\GroovyLib\com\Linos\readyapi\util\property\propertyvalidation
如果您使用的是 soapui,则将上面的路径设置为下面字段的值,
File>preferences>SoapUiPro>Script Library
如果您正在使用就绪 api 那么,
File>preferences>Ready! API>Script Library
然后通过先初始化 class 来调用您的方法
PropertyValidation classObject = new PropertyValidation()
classObject.propertystepvalidation()
//you might need to pass some parameters which are declared/initiated outside this class
ReadyAPI 允许用户创建库并将它们放在 Script
目录下并根据需要重复使用它们。
请注意,ReadyAPI 不允许在 Script
目录中包含 groovy 脚本,而应该是 Groovy classes.
看起来您正在尝试将一个脚本(根据之前的问题之一回答)转换为 class。
Groovy SoapUI 脚本中有一些变量可用。因此,这些需要传递给库 class。例如,context, log
很常见。
还有一些与您的方法相关的参数。在这种情况下,您需要通过 file, property step name
等
这里是GroovyClass,是从脚本转换过来的。并且该方法可以是非静态的或静态的。但我选择非静态。
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
这样的名字。当然,由你决定。
下面是如何使用/调用上述 class 或其方法,来自不同 soapui 项目中测试用例的 Groovy Script
测试步骤:
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 中提到的 class我的 Ready API 项目 路径: 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 静态方法中提及什么。我是这个过程的新手,还没有做过。有人可以指导我吗!我已阅读有关 Ready API 的文档!网站。但是我不清楚。
啊!你是说一个实用程序库? 假设您已将 groovy 库文件放在此路径中
D:\GroovyLib\com\Linos\readyapi\util\property\propertyvalidation
如果您使用的是 soapui,则将上面的路径设置为下面字段的值,
File>preferences>SoapUiPro>Script Library
如果您正在使用就绪 api 那么,
File>preferences>Ready! API>Script Library
然后通过先初始化 class 来调用您的方法
PropertyValidation classObject = new PropertyValidation()
classObject.propertystepvalidation()
//you might need to pass some parameters which are declared/initiated outside this class
ReadyAPI 允许用户创建库并将它们放在 Script
目录下并根据需要重复使用它们。
请注意,ReadyAPI 不允许在 Script
目录中包含 groovy 脚本,而应该是 Groovy classes.
看起来您正在尝试将一个脚本(根据之前的问题之一回答)转换为 class。
Groovy SoapUI 脚本中有一些变量可用。因此,这些需要传递给库 class。例如,context, log
很常见。
还有一些与您的方法相关的参数。在这种情况下,您需要通过 file, property step name
等
这里是GroovyClass,是从脚本转换过来的。并且该方法可以是非静态的或静态的。但我选择非静态。
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
这样的名字。当然,由你决定。
下面是如何使用/调用上述 class 或其方法,来自不同 soapui 项目中测试用例的 Groovy Script
测试步骤:
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)