Coder Social home page Coder Social logo

postman-tutorial-for-newbies's Introduction

postman-tutorial-for-newbies

Are you new to Postman? I got you covered!

Introduction

I have recently done coding challenge for TransferWise and realized I had to use a dev tool in order to submit my solutions to their server! I sort of knew about Postman and realized I would need to use it to send requests to submit my solutions (via Rest API). It was easy in the beginning of the problems that they want me to solve which only includes few requests. After few problems and as I moved up to more difficult problems, I realized holy molly there is no way I will send requests manually, and I need to figure out how to automate tests (the whole purpose of programming), so I don't need to spend hours on just sending request manually. This repo will help you how to use Postman in general and dive into Collection Runner which will help you run automated tests. I will discuss about some basics in the beginning and dive into collection runner which is a more advanced topic.

Setup

I would assume you already have Postman downloaded on your computer. I am using MacOS Version 10.12 and downloaded Postman for Mac.

How to do /GET request?

  1. Specify request to GET and put targeted URL

  2. Click Send alt text

  3. You can even add some paramaters too! Click params right next to url. I used FB about page. alt text

How to do /POST request?

  1. In this example, I will be applying to this cool startup my friend works at via API.
  2. Set body as raw JSON:
{
  "name": "name",
  "email": "email",
  "resume": "resume",
  "github": "blah",
  "twitter": "https://twitter.com/Stephen_S_Lee"
}
  1. Then send request as a POST request. alt text

Collection Runner

I will be using some similar examples that I did for TransferWise coding challenge but I will be explaining my logic. All the codes go into Tests section. Also, make sure to create a collection by clicking Collections tab and the plus sign. You have to include the requests that you will be running into that collection so it can do loops once Collection runner has launched.

  1. Let's say you have /payment as GET request to get JSON objects.
// GET /payment
let jsonData = JSON.parse(responseBody);

let foos = [],
    bars = [];
jsonData.forEach(function(element) {
    //...
    foos.push(element.id);
    bars.push(element.id);
    //...
});
// Removing the first element from foos and set it as foo env var.
postman.setEnvironmentVariable("foo", foos.shift());
// Convert the list object foos into JSON data type
postman.setEnvironmentVariable("foos", JSON.stringify(foos));

// ..
postman.setEnvironmentVariable("bar", bars.shift());
//..
postman.setEnvironmentVariable("bars", JSON.stringify(bars));
tests["let's start foo first: " + environment.id] = true;
postman.setNextRequest('foo'); // this will call the next request called foo (you can name your request)
  1. In /payment/{{foo}}/frog as PUT request, you update foo and mark it as frog. Wait a minute. What is {{}} thingy? That is environment variable we set. That value will be used in there. if foo is set to hello then the whole URL will be like /payment/hello/frog:)
tests["FROG!"] = true;              // debug message
const ids = JSON.parse(postman.getEnvironmentVariable('foos'));

// this ensures when to go to next request & terminate infinite loops
if (ids.length > 0) {
    postman.setEnvironmentVariable('foo', ids.shift());
    postman.setEnvironmentVariable('foos', JSON.stringify(ids));
    postman.setNextRequest('foo'); // we are running foo requests until foos run out of elements
    tests["PUT"] = true;
} else {
    // clear env. vars
    postman.clearEnvironmentVariable('foo');
    postman.clearEnvironmentVariable('foos');
    tests["Complete marking foosters!"] = true;
    // move onto the next request
    postman.setNextRequest('bar');
}
  1. In /payment/{{bar}}/frog as DELETE request, you do same thing
tests["FROG2!"] = true;              // debug message

const ids = JSON.parse(postman.getEnvironmentVariable('bars'));

if (ids.length > 0) {
    postman.setEnvironmentVariable('bar', ids.shift());
    postman.setEnvironmentVariable('bars', JSON.stringify(ids));
    postman.setNextRequest('bar');
    tests["DELETE"] = true;
} else {
    postman.clearEnvironmentVariable('bar');
    postman.clearEnvironmentVariable('bars');
    tests["THE END"] = true;
}

It is highly recommended to do testing for every request that you created (unit testing) and check if environment variables actually store the values by clicking eye icon on the top right corner. Once everything is working properly, start the collection runner by clicking runner button at the top right corner.

alt text Choose which collection to run and that's it!!

I really hope you found this useful. I struggled to use collection runner in the beginning but with trials and errors (and googling), I was able to figure out how to use Collection Runner properly. Good luck!

references

good tutorial: https://thisendout.com/2017/02/22/loops-dynamic-variables-postman-pt2/

postman-tutorial-for-newbies's People

Contributors

mr-perfection avatar

Stargazers

 avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.