W3Cschool
恭喜您成為首批注冊用戶
獲得88經(jīng)驗值獎勵
Postman 為 JavaScript API 提供對象pm,您可以在 gRPC 請求腳本中使用該對象,在Postman Sandbox中執(zhí)行。
pm對象提供測試請求和響應(yīng)數(shù)據(jù)、訪問變量和一些元信息的功能。
該pm.request對象提供對腳本中請求數(shù)據(jù)的訪問。在Before invoke和After responsepm.request腳本中都可用。
以下是對象的屬性pm.request:
注意:對象不支持請求變更pm。
pm.response對象提供對當(dāng)前請求執(zhí)行的響應(yīng)中返回的數(shù)據(jù)的訪問。僅在After 響應(yīng)pm.response腳本中可用。
以下是對象的屬性pm.response:
該pm.info對象提供與請求和腳本本身相關(guān)的元信息,包括請求名稱、請求 ID 和執(zhí)行掛鉤的名稱。
以下是對象的屬性pm.info:
您可以分別使用pm.test和pm.expect函數(shù)將測試規(guī)范和斷言添加到您的腳本中。
該方法允許您使用ChaiJS expect BDDpm.expect語法在請求和響應(yīng)數(shù)據(jù)上編寫斷言。
pm.expect: (assertOn: any) => Assertion
您還可以使用pm.request.to.have.*,pm.response.to.have.*和pm.response.to.be.*來構(gòu)建您的斷言。
pm.response.to.have.statusCode(0);
pm.expect(pm.response.responseTime).to.be.below(200);
查看示例部分以獲取更多斷言。
前往此處的綜合指南,了解如何在腳本中使用變量。
您可以使用該方法從Before invoke和After responsepm.sendRequest腳本異步發(fā)送 HTTP 請求。
pm.sendRequest: (request: string | RequestDefinition, callback?: (error: any, response: Response)) => void
您可以向該pm.sendRequest方法傳遞一個 URL 字符串,或一個完整的請求定義對象,包括標頭、方法、正文等。
// Example with a plain string URL
pm.sendRequest('https://postman-echo.com/get', (error, response) => {
if (error) {
console.error(error);
} else {
console.log(response.json());
}
});
// Example with a full-fledged request
const request = {
url: 'https://postman-echo.com/post',
method: 'POST',
header: {
'Content-Type': 'application/json',
'X-Foo': 'bar'
},
body: {
mode: 'raw',
raw: JSON.stringify({ key: 'this is json' })
}
};
pm.sendRequest(request, (error, response) => {
console.log(error ? error : response.json());
});
// Example containing a test
pm.sendRequest('https://postman-echo.com/get', (error, response) => {
if (error) {
console.log(error);
}
pm.test('Response is OK', () => {
pm.expect(error).to.equal(null);
pm.expect(response).to.have.property('code', 200);
pm.expect(response).to.have.property('status', 'OK');
});
});
要使用一個庫,require模塊通過傳遞它的名字,并將返回的導(dǎo)出模塊內(nèi)容分配給一個變量。
require(moduleName: string): any
該require方法允許您使用沙箱內(nèi)置庫模塊。下面列出了可用庫的列表以及相應(yīng)文檔的鏈接。
以下 NodeJS 模塊也可在沙箱中使用:
Copyright©2021 w3cschool編程獅|閩ICP備15016281號-3|閩公網(wǎng)安備35020302033924號
違法和不良信息舉報電話:173-0602-2364|舉報郵箱:jubao@eeedong.com
掃描二維碼
下載編程獅App
編程獅公眾號
聯(lián)系方式:
更多建議: