Forum Discussion

nbharadwaj's avatar
nbharadwaj
New Contributor
7 years ago

How to get response header values from a test step

I have multiple test steps in a testCase, and I want to pass the response header value from the 1st test step as an input header to the 2nd test step.

For instance, here's a clear description:

TestStep1: 

Response header has the following values:

Set-Cookie: sessionid=xxxxxxxxxxx

Set-Cookie: token1=xxxxxxxx

 

TestStep2:

Request Header should have the following values:

Cookie: sessionid=<sessionID value from the response header of testStep1>;token1=<token1 value from the response header of testStep1>

 

How do I do this?

 

Please note that there are 2 header properties with the same name, but with different values in TestStep1, and I need both the values to be passed to TestStep2

 

I did go through the other articles on achieving this, however I couldn't get an exact solution to what I am looking for. I have tried the property transfer step, but that doesn't seem to have an option of transferring properties from Response headers. 

5 Replies

  • JHunt's avatar
    JHunt
    Community Hero

    You can put a Groovy Script in between your two requests:

     

    def cookies = []
    testRunner.testCase.getTestStepByName("1st test step").testRequest.response.responseHeaders.each { 
    	if (it.key == "Set-Cookie") cookies += it.value
    }
    
    def cookieValue = ""
    cookies.each {	cookieValue += "${it};" }
    cookieValue = cookieValue.substring(0, cookieValue.length() - 1)
    
    testRunner.testCase.getTestStepByName("2nd test step").testRequest.requestHeaders += ["Cookie": [cookieValue]]

    Response for "1st test step" REST Test Step:

     

    HTTP/1.1 200 OK
    Set-Cookie: sessionid=abc
    Set-Cookie: token1=def
    Content-Type: application/json
    Content-Encoding: gzip
    Content-Length: 41
    Server: Jetty(6.1.26)
    
    {"Something":"Reply"}

    Request for "2nd test step" REST Test Step:

     

    POST http://localhost:8081/me HTTP/1.1
    Accept-Encoding: gzip,deflate
    Content-Type: application/json;charset=UTF-8
    Cookie: sessionid=abc;token1=def
    Content-Length: 0
    Host: localhost:8081
    Connection: Keep-Alive
    User-Agent: Apache-HttpClient/4.1.1 (java 1.5)

     

  • JHunt's avatar
    JHunt
    Community Hero

    Sorry, above script works but it's a bit misleading, because there aren't separate entries on responseHeaders for each Set-Cookie header. This may be more appropriate:

     

    def sessionidPair
    def token1Pair
    testRunner.testCase.getTestStepByName("1st test step").testRequest.response.responseHeaders["Set-Cookie"].each {
        if (it.contains("sessionid=")) sessionidPair = it
        if (it.contains("token1=")) token1Pair = it
    }
    testRunner.testCase.getTestStepByName("2nd test step").testRequest.requestHeaders += ["Cookie": ["${sessionidPair};${token1Pair}".toString()]]
  • nbharadwaj's avatar
    nbharadwaj
    New Contributor
    Thank you for your response. Another question, is there a way to pass parameters across test cases?
    As in, if I want to pass a response parameter from one test case, as an input to a test step in another test case, is it possible to do that?
  • JHunt's avatar
    JHunt
    Community Hero

    SoapUI discourages you from doing that. For example, in property expansions, you can't use:

     

    ${#TestSuite#Some Other Test Case#someProperty}

    The idea is that one test case shouldn't depend on another. Though it is possible to have the first test case set some property value on the TestSuite, and then have another test case read it:

     

    ${#TestSuite#somePropertyUsedInMoreThanOneTestCase}

    If you really would just rather read directly from a property belonging to another test case, you can do it in Groovy:

     

    def testSuite = testRunner.testCase.testSuite
    def otherTestCase = testSuite.testCases["TestCase 2"]
    log.info otherTestCase.getPropertyValue("startDate")
  • nbharadwaj's avatar
    nbharadwaj
    New Contributor

    Thank you very much Jhunt. I will try this out and reach out again if I see any issues.