5

我有下面的 Groovy 脚本,我需要将它放在集中式 Groovy 库中,然后从我的 Ready API 项目 路径中的任何脚本访问 Groovy 中提到的类:D:\GroovyLib\com\Linos\readyapi\util\property\属性验证

//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 的文档!网站。但我不清楚这一点。

4

2 回答 2

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

希望您已经知道在哪里复制上述课程。请注意,这也有包名。因此,将其复制到正确的目录中。我在这里有关于你的包名的建议,它太长了,你可以把它改成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)
于 2017-04-07T04:36:21.380 回答
1

啊! 你的意思是一个实用程序库?假设您已将 groovy 库文件放在此路径中

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

如果您使用的是soapui,则将上述路径设置为以下字段的值,

文件>首选项>SoapUiPro>脚本库

如果您使用的是现成的 api,

文件>首选项>准备好了!API>脚本库

然后通过首先初始化类来调用你的方法

PropertyValidation classObject = new PropertyValidation()
classObject.propertystepvalidation()
//you might need to pass some parameters which are declared/initiated outside this class
于 2017-04-06T22:30:49.830 回答