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

Loading External JS Files #1180

Open
jaybillhighway opened this Issue Jun 25, 2015 · 100 comments

Comments

@jaybillhighway
Copy link

jaybillhighway commented Jun 25, 2015

We have a lot of code that I like to load into Pre-Request script Sandbox instead of cutting and pasting . Can that be done, may be support requirejs ? or simple reference to a CDN js ?

Thanks

Jay

@abhijitkane

This comment has been minimized.

Copy link
Member

abhijitkane commented Jun 25, 2015

We're working on a way to allow import of custom JS files into the scripting sandbox, but can't offer an approximate date right now.

@johnnavarra

This comment has been minimized.

Copy link

johnnavarra commented Jun 25, 2015

You can modify the tester_sandbox.html in the AppData\Local\Google\Chrome\User Data\Default\Extensions\fhbjgbiflinjbdggehcddcbncdddomop directory to include the js files. The caveat is that an update to the packaged extension will overwrite your changes, so backup your changes regularly.

The other option is to import via jQuery:

// Import the CryptoJS library with jQuery
$.getScript("http://crypto-js.googlecode.com/svn/tags/3.1.2/build/rollups/sha256.js",
function()
{});

the problem with this is that using that import takes two actions so running this with collector will make a false test.

@abhijitkane abhijitkane added the feature label Jul 16, 2015

@abernix

This comment has been minimized.

Copy link

abernix commented Jul 22, 2015

I'm using getScript myself, as well. The caveat mentioned above is true, but as mentioned, it doesn't work properly until the second request, and all your results are from the previous call. I guess this is my 👍

@abernix

This comment has been minimized.

Copy link

abernix commented Jul 22, 2015

I guess some sort of callback would make it work okay too.

i.e. postman.wait() makes it not execute the actual request until you call postman.ready() yourself? Or something.

@dotCipher

This comment has been minimized.

Copy link

dotCipher commented Oct 1, 2015

+1 on this

@yiweig

This comment has been minimized.

Copy link

yiweig commented Oct 2, 2015

Any update on this? would also like to include external js libraries

@abtiddle

This comment has been minimized.

Copy link

abtiddle commented Jan 28, 2016

+1 on this

@jackrabb1t

This comment has been minimized.

Copy link

jackrabb1t commented Feb 4, 2016

+1

@rohan-pulekar

This comment has been minimized.

Copy link

rohan-pulekar commented Feb 11, 2016

Any updates on this?
I was using postman app and created a test case in it. My Tests tab has:

$.getScript("/Users/rpulekar/work/workspace_for_sothebys/sample.js",
function()
{});
function1();

And my sample.js has:
function function1() {
tests["test2"] = responseCode.code === 200;
}

When I run the test, I get:

There was an error evaluating the test script. function1 is not defined

@deive

This comment has been minimized.

Copy link

deive commented Mar 9, 2016

If it helps you can put js into an environment variable and load it with:
eval(postman.getGlobalVariable("environment variable key"));

@johnnavarra

This comment has been minimized.

Copy link

johnnavarra commented Mar 9, 2016

That's a great idea!

On Wed, Mar 9, 2016 at 5:24 AM, deive notifications@github.com wrote:

If it helps you can put js into an environment variable and load it with:
eval(postman.getGlobalVariable("environment variable key"));


Reply to this email directly or view it on GitHub
#1180 (comment)
.

AIM: obliviouslyaware
ICQ: 5147787
googletalk

@shamasis

This comment has been minimized.

Copy link
Member

shamasis commented Mar 9, 2016

Yeah. Make a request to your JS and in its test script, save the response in a global or environment variable. Then in subsequent scripts, simply eval the variable at the beginning.

@bijeebuss

This comment has been minimized.

Copy link

bijeebuss commented Mar 18, 2016

+1

@IPWright83

This comment has been minimized.

Copy link

IPWright83 commented Apr 27, 2016

I want to be able to do this - but I want to do this for the Tests rather than the pre-request script. I've put together a JavaScript snippet that mimicks Jasmine syntax to allow for a richer set of tests to be built (and will automatically include additional information in the test name if it fails) - but it's a lot of code to paste at the end of each test case.

I've also tried to use jQuery.getScript but the Tests run synchronously so I end up with no tests being executed when I'm reliant on the jQuery callback.

@IPWright83

This comment has been minimized.

Copy link

IPWright83 commented Apr 27, 2016

@johnnavarra I just read your comment about the tester_sandbox.html - that's incredibly useful, thanks! I've managed to include some custom scripts and now have a lovely testing interface. If I can I'll try and share the code to do this later:

image

