Skip to content
The Chat Archive
gpt-3.5-turbo-0301· OpenAI WildChat-4.8M (AI2)

can you show me some basic script for postman

  1. User

    can you show me some basic script for postman

  2. Assistant

    Of course, here are some basic scripts for Postman:

    1. Set a variable:
    // Set a variable
    pm.variables.set("variable_name", "variable_value");
    
    1. Get an environment variable:
    // Get an environment variable
    let environment_variable = pm.environment.get("variable_name");
    
    1. 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
    });
    
    1. 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
    
    1. 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.

  3. User

    แสดงวิธีเขียน test script ของ postman ได้ไหม

  4. Assistant

    แน่นอนครับ นี่คือวิธีการเขียน test script ของ Postman:

    1. ตรวจสอบ HTTP status code:
    // ตรวจสอบ HTTP status code เป็น 200 OK
    pm.test("HTTP status เป็น 200 OK", function () {
        pm.response.to.have.status(200);
    });
    
    1. ตรวจสอบ response time:
    // ตรวจสอบ response time ไม่เกิน 500 ms
    pm.test("Response time ไม่เกิน 500 ms", function () {
        pm.expect(pm.response.responseTime).to.be.below(500);
    });
    
    1. ตรวจสอบค่าภายใน 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");
    });
    
    1. ตรวจสอบข้อมูลภายใน 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");
    });
    
    1. ตรวจสอบค่าของตัวแปร 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 ในการทดสอบ

  5. User

    pm.expect ทำอะไรได้บ้าง

  6. Assistant

    pm.expect() เป็นเมธอดของ Postman ที่ใช้ในการทำ assertion เพื่อตรวจสอบค่าต่างๆ เช่น ค่าของตัวแปร, ค่าของ response body, ค่าของ response headers เป็นต้น โดยสามารถใช้เมธอดต่างๆ ของ pm.expect() เพื่อเปรียบเทียบค่าเจนรายละเอียดได้ดังนี้:

    1. to.eql()
    pm.expect(variable).to.eql(expected_value);
    

    ค่าของ variable ควรจะเท่ากับ expected_value เช่น

    pm.expect(response_json.id).to.eql(1);
    
    1. to.be.a()
    pm.expect(variable).to.be.a(data_type);
    

    variable ควรจะเป็นชนิดของ data_type เช่น

    pm.expect(test_value).to.be.a('string');
    
    1. to.be.below()
    pm.expect(variable).to.be.below(expected_value);
    

    ค่าของ variable ควรไม่เกิน expected_value เช่น

    pm.expect(pm.response.responseTime).to.be.below(500);
    
    1. to.have.header()
    pm.response.to.have.header(header_name);
    

    Response headers ควรมี header_name เช่น

    pm.response.to.have.header('content-type');
    
    1. 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"
    });
    
    1. 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 ให้ได้ถูกต้องและแม่นยำ

  7. 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'); ไม่ได้

  8. 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 หรือไม่