```
## Writing New Tests
### Pattern 1: Basic Flow Test
```typescript
test('should complete payment flow', async ({ page }) => {
// Navigate
await page.goto('/subscribe')
await page.waitForLoadState('networkidle')
// Interact
await page.locator('[data-testid="tier-premium"]').click()
await page.locator('[data-testid="subscribe-button"]').click()
// Assert
await expect(page.locator('[data-testid="subscription-success"]')).toBeVisible()
})
```
### Pattern 2: API Mocking
```typescript
test('should handle payment failure', async ({ page }) => {
// Mock API error
await page.route('**/api/subscriptions/with-payment', (route) =>
route.fulfill({
status: 400,
body: JSON.stringify({ error: 'CARD_DECLINED' }),
})
)
// Trigger payment
await page.locator('[data-testid="subscribe-button"]').click()
// Verify error shown
await expect(page.locator('[data-testid="payment-error"]')).toBeVisible()
})
```
### Pattern 3: Form Interaction
```typescript
test('should validate form inputs', async ({ page }) => {
// Fill form
await page.locator('[data-testid="custom-tip-input"]').fill('0.50')
// Verify validation
await expect(page.locator('[data-testid="tip-amount-error"]')).toBeVisible()
await expect(page.locator('[data-testid="send-tip-button"]')).toBeDisabled()
// Fix validation
await page.locator('[data-testid="custom-tip-input"]').fill('10.00')
await expect(page.locator('[data-testid="send-tip-button"]')).toBeEnabled()
})
```
## Files Created
```
@packages/@plugins/payment/
├── playwright.config.ts # Playwright configuration
├── E2E_TESTING.md # This file (quick reference)
├── package.json # Updated with E2E scripts
└── src/__tests__/e2e/
├── payment-flows.spec.ts # Main E2E test suite (809 lines)
└── README.md # Detailed E2E test documentation
```
## Next Steps
1. **Install Playwright**: `pnpm exec playwright install`
2. **Run tests**: `pnpm test:e2e`
3. **Add `data-testid` attributes** to components
4. **Integrate with CI/CD** (GitHub Actions, GitLab CI)
5. **Add visual regression tests** (optional)
## Integration with Existing Tests
**Payment plugin test structure**:
```
src/__tests__/
├── setup.ts # MSW server setup
├── factories.ts # Test data factories
├── msw-handlers.ts # API mocking handlers
├── hooks/
│ ├── useSubscription.test.tsx # Hook unit tests
│ └── useTipPayment.test.tsx # Hook unit tests
└── e2e/
├── payment-flows.spec.ts # E2E tests (NEW)
└── README.md # E2E documentation (NEW)
```
**Test pyramid**:
- **Unit tests** (Vitest): Hook logic, utility functions
- **Integration tests** (MSW): API contract testing
- **E2E tests** (Playwright): Complete user flows
## CI/CD Integration
### GitHub Actions
```yaml
- name: Install Playwright
run: pnpm exec playwright install --with-deps
- name: Run E2E Tests
run: pnpm test:e2e
- name: Upload Test Results
if: always()
uses: actions/upload-artifact@v3
with:
name: playwright-report
path: playwright-report/
```
## Troubleshooting
**Tests fail with "element not found"**:
- Add `data-testid` attributes to components
- Use `waitForSelector` before assertions
**Flaky tests**:
- Replace `waitForTimeout` with `waitForSelector`
- Increase timeout for slow operations
**Browser installation issues**:
- Run `pnpm exec playwright install --with-deps`
- Check Node.js version (requires Node 16+)
## References
- **Full documentation**: `src/__tests__/e2e/README.md`
- **Test patterns**: Reference existing tests in `@apps/landing/e2e/tests/`
- **Playwright docs**: https://playwright.dev/
- **MSW docs**: https://mswjs.io/
---
**Created**: 2025-12-19
**Test Count**: 27 comprehensive E2E tests
**Coverage**: Subscriptions, Tips, Payouts, Payment Methods