@shamasis

This comment has been minimized.

Copy link
Member

shamasis commented Apr 28, 2016

Hi, the presently suggested way to include external script is by using a request to fetch its content. See - http://blog.getpostman.com/2015/09/29/writing-a-behaviour-driven-api-testing-environment-within-postman/ and https://github.com/BigstickCarpet/postman-bdd

In this way, you'll be safe from breakage caused by future changes to internal app architecture or Chrome platform changes.

@IPWright83

This comment has been minimized.

Copy link

IPWright83 commented Apr 28, 2016

@shamasis Thanks for that - looks like a useful way of doing it.

@johnnavarra

This comment has been minimized.

Copy link

johnnavarra commented Apr 28, 2016

@shamasis this is FANTASTIC! Thank you!

On Thu, Apr 28, 2016 at 1:27 AM, Shamasis Bhattacharya <
notifications@github.com> wrote:

Hi, the presently suggested way to include external script is by using a
request to fetch its content. See -
http://blog.getpostman.com/2015/09/29/writing-a-behaviour-driven-api-testing-environment-within-postman/
and https://github.com/BigstickCarpet/postman-bdd

In this way, you'll be safe from breakage caused by future changes to
internal app architecture or Chrome platform changes.


You are receiving this because you were mentioned.
Reply to this email directly or view it on GitHub
#1180 (comment)

AIM: obliviouslyaware
ICQ: 5147787
googletalk

@shamasis

This comment has been minimized.

Copy link
Member

shamasis commented Apr 28, 2016

I'm so glad. We're also working towards getting to a more elegant solution to this and work around Chrome platform restrictions to get there. Will post an update when there's something interesting.

@stianlagstad

This comment has been minimized.

Copy link

stianlagstad commented Jul 21, 2016

+1. Glad you're working on this!

@tariqghalib

This comment has been minimized.

Copy link

tariqghalib commented Jul 21, 2016

I am new to postman testing. I am currently trying to load an external script that I wrote to pull some db data as part of pre-request script. The script is:

var express = require('express');
var app = express();
app.get('/', function (req, res) {

var sql = require("mssql");
// config for your database
var config = {
user: 'xxxx',
password: 'xxxx',
server: 'xxx.xx.xx.xx',
database: 'xxxxxxxx',
port: xxxx
};
// connect to your database
sql.connect(config, function (err) {
if (err) console.log(err);
// create Request object
var request = new sql.Request();
// query to the database and get the records
request.query('SELECT * FROM xxxxx', function (err, recordset) {
if (err) console.log(err)
// send records as a response
res.send(recordset);

});
});
});
var server = app.listen(5000, function () {
console.log('Server is running..');
});

I have tried to add this through $.getScript ('myScript') way and through adding the script as an environment variable but its throwing an error. Any help will be much appreciated!

Version/App Information:
-->

