jQuery replaced by CheerioJS in Postman Sandbox

Avatar

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.

Comment

Your email address will not be published. Required fields are marked *


This site uses Akismet to reduce spam. Learn how your comment data is processed.

7 thoughts on “jQuery replaced by CheerioJS in Postman Sandbox

    Avatar

    Nice!

    Avatar

    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’);

      Avatar

      This is available in Newman v3+ and Postman Native apps. (Not yet in Chrome App)

    Avatar

    The Cheerio library also has “.each” built-in, no need to use lodash for traversing Cheerio objects.

    Avatar

    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.

    Avatar

    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.