Forum Discussion

swee's avatar
swee
New Contributor
10 years ago

Fetch cookies from soapUI Request and Response and reuse for following Requests

Hi 

 

I have used follow script trying to get the cookies.

 

def myClient = HttpClientSupport.getHttpClient()
def myStore = myClient.getCookieStore()
def cookieList = myStore.getCookies()
log.info cookieList.size()

 

It returned the zero of the size.

But using this:

 

def setCookie_resp = testRunner.testCase.testSteps["HTTP Request 1"].testRequest.response.getResponseHeaders()
log.info setCookie_resp.size()

log.info setCookie_resp["#status#"]

 

It can get the size is same as the Headers on soapUI showing. 

But that size and real Cookie content is not same as the results got by browser developement tool.

"HTTP Request 1" is the first request to login on the test box.

 

Other question I would ask for the help. 

After successfully the first Request, which is Post for login, I expect to do the second Request to Get some json data, but it is never successful.

 

Thanks very much if any expert can help!

 

Cheers

 

 

 

  • nmrao's avatar
    nmrao
    Champion Level 3

    Still not clear what you are looking for exactly from the post.

     

    This may just be helpful information for better understanding of response headers.

    http://www.soapui.org/testing-dojo/best-practices/understanding-rest-headers-and-parameters.html

     

    What I understnad for the script you posted is that

    • You are getting http headers, not the coockie

     

    Here is script assertion(for your HTTP Request 1 step) instead of additional groovy script for the same (as per my understanding)

     

    • Define a custom property EXPECTED_COOKIE_SIZE and its value at test case level

     

    //Assuming a custom property EXPECTED_COOKIE_SIZE defined with needed value at test case level
    
    def expectedCookieSize = context.testCase.properties['EXPECTED_COOKIE_SIZE'].value as int
    def cookie
    if (messageExchange.responseHeaders.containsKey('Cookie')) {
      log.info "Found Cookie in the response headers"
      cookie = messageExchange.responseHeaders['Cookie'].value
      log.info "Cookie value : ${cookie},  and its size is : ${cookie.size()}"
      assert expectedCookieSize == cookie.size, "Cookie size mismatch"
    } else {
      log.warn "Not Found Cookie in the response headers"
    }
    
    

    The second question "I expect to do the second Request to Get some json data, but it is never successful." not clear, what do you mean by it? Could please add more details?

     

    • nmrao's avatar
      nmrao
      Champion Level 3

      Here is slightly improved answer based on the fact that -

      • to match data of any given header both in request and response
      • provide name of the header you wanted to assert i.e., is response header compared with request
      • Say a test case custom property HEADER_NAME and value as 'Cookie' in this case (can be any header in fact)
      /**
      * checks if given HEADER_NAME in request vs response
      * also its value and size
      * Assumes, a custom test case property HEADER_NAME is defined with 
      * appropriate value lie Content-Type, Cookie etc., any thing for that matter
      * This can be used as Script Assertion
      **/ def key=context.testCase.properties['HEADER_NAME'].value def isHeaderInRequest = messageExchange.requestHeaders.containsKey(key) def isHeaderInResponse = messageExchange.responseHeaders.containsKey(key) assert isHeaderInRequest == isHeaderInResponse, "Header ${key} cound not found either in request or response" def headerValueInRequest = messageExchange.requestHeaders[key].value def headerValueInResponse = messageExchange.responseHeaders[key].value printLog("Request", key, headerValueInRequest) printLog("Response", key, headerValueInResponse) assert headerValueInRequest == headerValueInResponse, "${key} value mismatch between request and response" assert headerValueInRequest.size() == headerValueInResponse.size(), "${key} value size mismatch" def printLog = { message, key, value -> log.info "${message} - Header - \n${key} : ${value} \nsize : ${value.size()}" }

      Hope this is what you are looking for in the first place.

      • swee's avatar
        swee
        New Contributor

        Hi Rao

         

        Before practicing your code, I would say appreciate very much.

        I feel I need to give you more details about my questions. 

        My task is to use soapUI to test the API which is already deployed to test box. Before using soapUI, I have tried login from web browser, no matter FF or Chrome and open web development tool (click F12 as we known). From the network tab showing, I can see the first method is login, which is Post method, then lots of Get methods and another Post method which is logevent. From that Post method, I can see the token Id.

        I thought I can use soapUI to do the same things. Am I right?

        I recon, the first step of request is login to the box, which should be a Post method. Yes it seems works and got http headers:

         

        Date Wed, 18 Nov 2015 20:08:32 GMT
        Transfer-Encoding chunked
        #status# HTTP/1.1 200 OK
        Expires Thu, 01 Jan 1970 00:00:00 GMT
        Keep-Alive timeout=10
        Content-Type text/html;charset=UTF-8
        Connection Keep-Alive
        X-Powered-By Servlet 2.5; JBoss-5.0/JBossWeb-2.1
        X-Powered-By JSF/1.2
        Pragma no-cache
        Cache-Control no-cache
        Cache-Control no-store
         

        But the size is quite different from what I got from web browser development tool Network tab showings. Why?

        This is my first question. 

        Then I tried using groovy scripts and hope to get more details of Cookies but no luck so far.

         

        After the first step of login, I expect to use the soapUI request with Get method to test the API -  to get the bundle of json data which the API should return. It works on web browser of course. But I am stuck on soapUI...

        I am wondering how to fetch the useful parameter from the first step and then HOW to re-use them to the following requests to test the API. 

        This is my second question.

        Hope you can help?

        Thank you very much.

         

        Cheers

         

        swee

         

         

         

  • Fetch cookies from soapUI Request and Response and reuse for following Requests....I am litle experienced in this field but I can suggest you to go to some cookie site to get the right.

     

     

    thanks

     

     

    Tapsi Sarkar