ตัวอย่างการทำ Robot Framework เพื่อทำ automation testing ระบบ POS/E-commerce โดยใช้ Next.js และ Robot Framework (Selenium) ตาม best practices
- Overview
- Project Structure
- Prerequisites
- Installation
- Running the Application
- Running Tests
- Test Structure
- Best Practices
- Test Data Management
- Reporting
โปรเจคนี้แสดงตัวอย่างการใช้ Robot Framework ในการทำ automation testing สำหรับระบบ E-commerce/POS ที่พัฒนาด้วย Next.js ประกอบด้วย:
- Next.js E-commerce Application: ระบบ E-commerce แบบ full-featured พร้อม shopping cart และ checkout
- Robot Framework Test Suites: Test cases ครบถ้วนตาม best practices
- Page Object Pattern: การจัดการ page objects แบบ modular และ reusable
- Test Data Management: การจัดการ test data ด้วย CSV files และ variables
- Custom Libraries: Python libraries สำหรับ utility functions
robot-framework-automation-testing/
├── ecommerce-app/ # Next.js Application
│ ├── pages/ # Next.js pages
│ │ ├── index.js # Home/Products page
│ │ ├── cart.js # Shopping cart page
│ │ ├── checkout.js # Checkout page
│ │ └── login.js # Login page
│ ├── components/ # React components
│ │ ├── Layout.js # Main layout component
│ │ └── ProductCard.js # Product card component
│ ├── lib/ # Utility functions
│ │ └── products.js # Product data
│ ├── styles/ # CSS styles
│ │ └── globals.css # Global styles
│ └── package.json # Dependencies
│
├── tests/ # Robot Framework Tests
│ ├── suites/ # Test suites
│ │ ├── test_home_page.robot # Home page tests
│ │ ├── test_shopping_cart.robot # Cart functionality tests
│ │ ├── test_checkout.robot # Checkout process tests
│ │ ├── test_login.robot # Authentication tests
│ │ └── test_e2e.robot # End-to-end tests
│ │
│ ├── resources/ # Reusable resources
│ │ ├── keywords/ # Custom keywords
│ │ │ ├── common_keywords.robot # Common utilities
│ │ │ └── business_keywords.robot # Business logic
│ │ ├── page_objects/ # Page object files
│ │ │ ├── home_page.robot # Home page object
│ │ │ ├── cart_page.robot # Cart page object
│ │ │ ├── checkout_page.robot # Checkout page object
│ │ │ └── login_page.robot # Login page object
│ │ └── variables/ # Variable files
│ │ └── common_variables.robot # Common variables
│ │
│ ├── data/ # Test data files
│ │ ├── checkout_data.csv # Checkout test data
│ │ ├── login_data.csv # Login test data
│ │ └── products_data.csv # Product test data
│ │
│ ├── libraries/ # Custom Python libraries
│ │ └── TestUtils.py # Test utility functions
│ │
│ └── reports/ # Test execution reports
│
├── requirements.txt # Python dependencies
├── run_tests.sh # Test execution script
├── Makefile # Make commands
└── README.md # This file
- Node.js (v16 or higher)
- Python (v3.8 or higher)
- pip (Python package manager)
- Google Chrome or Chromium browser
- ChromeDriver (จะติดตั้งอัตโนมัติผ่าน webdriver-manager)
git clone https://github.com/somkheartk/robot-framework-automation-testing.git
cd robot-framework-automation-testingpip install -r requirements.txtcd ecommerce-app
npm installcd ecommerce-app
npm run devApplication จะรันที่: http://localhost:3000
หน้าเว็บที่มี:
- Home (
/): หน้าแสดงสินค้า - Cart (
/cart): หน้าตะกร้าสินค้า - Checkout (
/checkout): หน้าชำระเงิน - Login (
/login): หน้าเข้าสู่ระบบ (ใช้: test@example.com / password123)
# Run all tests
./run_tests.sh
# Run specific suite
./run_tests.sh tests/suites/test_home_page.robot
# Run in headless mode
./run_tests.sh tests/suites/ True
# Run with tags
./run_tests.sh tests/suites/ False smoke# Run all tests
make run_all_tests
# Run specific test suites
make run_home_tests
make run_cart_tests
make run_checkout_tests
make run_login_tests
make run_e2e_tests
# Run by tags
make run_smoke_tests # Run smoke tests
make run_critical_tests # Run critical tests
make run_e2e_only # Run E2E tests only
# Run in headless mode
make run_headless
# Clean reports
make clean# Run all tests
robot --outputdir tests/reports tests/suites/
# Run specific suite
robot --outputdir tests/reports tests/suites/test_home_page.robot
# Run with tags
robot --outputdir tests/reports --include smoke tests/suites/
# Run in headless mode
robot --outputdir tests/reports --variable HEADLESS:True tests/suites/
# Run with custom variables
robot --outputdir tests/reports \
--variable BROWSER:chrome \
--variable SELENIUM_SPEED:0.5 \
tests/suites/-
test_home_page.robot: ทดสอบหน้าแรกและการแสดงสินค้า
- การโหลดหน้าเว็บ
- การแสดงสินค้า
- การเพิ่มสินค้าลงตะกร้า
- Navigation menu
-
test_shopping_cart.robot: ทดสอบฟังก์ชันตะกร้าสินค้า
- เพิ่ม/ลบสินค้า
- อัพเดทจำนวนสินค้า
- คำนวณราคารวม
- ล้างตะกร้า
-
test_checkout.robot: ทดสอบกระบวนการชำระเงิน
- กรอกข้อมูลลูกค้า
- กรอกข้อมูลบัตรเครดิต
- สั่งซื้อสินค้า
- ตรวจสอบ Order ID
-
test_login.robot: ทดสอบการ login
- Login ด้วยข้อมูลถูกต้อง
- Login ด้วยข้อมูลผิด
- Form validation
-
test_e2e.robot: End-to-End tests
- กระบวนการซื้อสินค้าแบบสมบูรณ์
- Multiple scenarios
smoke: Smoke tests (basic functionality)e2e: End-to-end testscritical: Critical test casescart: Cart functionalitycheckout: Checkout processlogin: Authenticationnavigation: Navigation tests
แยก page objects ออกเป็นไฟล์แยกเพื่อความง่ายในการ maintain:
# resources/page_objects/home_page.robot
*** Keywords ***
Navigate To Home Page
Go To ${HOME_URL}
Wait Until Page Contains Element ${HOME_TITLE}
Add Product To Cart By ID
[Arguments] ${product_id}
${add_btn}= Set Variable css=.add-to-cart-btn[data-product-id="${product_id}"]
Click Element ${add_btn}สร้าง keywords ที่ใช้ซ้ำได้:
# resources/keywords/common_keywords.robot
*** Keywords ***
Setup Browser
Open Browser ${BASE_URL} ${BROWSER}
Set Selenium Speed ${SELENIUM_SPEED}
Set Selenium Timeout ${SELENIUM_TIMEOUT}จัดการ variables ไว้ที่เดียว:
# resources/variables/common_variables.robot
*** Variables ***
${BASE_URL} http://localhost:3000
${BROWSER} chrome
${VALID_EMAIL} test@example.comใช้ CSV files สำหรับ data-driven testing:
# data/checkout_data.csv
name,email,address,city,zipCode
John Doe,john@example.com,123 Main St,Bangkok,10110
สร้าง Python libraries สำหรับ complex logic:
# libraries/TestUtils.py
class TestUtils:
def calculate_expected_total(self, *price_quantity_pairs):
# Complex calculation logic
passเขียน documentation ที่ชัดเจน:
*** Test Cases ***
TC001: Verify Home Page Loads Successfully
[Documentation] Verifies that the home page loads and displays correctly
[Tags] smoke homepage
Navigate To Home Page
Verify Home Page Is DisplayedTest data จัดเก็บใน CSV format เพื่อง่ายต่อการ maintain:
tests/data/
├── checkout_data.csv # Customer and payment information
├── login_data.csv # Login credentials
└── products_data.csv # Product information
Variables แบ่งตาม scope:
*** Variables ***
# Application URLs
${BASE_URL} http://localhost:3000
${HOME_URL} ${BASE_URL}/
# Test Credentials
${VALID_EMAIL} test@example.com
${VALID_PASSWORD} password123
# Product IDs
${PRODUCT_LAPTOP_ID} 1
${PRODUCT_MOUSE_ID} 2Test reports จะถูกสร้างอัตโนมัติหลังจากรันเทส:
tests/reports/
├── log.html # Detailed execution log
├── output.xml # Machine-readable output
└── report.html # Summary report
เปิด tests/reports/report.html ใน browser เพื่อดู test results
- Test execution statistics
- Pass/Fail status
- Execution time
- Screenshots on failure
- Detailed logs
- Tag statistics
*** Test Cases ***
TC001: Complete Purchase Journey
[Documentation] Tests complete user journey from browsing to purchase
[Tags] e2e smoke
# Browse products
Navigate To Home Page
Verify Home Page Is Displayed
# Add to cart
Add Product To Cart By ID ${PRODUCT_LAPTOP_ID}
# View cart
Navigate To Cart Page
Verify Cart Contains Items
# Checkout
Click Checkout Button
Complete Checkout Form
Place Order
# Verify success
Verify Order Success
${order_id}= Get Order ID
Should Not Be Empty ${order_id}Feel free to submit issues and enhancement requests!
This project is for educational purposes.
somkheartk
หมายเหตุ: โปรเจคนี้เป็นตัวอย่างเพื่อการเรียนรู้ ไม่ควรนำไปใช้ใน production โดยตรง