Playwright 组件测试指南这个话题社区讨论了很多次,但随着版本迭代,很多结论需要更新。本文基于最新版本重新梳理。
入門ガイド
実際のプロジェクトでの使い方はやや複雑になります:
javascript
import { render, screen, waitFor } from '@testing-library/react'
import userEvent from '@testing-library/user-event'
import { LoginForm } from './LoginForm'
describe('LoginForm', () => {
it('提交有效的登录表单', async () => {
const onSubmit = jest.fn()
render(<LoginForm onSubmit={onSubmit} />)
await userEvent.type(screen.getByLabelText(/邮箱/), 'user@example.com')
await userEvent.type(screen.getByLabelText(/密码/), 'password123')
await userEvent.click(screen.getByRole('button', { name: /登录/ }))
await waitFor(() => {
expect(onSubmit).toHaveBeenCalledWith({
email: 'user@example.com', password: 'password123'
})
})
})
})
このアプローチにより、コードのテスト可能性とスケーラビリティが向上します。
ソースコード分析
完全な例を以下に示します:
javascript
import { test, expect } from '@playwright/test'
test.describe('用户登录流程', () => {
test('成功登录跳转到首页', async ({ page }) => {
await page.goto('/login')
await page.fill('[data-testid="email"]', 'admin@example.com')
await page.fill('[data-testid="password"]', 'admin123')
await page.click('[data-testid="submit-btn"]')
await expect(page).toHaveURL('/dashboard')
await expect(page.locator('.welcome')).toContainText('欢迎回来')
})
})
境界条件の処理に注意してください。これは本番環境で非常に重要です。
実際のシナリオへの応用
コアロジックを理解することが重要です:
javascript
import { render, screen, waitFor } from '@testing-library/react'
import userEvent from '@testing-library/user-event'
import { LoginForm } from './LoginForm'
describe('LoginForm', () => {
it('提交有效的登录表单', async () => {
const onSubmit = jest.fn()
render(<LoginForm onSubmit={onSubmit} />)
await userEvent.type(screen.getByLabelText(/邮箱/), 'user@example.com')
await userEvent.type(screen.getByLabelText(/密码/), 'password123')
await userEvent.click(screen.getByRole('button', { name: /登录/ }))
await waitFor(() => {
expect(onSubmit).toHaveBeenCalledWith({
email: 'user@example.com', password: 'password123'
})
})
})
})
パフォーマンスの最適化は具体的なシナリオに合わせる必要があり、すべてのケースで過度な最適化が必要というわけではありません。
最適化テクニック
以下の方法で改善できます:
javascript
import { test, expect } from '@playwright/test'
test.describe('用户登录流程', () => {
test('成功登录跳转到首页', async ({ page }) => {
await page.goto('/login')
await page.fill('[data-testid="email"]', 'admin@example.com')
await page.fill('[data-testid="password"]', 'admin123')
await page.click('[data-testid="submit-btn"]')
await expect(page).toHaveURL('/dashboard')
await expect(page.locator('.welcome')).toContainText('欢迎回来')
})
})
このアプローチは6ヶ月以上本番環境で安定して動作し、実際に検証されています。
まとめ
- 新しい技術を使うためだけに新しい技術を使わないでください
- コードサンプルは参考用のみであり、ビジネスシナリオに応じて調整する必要があります
- Playwright 组件测试指南不是银弹,需要根据项目规模和技术栈选择
- 基礎的な原理を理解することは、APIを暗記することより重要です
- 本番環境で使用する前に必ず互換性を確認してください