Bỏ qua

Module 08: Chạy Test — Local, CI/CD, Regression

🎯 Mục Tiêu Module

  • Biết cách chạy test trên local
  • Hiểu CI/CD và cách setup
  • Thực hành chạy regression test
  • Hiểu lợi ích của regression test

8.1. Chạy Test trên Local

Cách 1: Chạy tất cả test

npx playwright test

Cách 2: Chạy 1 file test

npx playwright test tests/login.spec.ts

Cách 3: Chạy test cụ thể

npx playwright test -g "test login success"

Cách 4: Chạy với giao diện

npx playwright test --ui

Cách 5: Chạy với debug

npx playwright test --debug

Cách 6: Chạy trên browser cụ thể

npx playwright test --project=chromium
npx playwright test --project=firefox
npx playwright test --project=webkit

8.2. Cấu Hình Playwright

playwright.config.ts

import { defineConfig, devices } from '@playwright/test';

export default defineConfig({
  // Thư mục test
  testDir: './tests',

  // Chạy song song
  fullyParallel: true,

  // Retry khi fail
  retries: process.env.CI ? 2 : 0,

  // Số worker
  workers: process.env.CI ? 1 : undefined,

  // Reporter
  reporter: 'html',

  // Cấu hình cho từng browser
  projects: [
    {
      name: 'chromium',
      use: { ...devices['Desktop Chrome'] },
    },
    {
      name: 'firefox',
      use: { ...devices['Desktop Firefox'] },
    },
    {
      name: 'webkit',
      use: { ...devices['Desktop Safari'] },
    },
  ],

  // Cấu hình chung
  use: {
    // URL gốc
    baseURL: 'https://practice.automationtesting.in',

    // Screenshot khi fail
    screenshot: 'only-on-failure',

    // Video khi fail
    video: 'retain-on-failure',

    // Trace khi fail
    trace: 'on-first-retry',
  },
});

8.3. CI/CD là gì?

Tổng quan

┌─────────────────────────────────────────────────────┐
│                                                     │
│  CI/CD = Continuous Integration / Continuous Delivery│
│                                                     │
│  CI: Tự động chạy test khi có code mới              │
│  CD: Tự động deploy khi test pass                   │
│                                                     │
│  Lợi ích:                                           │
│  ✅ Phát hiện lỗi sớm                               │
│  ✅ Không cần chạy test thủ công                    │
│  ✅ Đảm bảo code luôn hoạt động                    │
└─────────────────────────────────────────────────────┘

Ví dụ: GitHub Actions

# .github/workflows/playwright.yml
name: Playwright Tests

on:
  push:
    branches: [main, master]
  pull_request:
    branches: [main, master]

jobs:
  test:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v3

      - uses: actions/setup-node@v3
        with:
          node-version: 18

      - name: Install dependencies
        run: npm ci

      - name: Install Playwright Browsers
        run: npx playwright install --with-deps

      - name: Run Playwright tests
        run: npx playwright test

      - name: Upload test results
        uses: actions/upload-artifact@v3
        if: always()
        with:
          name: playwright-report
          path: playwright-report/
          retention-days: 30

8.4. Regression Testing

Tại sao cần Regression Test?

┌─────────────────────────────────────────────────────┐
│                                                     │
│  Regression Test = Chạy lại test đã có              │
│                                                     │
│  Khi nào cần chạy?                                  │
│  - Sau khi fix bug                                  │
│  - Sau khi thêm feature mới                         │
│  - Trước khi deploy                                 │
│  - Định kỳ (hàng ngày, hàng tuần)                  │
│                                                     │
│  Lợi ích:                                           │
│  ✅ Đảm bảo code cũ không bị gãy                   │
│  ✅ Phát hiện regression sớm                        │
│  ✅ Tự động, không tốn thời gian                    │
└─────────────────────────────────────────────────────┘

Regression Test với 0 Token Cost

┌─────────────────────────────────────────────────────┐
│                                                     │
│  Giai đoạn tạo code (AI):                           │
│  - Dùng Antigravity IDE                             │
│  - Tốn token                                        │
│  - Chỉ làm 1 lần                                    │
│                                                     │
│  Giai đoạn regression (không AI):                   │
│  - Chạy code Playwright đã có                       │
│  - 0 token cost                                     │
│  - Chạy được nhiều lần                              │
│                                                     │
└─────────────────────────────────────────────────────┘

8.5. Chạy Test Định Kỳ

Cron Job với GitHub Actions

# .github/workflows/playwright-scheduled.yml
name: Playwright Scheduled Tests

on:
  schedule:
    # Chạy hàng ngày lúc 8h sáng
    - cron: '0 8 * * *'

jobs:
  test:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v3
      - uses: actions/setup-node@v3
        with:
          node-version: 18
      - run: npm ci
      - run: npx playwright install --with-deps
      - run: npx playwright test
      - uses: actions/upload-artifact@v3
        if: always()
        with:
          name: playwright-report
          path: playwright-report/

8.6. Xem Kết Quả Test

HTML Report

# Mở report
npx playwright show-report

Xem trong terminal

# Chạy với reporter chi tiết
npx playwright test --reporter=list

📝 Bài Tập

Bài Tập 1: Chạy Test trên Local

  1. Chạy tất cả test đã viết
  2. Chạy 1 file test cụ thể
  3. Chạy với debug mode

Bài Tập 2: Setup CI/CD

  1. Tạo file .github/workflows/playwright.yml
  2. Push code lên GitHub
  3. Kiểm tra CI chạy

Bài Tập 3: Chạy Regression

  1. Chạy lại test đã viết
  2. Kiểm tra kết quả
  3. Phân tích test fail (nếu có)

✅ Checklist Hoàn Thành Module

  • [ ] Biết cách chạy test trên local
  • [ ] Hiểu CI/CD
  • [ ] Setup CI/CD với GitHub Actions
  • [ ] Thực hành regression test
  • [ ] Hoàn thành bài tập