Forum Discussion

krogold's avatar
krogold
Regular Contributor
5 years ago

determine the type of a request programmatically

Hello,

I'm trying to find a way, by groovy scripting, to determine which is the type of a request in my project (PUT, POST, GET, etc.). I could only go as far as parsing the step and get its rest method (getRestMethod() on a RestTestRequestStep but it does not always give me relevant information.

for some of my APIs it gives setterXX or getterYY which is exactly what I need but for many others it does not give this type of information.

is there a property that clearly provide this information ?

 

thank you

  • Hello,

    finally I found out the information.

    unfortunately, in the project under test, som PUT requests are, in fact, get requests so I have an additional test to do to determine the 'functional' role of the request.

    To get the rest method of a REST request step :

    testRunner.testCase.testSuite.project.testSuiteList.each
    {
    	suite ->
    	name = suite.getName()
        
        suite.testCaseList.each{
            TC -> 
            log.info "test case ****** " + TC.getName()
            TC.testSteps.each{
    
                if (it.getValue().config.type.equals("restrequest"))
                {
                    method_functional_type = "unknown"
                    method_functional_type = it.getValue().getRestMethod().getMethod().toString() // provides 'PUT', 'GET', etc.
                    
                    if (method_functional_type != "GET")
                    {
                        // do an additional check
    					// ie. the API /m2m/fim/items/fim:users:settings:manager/operations/getServerTimestamp
    					// it is a PUT request but it is senseless to not use the output                    
                        op = it.getValue().getOperation().toString()
                        if (op.split("\\/")[-1].take(3) == "get")
                        {
                            // the PUT request returns relevant data that has to be used
                            method_functional_type = "GET"
                        }
                    }                
                    
                    
                    if (method_functional_type == "GET")
                    {
                        if (TC.tearDownScript == null){
                            log.info "update teardown script for $name - ${TC.name}"
                            // if there is no teardown script available, set the generic teardown script for GET API's
                            TC.setTearDownScript('testCase.setPropertyValue("testResponse", context.response)')
                        }
                    }
                }
            }
        }
    }
  • krogold's avatar
    krogold
    Regular Contributor

    Hello,

    finally I found out the information.

    unfortunately, in the project under test, som PUT requests are, in fact, get requests so I have an additional test to do to determine the 'functional' role of the request.

    To get the rest method of a REST request step :

    testRunner.testCase.testSuite.project.testSuiteList.each
    {
    	suite ->
    	name = suite.getName()
        
        suite.testCaseList.each{
            TC -> 
            log.info "test case ****** " + TC.getName()
            TC.testSteps.each{
    
                if (it.getValue().config.type.equals("restrequest"))
                {
                    method_functional_type = "unknown"
                    method_functional_type = it.getValue().getRestMethod().getMethod().toString() // provides 'PUT', 'GET', etc.
                    
                    if (method_functional_type != "GET")
                    {
                        // do an additional check
    					// ie. the API /m2m/fim/items/fim:users:settings:manager/operations/getServerTimestamp
    					// it is a PUT request but it is senseless to not use the output                    
                        op = it.getValue().getOperation().toString()
                        if (op.split("\\/")[-1].take(3) == "get")
                        {
                            // the PUT request returns relevant data that has to be used
                            method_functional_type = "GET"
                        }
                    }                
                    
                    
                    if (method_functional_type == "GET")
                    {
                        if (TC.tearDownScript == null){
                            log.info "update teardown script for $name - ${TC.name}"
                            // if there is no teardown script available, set the generic teardown script for GET API's
                            TC.setTearDownScript('testCase.setPropertyValue("testResponse", context.response)')
                        }
                    }
                }
            }
        }
    }
  • krogold's avatar
    krogold
    Regular Contributor

    I have to types of requests. For rest requests, it can do 'getRestMethod' which gives information containing 'setter' or 'getter' or, at least, a string that contains the information. It's not straightforward but it works.

    The problem is with http requests, in fact I found the information in the config data of the request (which is a xml structure) but I do not manage to parse it correctly.

    I can get the highest level information by just pointing to it, ie. config.type = httprequest, I can also get name, id ... but the config method is not at the same level.

    I get this information, which is a class com.eviware.soapui.config.impl.TestStepConfigImpl

    <xml-fragment type="httprequest" name="Request 1" id="373fc2ed-1b98-4ce7-9695-dfcca6469ce8" xmlns:con="http://eviware.com/soapui/config">
      <con:settings/>
      <con:config method="PUT" xsi:type="con:HttpRequest" id="b85fadf8-b989-4b71-ba0b-f0c5aa73e5b4" name="Request 1" postQueryString="false" mediaType="application/json" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
        <con:settings>
          <con:setting id="com.eviware.soapui.impl.wsdl.WsdlRequest@request-headers">&lt;entry key="Accept" value="application/json" xmlns="http://eviware.com/soapui/config"/></con:setting>
        </con:settings>
    ...

    How can I get the config method field content ?

     

    I tried to use xmlparser and xmlslurper but it does not accept the data from TestStepConfigImpl object.

    Is there a way to cast it ? is there a particular way to use xmlparser or slurper to achieve this ?

     

    • nmrao's avatar
      nmrao
      Champion Level 3
      If you have script that which was not working?