get query params from request url soapui using gro

2019-03-06 23:04发布

I only used SOAP UI to just test the WSDL/URL but not in this extent. I need to get the request url query parameters from SOAP UI and use them to test some stuff using groovy script.

Lets say i have a GetCustomers request url as follows

`http://myendpoint.com/customers?Id=111&ModeName=abc&DeltaId=023423`

i need the following from URL Id=111 ModeName=abc DeltaId=023423

I created a groovy script in SOAP UI which in the following hierarchy TestSuit->TestCase-> TestStep->GroovyScript

In the groovy script i tried

def id = testRunner.testCase.getPropertyValue("Id")

but when i print id i am getting it as null. I am not sure of any other configurations i need to do in order to access those query params. Is there a way I can get those query params and access those directly in my groovy script?

1条回答
萌系小妹纸
2楼-- · 2019-03-06 23:43

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'
查看更多
登录 后发表回答