
便利だけど何かと調べるのが億劫なので、コピペでいけるようにメモ。
環境変数を設定する
postman.setEnvironmentVariable("key", "value")
環境変数を取得する
postman.getEnvironmentVariable("key")
これはenvironment.key
でもOK。
環境変数を初期化する
ひとつだけなら postman.clearEnvironmentVariable("key")
ぜんぶならpostman.clearEnvironmentVariables()
グローバル変数を設定する
postman.setGlobalVariable("key", "value")
グローバル変数を取得する
postman.getGlobalVariable("key")
これはglobals.key
でもOK。
グローバル変数を初期化する
ひとつだけならpostman.clearGlobalVariable("key")
ぜんぶならpostman.clearGlobalVariables()
レスポンスの変換
XMLからJSONへの変換。
var jsonObject = xml2Json(responseBody);
JSONからJSONオブジェクトへの変換。
var jsonObject = JSON.parse(responseBody);
UUID/GUID
{{$guid}}
これを使うとこんな感じのデータを都度自動生成してくれる。
25b70867-09d8-435b-af14-a05105bf8722
タイムスタンプを取得する
{{$timestamp}}
UNIXTIME(ユニックスタイム)の取得。乱数としてつかえるかんじ。
1490871792
ランダムな整数を取得する
{{$randomInt}}
0から1000の範囲らしい。0詰めはしてくれない。
493
レスポンスに文字列を含んでいるか調べる
tests["Body matches string"] = responseBody.has("string_you_want_to_search");
参考
- Sandbox: https://www.getpostman.com/docs/sandbox
- Testing examples: https://www.getpostman.com/docs/testing_examples