跳到正文
Skillpedia
测试14k · #11275

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。它会读取这个技能并按说明操作。

可直接粘贴的消息

请用这个技能帮我:https://github.com/andrewyng/context-hub/blob/main/content/playwright-community/skills/login-flows/SKILL.md 阅读该链接中的 SKILL.md 文件,并在我的任务中按照它的说明来做。
想让你的 AI 自己找到这样的技能吗?安装免费的 Skillpedia MCP:只需一行,每次任务前它都会在 15,000+ 个技能中搜索。1 分钟完成安装 →

可以这样问 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 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
常见问题

它只适用于 Playwright 吗?

这些示例是为 Playwright 编写的,Playwright 是一款浏览器测试工具。

保存登录状态有什么帮助?

它会在设置步骤中登录一次,之后测试可以复用保存的状态。

它涵盖使用 Google 登录吗?

涵盖。它包括一个 OAuth 或 SSO 登录的示例,会跳转到外部服务商。

详情

作者的原始描述

Common login automation patterns for web apps using Playwright

repo: andrewyng/context-hub

热度

GitHub 星标
14k
排名
#11275
许可证
MIT
更新时间
2026/5/31

这个技能有用吗?

0

登录 后即可投票。

评论

  • …

同一仓库的更多技能

相似技能