postmanlabs / postman-app-support

Postman is an API platform for building and using APIs. Postman simplifies each step of the API lifecycle and streamlines collaboration so you can create better APIs—faster.
https://www.postman.com
5.82k stars 839 forks source link

Custom Snippets #675

Open duepayer opened 10 years ago

duepayer commented 10 years ago

It would be GREAT if I could create my own custom snippets for Tests and Pre-request Scripts. I have some boilerplate test scripts that I use as a baseline for error testing and find myself copying and pasting the same script from other tests repeatedly. This would greatly improve my productivity!

konstad commented 5 years ago

Okay, here is what I did:

I used Notepad++ to search through the Postman directories/files for existing snippet text. I found a match in the 'app.asar' file, located in '..\AppData\Local\Postman\app-7.2.2\resources' folder. The .asar file extension indicates this file is an Electron archive. To open and unpack the archive, I downloaded 7zip, and then installed the .asar plugin from http://www.tc4shell.com/en/7zip/asar/. After unpacking the app.asar archive, I used Notepad++ to search for the snippet text again. This time I found a match in the file named 'vendor-shared.js', located in the '..\js' folder. I opened this file up in VS Code, and again performed a search for existing snippet text, which I found towards the end of this file. The snippets are stored in JSON format, as follows:

{ "id": "get-env-var", "name": "Get an environment variable", "description": "Gets an environment variable", "code": "pm.environment.get(\"variable_key\");", "validity": [ "prerequest", "test" ] }

Simply add your own snippets, following the above format, and then save your changes. Repack the .asar archive again, including the edited .js file. Replace the app.asar file in the Postman directory with the newly modified app.asar file. Fire up Postman, and the new snippets will be available.

I realize this is a bit involved, but it doesn't seem like it would be too difficult to create a quick tool to automate much of the work. I've got a prototype in the works that provides a visual editor, and I can post the solution when finished if anyone is interested.

I hope this helps some of you guys out!

NOTE: Be careful not to delete the 'module.exports = ' from the beginning of the line with the snippet JSON. Doing so will not cause Postman to crash, but you will not be able to view/create any calls.

image

konstad commented 5 years ago

Was this not helpful?

txrackack commented 5 years ago

This is awesome. Thank you very much for sharing. I can't wait to see what else you come up with... ;)

On Sat, Jun 29, 2019, 5:39 PM Kristopher Onstad notifications@github.com wrote:

Was this not helpful?

— You are receiving this because you commented. Reply to this email directly, view it on GitHub https://github.com/postmanlabs/postman-app-support/issues/675?email_source=notifications&email_token=AIDQCOUUUDS6EPYZGYSXVODP47QDFA5CNFSM4ASTNQPKYY3PNVWWK3TUL52HS4DFVREXG43VMVBW63LNMVXHJKTDN5WW2ZLOORPWSZGODY4BM3A#issuecomment-506992236, or mute the thread https://github.com/notifications/unsubscribe-auth/AIDQCOWP5KDTYHRFBFKFYDTP47QDFANCNFSM4ASTNQPA .

DeadcatDev commented 4 years ago

+1

10yearschallenge

atomical commented 4 years ago

+1

techinpark commented 4 years ago

not yet?

Alveona commented 4 years ago

Almost 2020 out here :/

dsicari commented 4 years ago

2020 here \o/ Would love to see this feature!

xtbe commented 4 years ago

+1 For me as well - Would be useful for collaboration in teams and help writing consistent test for developed APIs

cmdd80 commented 4 years ago

+1 Come on! It is quarantine time! Give us this feature :-)

pss-miracle commented 4 years ago

Hello, Came across this one - this is also a feature we would like to vote for; custom templates would help us maintain better code quality

Also: a bit more documented version of the earlier mentioned "hack" exists here: https://medium.com/@gopikrishna_82732/hack-to-add-your-own-snippets-to-postman-af295ddd68e7 This does not sounds like something we would do individually; but rather hope for a more syncronised solution to share the agreed templates

rmnrdi commented 3 years ago

This would be an awesome feature.

What would be even better is if the custom tests we create were to be tied to our own external library and not just copy\paste.

That way if we needed to modify the test script itself, we could modify it in the library and not have to track down all it's uses.

ejwinter commented 3 years ago

+1 This would definitely be useful to have such template.

Najtu commented 3 years ago

+1 Could be very useful.

Its weird to being able to do it only the "hacky" way.

luyijingxiu commented 3 years ago

+1 really want this feature

Shadowfita commented 3 years ago

+1 Would be fantastic.

Khuongnb commented 3 years ago

+1, so 6 years already and we still haven't got this fantastics idea

prchann commented 3 years ago

I do it by writing test code in Collection Variables as string, and use eval() function to run it in the Tests.

截屏2021-05-11 下午4 28 42 截屏2021-05-11 下午4 29 40
henghengyang commented 3 years ago

+1 really usefull feature

sciamano89 commented 2 years ago

+1 Definitely would be useful and a time saver. Or even better, have a way for us to create a global class (or even function) that we can reference in any test or pre-request script! I wonder if this would even be possible, not too familiar with js...

it-at-cst commented 2 years ago

+1 Can't believe postman team has not yet delivered such an anticipated feature.

nbjwe1 commented 2 years ago

This would be very valuable to me as well. In many ways it's an extention of https://github.com/postmanlabs/postman-app-support/issues/9255 where he asks for updated snippets. My main thing is to add my insights as snippets to make life easier for others in the company, and for my own benefit.

w4dd325 commented 2 years ago

@perrychann I had a similar solution that I added here; https://community.postman.com/t/a-way-to-create-custom-test-snippets/36142

the eval() option is interesting, I never thought of using that!!!

petar19 commented 2 years ago

I've been using custom snippets based on solution someone previously linked here https://medium.com/@gopikrishna_82732/hack-to-add-your-own-snippets-to-postman-af295ddd68e7

Basically, it's done by modifying the _vendorshared.js file. Everything worked perfectly (onversion 8.5.1) until I updated Postman, it would seem new version (9.8) doesn't have that _vendorshared.js file anymore and I can't find any mention of snippets anywhere else in the whole app folder.

Problem is I need the new version since the old one doesn't work with the Auth method used in my company but at the same time using Postman without custom snippets is a pain and while solutions like eval() seem decent, custom snippets is still easier and faster to use imo.

Does anyone know where are snippets stored in the latest version of Postman maybe?

edit: I looked a bit deeper and seems like snippets are now stored in vendor-ui.js at line 4945 (for me at least). It's not as easy to edit as before since now this line also contains lot of other things but it's still possible to add new and delete existing snippets.