Ask a Question

Use a timestamp in request and test for it in response

KrisVH
Occasional Contributor

Use a timestamp in request and test for it in response

Hi,

 

I have a service where I'm sending a correlation ID: I send this value in the request, and I expect the same value in the response.

 

For testing this, I generate a timestamp that I send in the request, and I get the same answer in the response. So far so good, but I can't figure out how to put this into an assertion.

 

This is how I generate the timestamp for the request:

 

${=import java.text.SimpleDateFormat ; new SimpleDateFormat("YYYY-MM-dd HH:mm:ss").format(new Date())}

 

For the assertion, I try to refer to the correlation ID property in the request. But when it actually does the assertion, it just evaluates the above function again, thus getting a new timestamp, and failing the test.

 

How can I make it so that it doesn't run the expression again during the assertion, but refers to what was sent in the actual request?

 

Or failing that, can I somehow prepare the timestamp somehow (*), save its value, and then use that to both send the request and check the response? I tried the 'Properties' and the 'Property Transfer' teststeps, but those didn't help.

 

(*: without manually typing it in, obviously)

 

Thanks & best regards,

 

Kris

 

1 REPLY 1
RiteshY
Occasional Contributor

Try Following:

1. Create A Groovy Step Before your request

2. Set variable on testcase level (you can use properties as well)

testRunner.testCase.setPropertyValue('TimeStamp', new SimpleDateFormat("YYYY-MM-dd HH:mm:ss").format(new Date())) 

3. Use TimeStamp variable in your request and response

  

${#TestCase#TimeStamp}

Hope this help!

 

-Ritesh

cancel
Showing results for 
Search instead for 
Did you mean: