Postman Sandbox API參考
測(cè)試?yán)?/h1>
在發(fā)送請(qǐng)求并從服務(wù)器收到響應(yīng)后運(yùn)行測(cè)試腳本蚯窥。
讓我們看一下Postman測(cè)試的一些例子掸鹅。其中大部分都是Postman內(nèi)部的片段。您可以根據(jù)需要為請(qǐng)求提供盡可能多的測(cè)試拦赠。
設(shè)置環(huán)境變量
pm.environment.set("variable_key","variable_value");
將嵌套對(duì)象設(shè)置為環(huán)境變量
vararray=[1,2,3,4];pm.environment.set("array",JSON.stringify(array,null,2));varobj={a:[1,2,3,4],b:{c:'val'}};pm.environment.set("obj",JSON.stringify(obj));
獲取環(huán)境變量
pm.environment.get("variable_key");
獲取環(huán)境變量(其值是字符串化對(duì)象)
// These statements should be wrapped in a try-catch block if the data is coming from an unknown source.vararray=JSON.parse(pm.environment.get("array"));varobj=JSON.parse(pm.environment.get("obj"));
清除環(huán)境變量
pm.environment.unset("variable_key");
設(shè)置全局變量
pm.globals.set("variable_key","variable_value");
獲取全局變量
pm.globals.get("variable_key");
清除全局變量
pm.globals.unset("variable_key");
獲取變量
此函數(shù)在全局變量和活動(dòng)環(huán)境中搜索變量河劝。
pm.variables.get("variable_key");
檢查響應(yīng)主體是否包含字符串
pm.test("Body matches string",function(){pm.expect(pm.response.text()).to.include("string_you_want_to_search");});
檢查響應(yīng)主體是否等于字符串
pm.test("Body is correct",function(){pm.response.to.have.body("response_body_string");});
檢查JSON值
pm.test("Your test name",function(){varjsonData=pm.response.json();pm.expect(jsonData.value).to.eql(100);});
內(nèi)容類型存在
pm.test("Content-Type is present",function(){pm.response.to.have.header("Content-Type");});
響應(yīng)時(shí)間小于200毫秒
pm.test("Response time is less than 200ms",function(){pm.expect(pm.response.responseTime).to.be.below(200);});
狀態(tài)代碼是200
pm.test("Status code is 200",function(){pm.response.to.have.status(200);});
代碼名稱包含一個(gè)字符串
pm.test("Status code name has string",function(){pm.response.to.have.status("Created");});
成功的POST請(qǐng)求狀態(tài)代碼
pm.test("Successful POST request",function(){pm.expect(pm.response.code).to.be.oneOf([201,202]);});
使用TinyValidator獲取JSON數(shù)據(jù)
varschema={"items":{"type":"boolean"}};vardata1=[true,false];vardata2=[true,123];pm.test('Schema is valid',function(){pm.expect(tv4.validate(data1,schema)).to.be.true;pm.expect(tv4.validate(data2,schema)).to.be.true;});
解碼base64編碼數(shù)據(jù)
varintermediate,base64Content,// assume this has a base64 encoded valuerawContent=base64Content.slice('data:application/octet-stream;base64,'.length);intermediate=CryptoJS.enc.Base64.parse(base64content);// CryptoJS is an inbuilt object, documented here: https://www.npmjs.com/package/crypto-jspm.test('Contents are valid',function(){pm.expect(CryptoJS.enc.Utf8.stringify(intermediate)).to.be.true;// a check for non-emptiness});
發(fā)送異步請(qǐng)求
此功能既可用作預(yù)請(qǐng)求腳本,也可用作測(cè)試腳本矛紫。
pm.sendRequest("https://postman-echo.com/get",function(err,response){console.log(response.json());});
將XML主體轉(zhuǎn)換為JSON對(duì)象
varjsonObject=xml2Json(responseBody);
示例數(shù)據(jù)文件
JSON文件由鍵/值對(duì)組成赎瞎。
對(duì)于CSV文件,頂行需要包含變量名稱颊咬。
較舊的寫作郵差測(cè)試風(fēng)格
較舊的Postman測(cè)試編寫風(fēng)格依賴于特殊tests對(duì)象的設(shè)置值务甥。您可以為對(duì)象中的元素設(shè)置描述性鍵,然后說明它是真還是假喳篇。例如敞临,tests["Body contains user_id"] = responsebody.has("user_id");將檢查響應(yīng)主體是否包含user_id字符串。
您可以根據(jù)需要添加任意數(shù)量的密鑰麸澜,具體取決于您要測(cè)試的內(nèi)容挺尿。在響應(yīng)查看器下的“?測(cè)試”選項(xiàng)卡下,您可以查看測(cè)試結(jié)果。選項(xiàng)卡標(biāo)題顯示傳遞了多少測(cè)試编矾,并在此處列出了您在tests變量中設(shè)置的鍵熟史。如果值的計(jì)算結(jié)果為true,則測(cè)試通過窄俏。
設(shè)置環(huán)境變量
postman.setEnvironmentVariable("key","value");
將嵌套對(duì)象設(shè)置為環(huán)境變量
vararray=[1,2,3,4];postman.setEnvironmentVariable("array",JSON.stringify(array,null,2));varobj={a:[1,2,3,4],b:{c:'val'}};postman.setEnvironmentVariable("obj",JSON.stringify(obj));
獲取環(huán)境變量
postman.getEnvironmentVariable("key");
獲取環(huán)境變量(其值是字符串化對(duì)象)
// These statements should be wrapped in a try-catch block if the data is coming from an unknown source.vararray=JSON.parse(postman.getEnvironmentVariable("array"));varobj=JSON.parse(postman.getEnvironmentVariable("obj"));
清除環(huán)境變量
postman.clearEnvironmentVariable("key");
設(shè)置全局變量
postman.setGlobalVariable("key","value");
獲取全局變量
postman.getGlobalVariable("key");
清除全局變量
postman.clearGlobalVariable("key");
檢查響應(yīng)主體是否包含字符串
tests["Body matches string"]=responseBody.has("string_you_want_to_search");
將XML主體轉(zhuǎn)換為JSON對(duì)象
varjsonObject=xml2Json(responseBody);
檢查響應(yīng)主體是否等于字符串
tests["Body is correct"]=responseBody==="response_body_string";
檢查JSON值
vardata=JSON.parse(responseBody);tests["Your test name"]=data.value===100;
存在Content-Type(不區(qū)分大小寫的檢查)
tests["Content-Type is present"]=postman.getResponseHeader("Content-Type");//Note: the getResponseHeader() method returns the header value, if it exists.
內(nèi)容類型存在(區(qū)分大小寫)
tests["Content-Type is present"]=responseHeaders.hasOwnProperty("Content-Type");
響應(yīng)時(shí)間小于200毫秒
tests["Response time is less than 200ms"]=responseTime<200;
響應(yīng)時(shí)間在特定范圍內(nèi)(包含下限蹂匹,上限不包括)
tests["Response time is acceptable"]=_.inRange(responseTime,100,1001);// _ is the inbuilt Lodash v3.10.1 object, documented at https://lodash.com/docs/3.10.1
狀態(tài)代碼是200
tests["Status code is 200"]=responseCode.code===200;
代碼名稱包含一個(gè)字符串
tests["Status code name has string"]=responseCode.name.has("Created");
成功的POST請(qǐng)求狀態(tài)代碼
tests["Successful POST request"]=responseCode.code===201||responseCode.code===202;
使用TinyValidator獲取JSON數(shù)據(jù)
varschema={"items":{"type":"boolean"}};vardata1=[true,false];vardata2=[true,123];tests["Valid Data1"]=tv4.validate(data1,schema);tests["Valid Data2"]=tv4.validate(data2,schema);console.log("Validation failed: ",tv4.error);
解碼base64編碼數(shù)據(jù)
varintermediate,base64Content,// assume this has a base64 encoded valuerawContent=base64Content.slice('data:application/octet-stream;base64,'.length);intermediate=CryptoJS.enc.Base64.parse(base64content);// CryptoJS is an inbuilt object, documented here: https://www.npmjs.com/package/crypto-jstests["Contents are valid"]=CryptoJS.enc.Utf8.stringify(intermediate);// a check for non-emptiness