Git Product home page Git Product logo

postman-api-javascript-resources's Introduction

Postman application logo

Postman-API-Javascript-resources

Test Scripts

Introduction

In Postman, a test script is a piece of code that allows you to automate the validation of the responses your API returns. Think of it as your personal QA assistant.

How to Write a Basic Test Script

  1. Access the Tests Tab

    • After sending a request, click on the "Tests" tab in the request pane.
  2. JavaScript Syntax

    • Test scripts are written in JavaScript. Familiarize yourself with basic JavaScript if you haven't already.
  3. Example Script

    • Let's say you want to check if the response status is 200 (OK).
    pm.test("Status code is 200", function () {
        pm.response.to.have.status(200);
    });

Snippets (from Postman application)

Pre-request Scripts

Introduction

A pre-request script runs before the actual request is sent. It's your chance to set up variables, modify the request, or authenticate before hitting the endpoint.

How to Write a Basic Pre-request Script

  1. Access the Pre-request Script Tab

    • Click on the "Pre-request Script" tab in the request pane.
  2. JavaScript Syntax

    • Pre-request scripts also use JavaScript. Utilize this to prepare your request.
  3. Example Script

    • If you need to set a header before each request, you can do it like this:
    pm.headers.add({ key: 'Authorization', value: 'Bearer YOUR_ACCESS_TOKEN' });

Snippets (from Postman application)

  • 001.Get an environment variable.js
  • 002.Get a global variable.js
  • 003.Get a variable.js
  • 004.Get a collection variable.js
  • 005.Set an environment variable.js
  • 006.Set a global variable.js
  • 007.Set a collection variable.js
  • 008.Clear an environment variable.js
  • 009.Clear a global variable.js
  • 010.Clear a collection variable.js
  • 011.Send a request.js

Execution Timing of Pre-request and Test Scripts in Postman

Pre-request Scripts

A "Pre-request" script is executed before the actual API request is sent. It provides developers with a designated space to perform setup tasks, such as:

  • Authentication: Set up tokens or credentials required for the upcoming request.
  • Data Preparation: Generate dynamic data or variables needed for the request.
  • Header Configuration: Modify headers to meet specific requirements.

By running before the request, the "Pre-request" script ensures that the request is properly configured and authenticated, setting the stage for a successful API call.

Test Scripts

Conversely, a "Test" script is executed after the API request has been sent, and the response has been received. The primary purpose of the "Test" script is to automate the validation of the response. This includes:

  • Response Assertions: Check if the response meets expected criteria, such as status codes or specific data.
  • Chaining Requests: Utilize data from the response to modify or trigger subsequent requests.
  • Dynamic Variables: Extract information from the response and use it in other parts of the collection.

In summary, while the "Pre-request" script prepares the groundwork for the request, the "Test" script analyzes and verifies the results after the request has been processed. Together, these scripts empower developers to create robust and automated API workflows within Postman.

postman-api-javascript-resources's People

Contributors

simboli avatar

Stargazers

 avatar  avatar

Watchers

 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.