Skip to content

ever-co/ever-works / .agents/skills/playwright-best-practices

playwright-best-practices

Verified

Use when writing Playwright tests, fixing flaky tests, debugging failures, implementing Page Object Model, configuring CI/CD, optimizing performance, mocking APIs, handling authentication or OAuth, testing accessibility (axe-core), file uploads/downloads, date/time mocking, WebSockets, geolocation, permissions, multi-tab/popup flows, mobile/responsive layouts, touch gestures, GraphQL, error handling, offline mode, multi-user collaboration, third-party services (payments, email verification), console error monitoring, global setup/teardown, test annotations (skip, fixme, slow), test tags (@smoke, @fast, @critical, filtering with --grep), project dependencies, security testing (XSS, CSRF, auth), performance budgets (Web Vitals, Lighthouse), iframes, component testing, canvas/WebGL, service workers/PWA, test coverage, i18n/localization, Electron apps, or browser extension testing. Covers E2E, component, API, visual, accessibility, security, Electron, and extension testing.

FileKindSize
LICENSE.mdmarkdown1.0 KB
README.mdmarkdown9.9 KB
SKILL.mdskill md25.7 KB
advanced/authentication-flows.mdmarkdown10.4 KB
advanced/authentication.mdmarkdown29.4 KB
advanced/clock-mocking.mdmarkdown9.4 KB
advanced/mobile-testing.mdmarkdown10.3 KB
advanced/multi-context.mdmarkdown8.2 KB
advanced/multi-user.mdmarkdown10.9 KB
advanced/network-advanced.mdmarkdown10.1 KB
advanced/third-party.mdmarkdown11.1 KB
architecture/pom-vs-fixtures.mdmarkdown11.4 KB
architecture/test-architecture.mdmarkdown14.3 KB
architecture/when-to-mock.mdmarkdown11.9 KB
browser-apis/browser-apis.mdmarkdown10.1 KB
browser-apis/iframes.mdmarkdown11.1 KB
browser-apis/service-workers.mdmarkdown13.1 KB
browser-apis/websockets.mdmarkdown9.9 KB
core/annotations.mdmarkdown10.4 KB
core/assertions-waiting.mdmarkdown9.1 KB
core/configuration.mdmarkdown13.7 KB
core/fixtures-hooks.mdmarkdown11.2 KB
core/global-setup.mdmarkdown12.4 KB
core/locators.mdmarkdown6.4 KB
core/page-object-model.mdmarkdown7.7 KB
core/projects-dependencies.mdmarkdown8.1 KB
core/test-data.mdmarkdown11.3 KB
core/test-suite-structure.mdmarkdown9.1 KB
core/test-tags.mdmarkdown6.7 KB
debugging/console-errors.mdmarkdown9.3 KB
debugging/debugging.mdmarkdown14.7 KB
debugging/error-testing.mdmarkdown9.2 KB
debugging/flaky-tests.mdmarkdown14.5 KB
frameworks/angular.mdmarkdown18.9 KB
frameworks/nextjs.mdmarkdown13.4 KB
frameworks/react.mdmarkdown17.5 KB
frameworks/vue.mdmarkdown17.3 KB
infrastructure-ci-cd/ci-cd.mdmarkdown10.4 KB
infrastructure-ci-cd/docker.mdmarkdown7.0 KB
infrastructure-ci-cd/github-actions.mdmarkdown15.1 KB
infrastructure-ci-cd/gitlab.mdmarkdown10.9 KB
infrastructure-ci-cd/other-providers.mdmarkdown14.2 KB
infrastructure-ci-cd/parallel-sharding.mdmarkdown11.6 KB
infrastructure-ci-cd/performance.mdmarkdown11.3 KB
infrastructure-ci-cd/reporting.mdmarkdown11.4 KB
infrastructure-ci-cd/test-coverage.mdmarkdown12.0 KB
testing-patterns/accessibility.mdmarkdown8.7 KB
testing-patterns/api-testing.mdmarkdown23.5 KB
testing-patterns/browser-extensions.mdmarkdown13.0 KB
testing-patterns/canvas-webgl.mdmarkdown12.3 KB
testing-patterns/component-testing.mdmarkdown11.0 KB
testing-patterns/drag-drop.mdmarkdown17.4 KB
testing-patterns/electron.mdmarkdown12.8 KB
testing-patterns/file-operations.mdmarkdown10.0 KB
testing-patterns/file-upload-download.mdmarkdown16.5 KB
testing-patterns/forms-validation.mdmarkdown21.3 KB
testing-patterns/graphql-testing.mdmarkdown8.6 KB
testing-patterns/i18n.mdmarkdown12.3 KB
testing-patterns/performance-testing.mdmarkdown11.5 KB
testing-patterns/security-testing.mdmarkdown11.5 KB
testing-patterns/visual-regression.mdmarkdown18.4 KB

