Skip to main content

Transfer Variabiles In Test Case

This article will make you learn how to transfer variables between APIs in test case.

Now assume that in an automated test plan, we need to extract the petid value from the returned data of the A API and use it as the request parameter of the B API. The following will demonstrate how to complete the parameter passing and connection process between APIs through two methods.

  1. Page visualization operation
  2. Operation based on syntactic rules

Page Visualization Operation

View Parameter to Extract

When defining the parameter value in the B API, click the "magic wand" icon and select the "Retrieve Pre-step Data" option.

Select to extract information from the response data Body of the A API, and specify to extract "Partial Data". You can see in the A API request response that the target petid parameter value is located in the data column.

Fill in Parameter Name/Path

Fill in data.petid in the JSONPath expression of the B API and click "Insert". You can refer to the JSONPath syntax to understand more complex data extraction methods.

To ensure that the correct parameter can be extracted, you can click the "jump" icon to preview the expected return of the previous step and whether the correct data is extracted.

Verify Parameter Passing

Click the "Run" button to start the testing process.

You can view the parameter passing situation in the test report. You can see that the petid parameter output by the A API is 41, and the actual request value of the B API is also 41, which means the parameter passing succeeded.

Operation Based on Syntactic Rules

You can also directly fill in instructions in accordance with syntactic rules to reference the return data of the previous step.

For example, now you need to extract the petid value from the returned data of the A API:

So you can fill in {{$.1.response.body.data.petid}} in the parameter value of the B API. The correspondence of this instruction can refer to the following figure:

info

The number 1 refers to the test step ID.

response.body means to get data from the response Body.

data.petid means to extract the petid from the data column in the returned JSON file.

If the instruction is correct, it will show the "Pre-step" preview after filling in.

Syntax Rule Table

Category Function Syntax Example
RequestURL{{$.<step id>.request.url}}
Path parameter{{$.<step id>.request.pathParam.<field name>}}
Query{{$.<step id>.request.query.<field name>}}
Header{{$.<step id>.request.header.<field name>}}
Body (form){{$.<step id>.request.body.<field name>}}
Body (json){{$.<step id>.request.body.<field path>}}
ResponseBody{{$.<step id>.response.body.<field path>}}
Header{{$.<step id>.response.header.<field name>}}
Cookie{{$.<step id>.response.cookie.<field name>}}
LoopElement (array element in ForEach loop){{$.<loop step id>.element.<field path>}}
Index{{$.<loop step id>.index}}