05、腳本語法

Postman Sandbox API參考


測試例子

在發(fā)送請求并從服務器收到響應后運行測試腳本。

讓我們看一下Postman測試的一些例子。其中大部分都是Postman內(nèi)部的片段。您可以根據(jù)需要為請求提供盡可能多的測試。

設置環(huán)境變量

pm.environment.set("variable_key","variable_value");

將嵌套對象設置為環(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)境變量(其值是字符串化對象)

// 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");

設置全局變量

pm.globals.set("variable_key","variable_value");

獲取全局變量

pm.globals.get("variable_key");

清除全局變量

pm.globals.unset("variable_key");

獲取變量

此函數(shù)在全局變量和活動環(huán)境中搜索變量。

pm.variables.get("variable_key");

檢查響應主體是否包含字符串

pm.test("Body matches string",function(){pm.expect(pm.response.text()).to.include("string_you_want_to_search");});

檢查響應主體是否等于字符串

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");});

響應時間小于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);});

代碼名稱包含一個字符串

pm.test("Status code name has string",function(){pm.response.to.have.status("Created");});

成功的POST請求狀態(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ā)送異步請求

此功能既可用作預請求腳本,也可用作測試腳本。

pm.sendRequest("https://postman-echo.com/get",function(err,response){console.log(response.json());});

將XML主體轉換為JSON對象

varjsonObject=xml2Json(responseBody);

示例數(shù)據(jù)文件

JSON文件由鍵/值對組成。

下載JSON文件

對于CSV文件,頂行需要包含變量名稱。

下載CSV文件

較舊的寫作郵差測試風格

較舊的Postman測試編寫風格依賴于特殊tests對象的設置值。您可以為對象中的元素設置描述性鍵,然后說明它是真還是假。例如,tests["Body contains user_id"] = responsebody.has("user_id");將檢查響應主體是否包含user_id字符串。

您可以根據(jù)需要添加任意數(shù)量的密鑰,具體取決于您要測試的內(nèi)容。在響應查看器下的“?測試”選項卡下,您可以查看測試結果。選項卡標題顯示傳遞了多少測試,并在此處列出了您在tests變量中設置的鍵。如果值的計算結果為true,則測試通過。

設置環(huán)境變量

postman.setEnvironmentVariable("key","value");

將嵌套對象設置為環(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)境變量(其值是字符串化對象)

// 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");

設置全局變量

postman.setGlobalVariable("key","value");

獲取全局變量

postman.getGlobalVariable("key");

清除全局變量

postman.clearGlobalVariable("key");

檢查響應主體是否包含字符串

tests["Body matches string"]=responseBody.has("string_you_want_to_search");

將XML主體轉換為JSON對象

varjsonObject=xml2Json(responseBody);

檢查響應主體是否等于字符串

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");

響應時間小于200毫秒

tests["Response time is less than 200ms"]=responseTime<200;

響應時間在特定范圍內(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;

代碼名稱包含一個字符串

tests["Status code name has string"]=responseCode.name.has("Created");

成功的POST請求狀態(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

最后編輯于
?著作權歸作者所有,轉載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務。

相關閱讀更多精彩內(nèi)容

  • Spring Cloud為開發(fā)人員提供了快速構建分布式系統(tǒng)中一些常見模式的工具(例如配置管理,服務發(fā)現(xiàn),斷路器,智...
    卡卡羅2017閱讀 136,554評論 19 139
  • mean to add the formatted="false" attribute?.[ 46% 47325/...
    ProZoom閱讀 3,197評論 0 3
  • 一、 安裝與更新 1、安裝的方式 方式1:chrome插件版本:chrome--->設置--->擴展程序; 方式2...
    MR_LiY閱讀 10,369評論 3 3
  • 一、Pre Request Scripts Postman v0.10+ 版本支持pre-request scri...
    常大鵬閱讀 68,730評論 4 69
  • 一、債券類投資品:債券、P2P 1.國家、地方政府、企業(yè)可以依照規(guī)定發(fā)行債券 2.個人投資者通過互聯(lián)網(wǎng)金融平臺把錢...
    Jo_608a閱讀 212評論 0 0

友情鏈接更多精彩內(nèi)容