API Testing với Postman (Phần 24) – Build dynamic request body

Thế nào là một dynamic request body, đó là 1 cái request body có data linh động, mỗi lần run test bạn không phải sửa lại data. Mình đã nhắc đến nội dung này trong 1 bài viết khác.

Đây là request body thông thường

{
  "firstname": "Jim",
  "lastname": "Brown",
  "totalprice": 111,
  "depositpaid": true,
  "bookingdates": {
    "checkin": "2018-01-01",
    "checkout": "2019-01-01"
  },
  "additionalneeds": "Breakfast"
}

Đây là dynamic request body, tất cả các vị trí đều là variable (biến)

{
  "firstname": "{{firstname}}",   //random first name
  "lastname": "{{lastname}}",     //random last name
  "totalprice": {{totalprice}},   // 100 < price < 200
  "depositpaid": {{depositpaid}}, // true or fail
  "bookingdates": {
    "checkin": "{{checkin}}",     // >= today
    "checkout": "{{checkout}}"    // = today + (2/4) days
  },
  "additionalneeds": "{{additionalneeds}}" 
  // one in ["Breakfast", "view sea", "TV", "free transport"]
}

I. Postman cung cấp fake data cực dễ dùng

Full list ở đây: https://learning.postman.com/docs/writing-scripts/script-references/variables-list/

Mình đã hướng dẫn cách sử dụng ở bài 19

Chốt lại ta có:

"firstname": "{{$randomFirstName}}",
"lastname": "{{$randomLastName}}"

II. Random number

"totalprice": {{totalprice}}

Để random số trong 1 khoảng, có 1 vài cách làm như sau:

//Math.floor(Math.random() * (max - min + 1) ) + min;
let price = Math.floor(Math.random() * (200 - 100 + 1) ) + 100;
pm.environment.set("totalprice", price);
//_.random(min,max)
let price = _.random(100,200)
pm.environment.set("totalprice", price);

III. Lấy random 1 hoặc nhiều items trong 1 list

Trong bài trên có 2 biến như vậy:

"depositpaid": {{depositpaid}},
"additionalneeds": "{{additionalneeds}}"

Cách lấy random trong 1 list như sau

//_.sample(list)
let depositpaid = _.sample([true,false])
pm.environment.set("depositpaid", depositpaid);

const list = ["Breakfast", "view sea", "TV", "free transport"];
let additionalneeds = _.sample(list);
pm.environment.set("additionalneeds", additionalneeds);

IV. Làm việc với data là thời gian

"checkin": "{{checkin}}",
"checkout": "{{checkout}}"

Mình đã viết về date-time ở bài 17

const moment = require("moment");
const now = moment()

let checkin = now.add(_.random(5,10), 'h');
pm.environment.set("checkin", checkin.format("YYYY-MM-DD"));

let checkout = checkin.add(_.random(2,4), 'd');
pm.environment.set("checkout", checkout.format("YYYY-MM-DD"));

V. Tổng hợp

Muốn biết thêm nhiều kỹ thuật nữa, hãy đăng ký lớp postman script

0 0 votes
Article Rating
Subscribe
Notify of
guest
8 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
ngô văn ngọc
ngô văn ngọc
2 years ago

ok

Hồng Uyên
Hồng Uyên
2 years ago

Anh ơi, em đang test performance bằng tool Jmeter , em muốn viết test script để auto test thì có viết được không ạ, và cách kết nói như nào ạ. Anh có thể share cho em với được không ạ. Em cảm ơn anh ạ

thuuu
thuuu
2 years ago

Có cách nào để chuyển từ ảnh sang base64 không ạ

thuuu
thuuu
2 years ago
Reply to  Giang Nguyen

dạ vâng

Khánh
Khánh
2 years ago

Anh ơi cho em.hỏi random ngày tháng dạng unix time thì làm như nào ạ ?