login-flows
它能做什么
这项技能提供使用 Playwright 在端到端测试中自动化网站登录的模式。适合测试 Web 应用、并希望避免每次测试前都要登录的开发者。
适用场景
- ✓当你为需要登录的网站编写自动化测试的时候。
- ✓当你的测试使用 Google 或其他单点登录服务商的时候。
- ✓当你想保存登录状态,使测试运行得更快的时候。
技能链接
https://github.com/andrewyng/context-hub/blob/main/content/playwright-community/skills/login-flows/SKILL.md复制后粘贴给你的 AI(Claude、ChatGPT 或 Cursor),让它按照说明操作。
使用方法
最简单的方法:复制下面的消息,粘贴给你的 AI。它会读取这个技能并按说明操作。
可直接粘贴的消息
适用于 Claude Code、Codex 或 Cursor:在项目文件夹中运行,即可安装该技能。
下载 SKILL.md,放进智能体的技能文件夹。
可以这样问 AI
“我想要一个 Playwright 测试辅助函数,用用户名和密码登录我的应用。”
每周 5 个新技能,1 分钟读完。
工作原理
该技能为每种登录方式展示简短的代码示例,包括只需登录一次并保存浏览器登录状态、之后反复使用。你的智能体会根据你的应用调整这些示例。
技术内容(原始 SKILL.md)
作者在 GitHub 上的英文原文。
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
storageStateto 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 arbitrarywaitForTimeout()after login
常见问题
它只适用于 Playwright 吗?
这些示例是为 Playwright 编写的,Playwright 是一款浏览器测试工具。
保存登录状态有什么帮助?
它会在设置步骤中登录一次,之后测试可以复用保存的状态。
它涵盖使用 Google 登录吗?
涵盖。它包括一个 OAuth 或 SSO 登录的示例,会跳转到外部服务商。
详情
热度
- GitHub 星标
- 14k
- 排名
- #11275
- 许可证
- MIT
- 更新时间
- 2026/5/31
评论
- …
同一仓库的更多技能
get-api-docs
14k帮你的智能体在编写代码之前获取第三方 API 和库的最新文档,而不是依赖旧的记忆。
bloc-cubit
14k教你的智能体如何用 Bloc 或 Cubit 管理 Flutter 应用的状态,包括何时选择哪一个,以及便于测试的安全默认设置。
riverpod
14k教你的智能体如何用 Riverpod 管理 Flutter 应用的状态,涵盖 provider、异步数据和测试,并为每种情况提供合理的默认设置。
document-extraction
14k帮你的智能体读取 PDF、图片和电子表格,将其转换为干净的文本,并使用 LandingAI 的 ADE 服务提取发票总额等字段。