Skip to content

somkheartk/robot-framework-automation-testing

Repository files navigation

Robot Framework Automation Testing - E-commerce POS System

ตัวอย่างการทำ Robot Framework เพื่อทำ automation testing ระบบ POS/E-commerce โดยใช้ Next.js และ Robot Framework (Selenium) ตาม best practices

📋 Table of Contents

🎯 Overview

โปรเจคนี้แสดงตัวอย่างการใช้ 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

📁 Project Structure

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

🔧 Prerequisites

  • Node.js (v16 or higher)
  • Python (v3.8 or higher)
  • pip (Python package manager)
  • Google Chrome or Chromium browser
  • ChromeDriver (จะติดตั้งอัตโนมัติผ่าน webdriver-manager)

📦 Installation

1. Clone the repository

git clone https://github.com/somkheartk/robot-framework-automation-testing.git
cd robot-framework-automation-testing

2. Install Python dependencies

pip install -r requirements.txt

3. Install Next.js dependencies

cd ecommerce-app
npm install

🚀 Running the Application

Start the Next.js development server

cd ecommerce-app
npm run dev

Application จะรันที่: http://localhost:3000

หน้าเว็บที่มี:

  • Home (/): หน้าแสดงสินค้า
  • Cart (/cart): หน้าตะกร้าสินค้า
  • Checkout (/checkout): หน้าชำระเงิน
  • Login (/login): หน้าเข้าสู่ระบบ (ใช้: test@example.com / password123)

🧪 Running Tests

ต้องแน่ใจว่า Next.js app กำลังรันอยู่ก่อนรันเทส!

Option 1: ใช้ shell script

# 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

Option 2: ใช้ Makefile

# 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

Option 3: ใช้ Robot Framework command โดยตรง

# 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 Structure

Test Suites

  1. test_home_page.robot: ทดสอบหน้าแรกและการแสดงสินค้า

    • การโหลดหน้าเว็บ
    • การแสดงสินค้า
    • การเพิ่มสินค้าลงตะกร้า
    • Navigation menu
  2. test_shopping_cart.robot: ทดสอบฟังก์ชันตะกร้าสินค้า

    • เพิ่ม/ลบสินค้า
    • อัพเดทจำนวนสินค้า
    • คำนวณราคารวม
    • ล้างตะกร้า
  3. test_checkout.robot: ทดสอบกระบวนการชำระเงิน

    • กรอกข้อมูลลูกค้า
    • กรอกข้อมูลบัตรเครดิต
    • สั่งซื้อสินค้า
    • ตรวจสอบ Order ID
  4. test_login.robot: ทดสอบการ login

    • Login ด้วยข้อมูลถูกต้อง
    • Login ด้วยข้อมูลผิด
    • Form validation
  5. test_e2e.robot: End-to-End tests

    • กระบวนการซื้อสินค้าแบบสมบูรณ์
    • Multiple scenarios

Tags

  • smoke: Smoke tests (basic functionality)
  • e2e: End-to-end tests
  • critical: Critical test cases
  • cart: Cart functionality
  • checkout: Checkout process
  • login: Authentication
  • navigation: Navigation tests

✨ Best Practices ที่ใช้

1. Page Object Pattern

แยก 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}

2. Reusable Keywords

สร้าง keywords ที่ใช้ซ้ำได้:

# resources/keywords/common_keywords.robot
*** Keywords ***
Setup Browser
    Open Browser    ${BASE_URL}    ${BROWSER}
    Set Selenium Speed    ${SELENIUM_SPEED}
    Set Selenium Timeout    ${SELENIUM_TIMEOUT}

3. Centralized Variables

จัดการ variables ไว้ที่เดียว:

# resources/variables/common_variables.robot
*** Variables ***
${BASE_URL}              http://localhost:3000
${BROWSER}               chrome
${VALID_EMAIL}           test@example.com

4. Test Data Management

ใช้ CSV files สำหรับ data-driven testing:

# data/checkout_data.csv
name,email,address,city,zipCode
John Doe,john@example.com,123 Main St,Bangkok,10110

5. Custom Libraries

สร้าง Python libraries สำหรับ complex logic:

# libraries/TestUtils.py
class TestUtils:
    def calculate_expected_total(self, *price_quantity_pairs):
        # Complex calculation logic
        pass

6. Clear Test Documentation

เขียน 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 Displayed

📊 Test Data Management

CSV Data Files

Test data จัดเก็บใน CSV format เพื่อง่ายต่อการ maintain:

tests/data/
├── checkout_data.csv    # Customer and payment information
├── login_data.csv       # Login credentials
└── products_data.csv    # Product information

Variable Files

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}      2

📈 Reporting

Test reports จะถูกสร้างอัตโนมัติหลังจากรันเทส:

tests/reports/
├── log.html      # Detailed execution log
├── output.xml    # Machine-readable output
└── report.html   # Summary report

เปิด tests/reports/report.html ใน browser เพื่อดู test results

Report Features

  • Test execution statistics
  • Pass/Fail status
  • Execution time
  • Screenshots on failure
  • Detailed logs
  • Tag statistics

🔍 Example Test Case

*** 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}

🎓 Learning Resources

🤝 Contributing

Feel free to submit issues and enhancement requests!

📝 License

This project is for educational purposes.

👤 Author

somkheartk


หมายเหตุ: โปรเจคนี้เป็นตัวอย่างเพื่อการเรียนรู้ ไม่ควรนำไปใช้ใน production โดยตรง

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Contributors 2

  •  
  •