简体   繁体   English

SoapUi断言-使用groovy将字符串用作json路径

[英]SoapUi Assertions - Use a string as a json path with groovy

I am using groovy to automate some tests on SoapUI, and I wanted to also automate assertions in a way I would get a field's name and value from a *.txt file and check if the wanted field does exist with the wanted value in the SOapUI response. 我正在使用groovy在SoapUI上自动化一些测试,并且我还希望以某种方式自动化断言,以便从* .txt文件中获取字段的名称和值,并检查所需字段在SOapUI中是否确实存在所需的值响应。

Suppose I have the following json response: 假设我有以下json响应:

{
   "path" : {
         "field" : "My Wanted Value"
    }
}

And from my text file I would have the following two strings : 从我的文本文件中,我将具有以下两个字符串:

path="path.field"
value="My Wanted Value"

I tried the following : 我尝试了以下方法:

import groovy.json.JsonSlurper
def response = messageExchange.response.responseContent
def slurper = new JsonSlurper()
def json = slurper.parseText response

assert json.path==value;

But of course it doesn't work. 但是,当然不行。

Any idea how can I get it done please? 我知道如何完成它吗?

Thank you 谢谢

I think your problem is to access a json value from a path based with . 我认为您的问题是从基于的路径访问json值. notation, in your case path.field to solve this you can use the follow approach: 表示法,在您要解决的问题path.field ,可以使用以下方法:

import groovy.json.JsonSlurper

def path='path.field'
def value='My Wanted Value'
def response = '''{
   "path" : {
         "field" : "My Wanted Value"
    }
}'''

def json = new JsonSlurper().parseText response

// split the path an iterate over it step by step to 
// find your value
path.split("\\.").each {
  json = json[it]
}

assert json == value

println json // My Wanted Value
println value // My Wanted Value

Additionally I'm not sure if you're also asking how to read the values from a file, if it's also a requirement you can use ConfigSlurper to do so supposing you've a file called myProps.txt with your content: 另外,我不确定您是否也在询问如何从文件中读取值,如果这也是一项要求,则可以使用ConfigSlurper进行此操作,假设您拥有一个名为myProps.txt的文件, myProps.txt其中包含以下内容:

path="path.field"
value="My Wanted Value"

You can access it using the follow approach: 您可以使用以下方法访问它:

import groovy.util.ConfigSlurper

def urlFile = new File('C:/temp/myProps.txt').toURI().toURL()
def config = new ConfigSlurper().parse(urlFile);
println config.path // path.field
println config.value // My Wanted Value

All together (json path + read config from file): 全部在一起(json路径+从文件读取配置):

import groovy.json.JsonSlurper
import groovy.util.ConfigSlurper

def response = '''{
   "path" : {
         "field" : "My Wanted Value"
    }
}'''

// get the properties from the config file
def urlFile = new File('C:/temp/myProps.txt').toURI().toURL()
def config = new ConfigSlurper().parse(urlFile);
def path=config.path
def value=config.value

def json = new JsonSlurper().parseText response

// split the path an iterate over it step by step
// to find your value
path.split("\\.").each {
 json = json[it]
}

assert json == value

println json // My Wanted Value
println value // My Wanted Value

Hope this helps, 希望这可以帮助,

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

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