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);
A live sample of a similar code in action inside Newman: https://tonicdev.com/shamasis/newman-cheerio
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 cheerio
with 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 $.each
and $.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.
Nice!
Is cheerio pre-installed with Postman? or needs to be manually installed to make it work
I added the following lines in a pre-request script and got “cheerio is not defined” ?
var html;
html = cheerio(‘h2 class = “title”>Hello world’);
This is available in Newman v3+ and Postman Native apps. (Not yet in Chrome App)
The Cheerio library also has “.each” built-in, no need to use lodash for traversing Cheerio objects.
Excellent post. I was checking continuously this blog and
I’m impressed! Extremely helpful information particularly the remaining section 🙂
I take care of such info much. I was looking for this
certain info for a long time. Thanks and best of luck.
Where should I go to figure out the syntax to use this new mode – I can see that I should use ` _.forEach ` but how? understand the article was written back in 2016, so there has been some water under the bridge since.
Hi Eric, Please contact our support team at https://www.postman.com/support, and they’ll be glad to help you!