Selecting a Reference in a response
When a response contains multiple matches for the regular expression that defines a Reference, you can select the match to be used subsequently as the data source. You can specify a particular occurrence, or you can specify a random occurrence.
About this task
An application under test might return responses that contain multiple matches for a regular expression that defines a Reference. For example, a response might contain multiple links to rows of data, where each row represents a different user. You can control the occurrence of the regular expression that is used as the data source in subsequent data correlation.
When you edit the Regular Expression that is associated with a Reference, you can click Verify to validate the regular expression. The new regular expression still connects to the highlighted string in the preview window and also the Specific occurrence number is updated automatically, overwriting any changes.
Procedure
- Browse to the test from Test Navigator and double-click it.
- Locate the response that contains the Reference that you want to specify.
-
Right-click the Reference value in the
Content field under the Test Element
Details area, and then select
Properties.
The Reference dialog is displayed.
-
Perform the following actions to edit the properties of the
Reference value.
Options Actions Enabled Clear the checkbox to disable the Reference that you created within the test.
Name Enter a name of your choice to edit the default name of the Reference.
Click Check to verify that the name that you entered is unique throughout the test.
Always log details Select the checkbox to always log the details about the Reference.
Note: When you selected the Always log details checkbox, the details are logged irrespective of the log level set for a schedule. You must use this option only for debugging purposes.Regular Expression Enter the regular expression or JSON expression to edit the default expression that was used to locate the Reference. You must click Verify to ensure that your edit is valid.
If an HTTP response is JSON, then the Regular Expression field displays the JSON expression instead of the regular expression.
Based on the JSON Path expression that you entered in the Regular Expression field, you can extract the Reference value. You can also use Jayway JsonPath Evaluator to examine your JSON path expressions.
Consider you have a JSON data as follows:
{ "employees":[ { "details":{ "id":"1234", "firstName":"pt", "lastName:":"test1", "email":"pttest1@test.org" } }, { "details":{ "id":"1234", "firstName":"pt1", "lastName:":"test2", "email":"pt1@test.org" } }, { "details":{ "id":"1234", "firstName":"pt", "lastName:":"test3", "email":"pt1@test.org" } }, { "details":{ "id":"1234", "firstName":"pt1", "lastName:":"test4", "email":"pt1@test.org" } }, { "details":{ "id":"1234", "firstName":"pt1", "lastName:":"test4", "email":"pt1@test.org" } } ] }
Then you can enter the JSON expression in the Regular Expression field to extract the last name of the employee whose email id is pttest1@test.org as follows:
regex: $.employees[?(@.details.email=='pttest1@test.org')].details.lastName:You can click the Toggle regular expression assistant icon to display the response content matched by the regular expression and the groups captured by the regular expression.
Occurrence Use the Occurrence option to run the regular or JSON expression specified number of times. The first occurrence of a match for the Regular Expression is used as the default data source. The default selection for Occurrence is a Specific occurrence number.
The Occurrence option has the following choices:
-
Specific occurrence number: Select this option to use a value specified and to run the regular or JSON expression
x
number of times. For example, enter 4 to specify the fourth match of the regular expression in the response. -
Random occurrence: Select this option if you know how many times you want to run the regular or JSON expression, and then use one of the random values. You can use the From and To fields to match it for a range of random values.
-
Last occurrence: Select this option to use the last value that you can extract by running the specified regular or JSON expression.
-
All occurrences: Select this option to match the regular or JSON expression with all the occurrences of the selected Reference. You can use the From and To fields to match it for a range of consecutive occurrences.
The occurrence that you specified is used as the data source for data correlation when you run the test.
-
- Click Apply and Close to apply the changes made to the properties of the Reference value.