How to run and/or convert script to collection using postman sdk

Hello, I’m trying postman SDK and all examples are with console.log().

I couldn’t find how to:

  1. run a .js file that runs a collection (assuming it’s possible)
  2. command to convert .js files onto .json collection

Thank you

Hello,

Any idea or suggestion? It’s not clear nor described in the documentation.
http://www.postmanlabs.com/postman-collection/

Thank you

Hi, anyone?

Thank you

Could you elaborate and expand on what you’re trying to do @nifepo.

Provide an example so folks can visualise what you have done so far and what’s not working?

Are you looking at running the collection with Newman?

Hi Danny,

I’m trying to understand how to run collections built/coded with postman SDK.

Question 1) The only option to run them is with Newman?

  1. Let’s say I work this Getting Started suggested code.

     var fs = require('fs'), // needed to read JSON file from disk
     Collection = require('postman-collection').Collection,
     myCollection;
    
     // Load a collection to memory from a JSON file on disk (say, sample-collection.json)
     myCollection = new Collection(JSON.parse(fs.readFileSync('sample- collection.json').toString()));
    
     // log items at root level of the collection
     console.log(myCollection.toJSON());
    

As a next step, I would like to learn how to add a test to a specific request from the uploaded collection. But, the test, since is the same test everywhere I want to begin reusing code… , like importing a separate commonTests.js file with the following test inside of it (return from the function, etc).

pm.test("Status code is 200", function(){
    pm.response.to.have.status(200);
});

pm.test("response must be valid and have a body", function () {
    pm.response.to.be.ok;
    pm.response.to.be.withBody;
    pm.response.to.be.json;
});

Did I manage to express better this time?

Thank you for your time,
Regards

The Collection SDK will allow you to create a Collection, with certain elements within it, that can be either imported into the Postman App and run manually in the UI or run with Newman from the CLI.

In a node project, with the Collection SDK module installed, a basic Collection with those tests in a request, it can be created like this:

var fs = require('fs'),
    Collection = require('postman-collection').Collection,
    mycollection;

myCollection = new Collection({
    info: {
        name: "Basic Collection"
    },
    item: [
        {
            name: "Basic Request",
            request: {
                method: "GET",
                url: "https://httpbin.org/get"
            },
            event: [
                {
                    listen: "test",
                    script: {
                        type: "text/javascript",
                        exec:[
                        "pm.test('Status code is 200', () => { ",
                        "    pm.response.to.have.status(200);",
                        "})",
                        "",
                        "pm.test(\"response must be valid and have a body\", () => {",
                        "    pm.response.to.be.ok;",
                        "    pm.response.to.be.withBody;",
                        "    pm.response.to.be.json;",
                        "})"
                        ]
                    }
                }
            ]
        }
    ]
});

fs.writeFileSync('./basicCollection.json', JSON.stringify(myCollection, null, 2)); 

This would create a new file basicCollection.json which can be imported into Postman. From here you should be able to create those common JS files in the project and inject them into the script to produce the result that you want.

A horrible hacky way could be:

Create a commonTests.js file

exports.tests = `pm.test('Status code is 200', () => { 
    pm.response.to.have.status(200);
})

pm.test("response must be valid and have a body", () => {
    pm.response.to.be.ok;
    pm.response.to.be.withBody;
    pm.response.to.be.json;
})`

Use that file is the main Collection script like this, it will add the test at the Collection level:

var fs = require('fs'),
    Collection = require('postman-collection').Collection,
    commonTests = require('./commonTests.js'),
    mycollection;

myCollection = new Collection({
    info: {
        name: "Basic Collection"
    },
    item: [
        {
            name: "Basic Request",
            request: {
                method: "GET",
                url: "https://httpbin.org/get"
            }
        }
    ],
    event: [
		{
			listen: "test",
			script: {
				type: "text/javascript",
                exec:[
                    `${commonTests.tests}`
                ]
			}
		}
	]
});

fs.writeFileSync('./basicCollection.json', JSON.stringify(myCollection, null, 2));