【Web_接口測試_postman_JS常用變量函數】獲取並切割當前年月日/時分秒/隨機數等,自動化測試案例

【pre-request script】

//獲取當前時間,並設置環境變量 

eval(globals.format_time); 

pm.globals.set("timeNow",timeNow);



//獲取當前時間到時分秒

var dateNow = pm.globals.get("timeNow");



//切割當前時間,獲取年月日,不要時分秒(8位)

var date = dateNow.split(" ")[0].split("-")[0]+

dateNow.split(" ")[0].split("-")[1]+dateNow.split(" ")[0].split("-")[2];



//切割當前時間,獲取時分秒,不要年月日(6位)

var time = dateNow.split(" ")[1].split(":")[0]+

dateNow.split(" ")[1].split(":")[1]+dateNow.split(" ")[1].split(":")[2];



//切割當前時間,獲取年月日+固定時分秒

var datetime = dateNow.split(" ")[0]+" 23:59:59";



//設置環境變量開始時間和結束時間

pm.globals.set("date",date);pm.globals.set("time",time);pm.globals.set("datetime",datetime);

console.log("當前日期:"+date,"當前時間:"+time,"--當前日期和時間:"+timeNow,datetime);

【Tests】

pm.test("Status code is 200", function () {

    pm.response.to.have.status(200);

});

pm.test("Response time is less than 200ms", function () {

    pm.expect(pm.response.responseTime).to.be.below(500);

});



pm.test("Body matches string", function () {

    pm.expect(pm.response.text()).to.include("北京");

});

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章