Robot Framework — ep.7 มาทำความรู้จักกับ Arguments กันเถอะ
😸 สวัสดีครับ วันนี้จะมาพาทัวร์ทำความรู้จักกับ Arguments คืออะไร พร้อมกับยกตัวอย่างให้ดูกันครับ ก็ถ้าพร้อมแล้วก็มาเริ่มกันดีกว่าครับ
Robot Framework — ep.7 มาทำความรู้จักกับ Arguments กันเถอะ

😸 สวัสดีครับ วันนี้จะมาพาทัวร์ทำความรู้จักกับ Arguments คืออะไร พร้อมกับยกตัวอย่างให้ดูกันครับ ก็ถ้าพร้อมแล้วก็มาเริ่มกันดีกว่าครับ
Arguments คืออะไร🧐
[Arguments] คือ ตัวแปรที่ keyword ต้องรับจากข้างนอกตอนเรียกใช้งาน
เช่น ต้องส่งค่าตัวแปรไปในช่อง Test Case ไม่งั้น Keyword จะไม่รู้จะใช้ค่าอะไร
🔍 ง่ายๆเลย คือจะใช้ Keyword นี้ได้ ต้องส่งตัวแปรที่กำหนดก่อนในช่อง Test Case
[Arguments]จะอยู่บรรทัดถัดมาจากชื่อ keyword- ตัวแปรอื่น ๆ ที่ไม่อยู่ใน
[Arguments]ก็คือตัวแปรที่ประกาศไว้แล้วที่ไหนสักแห่งใน Variables section หรือไฟล์ .robot/.py แยกต่างหาก (ไม่ใช่ส่งเข้ามาตอนเรียก keyword)
มาดูหน้าตากัน 😤
เมื่อกำหนด [Arguments] ใน Keywords แล้ว ต้องกำหนดตัวแปรใน Test Cases ด้วยนะ เช่น
*** Keywords ***
Testtttt
[Arguments] ${Message} ${id_number}
*** Test Cases ***
Testtttt
Write Message ${Name} ${id}
ใน Test Cases จะมองง่ายๆแบบนี้ ค่าตัวที่ 1 ก็ map กับ Arguments ตัวที่1, ค่าตัวที่ 2 ก็ map กับ Arguments ตัวที่2 เช่น
${Name}จะ map กับ${Message}${id}จะ map กับ${id_number}
เอาล่ะมาดูตัวอย่างกันครับ
ถ้า [Arguments] = 1 ตัว ☝️
มาดูกันครับ ถ้ามี [Arguments] แค่ตัวเดียว
ตัวอย่าง 1
*** Keywords ***
Write Message
[Arguments] ${Employee} <------------------ ประกาศตัวแปร ${Employee}
Input Text name=q ${Employee}
#(ตัวอย่าง 1)(ถ้ายังไม่มี variable ก็เขียนข้อความไปได้เลย)
*** Test Cases ***
Testtttt
Write Message Testtt666 <-------- เขียนแบบนี้ก็ได้นะกรณียังไม่มี variable
จากตัวอย่าง 1
มันจะมอง Testtt666 คือค่าของ ${Employee} (มันจะ map กัน)
ตัวอย่าง 2
*** Keywords ***
Write Message
[Arguments] ${Employee} <------------------ ประกาศตัวแปร ${Employee}
Input Text name=q ${Employee}
#(ตัวอย่าง 2)(ถ้ามี variable แล้วก็ใช้ Variable ได้เลย)
*** Variable ***
${Name} Testtt666
*** Test Cases ***
Testtttt
Write Message ${Name} <----------- เขียนแบบนี้ก็ได้ถ้ามี variable แล้ว
จากตัวอย่าง 2 , เมื่อใส่ Variable
${Name} ซึ่งก็คือ Testtt666 จะคือค่าของ${Employee}
(${Name} จะ map กับ ${Employee})
ถ้า [Arguments] > 1 ตัว ✌️
ถ้ามี [Arguments] มากกว่า 1 ตัวล่ะ จะเขียนยังไง มาลองดูกันครับ
ตัวอย่าง 1
*** Keywords ***
Write Message
[Arguments] ${Employee} ${id_number} <-- ประกาศตัวแปร ${Employee} และ ${id_number}
Input Text name=q ${Employee}
Input Text id=qwert ${id_number}
#(ตัวอย่าง 1)(ถ้ายังไม่มี variable ก็เขียนข้อความไปได้เลย)
*** Test Cases ***
Testtttt
Write Message Testtt666 058 <-------- ถ้ายังไม่มี variable
จากตัวอย่าง 1
มันจะมอง Testtt666 คือค่าของ ${Employee} (มันจะ map กัน)
และ 058 คือค่าของ ${id_number} (มันจะ map กัน)
ตัวอย่าง 2
*** Keywords ***
Write Message
[Arguments] ${Employee} ${id_number} <-- ประกาศตัวแปร ${Employee} และ ${id_number}
Input Text name=q ${Employee}
Input Text id=qwert ${id_number}
#(ตัวอย่าง 2)(ถ้ามี variable แล้วก็ใช้ Variable ได้เลย)
*** Variable ***
${Name} Testtt666
${id} 058
*** Test Cases ***
Testtttt
Write Message ${Name} ${id} <---------- ถ้ามี variable แล้ว
จากตัวอย่าง 2 , เมื่อใส่ Variable
${Name}ซึ่งก็คือTesttt666จะคือค่าของ${Employee}(${Name}จะ map กับ${Employee})- และ
${id}ซึ่งก็คือ058จะคือค่าของ${id_number}(${id}จะ map กับ${id_number})
⚠️ ข้อควรระวัง ⚠️
⚠️ถ้าสลับ Variable ใน Test Cases จะเป็นยังไง? 🤔
ตัวอย่าง
*** Keywords ***
Write Message
[Arguments] ${Employee} ${id_number} <-- ประกาศตัวแปร ${Employee} และ ${id_number}
Input Text name=q ${Employee}
Input Text id=qwert ${id_number}
#(ตัวอย่าง 3)(ถ้าสลับ Variable ใน Test Cases)
*** Variable ***
${Name} Testtt666
${id} 058
*** Test Cases ***
Testtttt
Write Message ${id} ${Name} <------------ สลับที่กันแล้วนะตรงนี้
- ❌
${id}จะ map กับ${Employee}ทำให้ค่าออกมาเป็น058 - ❌
${Name}จะ map กับ${id_number}ทำให้ค่าออกมาเป็นTesttt666
ดังนั้น จะเห็นว่าผลลัพธ์มันสลับกันเลย มันจึงผิดครับ
⚠️ถ้าไม่ส่ง Arguments เข้าไปใน Keyword 😮
ถ้าไม่ส่ง Arguments เข้าไปใน Keyword จะขึ้น Error ฟ้องประมาณนี้
Keyword 'Write Message' expected 2 arguments, got 0.
- Keyword นั้น ประกาศ
[Arguments]ไว้ 2 ตัว - แต่ ไม่ได้ส่ง Arguments ใด ๆ ไปเลย (got 0)
Keyword 'Write Message' expected 1 arguments, got 2.
- Keyword นั้น ประกาศ
[Arguments]ไว้ 1 ตัว - แต่ ส่ง Arguments เข้าไป 2 ตัว (got 2)
🔑 สรุป:
ลำดับของ Arguments ที่ส่งเข้าไปใน Test Case จะต้อง ตรงกับลำดับที่กำหนดไว้ใน [Arguments] ของ Keyword
เพราะว่า Robot Framework ไม่มีการ map โดยชื่อ แต่ใช้ลำดับเป็นหลัก
*** Keywords ***
Write Message
[Arguments] ${Message} ${id_number}
*** Test Cases ***
Testtttt
Write Message ${Name} ${id}
ถ้าไม่ใช้ [Arguments] ได้มั้ย🤔
เผื่อมีคำถามว่า ไม่ใช้ Arguments ได้มั้ย ก็เขียนธรรมดาลงไปก็จบ จริงๆก็ได้นะ ไม่ต้องใช้ก็ได้ แค่ถ้าใช้ Arguments มันก็จะยืดหยุ่นกว่า ไม่ต้องไปเขียน keyword ใหม่ทุกครั้งที่ค่าต่างกัน
🔍ซึ่ง [Arguments]เหมาะกับ พวกที่ต้องกรอกข้อมูลต่างๆ เช่น
- กรอก Username/Password
- อะไรก็ตามที่อาจเปลี่ยนไปในแต่ละ Test case
- เหมาะกับ Keywords: input text
👉 ควรแก้ที่ Test Case โดยส่ง Arguments เข้า Keyword ไม่ควรไปแก้ค่าภายใน Keyword ทุกครั้ง เพราะจะทำให้ Keyword ใช้ซ้ำไม่ได้ และเสี่ยงต่อการผิดพลาด
- Keyword ควร “เขียนครั้งเดียว” แล้วรับค่าผ่าน
[Arguments] - Test Case เป็นที่ที่ควร “เปลี่ยนค่า” ตามแต่ละสถานการณ์
⚠️ เสริม ❌ ถ้าไปแก้ที่ Keyword ทุกครั้ง มันไม่ดี
- ใช้ซ้ำไม่ได้ (Hard-code ค่าไว้แล้ว)
- ยากต่อการดูแล และเกิดการซ้ำซ้อน
- มีความเสี่ยงหากคนอื่นมาใช้งานแล้วไม่รู้ว่าแก้ตรงไหน
คร่าวๆก็จะประมาณนี้ครับ อาจจะมีอะไรที่ผมตกหล่นไปหรือมีผิดพลาดบ้าง ก็ขออภัยมา ณ ที่นี้ด้วยครับ
ทิ้งท้าย 😇 บางคนอาจจะงงว่าใส่ภาพมีมตอนแรกมาทำไม หรือเกี่ยวอะไรด้วย ที่จะสื่อก็คือ…. ผมก็ไม่รู้เหมือนกันว่าจะใส่มีมอะไรดี ผมก็เลยเอาอันนี้ละกัน55
🫠 ทั้งหมดก็จะมีประมาณนี้ ถ้าชอบก็อย่าลืมกดตบมือและแชร์เพื่อเป็นกำลังใจให้ผมด้วยนะครับ และอย่าลืมติดตามบทความถัดๆไปของผมด้วย ขอบคุณทุกๆท่านที่อ่านจนจบนะครับ ขอบคุณมากครับ 🥳
메타데이터
- post_id
- c00fe5ad91c0
- slug
- robot-framework-ep-7-มาทำความรู้จักกับ-arguments-กันเถอะ-c00fe5ad91c0
- url
- https://medium.com/@punthanet/robot-framework-ep-7-%E0%B8%A1%E0%B8%B2%E0%B8%97%E0%B8%B3%E0%B8%84%E0%B8%A7%E0%B8%B2%E0%B8%A1%E0%B8%A3%E0%B8%B9%E0%B9%89%E0%B8%88%E0%B8%B1%E0%B8%81%E0%B8%81%E0%B8%B1%E0%B8%9A-arguments-%E0%B8%81%E0%B8%B1%E0%B8%99%E0%B9%80%E0%B8%96%E0%B8%AD%E0%B8%B0-c00fe5ad91c0
- canonical_url
- https://medium.com/@punthanet/robot-framework-ep-7-%E0%B8%A1%E0%B8%B2%E0%B8%97%E0%B8%B3%E0%B8%84%E0%B8%A7%E0%B8%B2%E0%B8%A1%E0%B8%A3%E0%B8%B9%E0%B9%89%E0%B8%88%E0%B8%B1%E0%B8%81%E0%B8%81%E0%B8%B1%E0%B8%9A-arguments-%E0%B8%81%E0%B8%B1%E0%B8%99%E0%B9%80%E0%B8%96%E0%B8%AD%E0%B8%B0-c00fe5ad91c0
- author_url
- https://medium.com/@punthanet
- status
- ok
- fetched_at
- 2026-06-15 20:49:13