🧑‍💻 Postscript - run code
Create and run various assertions by adding custom codes.
Loadmill provides a wide range of features that you may use in your tests. Having said that, sometimes you may find yourself needing to create a custom validation or it is just more intuitive for you to write a piece of code instead of using the GUI. So, we’ve got it covered by introducing the Postscript request section.
The Postscript section is disabled by default but we are happy to enable it for you. Just drop us a line at [email protected] or chat with us by clicking
in the lower-right corner of the screen. ‌
The Postscript section within requests allows inserting Javascript (JS) code that may contain parameter extractions, custom assertions (validations), and much more. ‌

Adding JS Code ‌

Let's take a look at a very simple example showing how Postscript works. ‌
1
const my_fruit = $.fruit; // Extracting the response body value and assigning my_fruit with the "fruit" property.
2
3
assert.equal(my_fruit,'banana'); // Asserting that the "my_fruit" value equals 'banana'.//
Copied!
In the example above, we have the response body that is an object {“fruit”:”banana”}. By using Postscript, we can access the response body value ‘banana’ and then assert it:
const my_fruit = $.fruit; - here we access the response body value by using JSONPath expression - $.fruit and assigning “my_fruit” with the fruit property.
assert.equal(my_fruit,'banana'); - then, we add the assertion validating that “my_fruit” equals ‘banana’. Postscript uses the assert Node.js module, find more info about it here. Let’s say we have a bug and API returned my_fruit equals ‘apple’ for some reason, so the test will fail and we’ll get a pretty error message like this:

Test Flow example

Below is an example of a similar scenario that we covered above. Go ahead, copy & paste it to your suite and see live how Postscript works!
🎉
1
{ "url": "httpbin.org/anything", "delay": "", "stopBefore": "", "description": "My awesome request", "method": "POST", "expectedStatus": "SUCCESS", "postScript": "const my_fruit = $.json.fruit; // Extracting the response body value and assigning my_fruit with the \"fruit\" property.\n\nassert.equal(my_fruit,'banana'); // Asserting that the \"my_fruit\" value equals 'banana'.", "timeout": 60000, "postData": { "text": "{\"fruit\":\"banana\"}", "mimeType": "application/json" }, "headers": {}, "extract": [], "assert": [], "parameters": null }
Copied!
Pasting & running the test flow example

Important notes when using Postscript ‌

    1.
    Supported scripting languages: Javascript.
    2.
    Supported parameter extraction types: JSONPath. Others are coming soon. Meanwhile, if you are using another Extraction type like jQuery, Clojure etc, you can extract a parameter within Extractions and use it for validations in Postscript.
    3.
    Note, when using parameters from Extractions, it will give you a string value. If you want to use it as a non-string value in Postscript, you should JSON.parse it first:
4. Loadmill built-in functions support: coming soon, stay tuned. 😉
5. Remember with great power comes great responsibility so use it wisely. 🕷
Last modified 4d ago