// Set timestamppm.environment.set("timestamp",Date.now());// Generate random datapm.environment.set("randomEmail","user"+Math.random().toString(36).substring(7)+"@example.com");// Set authentication tokenconsttoken=pm.environment.get("auth_token");pm.request.headers.add(\\\\{key:"Authorization",value:"Bearer "+token\\\\});// Generate UUIDconstuuid=require('uuid');pm.environment.set("requestId",uuid.v4());// Base64 encode credentialsconstusername=pm.environment.get("username");constpassword=pm.environment.get("password");constcredentials=btoa(username+":"+password);pm.environment.set("basicAuth",credentials);
// Status code testspm.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("OK");\\\\});// Response time testpm.test("Response time is less than 200ms",function()\\\\{pm.expect(pm.response.responseTime).to.be.below(200);\\\\});// Header testspm.test("Content-Type is present",function()\\\\{pm.response.to.have.header("Content-Type");\\\\});pm.test("Content-Type is application/json",function()\\\\{pm.expect(pm.response.headers.get("Content-Type")).to.include("application/json");\\\\});
// Parse JSON responseconstresponseJson=pm.response.json();// Test JSON structurepm.test("Response has required fields",function()\\\\{pm.expect(responseJson).to.have.property("id");pm.expect(responseJson).to.have.property("name");pm.expect(responseJson).to.have.property("email");\\\\});// Test specific valuespm.test("User ID is correct",function()\\\\{pm.expect(responseJson.id).to.eql(123);\\\\});pm.test("Email format is valid",function()\\\\{pm.expect(responseJson.email).to.match(/^[^\s@]+@[^\s@]+\.[^\s@]+$/);\\\\});// Test array responsespm.test("Response is an array",function()\\\\{pm.expect(responseJson).to.be.an('array');\\\\});pm.test("Array has correct length",function()\\\\{pm.expect(responseJson).to.have.lengthOf(5);\\\\});
// Extract data from responseconstresponseJson=pm.response.json();// Set environment variablespm.environment.set("userId",responseJson.id);pm.environment.set("userToken",responseJson.token);// Extract from headersconstlocation=pm.response.headers.get("Location");pm.environment.set("resourceUrl",location);// Extract using regexconstresponseText=pm.response.text();constmatch=responseText.match(/token:\s*"([^"]+)"/);if(match)\\\\{pm.environment.set("extractedToken",match[1]);\\\\}
// Collection variablespm.collectionVariables.set("baseUrl","https://api.example.com");pm.collectionVariables.get("baseUrl");// Data-driven testing// Use CSV or JSON files for test data// Access data using pm.iterationData.get("fieldName")// Workflow controlpostman.setNextRequest("Request Name");postman.setNextRequest(null);// Stop execution
// Pre-request script for loginpm.sendRequest(\\\\{url:pm.environment.get("baseUrl")+"/auth/login",method:"POST",header:\\\\{"Content-Type":"application/json"\\\\},body:\\\\{mode:"raw",raw:JSON.stringify(\\\\{username:pm.environment.get("username"),password:pm.environment.get("password")\\\\})\\\\}\\\\},function(err,response)\\\\{if(response.code===200)\\\\{consttoken=response.json().token;pm.environment.set("authToken",token);\\\\}\\\\});
// Test error responsespm.test("Returns 400 for invalid data",function()\\\\{pm.response.to.have.status(400);consterror=pm.response.json();pm.expect(error).to.have.property("message");\\\\});pm.test("Returns 401 for unauthorized access",function()\\\\{pm.response.to.have.status(401);\\\\});pm.test("Returns 404 for non-existent resource",function()\\\\{pm.response.to.have.status(404);\\\\});