繁体   English   中英

在groovy脚本中将groovy字符串作为另一个脚本运行

[英]Run a groovy string in a groovy script as a different script

在项目需求中,我试图将现有的SOAP UI测试套件移植到Maven Java项目中,以测试将常规服务与groovy断言一起编写的剩余服务。

该方法是使用XmlSlurper解析SOAP UI项目xml文件,以获取请求和其他所需的详细信息,以使用http客户端触发其余服务,并接收来自该服务的响应 ,然后使用已经嵌入在SOAP UI套件中相同的常规断言声明使用groovy脚本收到的响应。

之所以决定采用这种方法,是因为已经有成千上万的用SOAP UI编写的测试用例,我们希望利用它们,而不是在重写测试数据上花费更多的精力。

解析xml时,我们可以轻松地从xml中的变量中获取字符串或GPathResult更具体的断言

我无法弄清楚的是如何使“断言字符串”作为从我的服务收到的响应的“断言脚本”运行。

这是我尝试使用的代码:

DemoTest.groovy

package somepackage.groovy

import groovyx.net.http.ContentType
import groovyx.net.http.RESTClient

class Demotest extends GroovyTestCase {

    void testFail() {
        def client = new RESTClient('http://localhost:8080')
        def xmlfile = new XmlSlurper().parse(new File('/path/to/soap-ui.xml'))
        def endpoint = (xmlfile.testSuite.testCase[0].testStep[0].config.@service)
        def resourcePath =(xmlfile.testSuite.testCase[0].testStep[0].config.@resourcePath)
        def request = (xmlfile.testSuite.testCase[0].testStep[0].config.restRequest.request).text()
        def assertion = (xmlfile.testSuite.testCase[0].testStep[0].config.restRequest.assertion).text()
        def bodyMap = new groovy.json.JsonSlurper().parseText(request)
        try {
            def resp = client.post(
                    path: resourcePath,
                    body: bodyMap,
                    requestContentType: ContentType.JSON
            )
            println( resp.data )
    //            def jsonSlurper = resp.data
            def asserts = assertion.substring(assertion.indexOf('assert').intValue())
            String script = asserts
    //                    .replaceAll('jsonSlurper','resp.data')
            GroovyScriptEngine gse = new GroovyScriptEngine()
            Binding binding = new Binding();
            binding.setVariable('jsonSlurper', resp.data )
            Object result = gse.run(script, binding)
            println( result )
        } catch (ex){
            println( ex.printStackTrace() )
        }
    }
}

Soap UI断言

//imports
import groovy.json.JsonSlurper
//grab the response
def ResponseMessage = messageExchange.response.responseContent
//define a JsonSlurper
def jsonSlurper = new JsonSlurper().parseText(ResponseMessage)
//asserts
assert jsonSlurper.someValue == expectedValue
assert jsonSlurper.someValue.someOtherValue == expectedOtherValue
assert jsonSlurper...

我认为对于您的情况,使用GroovyShell代替GroovyScriptEngine更好,因为使用脚本作为Strings来运行更容易。

假设您正确获取了def assertions变量中的Script断言 ,则可以使用以下代码运行脚本:

import groovy.json.JsonSlurper

...
...

def assertion = '''//imports
 import groovy.json.JsonSlurper
//grab the response
 def ResponseMessage = messageExchange.response.responseContent
//define a JsonSlurper
 def jsonSlurper = new JsonSlurper().parseText(ResponseMessage)
//asserts
assert jsonSlurper.field == 'value'
assert jsonSlurper.otherField == 'secondValue' '''

def script = assertion.substring(assertion.indexOf('//asserts').intValue())

Binding binding = new Binding(); 
def shell = new GroovyShell(binding)
binding.setProperty('jsonSlurper', new JsonSlurper().parseText('{"field":"value", "otherField" : "secondValue"}') )
shell.evaluate(script)

另请注意,我更改了您的substring条件以获取脚本的一部分; 因为按照问题的原样,它与注释匹配//asserts不直接//asserts assert ,并且返回错误的脚本。 所以我将条件更改为substring(//asserts)以获取正确的脚本。

另外,由于您没有显示断言的方式,因此我假设该示例使用一个简单的json ,但是在您的代码中您必须更改此设置:

binding.setProperty('jsonSlurper', new JsonSlurper().parseText('{"field":"value", "otherField" : "secondValue"}') )

为此:

binding.setProperty('jsonSlurper', new JsonSlurper().parseText(resp.data) )

希望能帮助到你,

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM