How to Use Resource Variable with Recurring API Calls?
Functionize has a feature called Resource Variables that are used to validate network calls made by a page and to capture and verify the data in those network calls. There are some use cases that require capturing recurring API calls as resources within your test. The standard Resource Variables functionality can be prone to failure when you need to use a recurring API call as a resource, like fze.resource['myurl_2']
with the _2
suffix. These tend to be variable-numbered resources, and you may not be sure which request contains the data you want to capture.
For this recurring API call scenario, you will need to evaluate which Resource Variable (fze.resource['myurl_2']
) request has the payload that you want to capture. This has to be done using a Functionize Custom Code Action. The Custom Code Action will need to specify the parameter you are looking to capture, which may only be part of some of the requests.
In this example, the tester is searching for an authentication token that we need to capture in a Local Variable and then use that Local Variable to enter the token later on in our test flow.
// You must return a boolean value to determine the success or failure of this step.
let limit = 20;
for (let it = 0; it < limit; it++) {
let suffix = ''
if (it > 0) {
suffix = '_' + it
}
if (typeof fze.resource['https://myurl.com/api/auth/oauth/v4/token' + suffix] != 'undefined') {
try {
fze.local.Collect_response = JSON.parse(fze.resource['myurl.com/api/auth/oauth/v4/token' + suffix].responseBody);
} catch(e) {}
}
}
return typeof fze.local.Collect_response != 'undefined'
The code sequentially iterates through a maximum of 20 recurring resources, examining a unique suffix for each one. For every suffix, it verifies the presence of a responseBody
associated with the resource. Upon finding a responseBody
, the code assigns it to a local variable, if it can be parsed into JSON. The outcome of the custom code action, either pass or fail, is contingent upon the existence of a valid JSON responseBody
.