Postman Script: Difference between revisions
Jump to navigation
Jump to search
Line 99: | Line 99: | ||
{| | {| | ||
| valign="top" | | | valign="top" | | ||
* [https://momentjscom.readthedocs.io/en/latest/moment/03-manipulating/02-subtract/ Postman | * [https://momentjscom.readthedocs.io/en/latest/moment/03-manipulating/02-subtract/ Postman subtract time using Moment.js] | ||
* [https://stackoverflow.com/questions/29861699/ Postman formate date using Moment.js] | * [https://stackoverflow.com/questions/29861699/ Postman formate date using Moment.js] | ||
* [https://learning.postman.com/docs/writing-scripts/pre-request-scripts/ Postman | * [https://learning.postman.com/docs/writing-scripts/pre-request-scripts/ Postman writing pre-request scripts] | ||
* [https://stackoverflow.com/questions/49637741/ Postman add time using Moment.js] | * [https://stackoverflow.com/questions/49637741/ Postman add time using Moment.js] | ||
* [https://learning.postman.com/docs/writing-scripts/script-references/test-examples/ Postman | * [https://learning.postman.com/docs/writing-scripts/script-references/test-examples/ Postman test script examples] | ||
* [https://learning.postman.com/docs/writing-scripts/script-references/postman-sandbox-api-reference/ Postman JavaScript reference] | * [https://learning.postman.com/docs/writing-scripts/script-references/postman-sandbox-api-reference/ Postman JavaScript reference] | ||
* [https://learning.postman.com/docs/writing-scripts/script-references/variables-list/ Postman dynamic variables] | * [https://learning.postman.com/docs/writing-scripts/script-references/variables-list/ Postman dynamic variables] | ||
* [https://learning.postman.com/docs/sending-requests/variables/#variable-scopes Postman | * [https://learning.postman.com/docs/sending-requests/variables/#variable-scopes Postman variable scopes] | ||
* [https://learning.postman.com/docs/writing-scripts/test-scripts/ Postman | * [https://learning.postman.com/docs/writing-scripts/test-scripts/ Postman writing tests] | ||
* [https://learning.postman.com/docs/writing-scripts/intro-to-scripts/ Scripting in Postman] | * [https://learning.postman.com/docs/writing-scripts/intro-to-scripts/ Scripting in Postman] | ||
Revision as of 20:55, 19 July 2021
global : pm.globals.set("key", "value"); collection : pm.collectionVariables.set("key", "value"); environment: pm.environment.set("key", "value"); local : pm.variables.set("key", "value"); |
global : pm.globals.unset("key"); collection : pm.collectionVariables.unset("key"); environment: pm.environment.unset("key"); local : pm.variables.unset("key"); |
Before Request
var moment = require("moment"),
init = function(cli, ide, eon, today, birthDateMin, birthDateMax){
birthDateMin = eon(today).subtract(18, 'years').add(1, 'days').format("DD/MM/YYYY");
birthDateMax = eon(today).subtract(66, 'years').format("DD/MM/YYYY");
ide.variables.set('adult.birth.date.invalid.min', birthDateMin);
ide.variables.set('adult.birth.date.invalid.max', birthDateMax);
cli.info(`adult.birth.date.invalid.min: ${birthDateMin}`);
cli.info(`adult.birth.date.invalid.max: ${birthDateMax}`);
};
init(console, pm, moment, new Date());
After Request
pm.test("Successful Quotation", function () {
pm.response.to.have.status(200);
pm.expect(pm.response.json().message).to.eql("Quotation has been created successfully.");
pm.collectionVariables.set("quotationRefId", pm.response.json().refId);
});
OOP Script
var moment = require("moment"),
init = function(console, postman, moment, birthDate, debug){
birthDate = new AdultBirthDate(console, postman, moment);
birthDate.setUserVars(debug);
};
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.variables.set(USER_VARS.BIRTH_DATE.VALID.MAX, this.validMaxDate);
this.ide.variables.set(USER_VARS.BIRTH_DATE.VALID.MIN, this.validMinDate);
this.ide.variables.set(USER_VARS.BIRTH_DATE.INVALID.MAX, this.invalidMaxDate);
this.ide.variables.set(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);
}
}
init(console, pm, moment);