เคล็ด(ไม่)ลับการใช้ Postman

เคล็ด(ไม่)ลับการใช้ Postman

29 March 2021
ขอบคุณโลโก้จาก (Postman)

Postman คือเครื่องมือสำหรับการพัฒนาและทดสอบ API service ซึ่งเป็นที่นิยมอย่างกว้างขว้าง เพราะการใช้งานที่แสนจะง่ายดาย มี UI สวยงาม และสามารถใช้ได้ฟรีอีกด้วย!!!

ฟังก์ชันทั่วไปที่คนใช้งานจะเป็นการทดสอบส่ง request เพื่อดู response ที่ได้กลับมา แต่บทความนี้จะพูดถึงฟังก์ชันอื่น ๆ ที่คนทั่วไปอาจยังไม่เคยใช้ หรือไม่เคยรู้มาก่อน เราจึงจะมาแนะนำฟังก์ชันอื่น ๆ ที่มีประโยชน์ต่อผู้ที่ใช้งาน Postman และอยากให้รู้จักกัน (ในบทความนี้จะเป็น Postman เวอร์ชั่น 8.0.7 )

1. สร้าง Collection เพื่อแบ่งกลุ่มของ API

หาก API เรามีเยอะ การจัดแยก API เป็น Collection จะทำเพื่อให้ง่ายต่อการค้นหาและทดสอบ โดยสามารถแบ่งเป็นโฟลเดอร์ย่อย ๆ ในแต่ละ collection ได้อีกด้วย

2. สร้าง Environment เก็บค่าตามแต่ละระบบที่เราทดสอบ

เมื่อเราต้องการทดสอบ API ใน environment ที่แตกต่างกัน เช่น มีการเปลี่ยน IP หรือ domain name เราสามารถทำเป็นตัวแปรให้อยู่ใน environment ต่าง ๆ เพื่อกำหนด IP หรือ domain name ที่แตกต่างกันได้ ตอนใช้งานก็เพียงกดเลือก environment ที่ต้องการ ก็จะได้ค่าที่เราตั้งเอาไว้ ทำให้ไม่ต้องมาแก้ไขเองทุกครั้งที่มีการเปลี่ยน environment

ขั้นตอนการสร้าง environment และตัวแปร

  1. กดเลือก Environments ที่ฝั่งซ้ายของโปรแกรม จากนั้นกดที่สัญลักษณ์รูปบวก หรือกดที่ข้อความ ‘Create a new Environment’ เพื่อสร้าง environment ใหม่
  1. ตั้งชื่อ  environment ของเรา และตั้งค่าตัวแปร โดยในตัวอย่างจะใช้ environment นี้สำหรับการพัฒนา จึงตั้งชื่อว่า Dev Environment และตั้งค่าตัวแปรชื่อ URL ไว้เก็บลิงก์ที่ใช้ในการทดสอบ API โดยในตัวอย่างจะใช้เป็น localhost:8000
  1. จากนั้นคุณสามารถสร้าง environment เพิ่มได้ตามที่คุณต้องการ เพื่อให้รองรับแต่ละ environment ของคุณ อย่างในตัวอย่างจะทำเป็น 3 environment สำหรับ Dev, UAT, และ Prod ซึ่งแต่ละ environment ก็จะมีลิงก์ URL ที่ต่างกัน
  1. การใช้งาน environment  สามารถกดเปลี่ยนได้ตามต้องเลยที่ด้านบนขวาของโปรแกรม ส่วนการเรียกใช้ค่าตัวแปร จะสามารถอ้างถึงได้โดยใส่ {{variable_name}} เช่น ในตัวอย่างของเรา ชื่อตัวแปรเราคือ url วิธีเรียกใช้จึงเป็น {{url}} ตามรูปตัวอย่างด้านล่าง
การเลือกใช้งาน environment
วิธีการอ้างถึงตัวตัวแปร

3. เขียน Script test แต่ละ API เพื่อตรวจสอบความถูกต้อง

เราสามารถเขียน script เพื่อทดสอบ response ที่ได้รับกลับมาว่า status code หรือเนื้อหาตรงตามที่เราต้องการไหม และยังสามารถตั้งค่าที่ส่งกลับมาเป็นตัวแปร environment ได้อีกด้วย เช่น ในรูปตัวอย่างด้านล่าง จะมีการทำงานอยู่ 2 ส่วน คือ

  1. ตรวจสอบ status code ที่ได้จากการยิง API นี้ ว่าเป็น 200 หรือไม่
  2. ตรวจสอบค่าที่ส่งกลับมาว่ามี access_token หรือไม่ และทำการตั้งค่า access_token ที่ได้กลับมาเป็นค่าในตัวแปรของ admin token ใน environment ที่เลือก

