get query params from request url soapui using groovy

后端 未结 1 523
天涯浪人
天涯浪人 2021-01-26 09: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 sc

相关标签:
1条回答
  • 2021-01-26 09:45

    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'
    
    0 讨论(0)
提交回复
热议问题