jQuery replaced by CheerioJS in Postman Sandbox
As many Postman users know, with the release of v4.5.0+ of the Postman Mac app and Newman v3, jQuery was discontinued as a built-in library in the scripting sandbox. Essentially, we moved to NodeJS-based script execution, which meant saying “bye-bye” to DOM and its allied functionalities. (I can see some of you about to suggest JSDOM – but we wanted something stable, fast and cross-platform.) jQuery is a brilliant library when you need to work with servers that return HTML or XML as responses. We do have
xml2js just for that, but … we & our users still love jQuery.
So, we included a new library called CheerioJS. Cheerio emulates the core API and functionalities of jQuery, without the need of browser or DOM. This approach appears rather neat when used inside Postman. Take a look at the following code sample inside a test script:
var html, titleText; // load the response body as HTML using cheerio // and using cheerio's jQuery like .find API, get the H1 tag html = cheerio(responseBody); titleText = html.find('h1').text(); // add a test that ensures that there is some H1 content tests["page must have h1 heading"] = Boolean(titleText);
The above piece of code extracts the title from an HTML response, and tests whether it has some text or not. The code would have had worked with jQuery by simply replacing
jQuery. In short, if you know your way around jQuery, you know CheerioJS too!
All cool – but, many folks love those nifty utility functions of jQuery such as
$.param. Fear not fellow developers, we have lodash to the rescue; with
_.forEach as one replacement. Lodash is insanely fast and in many ways, the jQuery of the NodeJS world (metaphorically speaking!)
With all these additions, Postman users now have consistent, blazing fast, scripting sandbox inside Postman that works cross-platform and is unaffected by browser quirks or versioning differences.