Postman Script: Difference between revisions

From Chorke Wiki
Jump to navigation Jump to search
No edit summary
Line 29: Line 29:
</source>
</source>


==OOP Script==
<source lang="js">
var moment      = require("moment"),
    beforeRequest = function(console, postman, moment, birthDate){
        birthDate = new AdultBirthDate(console, postman, moment);
        birthDate.setUserVars(true);
    };
const CHRONO_UNIT = { YEARS : 'years', DAYS  : 'days' },
      DATE_FORMAT = { DD_MM_YYY: 'DD/MM/YYYY' },
      USER_VARS  = { BIRTH_DATE: {
        VALID  : {
            MAX: 'adult.birth.date.valid.max',
            MIN: 'adult.birth.date.valid.min'
        },
        INVALID : {
            MAX: 'adult.birth.date.invalid.max',
            MIN: 'adult.birth.date.invalid.min'
        }
    }};
class AdultBirthDate {
    constructor(console, postman, moment){
        this.cli = console;
        this.ide = postman;
        this.eon = moment;
    }
   
    get validMinDate(){
        return this.eon(new Date()).subtract(18, CHRONO_UNIT.YEARS).format(DATE_FORMAT.DD_MM_YYY);
    }
   
    get validMaxDate(){
        return this.eon(new Date()).subtract(66, CHRONO_UNIT.YEARS).add(1, CHRONO_UNIT.DAYS).format(DATE_FORMAT.DD_MM_YYY);
    }
   
    get invalidMinDate(){
        return this.eon(new Date()).subtract(18, CHRONO_UNIT.YEARS).add(1, CHRONO_UNIT.DAYS).format(DATE_FORMAT.DD_MM_YYY);
    }
   
    get invalidMaxDate(){
        return this.eon(new Date()).subtract(66, CHRONO_UNIT.YEARS).format(DATE_FORMAT.DD_MM_YYY);
    }
    setUserVars(debug){
        debug && this.info();
        this.ide.setEnvironmentVariable(USER_VARS.BIRTH_DATE.VALID.MAX,  this.validMaxDate);
        this.ide.setEnvironmentVariable(USER_VARS.BIRTH_DATE.VALID.MIN,  this.validMinDate);
        this.ide.setEnvironmentVariable(USER_VARS.BIRTH_DATE.INVALID.MAX, this.invalidMaxDate);
        this.ide.setEnvironmentVariable(USER_VARS.BIRTH_DATE.INVALID.MIN, this.invalidMinDate);
    }
    info(message){
        message  = `${USER_VARS.BIRTH_DATE.VALID.MAX}  : ${this.validMaxDate}\n`;
        message += `${USER_VARS.BIRTH_DATE.VALID.MIN}  : ${this.validMinDate}\n`;
        message += `${USER_VARS.BIRTH_DATE.INVALID.MAX}: ${this.invalidMaxDate}\n`;
        message += `${USER_VARS.BIRTH_DATE.INVALID.MIN}: ${this.invalidMinDate}`;
        this.cli.info(message);
    }
}
beforeRequest(console, postman, moment);
</source>


==References==
==References==

Revision as of 11:07, 19 July 2021

Scripting

Before Request

var moment = require("moment");
(function(pm, moment, today, adultBithDateMin, adultBithDateMax){
    adultBithDateMin = moment(today).subtract(18, 'years').add(1, 'days').format("DD/MM/YYYY");
    adultBithDateMax = moment(today).subtract(66, 'years').format("DD/MM/YYYY");
    pm.setEnvironmentVariable('adult.birth.date.min', adultBithDateMin);
    pm.setEnvironmentVariable('adult.birth.date.max', adultBithDateMax);
    console.info("Adult Min birth Date: " + adultBithDateMin);
    console.info("Adult Max birth Date: " + adultBithDateMax);
}(postman, moment, new Date()));

After Request

pm.test("Status code is 200", function () {
    pm.response.to.have.status(200);
});

var jsonData = JSON.parse(responseBody);
postman.setEnvironmentVariable("quotationRefId", jsonData.refId);


pm.test("Successful Quotation", function () {
    var jsonData = pm.response.json();
    pm.expect(jsonData.message).to.eql("Quotation has been created successfully.");
});


OOP Script

var moment       = require("moment"),
    beforeRequest = function(console, postman, moment, birthDate){
        birthDate = new AdultBirthDate(console, postman, moment);
        birthDate.setUserVars(true);
    };

const CHRONO_UNIT = { YEARS : 'years', DAYS  : 'days' },
      DATE_FORMAT = { DD_MM_YYY: 'DD/MM/YYYY' },
      USER_VARS   = { BIRTH_DATE: {
        VALID   : {
            MAX: 'adult.birth.date.valid.max',
            MIN: 'adult.birth.date.valid.min'
        },
        INVALID : {
            MAX: 'adult.birth.date.invalid.max',
            MIN: 'adult.birth.date.invalid.min'
        }
    }};

class AdultBirthDate {
    constructor(console, postman, moment){
        this.cli = console;
        this.ide = postman;
        this.eon = moment;
    }
    
    get validMinDate(){
        return this.eon(new Date()).subtract(18, CHRONO_UNIT.YEARS).format(DATE_FORMAT.DD_MM_YYY);
    }
    
    get validMaxDate(){
        return this.eon(new Date()).subtract(66, CHRONO_UNIT.YEARS).add(1, CHRONO_UNIT.DAYS).format(DATE_FORMAT.DD_MM_YYY);
    }
    
    get invalidMinDate(){
        return this.eon(new Date()).subtract(18, CHRONO_UNIT.YEARS).add(1, CHRONO_UNIT.DAYS).format(DATE_FORMAT.DD_MM_YYY);
    }
    
    get invalidMaxDate(){
        return this.eon(new Date()).subtract(66, CHRONO_UNIT.YEARS).format(DATE_FORMAT.DD_MM_YYY);
    }

    setUserVars(debug){
        debug && this.info();
        this.ide.setEnvironmentVariable(USER_VARS.BIRTH_DATE.VALID.MAX,   this.validMaxDate);
        this.ide.setEnvironmentVariable(USER_VARS.BIRTH_DATE.VALID.MIN,   this.validMinDate);
        this.ide.setEnvironmentVariable(USER_VARS.BIRTH_DATE.INVALID.MAX, this.invalidMaxDate);
        this.ide.setEnvironmentVariable(USER_VARS.BIRTH_DATE.INVALID.MIN, this.invalidMinDate);
    }

    info(message){
        message  = `${USER_VARS.BIRTH_DATE.VALID.MAX}  : ${this.validMaxDate}\n`;
        message += `${USER_VARS.BIRTH_DATE.VALID.MIN}  : ${this.validMinDate}\n`;
        message += `${USER_VARS.BIRTH_DATE.INVALID.MAX}: ${this.invalidMaxDate}\n`;
        message += `${USER_VARS.BIRTH_DATE.INVALID.MIN}: ${this.invalidMinDate}`;
        this.cli.info(message);
    }
}

beforeRequest(console, postman, moment);

References