Postman Version: 4.4.2
App (Chrome app or Mac app): Chrome
OS details: Windows 7 professional
Is the Interceptor on and enabled in the app: Yes
Did you encounter this recently, or has this bug always been there: Its not a default feature
Expected behaviour: Import the external JS scripts successfully
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):
Error: Cannot find module 'express'
at s (chrome-extension://fhbjgbiflinjbdggehcddcbncdddomop/js/libs/request-snippet-generator.js:1:156)
at s (chrome-extension://fhbjgbiflinjbdggehcddcbncdddomop/js/libs/request-snippet-generator.js:1:122)
at eval (eval at loadCustomFile (chrome-extension://fhbjgbiflinjbdggehcddcbncdddomop/js/tester/Evaluator.js:238:39), :1:27)
at eval (eval at loadCustomFile (chrome-extension://fhbjgbiflinjbdggehcddcbncdddomop/js/tester/Evaluator.js:238:39), :40:3)
at r.loadCustomFile (chrome-extension://fhbjgbiflinjbdggehcddcbncdddomop/js/tester/Evaluator.js:238:34)
at chrome-extension://fhbjgbiflinjbdggehcddcbncdddomop/js/tester/Evaluator.js:215:25

STEPS:

Add the above script as a pre-request script
In the Test tab do: $.getScript('Path_to_myscript');

Second Way:

Add the script as a global environment variable
In the Test tab do: eval(postman.getGlobalVariable('Path_to_myscript'));

Can someone please give a simple guide on how to do this?

@johnnavarra

This comment has been minimized.

Copy link

johnnavarra commented Sep 20, 2016

Example, generate code displays:
POST /premierleadexchange.asmx HTTP/1.1
Host: production.detroittradingexchange.com
Content-Type: text/xml
SOAPAction: "www.detroittradingexchange.com/NEWCAR_PostGX"
Cache-Control: no-cache
Postman-Token: 692312f6-181b-5a18-bd93-461621fe5e8c

<soap:Envelope
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:xsd="http://www.w3.org/2001/XMLSchema"
xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/"
xmlns:SOAP-ENC="http://schemas.xmlsoap.org/soap/encoding/">
soap:Body
<NEWCAR_PostGX xmlns="www.detroittradingexchange.com/">
0000-3178
3178AJG
<?xml version="1.0" encoding="UTF-8"?>
<NEWCAR_POSTGX_REQUEST
xmlns="www.detroittradingexchange.com/SellerMessages"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="
www.detroittradingexchange.com/SellerMessages
http://www.detroittradingexchange.com/SellerMessages/NewCarSellGx_request.xsd
">
<Lead_Metadata>

  •                &lt;DTX_GEN_ID&gt; 0000-3178&lt;/DTX_GEN_ID&gt;*
                &lt;LEAD_GEN_METHOD&gt; null&lt;/LEAD_GEN_METHOD&gt;
                &lt;LEAD_SOURCE_IP&gt;
    
    66.11.125.12</LEAD_SOURCE_IP>
    <CAMPAIGN_ID> null</CAMPAIGN_ID>
    <GEN_LEAD_ID> DTEVALUATION1</GEN_LEAD_ID>
    <SELF_GENERATED_LEAD>
    false</SELF_GENERATED_LEAD>
    <SOURCE> DTVERIFIED_151000</SOURCE>
    </Lead_Metadata>
    <Post_Instructions>
    <Reservation_ID>
    4512002053|0000-2120|4f4f2622-d751-4954-b8f9-7603875ad12e|02750</Reservation_ID>
    </Post_Instructions>
    <Lead_Data>
    <VEHICLE_INFO>
    <YEAR> 2016</YEAR>
    <MAKE> Ford</MAKE>
    <MODEL> Focus</MODEL>
    <TRIM> null</TRIM>
    <INTERIOR_COLOR> black</INTERIOR_COLOR>
    <EXTERIOR_COLOR> White</EXTERIOR_COLOR>
    </VEHICLE_INFO>
    <PURCHASE_INFO>
    <FINANCING> lease</FINANCING>
    <TIME_FRAME_DAYS> 7</TIME_FRAME_DAYS>
    </PURCHASE_INFO>
    <PERSONAL_INFO>
    <FIRST_NAME> Barack</FIRST_NAME>
    <LAST_NAME> Obama</LAST_NAME>
    </PERSONAL_INFO>
    <CONTACT_INFO>
  •                    &lt;HOME_PHONE&gt; 5869338914&lt;/HOME_PHONE&gt; *
    
  •                    &lt;EMAIL&gt; Juan933@detroittrading.net
    
    Juan933@detroittrading.net</EMAIL> *
  •                    &lt;WORK_PHONE&gt; 2489338914&lt;/WORK_PHONE&gt; *
    
  •                    &lt;WORK_PHONE_EXT&gt;
    
    1205</WORK_PHONE_EXT> *
  •                    &lt;MOBILE_PHONE&gt; 5864658914&lt;/MOBILE_PHONE&g*
    
    t;
    <CONTACT_TIME> Any</CONTACT_TIME>
    </CONTACT_INFO>
    <RESIDENCE_INFO>
    <ADDRESS> 2898 Colonial</ADDRESS>
    <CITY> Southfield</CITY>
    <STATE_PROVINCE> MI</STATE_PROVINCE>
    <ZIP_POSTAL_CODE>
    48075</ZIP_POSTAL_CODE>
    <COUNTRY> USA</COUNTRY>
    </RESIDENCE_INFO>
    </Lead_Data>
    <BUYER_DETAILS>
    <TRADE_IN>
    <YEAR> 2010</YEAR>
    <MAKE> Ford</MAKE>
    <MODEL> Fusion</MODEL>
    <TRIM> SEL</TRIM>
    <EXTERIOR_COLOR> Grey</EXTERIOR_COLOR>
    <MILEAGE> 100000</MILEAGE>
    </TRADE_IN>
    </BUYER_DETAILS>
    </NEWCAR_POSTGX_REQUEST>

    false
    </NEWCAR_PostGX>
    /soap:Body
    /soap:Envelope

Actual sent payload received by the server:

            <NEWCAR_POSTGX_REQUEST
                xmlns="www.detroittradingexchange.com/SellerMessages"
                xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
                xsi:schemaLocation="

www.detroittradingexchange.com/SellerMessages
http://www.detroittradingexchange.com/SellerMessages/NewCarSellGx_request.xsd
">
<Lead_Metadata>

  •                <DTX_GEN_ID> {{User}}</DTX_GEN_ID> *
                <LEAD_GEN_METHOD> null</LEAD_GEN_METHOD>
                <LEAD_SOURCE_IP> 66.11.125.12</LEAD_SOURCE_IP>
                <CAMPAIGN_ID> null</CAMPAIGN_ID>
                <GEN_LEAD_ID> DTEVALUATION1</GEN_LEAD_ID>
                <SELF_GENERATED_LEAD> false</SELF_GENERATED_LEAD>
                <SOURCE> DTVERIFIED_151000</SOURCE>
            </Lead_Metadata>
            <Post_Instructions>
                <Reservation_ID>
    
    4512002053|0000-2120|4f4f2622-d751-4954-b8f9-7603875ad12e|02750</Reservation_ID>
    </Post_Instructions>
    <Lead_Data>
    <VEHICLE_INFO>
    2016
    Ford
    Focus
    null
    <INTERIOR_COLOR> black</INTERIOR_COLOR>
    <EXTERIOR_COLOR> White</EXTERIOR_COLOR>
    </VEHICLE_INFO>
    <PURCHASE_INFO>
    lease
    <TIME_FRAME_DAYS> 7</TIME_FRAME_DAYS>
    </PURCHASE_INFO>
    <PERSONAL_INFO>
    <FIRST_NAME> Barack</FIRST_NAME>
    <LAST_NAME> Obama</LAST_NAME>
    </PERSONAL_INFO>
    <CONTACT_INFO>
  •                    <HOME_PHONE> 586{{3D1}}{{4D1}}</HOME_PHONE> *
    
  •                    <EMAIL> Juan{{3D1}}@detroittrading.net
    
    Juan%7B%7B3D1%7D%7D@detroittrading.net *
  •                    <WORK_PHONE> 248{{3D1}}{{4D1}}</WORK_PHONE> *
    
  •                    <WORK_PHONE_EXT> 1205</WORK_PHONE_EXT> *
    
  •                    <MOBILE_PHONE> 586{{3D3}}{{4D1}}</MOBILE_PHONE> *
    
  •                    <CONTACT_TIME> Any</CONTACT_TIME> *
                </CONTACT_INFO>
                <RESIDENCE_INFO>
                    <ADDRESS> 2898 Colonial</ADDRESS>
                    <CITY> Southfield</CITY>
                    <STATE_PROVINCE> MI</STATE_PROVINCE>
                    <ZIP_POSTAL_CODE> 48075</ZIP_POSTAL_CODE>
                    <COUNTRY> USA</COUNTRY>
                    </RESIDENCE_INFO>
                </Lead_Data>
                <BUYER_DETAILS>
        <TRADE_IN>
          <YEAR> 2010</YEAR>
          <MAKE> Ford</MAKE>
          <MODEL> Fusion</MODEL>
          <TRIM> SEL</TRIM>
          <EXTERIOR_COLOR> Grey</EXTERIOR_COLOR>
          <MILEAGE> 100000</MILEAGE>
        </TRADE_IN>
      </BUYER_DETAILS>
            </NEWCAR_POSTGX_REQUEST>
    
@garunski

This comment has been minimized.

Copy link

garunski commented Jan 10, 2017

+1

@bquack

This comment has been minimized.

Copy link

bquack commented Jan 11, 2017

+1
I have been using the method of loading JS into a global variable and then using eval(), but I don't like the fact that I have to make this a first step in all of my collections. It would be nice if there was a more elegant solution for this such as having an external list of JS resources. It would also be great if external JSON Schemas could have something similar.... maybe even a third variable scope that can store schemas.

@jarrodparkes

This comment has been minimized.

Copy link

jarrodparkes commented Feb 21, 2018

+1

2 similar comments
@ivaaaan

This comment has been minimized.

Copy link

ivaaaan commented Feb 22, 2018

+1

@rhostem

This comment has been minimized.

Copy link

rhostem commented Feb 26, 2018

+1

@SuperAL

This comment has been minimized.

Copy link

SuperAL commented Feb 27, 2018

My solution for running some scripts every time a request was sent:

// in collection Tests tab
pm.sendRequest("path/to/the/script.js", (err, res) => { eval(res.text()); });
@Nahis

This comment has been minimized.

Copy link

Nahis commented Apr 9, 2018

+1

5 similar comments
@rfarinel

This comment has been minimized.

Copy link

rfarinel commented Apr 19, 2018

+1

@mccannt

This comment has been minimized.

Copy link

mccannt commented Apr 24, 2018

+1

@nickclicksco

This comment has been minimized.

Copy link

nickclicksco commented Apr 29, 2018

+1

@0Steve0

This comment has been minimized.

Copy link

0Steve0 commented May 10, 2018

+1

@mozowski

This comment has been minimized.

Copy link

mozowski commented May 25, 2018

+1

@aking1012

This comment has been minimized.

Copy link

aking1012 commented Jun 8, 2018

For Content-Encoding: gzip, it's looking like the only semi-reasonable way to do it is to smush it inside of the environment variable. Still have to try it out, but https://github.com/beatgammit/gzip-js/blob/master/lib/gzip.js should be useful, since it's a single file gzip implementation.

@RobynLiu

This comment has been minimized.

Copy link

RobynLiu commented Jun 28, 2018

+1

@a85 a85 self-assigned this Jun 29, 2018

@vkaegis vkaegis added this to the Mid term roadmap milestone Jul 2, 2018

@vkaegis vkaegis self-assigned this Jul 2, 2018

@p00j4

This comment has been minimized.

Copy link

p00j4 commented Jul 5, 2018

I added the js (moment.js) I wanted in the tester_sandbox.html file and reloaded the native postman MAC app, but it didn't help. I get not found error ReferenceError: moment is not defined
Am I missing anything here?

@grinderX19

This comment has been minimized.

Copy link

grinderX19 commented Jul 5, 2018

p00j4, this approach doesn't work with native apps.
It seems the only way to use external JS is as follows:

If it helps you can put js into an environment variable and load it with:
eval(postman.getGlobalVariable("environment variable key"));

@Ocsidot

This comment has been minimized.

Copy link

Ocsidot commented Jul 5, 2018

@grinderX19 and @p00j4 , MomentJS can be loaded in postman siince it's one of available internal module (https://www.getpostman.com/docs/v6/postman/scripts/postman_sandbox_api_reference)

To use MomentJs, you just need to do var moment = require("moment");
Then the library is loaded into your variable

Regards

@kimpettersen

This comment has been minimized.

Copy link

kimpettersen commented Jul 9, 2018

+1!!

I've done something similar to @SuperAL mentioned
pm.sendRequest("path/to/the/script.js", (err, res) => { eval(res.text()); });
But when the script grows above a certain size Postman just crashes immediately. Has anyone else experienced this?

@PawelKazimirowicz

This comment has been minimized.

Copy link

PawelKazimirowicz commented Jul 12, 2018

Is it possible to configure in Postman Adyen secured fields?
https://docs.adyen.com/developers/checkout/api-integration/using-secured-fields

The js file is used to encrypt credit card data and send it to the API endpoint.

What I want to achieve is

  1. In Pre-request Script request js filed
  2. Parse response and set environment variables
  3. Use those variable in the request body
@p00j4

This comment has been minimized.

Copy link

p00j4 commented Jul 23, 2018

thanks @Ocsidot yes this works now. Wondering why it wasn't working earlier is more scarier 🤦‍♀️

@RobertAKARobin

This comment has been minimized.

Copy link

RobertAKARobin commented Jul 30, 2018

+1 on this. JS embedded in JSON is super lame, and has me looking at alternatives.

@mohit-sentieo

This comment has been minimized.

Copy link

mohit-sentieo commented Oct 25, 2018

+1 on this

@ashwani-kumar04

This comment has been minimized.

Copy link

ashwani-kumar04 commented Nov 15, 2018

Hi,
I also faced this issue as maintaining js code in separate files makes more sense. I created a gulp plugin and a sample project to replace the javascript code in the Json using Gulp build. The plugin is still in its early stage and I am in the process to put together a how-to guide for the same.
It can be found here:

Npm plugin
Sample project

@a85 a85 removed this from the Mid term roadmap milestone Jan 23, 2019

@a85 a85 added the runtime label Jan 23, 2019

@jhardin-aptos

This comment has been minimized.

Copy link

jhardin-aptos commented Jan 25, 2019

A definite +1 on this feature.

We have a desire to run a database query to get the data to verify the REST results (potentially in a loop) vs. hardcoding the data in the tests or running some external scripted step to generate an environment file - the npm package mssql looks like it would be perfect, but the web of dependencies and potentially their size makes the prospect of trying to include it all via the approaches described above somewhat daunting.

It would be wonderful if there was some mechanism to install arbitrary npm modules in the postman sandbox (with dependency resolution); a usable alternative might be to make require() in a postman script be able to look in the global npm repository if the module isn't already in the postman sandbox.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.