How to make a 'Call To Collection'?

ReadyApi has the ‘Run Test Case’ step, which will run one test case from within another test case, with the option to bring back data.

I’m looking for a similar action in the Postman client, but can’t find anything, so here I am asking: Is there a run collection function in Postman?

A little more detail.
On my team there are several non-technical tester who need to run some API script manually. I’ve taught them to click the 3 dots and select Run Collection.

Most of our tests require a testuser to be logged in with a session id, so every single test has to start with the same 2 requests:

POST Validate User (yields a temp token used in the next request)
POST login info (yields a session id)

I want to put these 2 requests into their own collection
Have the collection return the session id and save it to a global variable
The use this var for the manually started ad-hoc tests.

I’ve been looking at the postman.setNextRequest(‘test step name’); hoping I could replace the test step name with a link to the shared collection. That doesn’t seem to be the case.

If every test in a collection has to start with the same two requests, then this is what the collection pre-request script is for.

image

You can send requests using the sendRequest() feature and then store the session ID from the response into a collection variable (collection not global, as I suspect you don’t need this session ID outside of the collection).

The following is an example of a pre-request script to authenticated to Microsoft.

The fields you need, and the token will be dependant on your API but hopefully you can see how the request works.

pm.test("Check for collectionVariables", function () {
    let vars = ['clientId', 'clientSecret', 'tenantId', 'username', 'password', 'scope'];
    vars.forEach(function (item, index, array) {
        console.log(item, index);
        pm.expect(pm.collectionVariables.get(item), item + " variable not set").to.not.be.undefined;
        pm.expect(pm.collectionVariables.get(item), item + " variable not set").to.not.be.empty; 
    });

    if (!pm.collectionVariables.get("bearerToken") || Date.now() > new Date(pm.collectionVariables.get("bearerTokenExpiresOn") * 1000)) {
        pm.sendRequest({
            url: 'https://login.microsoftonline.com/' + pm.collectionVariables.get("tenantId") + '/oauth2/v2.0/token',
            method: 'POST',
            header: 'Content-Type: application/x-www-form-urlencoded',
            body: {
                mode: 'urlencoded',
                urlencoded: [
                    { key: "client_id", value: pm.collectionVariables.get("clientId"), disabled: false },
                    { key: "scope", value: pm.collectionVariables.get("scope"), disabled: false },
                    { key: "username", value: pm.collectionVariables.get("username"), disabled: false },
                    { key: "password", value: pm.collectionVariables.get("password"), disabled: false },                    
                    { key: "client_secret", value: pm.collectionVariables.get("clientSecret"), disabled: false },
                    { key: "grant_type", value: "password", disabled: false },
                ]
            }
        }, function (err, res) {
            if (err) {
                console.log(err);
            } else {
                let resJson = res.json();
                pm.collectionVariables.set("bearerTokenExpiresOn", resJson.expires_in);
                pm.collectionVariables.set("bearerToken", resJson.id_token);
            }
        });
    }
});
1 Like

Appreciate the thorough reply, thanks

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.