Site logo

Các Command Phổ Biến Trong Playwright - Hướng Dẫn Chi Tiết

5:00 read

Giới Thiệu Về Playwright Commands

Playwright là một framework mạnh mẽ cho automation testing, cung cấp nhiều command hữu ích để thực hiện các tác vụ testing khác nhau. Trong bài viết này, chúng ta sẽ tìm hiểu các command phổ biến và quan trọng nhất mà bạn cần biết khi làm việc với Playwright.

Các Command Cơ Bản Và Thường Dùng Nhất

1. npx playwright test - Chạy Tests

Command này là quan trọng nhất và được sử dụng nhiều nhất trong quá trình phát triển.

# Chạy tất cả tests
npx playwright test

# Chạy test với browser cụ thể
npx playwright test --browser=chromium
npx playwright test --browser=firefox
npx playwright test --browser=webkit

# Chạy test với headed mode (có giao diện)
npx playwright test --headed

# Chạy test với debug mode
npx playwright test --debug

# Chạy test file cụ thể
npx playwright test my.spec.ts

# Chạy test với grep pattern
npx playwright test --grep "login"

Các options phổ biến:

  • --headed: Chạy test với giao diện browser
  • --debug: Chạy test với Playwright Inspector
  • --browser: Chỉ định browser (chromium, firefox, webkit)
  • --grep: Lọc test theo pattern
  • --workers: Số lượng worker chạy song song
  • --retries: Số lần retry khi test fail

2. npx playwright codegen - Sinh Code Tự Động

Command này giúp sinh code test tự động bằng cách ghi lại các hành động của bạn trên browser.

# Mở codegen với website
npx playwright codegen https://example.com

# Sinh code cho Python
npx playwright codegen --target=python https://example.com

# Sinh code cho Java
npx playwright codegen --target=java https://example.com

# Lưu code vào file
npx playwright codegen --output=test.spec.ts https://example.com

# Sử dụng device emulation
npx playwright codegen --device="iPhone 11" https://example.com

Các target languages hỗ trợ:

  • javascript
  • playwright-test (mặc định)
  • python
  • python-pytest
  • java
  • csharp

3. npx playwright install - Cài Đặt Browsers

# Cài đặt tất cả browsers
npx playwright install

# Cài đặt browser cụ thể
npx playwright install chromium
npx playwright install firefox
npx playwright install webkit

# Cài đặt với dependencies
npx playwright install --with-deps

# Kiểm tra list browsers
npx playwright install --list

4. npx playwright show-report - Xem Test Report

# Mở report mới nhất
npx playwright show-report

# Mở report cụ thể
npx playwright show-report playwright-report

# Chỉ định host và port
npx playwright show-report --host=0.0.0.0 --port=8080

Yêu cầu đăng nhập

Vui lòng đăng nhập để truy cập nội dung này

Additional Resources

Course Guide

Comprehensive PDF guide with examples

GitHub Repository

Example code for all lessons

Discussion

Have a question about this lesson? Post it here and get answers from instructors and peers.