can you show me some basic script for postman
- User
can you show me some basic script for postman
- Assistant
Of course, here are some basic scripts for Postman:
- Set a variable:
// Set a variable pm.variables.set("variable_name", "variable_value");- Get an environment variable:
// Get an environment variable let environment_variable = pm.environment.get("variable_name");- Make a request and access the response body:
// Make a request and access response body pm.sendRequest({ url: "https://jsonplaceholder.typicode.com/posts", method: "GET" }, function (err, res) { let response_body = res.body; // Do something with the response body });- Set a delay before making a request:
// Set a delay before making a request setTimeout(function() { pm.sendRequest({ url: "https://jsonplaceholder.typicode.com/posts", method: "GET" }, function (err, res) { // Handle the response }); }, 5000); // Wait for 5 seconds before making the request- Extract a value from the response body using regex:
// Extract a value from the response body using regex let response_body = pm.response.body; let regex_pattern = /"id":(\d+)/; let match = response_body.match(regex_pattern); if (match && match[1]) { let extracted_value = match[1]; // Do something with the extracted value }These are just some basic examples, you can find more script examples and documentation in the Postman documentation.
- User
แสดงวิธีเขียน test script ของ postman ได้ไหม
- Assistant
แน่นอนครับ นี่คือวิธีการเขียน test script ของ Postman:
- ตรวจสอบ HTTP status code:
// ตรวจสอบ HTTP status code เป็น 200 OK pm.test("HTTP status เป็น 200 OK", function () { pm.response.to.have.status(200); });- ตรวจสอบ response time:
// ตรวจสอบ response time ไม่เกิน 500 ms pm.test("Response time ไม่เกิน 500 ms", function () { pm.expect(pm.response.responseTime).to.be.below(500); });- ตรวจสอบค่าภายใน response body:
// ตรวจสอบค่าจาก response body ว่าตรงกับที่คาดหวังหรือไม่ pm.test("ค่าภายใน response body ตรงกับที่คาดหวัง", function () { let response_json = pm.response.json(); pm.expect(response_json.id).to.eql(1); pm.expect(response_json.title).to.eql("sunt aut facere repellat provident occaecati excepturi optio reprehenderit"); });- ตรวจสอบข้อมูลภายใน response headers:
// ตรวจสอบข้อมูลภายใน response headers ว่าตรงกับที่คาดหวังหรือไม่ pm.test("ข้อมูลภายใน response headers ตรงกับที่คาดหวัง", function () { pm.response.to.have.header("content-type"); pm.expect(pm.response.headers.get("content-type")).to.equal("application/json; charset=utf-8"); });- ตรวจสอบค่าของตัวแปร environment:
// ตรวจสอบค่าของตัวแปร environment ว่าตรงกับที่คาดหวังหรือไม่ pm.test("ค่าของตัวแปร environment ตรงกับที่คาดหวัง", function () { let environment_variable = pm.environment.get("variable_name"); pm.expect(environment_variable).to.eql("expected_value"); });นี่เป็นตัวอย่างเบื้องต้นของ test script ของ Postman แล้วครับ หวังว่าจะเป็นประโยชน์มากสำหรับคุณ แต่โปรดทราบว่าการเขียน test script ดีหรือไม่ดีขึ้นอยู่กับความเหมาะสมและรายละเอียดของ API ในการทดสอบ
- User
pm.expect ทำอะไรได้บ้าง
- Assistant
pm.expect() เป็นเมธอดของ Postman ที่ใช้ในการทำ assertion เพื่อตรวจสอบค่าต่างๆ เช่น ค่าของตัวแปร, ค่าของ response body, ค่าของ response headers เป็นต้น โดยสามารถใช้เมธอดต่างๆ ของ pm.expect() เพื่อเปรียบเทียบค่าเจนรายละเอียดได้ดังนี้:
- to.eql()
pm.expect(variable).to.eql(expected_value);ค่าของ
variableควรจะเท่ากับexpected_valueเช่นpm.expect(response_json.id).to.eql(1);- to.be.a()
pm.expect(variable).to.be.a(data_type);variableควรจะเป็นชนิดของdata_typeเช่นpm.expect(test_value).to.be.a('string');- to.be.below()
pm.expect(variable).to.be.below(expected_value);ค่าของ
variableควรไม่เกินexpected_valueเช่นpm.expect(pm.response.responseTime).to.be.below(500);- to.have.header()
pm.response.to.have.header(header_name);Response headers ควรมี
header_nameเช่นpm.response.to.have.header('content-type');- to.have.jsonBody()
pm.response.to.have.jsonBody(json_object);Response body ควรมี
json_objectเช่นpm.response.to.have.jsonBody({ "id": 1, "title": "sunt aut facere repellat provident occaecati excepturi optio reprehenderit" });- to.have.status()
pm.response.to.have.status(status_code);HTTP status code ควรเป็น
status_codeเช่นpm.response.to.have.status(200);นี่เป็นตัวอย่างของเมธอดที่ใช้ใน pm.expect() เพื่อช่วยในการ assertion ค่าต่างๆ ใน Postman ให้ได้ถูกต้องและแม่นยำ
- User
postman โค๊ดนี้ pm.expect(jsonData).to.have.all.keys('won','lost','drew'); กับ pm.expect(jsonData).to.have.property('won'); ทำไมเราจึงใช้ pm.expect(jsonData).to.have.a.keys('won'); ไม่ได้
- Assistant
การใช้
pm.expect(jsonData).to.have.all.keys('won', 'lost', 'drew')จะตรวจสอบว่า JSON data ที่เราต้องการตรวจสอบมี keys ชื่อwon,lost, และdrewทั้งหมดในตัวเองหรือไม่ โดยไม่ได้ต้องกำหนดค่าของแต่ละ key นั่นเองในขณะที่
pm.expect(jsonData).to.have.property('won')จะตรวจสอบว่า JSON data ที่เราต้องการตรวจสอบมี property ชื่อwonหรือไม่ โดยไม่ต้องกำหนดค่าของ key นั้นเองส่วน
pm.expect(jsonData).to.have.a.keys('won')นั้นไม่ใช่วิธีการตรวจสอบ keys ใน JSON data แต่เป็นวิธีการตรวจสอบว่า JSON data ที่เราต้องการตรวจสอบมี keys อยู่หรือไม่ โดยใช้aแทนคำว่าallหรือanyเพื่อหา keys ใน JSON data ทั้งหมด (all keys) หรือบางตัว (any key)ดังนั้น เราต้องใช้
pm.expect(jsonData).to.have.all.keys('won', 'lost', 'drew')หรือpm.expect(jsonData).to.have.property('won')แทนpm.expect(jsonData).to.have.a.keys('won')ในการตรวจสอบว่า JSON data มี keys ชื่อwonหรือไม่