$npx -y skills add LambdaTest/agent-skills --skill postman-testcase-generatorWrite Postman test scripts (JavaScript) for pre-request logic and post-response assertions, and embed them directly into a Postman collection. Use this skill whenever the user wants to add tests to Postman requests, asks about "pm.test", "pm.expect", "Postman assertions", "test s
| 1 | # Postman Test Script Writer |
| 2 | |
| 3 | Writes and embeds **Postman JavaScript test scripts** — both `prerequest` and `test` scripts — |
| 4 | into collection requests. Works from an existing collection JSON or a plain description. |
| 5 | |
| 6 | --- |
| 7 | |
| 8 | ## Postman Scripting Basics |
| 9 | |
| 10 | Postman scripts use the `pm` object and run in a sandboxed JS environment (no Node.js builtins). |
| 11 | |
| 12 | ### Key APIs |
| 13 | |
| 14 | ```javascript |
| 15 | // --- Assertions --- |
| 16 | pm.test("description", () => { pm.expect(...).to... }); |
| 17 | |
| 18 | // --- Response access --- |
| 19 | pm.response.code // HTTP status code (number) |
| 20 | pm.response.json() // Parse body as JSON |
| 21 | pm.response.text() // Body as string |
| 22 | pm.response.headers.get("Content-Type") |
| 23 | pm.response.responseTime // ms (number) |
| 24 | |
| 25 | // --- Variables --- |
| 26 | pm.environment.set("key", value); |
| 27 | pm.environment.get("key"); |
| 28 | pm.collectionVariables.set("key", value); |
| 29 | pm.collectionVariables.get("key"); |
| 30 | pm.variables.get("key"); // resolves: local > data > env > collection > global |
| 31 | |
| 32 | // --- Pre-request --- |
| 33 | pm.request.headers.add({ key: "X-Header", value: "val" }); |
| 34 | ``` |
| 35 | |
| 36 | --- |
| 37 | |
| 38 | ## Step 1 — Understand What to Test |
| 39 | |
| 40 | Identify the user's intent across these categories: |
| 41 | |
| 42 | | Category | Examples | |
| 43 | |---|---| |
| 44 | | **Status assertion** | "should return 200", "expect 201 on create" | |
| 45 | | **Schema/field check** | "response must have `id` and `name`", "check nested field" | |
| 46 | | **Value assertion** | "user.email equals input", "count > 0" | |
| 47 | | **Response time** | "must respond under 500ms" | |
| 48 | | **Chaining** | "save token from login response for next request" | |
| 49 | | **Dynamic pre-request** | "generate timestamp before request", "set random ID" | |
| 50 | | **Error handling** | "if 401, log warning", "check error message format" | |
| 51 | |
| 52 | --- |
| 53 | |
| 54 | ## Step 2 — Write the Scripts |
| 55 | |
| 56 | ### Status Code |
| 57 | ```javascript |
| 58 | pm.test("Status is 200", () => { |
| 59 | pm.response.to.have.status(200); |
| 60 | }); |
| 61 | ``` |
| 62 | |
| 63 | ### JSON Field Existence |
| 64 | ```javascript |
| 65 | pm.test("Response has required fields", () => { |
| 66 | const body = pm.response.json(); |
| 67 | pm.expect(body).to.have.property("id"); |
| 68 | pm.expect(body).to.have.property("name"); |
| 69 | }); |
| 70 | ``` |
| 71 | |
| 72 | ### Field Type & Value |
| 73 | ```javascript |
| 74 | pm.test("ID is a positive number", () => { |
| 75 | const body = pm.response.json(); |
| 76 | pm.expect(body.id).to.be.a("number").and.to.be.above(0); |
| 77 | }); |
| 78 | ``` |
| 79 | |
| 80 | ### Array Response |
| 81 | ```javascript |
| 82 | pm.test("Returns a non-empty array", () => { |
| 83 | const body = pm.response.json(); |
| 84 | pm.expect(body).to.be.an("array").with.length.above(0); |
| 85 | }); |
| 86 | ``` |
| 87 | |
| 88 | ### Response Time |
| 89 | ```javascript |
| 90 | pm.test("Response time under 500ms", () => { |
| 91 | pm.expect(pm.response.responseTime).to.be.below(500); |
| 92 | }); |
| 93 | ``` |
| 94 | |
| 95 | ### Chaining — Save token after login |
| 96 | ```javascript |
| 97 | // In Tests tab of login request: |
| 98 | pm.test("Login successful", () => { |
| 99 | pm.response.to.have.status(200); |
| 100 | const { access_token } = pm.response.json(); |
| 101 | pm.environment.set("token", access_token); |
| 102 | }); |
| 103 | ``` |
| 104 | |
| 105 | ### Pre-request — Dynamic value |
| 106 | ```javascript |
| 107 | // In Pre-request Script tab: |
| 108 | pm.collectionVariables.set("timestamp", Date.now()); |
| 109 | pm.collectionVariables.set("random_id", Math.floor(Math.random() * 10000)); |
| 110 | ``` |
| 111 | |
| 112 | ### Schema Validation (using Ajv-style via tv4) |
| 113 | ```javascript |
| 114 | const schema = { |
| 115 | type: "object", |
| 116 | required: ["id", "email"], |
| 117 | properties: { |
| 118 | id: { type: "number" }, |
| 119 | email: { type: "string" } |
| 120 | } |
| 121 | }; |
| 122 | pm.test("Response matches schema", () => { |
| 123 | const body = pm.response.json(); |
| 124 | pm.expect(tv4.validate(body, schema)).to.be.true; |
| 125 | }); |
| 126 | ``` |
| 127 | |
| 128 | --- |
| 129 | |
| 130 | ## Step 3 — Embed Into Collection JSON |
| 131 | |
| 132 | When the user provides a collection, add scripts to the relevant request items: |
| 133 | |
| 134 | ```json |
| 135 | { |
| 136 | "name": "Login", |
| 137 | "event": [ |
| 138 | { |
| 139 | "listen": "prerequest", |
| 140 | "script": { |
| 141 | "type": "text/javascript", |
| 142 | "exec": [ "// pre-request script lines as array of strings" ] |
| 143 | } |
| 144 | }, |
| 145 | { |
| 146 | "listen": "test", |
| 147 | "script": { |
| 148 | "type": "text/javascript", |
| 149 | "exec": [ |
| 150 | "pm.test(\"Status is 200\", () => {", |
| 151 | " pm.response.to.have.status(200);", |
| 152 | "});", |
| 153 | "", |
| 154 | "const { access_token } = pm.response.json();", |
| 155 | "pm.environment.set(\"token\", access_token);" |
| 156 | ] |
| 157 | } |
| 158 | } |
| 159 | ], |
| 160 | "req |