Reputation: 17553
I have to write a Groovy script which get some dynamic value for me from response
I need to pass this value to my request URL as a parameter in an another testcase/step
My first URL is as below from which I am getting response:-
https://mywebsite/api/Products
I am able to get this dynamic id by below groovy script:-
import groovy.json.JsonSlurper
responseContent = testRunner.testCase.getTestStepByName("3_Level_product").getPropertyValue("response")
slurperresponse = new JsonSlurper().parseText(responseContent)
log.info (slurperresponse.products.request_id)
I need to pass that value in below like URL:-
https://mywebsite/api/get_response?request_id=0d8fe4d9
Upvotes: 0
Views: 4483
Reputation: 21379
Within the same Test Case
Open your existing Groovy Script and add below lines to it. Follow the comment in the code snippet.
//As you mentioned you have the value(extracted from _1st step_) in groovy script,
//so assign value in place of "<replace your value here>" below variable
def valueFromPreviousResponse = <replace your value here>
context.testCase.setPropertyValue('REQUEST_ID_FROM_PREVIOUS_RESPONSE', valueFromPreviousResponse)
Now go to the next test step, edit your endpoint
from
https://mywebsite/get_response?request_id=0d8fe4d9-ed28-421f-8b90-b9b2afac4196
to
https://mywebsite/get_response?request_id=${#TestCase#REQUEST_ID_FROM_PREVIOUS_RESPONSE}
Then each time you run the test, dynamic value will be sent in the 2nd step. The will help you only within the test case as mentioned.
Within the Test Suite
If you want that value should be available to a test step which is in either same or different test case, follow below:
//As you mentioned you have the value(extracted from _1st step_) in groovy script,
//so assign value in place of "<replace your value here>" below variable
def valueFromPreviousResponse = <replace your value here>
context.testCase.testSuite.setPropertyValue('REQUEST_ID', valueFromPreviousResponse)
Now go to the next test step or any test step in any test case within the test suite, edit your endpoint
from
https://mywebsite/get_response?request_id=0d8fe4d9-ed28-421f-8b90-b9b2afac4196
to
https://mywebsite/get_response?request_id=${#TestSuite#REQUEST_ID}
Upvotes: 0