debugging/console-errors.md

View on GitHub
# Browser Console & JavaScript Error Handling

## Table of Contents

1. [Capturing Console Messages](#capturing-console-messages)
2. [Failing on Console Errors](#failing-on-console-errors)
3. [JavaScript Error Detection](#javascript-error-detection)
4. [Monitoring Warnings](#monitoring-warnings)
5. [Console Fixtures](#console-fixtures)

## Capturing Console Messages

### Basic Console Capture

```typescript
test('capture console logs', async ({ page }) => {
	const logs: string[] = [];

	page.on('console', (msg) => {
		logs.push(`${msg.type()}: ${msg.text()}`);
	});

	await page.goto('/');

	// Check what was logged
	console.log('Captured logs:', logs);
});
```

### Capture by Type

```typescript
test('capture specific console types', async ({ page }) => {
	const errors: string[] = [];
	const warnings: string[] = [];
	const infos: string[] = [];

	page.on('console', (msg) => {
		switch (msg.type()) {
			case 'error':
				errors.push(msg.text());
				break;
			case 'warning':
				warnings.push(msg.text());
				break;
			case 'info':
			case 'log':
				infos.push(msg.text());
				break;
		}
	});

	await page.goto('/dashboard');

	expect(errors).toHaveLength(0);
	console.log('Warnings:', warnings);
});
```

### Capture with Stack Trace

```typescript
test('capture errors with location', async ({ page }) => {
	const errors: { message: string; location?: string }[] = [];

	page.on('console', async (msg) => {
		if (msg.type() === 'error') {
			const location = msg.location();
			errors.push({
				message: msg.text(),
				location: location ? `${location.url}:${location.lineNumber}` : undefined
			});
		}
	});

	await page.goto('/buggy-page');

	// Log errors with source location
	errors.forEach((e) => {
		console.log(`Error: ${e.message}`);
		if (e.location) console.log(`  at ${e.location}`);
	});
});
```

## Failing on Console Errors

### Fail Test on Any Error

```typescript
test('no console errors allowed', async ({ page }) => {
	const errors: string[] = [];

	page.on('console', (msg) => {
		if (msg.type() === 'error') {
			errors.push(msg.text());
		}
	});

	await page.goto('/');
	await page.getByRole('button', { name: 'Load Data' }).click();

	// Fail if any console errors
	expect(errors, `Console errors found:\n${errors.join('\n')}`).toHaveLength(0);
});
```

### Fail with Allowed Exceptions

```typescript
test('no unexpected console errors', async ({ page }) => {
	const allowedErrors = [/Failed to load resource.*favicon/, /ResizeObserver loop/];

	const unexpectedErrors: string[] = [];

	page.on('console', (msg) => {
		if (msg.type() === 'error') {
			const text = msg.text();
			const isAllowed = allowedErrors.some((pattern) => pattern.test(text));
			if (!isAllowed) {
				unexpectedErrors.push(text);
			}
		}
	});

	await page.goto('/');

	expect(unexpectedErrors, `Unexpected console errors:\n${unexpectedErrors.join('\n')}`).toHaveLength(0);
});
```

### Auto-Fail Fixture

```typescript
// fixtures/console.fixture.ts
type ConsoleFixtures = {
	failOnConsoleError: void;
};

export const test = base.extend<ConsoleFixtures>({
	failOnConsoleError: [
		async ({ page }, use, testInfo) => {
			const errors: string[] = [];

			page.on('console', (msg) => {
				if (msg.type() === 'error') {
					errors.push(msg.text());
				}
			});

			await use();

			// After test, check for errors
			if (errors.length > 0) {
				testInfo.annotations.push({
					type: 'console-errors',
					description: errors.join('\n')
				});
				throw new Error(`Console errors detected:\n${errors.join('\n')}`);
			}
		},
		{ auto: true } // Runs for every test
	]
});
```

## JavaScript Error Detection

### Catch Uncaught Exceptions

```typescript
test('no uncaught exceptions', async ({ page }) => {
	const pageErrors: Error[] = [];

	page.on('pageerror', (error) => {
		pageErrors.push(error);
	});

	await page.goto('/');
	await page.getByRole('button', { name: 'Trigger Action' }).click();

	expect(pageErrors, `Uncaught exceptions:\n${pageErrors.map((e) => e.message).join('\n')}`).toHaveLength(0);
});
```

### Capture Error Details

```typescript
test('capture JS error details', async ({ page }) => {
	const errors: { message: string; stack?: string }[] = [];

	page.on('pageerror', (error) => {
		errors.push({
			message: error.message,
			stack: error.stack
		});
	});

	await page.goto('/error-page');

	if (errors.length > 0) {
		console.log('JavaScript errors:');
		errors.forEach((e) => {
			console.log(`  Message: ${e.message}`);
			console.log(`  Stack: ${e.stack}`);
		});
	}
});
```

### Test Error Boundary Triggers

```typescript
test('error boundary catches render error', async ({ page }) => {
	let errorCaught = false;

	page.on('pageerror', () => {
		// Note: React error boundaries catch errors before they become pageerrors
		// This would only fire for unhandled errors
		errorCaught = true;
	});

	// Trigger component error via props
	await page.route(
		'**/api/data',
		(route) => route.fulfill({ json: null }) // Will cause "cannot read property of null"
	);

	await page.goto('/dashboard');

	// Error boundary should show fallback, not crash
	await expect(page.getByText('Something went wrong')).toBeVisible();
	expect(errorCaught).toBe(false); // Error was caught by boundary
});
```

## Monitoring Warnings

### Capture Deprecation Warnings

```typescript
test('no deprecation warnings', async ({ page }) => {
	const deprecations: string[] = [];

	page.on('console', (msg) => {
		const text = msg.text();
		if (msg.type() === 'warning' && (text.includes('deprecated') || text.includes('Deprecation'))) {
			deprecations.push(text);
		}
	});

	await page.goto('/');

	if (deprecations.length > 0) {
		console.warn('Deprecation warnings found:');
		deprecations.forEach((d) => console.warn(`  - ${d}`));
	}

	// Optionally fail
	// expect(deprecations).toHaveLength(0);
});
```

### React Development Warnings

```typescript
test('no React warnings', async ({ page }) => {
	const reactWarnings: string[] = [];

	page.on('console', (msg) => {
		const text = msg.text();
		if (msg.type() === 'warning' && (text.includes('Warning:') || text.includes('React'))) {
			reactWarnings.push(text);
		}
	});

	await page.goto('/');

	// Common React warnings to check
	const criticalWarnings = reactWarnings.filter(
		(w) =>
			w.includes('Each child in a list should have a unique') ||
			w.includes('Cannot update a component') ||
			w.includes("Can't perform a React state update")
	);

	expect(criticalWarnings, `React warnings:\n${criticalWarnings.join('\n')}`).toHaveLength(0);
});
```

## Console Fixtures

### Comprehensive Console Fixture

```typescript
// fixtures/console.fixture.ts
type ConsoleMessage = {
	type: string;
	text: string;
	location?: { url: string; line: number };
	timestamp: number;
};

type ConsoleFixtures = {
	consoleMessages: ConsoleMessage[];
	getConsoleErrors: () => ConsoleMessage[];
	getConsoleWarnings: () => ConsoleMessage[];
	assertNoErrors: (allowedPatterns?: RegExp[]) => void;
};

export const test = base.extend<ConsoleFixtures>({
	consoleMessages: async ({ page }, use) => {
		const messages: ConsoleMessage[] = [];

		page.on('console', (msg) => {
			const location = msg.location();
			messages.push({
				type: msg.type(),
				text: msg.text(),
				location: location ? { url: location.url, line: location.lineNumber } : undefined,
				timestamp: Date.now()
			});
		});

		await use(messages);
	},

	getConsoleErrors: async ({ consoleMessages }, use) => {
		await use(() => consoleMessages.filter((m) => m.type === 'error'));
	},

	getConsoleWarnings: async ({ consoleMessages }, use) => {
		await use(() => consoleMessages.filter((m) => m.type === 'warning'));
	},

	assertNoErrors: async ({ getConsoleErrors }, use) => {
		await use((allowedPatterns = []) => {
			const errors = getConsoleErrors();
			const unexpected = errors.filter((e) => !allowedPatterns.some((p) => p.test(e.text)));

			if (unexpected.length > 0) {
				throw new Error(`Unexpected console errors:\n${unexpected.map((e) => e.text).join('\n')}`);
			}
		});
	}
});

// Usage
test('page loads without errors', async ({ page, assertNoErrors }) => {
	await page.goto('/dashboard');
	await page.getByRole('button', { name: 'Load' }).click();

	assertNoErrors([/favicon/]); // Allow favicon errors
});
```

### Attach Console to Report

```typescript
test('capture console for debugging', async ({ page }, testInfo) => {
	const logs: string[] = [];

	page.on('console', (msg) => {
		logs.push(`[${msg.type()}] ${msg.text()}`);
	});

	page.on('pageerror', (error) => {
		logs.push(`[EXCEPTION] ${error.message}`);
	});

	await page.goto('/');
	// ... test actions

	// Attach console log to test report
	await testInfo.attach('console-log', {
		body: logs.join('\n'),
		contentType: 'text/plain'
	});
});
```

## Anti-Patterns to Avoid

| Anti-Pattern               | Problem                    | Solution                    |
| -------------------------- | -------------------------- | --------------------------- |
| Ignoring console errors    | Bugs go unnoticed          | Check for errors in tests   |
| Too strict error checking  | Tests fail on minor issues | Allow known/expected errors |
| Not capturing stack traces | Hard to debug              | Include location info       |
| Checking only at end       | Miss errors during actions | Capture continuously        |

## Related References

- **Debugging**: See [debugging.md](debugging.md) for troubleshooting
- **Error Testing**: See [error-testing.md](error-testing.md) for error scenarios
rea-analysis-optimizationmorluto/reaOptimize REA providers and shared analysis workflows for agent context, latency, CPU, and memory while preserving correctness and Evidence. Use for analysis bottlenecks and cache or query design; CI/test-suite optimization belongs to rea-ci-tests-audit.Stars: 471kUpdated Claude CodeCodexVerifiedInstall
rea-ci-tests-auditmorluto/reaAudit REA CI and test suites for correctness, fixture drift, false passes, and measured performance; use for suite or workflow investigations and efficiency changes, rather than ordinary feature testing or a single test failure.Stars: 471kUpdated Claude CodeCodexVerifiedInstall
rea-tool-designmorluto/reaAudit, investigate, design, or change REA investigation tools, CLI/MCP contracts, provider capabilities, and Evidence semantics.Stars: 471kUpdated Claude CodeCodexVerifiedInstall
ponytail-auditDietrichGebert/ponytailQuality audit of the whole repo: bugs, security, real load, missing tests, speed, and what to delete. Most important first.Stars: 194kUpdated Claude CodeCodexVerifiedInstall