引用groovy变量作为JSON路径的一部分 [英] Referencing groovy variable as part of JSON path

查看:327
本文介绍了引用groovy变量作为JSON路径的一部分的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用Ready API测试用例中的groovy脚本来验证json web服务响应中的结果。



我想使用一个变量(即在数据源中指定)指定我想验证的json路径,因为这可能会在每次测试运行时发生变化。

下面的代码位正确地分配了所有的数据由路径results.address_components.long_name引用到变量'actualResponseOne'中。

  def actualResponseOne = jsonSlurper.results.address_components。 long_name.collect()

但是,因为在同一个测试中我可能想要验证不同的json元素。 results.geometry.location_type例如我不想在groovy脚本中硬编码路径,而是将其设置在数据源中,并将其分配给脚本中的groovy变量。

  def testElementOne1 = context.expand('$ {DataSource-Groovy-GoogleMaps#testElement1}'); 

如何在我的代码中引用此json路径,将数据分配给'actualResponseOne'?

  def actualResponseOne = jsonSlurper。$ {testElementOne} .collect()

任何帮助都将非常感谢。



p>

Tam。

解决方案

使用 Eval 会添加一个辅助方法来分离路径并迭代地遍历它:

  class嵌套{
静态类A {
B b
}
静态类B {
C c
}
静态类C {
列表列表

static void main(String [] args){
def a = new A(b:new B(c:new C(list:[1,2, 3))))
println getNestedProperty(a,bclist)。collect {String.format%03d,it}
}

static def getNestedProperty(def对象,字符串路径){
path.split(/ \ ./)。each {
object = object。$ {it}
}
返回对象
}
}




[001,002,003]


I am using a groovy script within a Ready API test case to validate the results in a json web service response.

I want to use a variable (that is specified within a data source) to specify the json path that I want to validate, as this may change for each test run.

The following bit of code correctly assigns all the data referred to by the path results.address_components.long_name to the variable 'actualResponseOne'

def actualResponseOne =  jsonSlurper.results.address_components.long_name.collect()

But, because within the same test I might want to validate different json elements ie. results.geometry.location_type for example I don't want to have the path hardcoded within the groovy script but instead set it up in a data source and assign it to a groovy variable within my script with ..

def testElementOne1   = context.expand( '${DataSource-Groovy-GoogleMaps#testElement1}' );

How to I refer to this json path within my code that assigns the data to 'actualResponseOne'? The below code doesn't work.

def actualResponseOne =  jsonSlurper.${testElementOne}.collect()

Any help would be much appreciated.

regards,

Tam.

解决方案

An alternative to using Eval would be to add a helper method to break apart the path and traverse it iteratively:

class Nested {
    static class A {
        B b
    }
    static class B {
        C c
    }
    static class C {
        List list
    }
    static void main(String[] args) {
        def a = new A(b: new B(c: new C(list: [1, 2, 3])))
        println getNestedProperty(a, "b.c.list").collect { String.format "%03d", it }
    }

    static def getNestedProperty(def object, String path) {
        path.split(/\./).each {
            object = object."${it}"
        }
        return object
    }
}

[001, 002, 003]

这篇关于引用groovy变量作为JSON路径的一部分的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