Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

pm.sendRequest in collection level global function #4404

Closed
andriikhmelkov opened this issue Mar 29, 2018 · 6 comments
Closed

pm.sendRequest in collection level global function #4404

andriikhmelkov opened this issue Mar 29, 2018 · 6 comments
Assignees

Comments

@andriikhmelkov
Copy link

andriikhmelkov commented Mar 29, 2018

App Details:

Postman for Mac
Version 6.0.10
macOS 10.12.6 (16G1212)

Issue Report:

  1. Did you encounter this recently, or has this bug always been there: Recently.
  2. Expected behaviour: Send a request from collection level global function.
  3. Console logs (http://blog.getpostman.com/2014/01/27/enabling-chrome-developer-tools-inside-postman/ for the Chrome App, View->Toggle Dev Tools for the Mac app):

requester.js:173357 foo
requester.js:173357 1
requester.js:173357 753348
requester.js:173357 386

  1. Screenshots (if applicable)

Basically, after each pm.test pass/failure send a request to TestRail API with test case id and result pass/fail. In order to do so, I need a reusable function that will accept arguments "test case id" and "test case result" and send a request to the TestRail server.

The following code does the job if placed to the "Tests" tab of a single request, but doesn't work if placed into the collection level "Pre-request Scripts" or "Tests" tabs.

globalFunctions = {
    addResult: function(testId, testResult) {
        console.log("foo");
        console.log(testResult);
        console.log(testId);
        console.log(pm.globals.get("testRunId"));
        pm.sendRequest({
            url: "https://url.hidden/index.php?/api/v2/add_result_for_case/" + pm.globals.get("testRunId") + "/" + testId,
            method: 'POST',
            header: {
                "Content-Type": "application/json",
                "Authorization": "Basic ....."
            },
            body: {
                mode: 'raw',
                raw: JSON.stringify({"status_id": testResult})
            }
        }, function (err, res) {
            // Doesn't log anything to the console
            console.log(err);
            console.log(res.json());
        });
    }
};

foo gets printed to the console, along with test case id, status, and test run id, but pm.sendRequest doesn't work nor a reason why pm.sendRequest failed printed to the console.

There are no indications that simple requests to https://postman-echo.com/get?test=123 from global collection level functions are successful as well. Seems like pm.sendRequest doesn't like being placed into global collection level functions.

@harryi3t
Copy link

harryi3t commented Apr 2, 2018

Seems to be working fine on my end. Please try this sample collection
Run in Postman

// Collection level test script
pm.sendRequest({
    url: 'https://postman-echo.com/post',
    method: 'POST',
    body: {
        mode: 'raw',
        raw: JSON.stringify(pm.variables.get('tests'))
    }
});

// Request 1 pre-request script
tests['r1 t1'] = true;
tests['r1 t2'] = false;
console.log('r1', tests)
pm.variables.set('tests', tests)


// Request 2 pre-request script
tests['r2 t1'] = true;
tests['r2 t2'] = false;
console.log('r2', tests)
pm.variables.set('tests', tests)

Output

It sent 4 requests as expected
image

Request 1

mitmproxy_-p_8081_--setheader___q_x-proxy__python3_6_

Request 2

mitmproxy_-p_8081_--setheader___q_x-proxy__python3_6__and_postman

@andriikhmelkov
Copy link
Author

@harryi3t Thank you for the sample and quick response.

pm.sendRequest will work fine if placed directly into the "Tests" of "Pre-request Scripts" tabs of the Collection. However, the problem is it doesn't work if placed in a global function (not sure if it's possible, though):
screen shot 2018-04-02 at 10 02 33 am

globalFunction = {
    testStuff: function() {
        pm.sendRequest({
            url: 'https://postman-echo.com/post',
            method: 'POST',
            body: {
                mode: 'raw',
                raw: JSON.stringify(pm.variables.get('tests'))
            }
        });
    }
};

Then call this global function from "Tests" tab of the request:

globalFunction.testStuff()

@harryi3t
Copy link

harryi3t commented Apr 3, 2018

@andriikhmelkov Now I see what the issue is. Sharing scripts (in this case a global function) across scripts is not supported. It might look like it's working but that is due to an implementation bug.

As mentioned here

Sharing javascript global variables or functions across scripts is not an intended feature and not reliable. It may work in some cases.

Related issues
#4058 (open feature request)
#4197
#2247
#2749
#3032

There is a (not so good) workaround for this, putting the common functions into a variable (global, env or temp) and then evaling them

// Collection pre-request script
function globalFunction () { ... }
pm.variables.set('globalFunction', globalFunction.toString())

// Request-1 test-script
var globalFunction = eval(pm.variables.get('globalFunction'));
globalFunction()

Note: Above code is written on the fly and not tested so might require some tweeking.

@kamranghiasvand
Copy link

if you want to call pm.sendRequest in global function ,try this:

  1. define global function in collection pre-request, like this:
    postman.setGlobalVariable("globalFunction", (parameters) => { console.log(parameters); pm.sendRequest('https://google.com/', function(err, resp) { pm.expect(err).to.not.be.ok; }); });

  2. use function like this:

    eval(globals.globalFunction)('hello world!!');

Note that, I declared function using arrow style ()=>{}. otherwise it wouldn't work

@gharika
Copy link

gharika commented Jun 11, 2019

Been a year, do we have something in progress so that we don't have to update a test in every request?

@timohuovinen
Copy link

timohuovinen commented May 7, 2021

I've worked around this issue in the following way

globalThing = {
    testStuff: function(pm) {
        pm.sendRequest({
            url: 'https://postman-echo.com/post',
            method: 'POST',
            body: {
                mode: 'raw',
                raw: JSON.stringify(pm.variables.get('tests'))
            }
        });
    }
};

and then you can call it like this

globalThing.testStuff(pm)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

5 participants