get query params from request url soapui using groovy

你。 提交于 2019-12-02 10:40:48

Supposing that your testStep request is called GetCustomers you can use the follow Groovy code to get the testStep and then the property with the endpoint value as String:

def ts = context.testCase.getTestStepByName('GetCustomers')
def endpoint =ts.getPropertyValue('Endpoint')
log.info endpoint // prints http://myendpoint.com/customers?Id=111&ModeName=abc&DeltaId=023423

Then you can parse the endpoint using the java.net.URL class and use getQuery() method to extract the query parameters. Then split by & to get each query name value pair and finally split again each pair with =and put the result in a Map. Altogether your code could be something like:

import java.net.*

def ts = context.testCase.getTestStepByName('GetCustomers')
def endpoint =ts.getPropertyValue('Endpoint')
// parse the endpoint as url
def url = new URL(endpoint)
// get all query params as list
def queryParams = url.query?.split('&') // safe operator for urls without query params
// transform the params list to a Map spliting 
// each query param
def mapParams = queryParams.collectEntries { param -> param.split('=').collect { URLDecoder.decode(it) }}
// assert the expected values
assert mapParams['Id'] == '111'
assert mapParams['ModeName']== 'abc'
assert mapParams['DeltaId']=='023423'

There is another option without using URL class; which simply consists on splitting the URL using ? to get the query parameters (as URL.getQuery() does):

def ts = context.testCase.getTestStepByName('GetCustomers')
def endpoint =ts.getPropertyValue('Endpoint')

// ? it's a special regex... so escape it
def queryParams = endpoint.split('\\?')[1].split('&')
// transform the params list to a Map spliting 
// each query param
def mapParams = queryParams.collectEntries { param -> param.split('=').collect { it }}
// assert the expected values
assert mapParams['Id'] == '111'
assert mapParams['ModeName']== 'abc'
assert mapParams['DeltaId']=='023423'
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!