Chuyển đến nội dung
Skillpedia
Kiểm thử14k · #11275

login-flows

Skill này làm gì

Skill này đưa ra các mẫu tự động hóa đăng nhập trang web trong test đầu cuối bằng Playwright. Skill dành cho lập trình viên kiểm thử ứng dụng web và muốn tránh phải đăng nhập trước mỗi test.

Khi nào nên dùng

  • ✓Khi bạn viết test tự động cho một trang cần đăng nhập.
  • ✓Khi test của bạn dùng Google hoặc nhà cung cấp đăng nhập một lần khác.
  • ✓Khi bạn muốn lưu trạng thái đăng nhập để test chạy nhanh hơn.

Liên kết skill

https://github.com/andrewyng/context-hub/blob/main/content/playwright-community/skills/login-flows/SKILL.md

Sao chép rồi dán vào AI của bạn (Claude, ChatGPT hoặc Cursor) và nhờ nó làm theo hướng dẫn.

Cách dùng

Cách dễ nhất: sao chép tin nhắn và dán vào AI của bạn. AI sẽ đọc skill và làm theo hướng dẫn.

Tin nhắn có sẵn để dán

Hãy dùng skill này để giúp tôi: https://github.com/andrewyng/context-hub/blob/main/content/playwright-community/skills/login-flows/SKILL.md Đọc tệp SKILL.md tại liên kết đó và làm theo hướng dẫn cho việc của tôi.
Bạn muốn AI tự tìm những skill như thế này?Cài MCP Skillpedia miễn phí: chỉ một dòng, và AI sẽ tìm trong 15.000+ skill trước mỗi việc.Cài đặt trong 1 phút →

Ví dụ cách nhờ AI

“Tôi muốn một hàm hỗ trợ test Playwright đăng nhập vào ứng dụng bằng tên người dùng và mật khẩu.”

5 skill mới mỗi tuần. Đọc chỉ 1 phút.

Cách hoạt động

Skill đưa ra các ví dụ mã ngắn cho từng kiểu đăng nhập, gồm việc lưu trạng thái đăng nhập của trình duyệt một lần rồi tái sử dụng. Agent điều chỉnh các ví dụ này cho ứng dụng của bạn.

Nội dung kỹ thuật (SKILL.md gốc)

Lấy nguyên văn từ GitHub, do tác giả viết bằng tiếng Anh.

Login Flow Patterns for Playwright

Reusable patterns for automating login flows in end-to-end tests.

Basic Username/Password Login

import { Page } from '@playwright/test';

async function login(page: Page, username: string, password: string) {
  await page.goto('/login');
  await page.fill('[name="username"]', username);
  await page.fill('[name="password"]', password);
  await page.click('button[type="submit"]');
  await page.waitForURL('/dashboard');
}

OAuth / SSO Login

For OAuth flows that redirect to an external provider:

async function loginWithOAuth(page: Page) {
  await page.goto('/login');
  await page.click('text=Sign in with Google');

  // Handle the OAuth popup or redirect
  await page.fill('input[type="email"]', process.env.TEST_EMAIL!);
  await page.click('text=Next');
  await page.fill('input[type="password"]', process.env.TEST_PASSWORD!);
  await page.click('text=Next');

  // Wait for redirect back to app
  await page.waitForURL('**/dashboard');
}

Persisting Auth State (storageState)

Avoid logging in before every test by saving and reusing browser state:

// global-setup.ts — runs once before all tests
import { chromium } from '@playwright/test';

async function globalSetup() {
  const browser = await chromium.launch();
  const page = await browser.newPage();

  await page.goto('/login');
  await page.fill('[name="username"]', 'testuser');
  await page.fill('[name="password"]', 'testpass');
  await page.click('button[type="submit"]');
  await page.waitForURL('/dashboard');

  // Save signed-in state
  await page.context().storageState({ path: './auth.json' });
  await browser.close();
}

export default globalSetup;

Then in playwright.config.ts:

export default defineConfig({
  globalSetup: './global-setup.ts',
  use: {
    storageState: './auth.json',
  },
});

MFA / 2FA Handling

For test environments with TOTP-based 2FA:

import { authenticator } from 'otplib';

async function loginWithMFA(page: Page, secret: string) {
  await login(page, 'user', 'pass');

  // Generate TOTP code
  const code = authenticator.generate(secret);
  await page.fill('[name="totp"]', code);
  await page.click('button[type="submit"]');
  await page.waitForURL('/dashboard');
}

Tips

  • Always use storageState to avoid repeated logins — it's the single biggest speedup for E2E suites
  • Use environment variables for credentials, never hardcode them
  • For CI, consider a dedicated test user with stable credentials
  • Use page.waitForURL() instead of arbitrary waitForTimeout() after login
Câu hỏi thường gặp

Skill chỉ làm việc với Playwright thôi à?

Các ví dụ được viết cho Playwright, một công cụ test trình duyệt.

Lưu trạng thái đăng nhập giúp ích thế nào?

Nó đăng nhập một lần trong bước thiết lập, nên các test có thể tái sử dụng trạng thái đã lưu.

Skill có đề cập đăng nhập bằng Google không?

Có. Nó có một ví dụ cho đăng nhập OAuth hoặc SSO chuyển hướng sang nhà cung cấp bên ngoài.

Chi tiết

Mô tả gốc của tác giả

Common login automation patterns for web apps using Playwright

repo: andrewyng/context-hub

Độ phổ biến

Sao GitHub
14k
Vị trí xếp hạng
#11275
Giấy phép
MIT
Cập nhật
31/5/2026

Skill này có hữu ích không?

0

Đăng nhập để bình chọn.

Bình luận

  • …

Thêm skill từ kho mã này

Skill tương tự