SoapUI - GET 方法需要显示 ID 范围

SoapUI - GET Method need to show range of IDs

我正在 SoapUI 中创建一个测试项目来测试我们的 API 网关,并且需要为报告自动化执行以下操作:

我有一个示例 JSON 文件:

 {
    "id": 1111111,
    "name": "name",
    "created_at": "create_at",
    "updated_at": "updated_at",
    "attributes": [{
        WHOLE ATTRIBIUTES
    }],
    "tags": [],
    "company_id": "A123"
 }

我想做几件事:

我正在使用 Groovy 脚本搜索几个解决方案,但没有成功。有任何想法吗?我应该专注于按 header 还是路径进行过滤?

我尝试使用 JsonSlurper,例如:

import groovy.json.JsonSlurper 
def response = messageExchange.response.responseContent 
def slurper = new JsonSlurper() 
def json = slurper.parseText response 
assert json.company_id = ("K",123 > 321)

有两件事可以帮助您:

  • 用于从完整 company_id 中提取 int 值部分的正则表达式。使用正则表达式而不是简单的字符串操作只是更易于维护、可读和更通用。无论如何,你总是可以使用 String.substring() 来提取那个 int val.
  • groovy 范围和 in 运算符,用于检查 id 是否在范围内。

我想您已经将所有响应收集为 allResponses 列表中的字符串,下面是过滤所有 json 并将其写入文件的方式:

def companyIdPattern = ~/A(\d+)/
def companyIdRange = 100..500 // X..Y inclusively, X..<Y - for exclusive Y
def resultFile = new File('result.txt')
def parser = new JsonSlurper()
allResponses.collect { parser.parseText(it) }
        .findAll {
            // match company_id text to regexp and extract int value part
            def idVal = (it.company_id =~ companyIdPattern)[0][1]?.toInteger()
            idVal in companyIdRange
        }.each {
            // there could be any pre-processing needed to write result (convert json to csv, etc.)
            resultFile << it.toString()
        }