Forum Discussion

JulianTest's avatar
JulianTest
New Contributor
8 years ago
Solved

Tranfert part of a header response as a property

Hello, I'm in front of something new and I don't know how to solve it.
I have a step case where I get this raw response :


HTTP/1.1 204 No Content
Server: Apache-Coyote/1.1
Location: identifiers/0000000004
Date: Wed, 14 Sep 2016 15:05:00 GMT
Connection: close

 

What I want to do is to take the identifier, here "0000000004", after "identifiers/" of the header "Location" and transfer it to another test case further.

 

How can I do that ?

Thank you very much,
Julian

  • I would suggest to use Script Assertion over Property Transfer test step. i.e., Add the script assertion to the request step and remove property transfer step altogether.

     

    The below script would read the given header, trims the required value and set that value at test suite level, so that all the remaining test cases can use that location value.

     

    //If you need it for another header, please change the value of key below
    def key = 'Location' if (messageExchange.responseHeaders.containsKey(key)) { def result = messageExchange.responseHeaders[key] def locationValue = result[0].replaceFirst(~/\w+\//,'') log.info "Setting the location value ${locationValue} at suite level" context.testCase.testSuite.setPropertyValue('LOCATION_VALUE', locationValue) } else { log.error "Response does not contain header : ${key}" }

    In the following test cases use location as given below:

     

    • If the step is request step (soap / rest etc.,), use ${#TestSuite#LOCATION_VALUE}
    • If the step is of groovy script step, use  context.expand('${#TestSuite#LOCATION_VALUE}')

4 Replies

  • nmrao's avatar
    nmrao
    Champion Level 3

    I would suggest to use Script Assertion over Property Transfer test step. i.e., Add the script assertion to the request step and remove property transfer step altogether.

     

    The below script would read the given header, trims the required value and set that value at test suite level, so that all the remaining test cases can use that location value.

     

    //If you need it for another header, please change the value of key below
    def key = 'Location' if (messageExchange.responseHeaders.containsKey(key)) { def result = messageExchange.responseHeaders[key] def locationValue = result[0].replaceFirst(~/\w+\//,'') log.info "Setting the location value ${locationValue} at suite level" context.testCase.testSuite.setPropertyValue('LOCATION_VALUE', locationValue) } else { log.error "Response does not contain header : ${key}" }

    In the following test cases use location as given below:

     

    • If the step is request step (soap / rest etc.,), use ${#TestSuite#LOCATION_VALUE}
    • If the step is of groovy script step, use  context.expand('${#TestSuite#LOCATION_VALUE}')
    • JulianTest's avatar
      JulianTest
      New Contributor

      Thank you very much, that's what I needed.

       

      Julian

    • JulianTest's avatar
      JulianTest
      New Contributor

      Hi Tanya,

       

      I'm trying to use the Property Transfer TestStep but I don't see any solution to catch a part of the header response in the tutorial.

       

      Thank you.

      Julian