How to parse a URL to fetch it's parameters in groovy?

亡梦爱人 提交于 2019-12-14 03:37:17

问题


I am new to groovy scripting and looking on to parse the URL and print it's parameter.

This url is : https://www.google.com/?aaa=111&bbb=222&ccc=33&dd=1484088989_b23f248ac6e5d9a9b47475526bb92ee1

How can i fetch dd parameter from the URL?

I appreciate your help!


回答1:


You need to add a groovy script.

def groovyUtils = new com.eviware.soapui.support.GroovyUtils( context );
def testCase = context.testCase;
def testStep = testCase.getTestStepByName("NAME_TESTStepRequest");  
def endpoint =testStep.getPropertyValue('Endpoint');
log.info endpoint;
def url = new URL(endpoint)
//def url = new URL("https://www.google.com/?aaa=111&bbb=222&ccc=33&dd=1484088989_b23f248ac6e5d9a9b47475526bb92ee1")

// 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
log.info mapParams['aaa']
//assert mapParams['aaa'] == '111'
log.info mapParams['bbb']
//assert mapParams['bbb']== 'abc'
log.info mapParams['dd']
//assert mapParams['dd']=='023423'

Please you check this post.Get query params from request url soapui using groovy



来源:https://stackoverflow.com/questions/41581611/how-to-parse-a-url-to-fetch-its-parameters-in-groovy

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!