我有下面的Groovy脚本,我需要将它放在集中的Groovy中,然后从就绪API项目Path:中的任何脚本中访问Groovy中提到的类
//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静态方法中应该提到什么。我对这个过程还不熟悉,还没有做过。有人能指点我吗!我已经阅读了现成API的文档!网站。但我不太清楚。
发布于 2017-04-07 04:36:21
ReadyAPI允许用户创建库,并将它们放在Script目录下,并根据需要重用它们。
请注意,ReadyAPI不允许在Script目录中有一个groovy脚本,而应该是Groovy类。
看起来你正试图把一个脚本,从前面的一个问题回答到课堂上。
下面是SoapUI在Groovy脚本中可用的某些变量。因此,这些需要传递给库类。例如,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())
}
}
}希望您已经知道在哪里复制上述课程。请注意,这也有包名。所以把它复制到正确的目录中。我在这里有关于您的包名的建议,它太长了,您可以将它更改为类似于com.linos.readyapi.util的东西。当然,你说了算。
下面是如何在不同的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)发布于 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库
然后先初始化类来调用您的方法
PropertyValidation classObject = new PropertyValidation()
classObject.propertystepvalidation()
//you might need to pass some parameters which are declared/initiated outside this classhttps://stackoverflow.com/questions/43254590
复制相似问题