โดยภาษาที่ใช้เขียนจะเป็น JavaScript ถ้าเรายังไม่รู้ว่าต้องเขียนยังไง สามารถกดดูโค้ดตัวอย่างได้ที่ฝั่งขวา กดแล้วจะมีโค้ดตัวอย่างปรากฏมา เราสามารถนำมาแก้ไขตามที่เราต้องการทดสอบได้เลย

หากเราเขียน script test ครบทุก API เราจะสามารถ run ทดสอบ โดยสามารถเลือกเป็นทั้ง collection หรือเลือกเป็น folder ย่อยได้เช่นกัน โดยกดเลือก folder หรือ collection ที่ต้องการ จากนั้นกดปุ่มที่เขียนว่า Run

เมื่อ run ครบทุก api จะมีรายละเอียดแสดงว่า request ที่เราส่งไปว่ามีผ่าน หรือไม่ผ่าน เท่าไร ตามการเช็คของ script โดยอันที่ไม่ผ่านจะมีข้อความบอกว่าทำไมไม่ผ่าน

4. Export to file เพื่อให้คนอื่นสามารถนำไปใช้ต่อได้

Collections และ Environments นั้น สามารถที่จะ export ออกมาเป็นไฟล์ เพื่อนำไปแชร์ให้คนอื่นต่อ หรือนำเข้า version control หรือจะ sync กับบัญชีแล้วใช้งานร่วมกับทีมได้เลย แต่จะมีจำกัดจำนวน API ที่แชร์ได้ หากต้องการมากกว่าที่กำหนดจะต้องเสียเงินเพิ่ม วิธีการสามารถทำได้โดยกดที่สัญลักษณ์จุด 3 จุดที่ collection หรือ environment ที่ต้องการ และเลือก export ได้เลย

การ export collection ออกมาเป็นไฟล์
การ export environment ออกมาเป็นไฟล์

ท้ายที่สุดนี้ผู้เขียนหวังว่าเคล็ด(ไม่)ลับนี้จะเป็นประโยชน์แก่ผู้ใช้งาน Postman ทุกคน สำหรับคนที่ไม่เคยรู้ว่ามีฟังก์ชันต่าง ๆ เหล่านี้ ก็อยากให้ลองนำไปใช้เพื่อเพิ่มประสิทธิภาพในการทดสอบ API

Data Engineer at Big Data Institute (Public Organization), BDI

แบ่งปันบทความ

กลุ่มเนื้อหา

แท็กยอดนิยม

แจ้งเรื่องที่อยากอ่าน

คุณสามารถแจ้งเรื่องที่อยากอ่านให้เราทราบได้ !
และเราจะนำไปพัฒนาบทความให้มีเนื้อหาที่น่าสนใจมากขึ้น

PDPA Icon

We use cookies to optimize your browsing experience and improve our website’s performance. Learn more at our Privacy Policy and adjust your cookie settings at Settings

Privacy Preferences

You can choose your cookie settings by turning on/off each type of cookie as needed, except for necessary cookies.

Accept all
Manage Consent Preferences
  • Strictly Necessary Cookies
    Always Active

    This type of cookie is essential for providing services on the website of the Personal Data Protection Committee Office, allowing you to access various parts of the site. It also helps remember information you have previously provided through the website. Disabling this type of cookie will result in your inability to use key services of the Personal Data Protection Committee Office that require cookies to function.
    Cookies Details

  • Performance Cookies

    This type of cookie helps the Big Data Institute (Public Organization) understand user interactions with its website services, including which pages or areas of the site are most popular, as well as analyze other related data. The Big Data Institute (Public Organization) also uses this information to improve website performance and gain a better understanding of user behavior. Although the data collected by these cookies is non-identifiable and used solely for statistical analysis, disabling them will prevent the Big Data Institute (Public Organization) from knowing the number of website visitors and from evaluating the quality of its services.

  • Functional Cookies

    This type of cookie enables the Big Data Institute (Public Organization)’s website to remember the choices you have made and deliver enhanced features and content tailored to your usage. For example, it can remember your username or changes you have made to font sizes or other customizable settings on the page. Disabling these cookies may result in the website not functioning properly.

  • Targeting Cookies

    "This type of cookie helps the Big Data Institute (Public Organization) understand user interactions with its website services, including which pages or areas of the site are most popular, as well as analyze other related data. The Big Data Institute (Public Organization) also uses this information to improve website performance and gain a better understanding of user behavior. Although the data collected by these cookies is non-identifiable and used solely for statistical analysis, disabling them will prevent the Big Data Institute (Public Organization) from knowing the number of website visitors and from evaluating the quality of its services.

Save settings
This site is registered on wpml.org as a development site. Switch to a production site key to remove this banner.